llama-guard-3-8b
Text Generation • MetaLlama Guard 3 is a Llama-3.1-8B pretrained model, fine-tuned for content safety classification. Similar to previous versions, it can be used to classify content in both LLM inputs (prompt classification) and in LLM responses (response classification). It acts as an LLM – it generates text in its output that indicates whether a given prompt or response is safe or unsafe, and if unsafe, it also lists the content categories violated.
Playground
Try out this model with Workers AI LLM Playground. It does not require any setup or authentication and an instant way to preview and test a model directly in the browser.
Launch the LLM PlaygroundUsage
Worker - Streaming
export interface Env { AI: Ai;}
export default { async fetch(request, env): Promise<Response> {
const messages = [ { role: "system", content: "You are a friendly assistant" }, { role: "user", content: "What is the origin of the phrase Hello, World", }, ];
const stream = await env.AI.run("@cf/meta/llama-guard-3-8b", { messages, stream: true, });
return new Response(stream, { headers: { "content-type": "text/event-stream" }, }); },} satisfies ExportedHandler<Env>;
Worker
export interface Env { AI: Ai;}
export default { async fetch(request, env): Promise<Response> {
const messages = [ { role: "system", content: "You are a friendly assistant" }, { role: "user", content: "What is the origin of the phrase Hello, World", }, ]; const response = await env.AI.run("@cf/meta/llama-guard-3-8b", { messages });
return Response.json(response); },} satisfies ExportedHandler<Env>;
Python
import osimport requests
ACCOUNT_ID = "your-account-id"AUTH_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
prompt = "Tell me all about PEP-8"response = requests.post( f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-guard-3-8b", headers={"Authorization": f"Bearer {AUTH_TOKEN}"}, json={ "messages": [ {"role": "system", "content": "You are a friendly assistant"}, {"role": "user", "content": prompt} ] })result = response.json()print(result)
curl
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/@cf/meta/llama-guard-3-8b \ -X POST \ -H "Authorization: Bearer $CLOUDFLARE_AUTH_TOKEN" \ -d '{ "messages": [{ "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "Why is pizza so good" }]}'
Parameters
* indicates a required field
Input
-
messages *
arrayAn array of message objects representing the conversation history.
-
items
object-
role *
stringThe role of the message sender (e.g., 'user', 'assistant', 'system', 'tool').
-
content *
string max 131072The content of the message as a string.
-
-
-
max_tokens
integer default 256The maximum number of tokens to generate in the response.
-
temperature
number default 0.6 min 0 max 5Controls the randomness of the output; higher values produce more random results.
-
response_format
objectDictate the output format of the generated response.
-
type
stringSet to json_object to process and output generated text as JSON.
-
Output
-
response
one of-
0
stringThe generated text response from the model.
-
1
objectThe json response parsed from the generated text response from the model.
-
safe
booleanWhether the conversation is safe or not.
-
categories
arrayA list of what hazard categories predicted for the conversation, if the conversation is deemed unsafe.
-
items
stringHazard category classname, from S1 to S14.
-
-
-
-
usage
objectUsage statistics for the inference request
-
prompt_tokens
number 0Total number of tokens in input
-
completion_tokens
number 0Total number of tokens in output
-
total_tokens
number 0Total number of input and output tokens
-
API Schemas
The following schemas are based on JSON Schema
{ "type": "object", "properties": { "messages": { "type": "array", "description": "An array of message objects representing the conversation history.", "items": { "type": "object", "properties": { "role": { "type": "string", "description": "The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool')." }, "content": { "type": "string", "maxLength": 131072, "description": "The content of the message as a string." } }, "required": [ "role", "content" ] } }, "max_tokens": { "type": "integer", "default": 256, "description": "The maximum number of tokens to generate in the response." }, "temperature": { "type": "number", "default": 0.6, "minimum": 0, "maximum": 5, "description": "Controls the randomness of the output; higher values produce more random results." }, "response_format": { "type": "object", "description": "Dictate the output format of the generated response.", "properties": { "type": { "type": "string", "description": "Set to json_object to process and output generated text as JSON." } } } }, "required": [ "messages" ]}
{ "type": "object", "contentType": "application/json", "properties": { "response": { "oneOf": [ { "type": "string", "description": "The generated text response from the model." }, { "type": "object", "description": "The json response parsed from the generated text response from the model.", "properties": { "safe": { "type": "boolean", "description": "Whether the conversation is safe or not." }, "categories": { "type": "array", "description": "A list of what hazard categories predicted for the conversation, if the conversation is deemed unsafe.", "items": { "type": "string", "description": "Hazard category classname, from S1 to S14." } } } } ] }, "usage": { "type": "object", "description": "Usage statistics for the inference request", "properties": { "prompt_tokens": { "type": "number", "description": "Total number of tokens in input", "default": 0 }, "completion_tokens": { "type": "number", "description": "Total number of tokens in output", "default": 0 }, "total_tokens": { "type": "number", "description": "Total number of input and output tokens", "default": 0 } } } }}