# Llama 3.3 - Prompt Formats ## Tokens Here is a list of special tokens that are supported by Llama 3.3: - `<|begin_of_text|>`: Specifies the start of the prompt - `<|end_of_text|>`: Model will cease to generate more tokens. This token is generated only by the base models. - `<|finetune_right_pad_id|>`: This token is used for padding text sequences to the same length in a batch. - `<|start_header_id|>` and `<|end_header_id|>`: These tokens enclose the role for a particular message. The possible roles are: [system, user, assistant and ipython] - `<|eom_id|>`: End of message. A message represents a possible stopping point for execution where the model can inform the executor that a tool call needs to be made. This is used for multi-step interactions between the model and any available tools. This token is emitted by the model when the Environment: ipython instruction is used in the system prompt, or if the model calls for a built-in tool. - `<|eot_id|>`: End of turn. Represents when the model has determined that it has finished interacting with the user message that initiated its response. This is used in two scenarios: - at the end of a direct interaction between the model and the user - at the end of multiple interactions between the model and any available tools This token signals to the executor that the model has finished generating a response. - `<|python_tag|>`: Is a special tag used in the model's response to signify a tool call. There are 4 different roles that are supported by Llama 3.3 - `system`: Sets the context in which to interact with the AI model. It typically includes rules, guidelines, or necessary information that helps the model respond effectively. - `user`: Represents the human interacting with the model. It includes the inputs, commands, and questions to the model. - `tool`: A new role introduced in Llama 3.3. This role is used to mark messages with the output of a tool call when sent back to the model from the executor. - `assistant`: Represents the response generated by the AI model based on the context provided in the `system`, `tool` and `user` prompts. ## Llama 3.3 Base Model Text completion for Llama 3.3 base model uses this format. ##### Input Prompt Format ``` <|begin_of_text|>Color of sky is blue but sometimes can also be ``` ##### Model Response Format ``` red, orange, yellow, green, purple, pink, brown, gray, black, white, and even rainbow colors. The color of the sky can change due to various reasons such as time of day, weather conditions, pollution, and atmospheric phenomena. The color of the sky is primarily blue because of a phenomenon called ``` Note start special tag ## Llama 3.3 Instruct Model ## User and assistant conversation Here is a regular multi-turn user assistant conversation and how its formatted. ##### Input Prompt Format ``` <|begin_of_text|><|start_header_id|>system<|end_header_id|> You are a helpful assistant<|eot_id|><|start_header_id|>user<|end_header_id|> Answer who are you in the form of jeopardy?<|eot_id|><|start_header_id|>assistant<|end_header_id|> ``` ##### Model Response Format ``` Here's my response "What is a helpful assistant?"<|eot_id|> ``` ## Tool Calling Formats Here we describe how to invoke the Llama 3.3 instruction tuned model for tool-calling (also called function-calling). We recommend zero-shot function calling over built-in tools. ### Zero shot function calling For Llama3.3 70B instruct models, we are continuing the format for zero shot function calling, introduced in llama 3.2. This format is designed to be more flexible and powerful than the format in 3.1. All available functions can be provided in the system message. Here is an example for the same, ##### Input Prompt Format ``` <|begin_of_text|><|start_header_id|>system<|end_header_id|> You are an expert in composing functions. You are given a question and a set of possible functions. Based on the question, you will need to make one or more function/tool calls to achieve the purpose. If none of the function can be used, point it out. If the given question lacks the parameters required by the function, also point it out. You should only return the function call in tools call sections. If you decide to invoke any of the function(s), you MUST put it in the format of [func_name1(params_name1=params_value1, params_name2=params_value2...), func_name2(params)] You SHOULD NOT include any other text in the response. Here is a list of functions in JSON format that you can invoke. [ { "name": "get_weather", "description": "Get weather info for places", "parameters": { "type": "dict", "required": [ "city" ], "properties": { "city": { "type": "string", "description": "The name of the city to get the weather for" }, "metric": { "type": "string", "description": "The metric for weather. Options are: celsius, fahrenheit", "default": "celsius" } } } } ]<|eot_id|><|start_header_id|>user<|end_header_id|> What is the weather in SF and Seattle?<|eot_id|><|start_header_id|>assistant<|end_header_id|> ``` ##### Model Response Format ``` [get_weather(city='San Francisco', metric='celsius'), get_weather(city='Seattle', metric='celsius')]<|eot_id|> ``` ##### Notes - The output supports multiple, and parallel tool calls natively - JSON format for defining the functions in the system prompt is similar to Llama3.1 ### Zero shot function calling with user message While the default is to provide all function calls in a system message, in Llama3.3 model you can also provide information for all the available tools in a user message. ##### Input Prompt Format ``` <|begin_of_text|><|start_header_id|>user<|end_header_id|> Questions: Can you retrieve the details for the user with the ID 7890, who has black as their special request? Here is a list of functions in JSON format that you can invoke: [ { "name": "get_user_info", "description": "Retrieve details for a specific user by their unique identifier. Note that the provided function is in Python 3 syntax.", "parameters": { "type": "dict", "required": [ "user_id" ], "properties": { "user_id": { "type": "integer", "description": "The unique identifier of the user. It is used to fetch the specific user details from the database." }, "special": { "type": "string", "description": "Any special information or parameters that need to be considered while fetching user details.", "default": "none" } } } } ] Should you decide to return the function call(s), put them in the format of [func1(params_name=params_value, params_name2=params_value2...), func2(params)] You SHOULD NOT include any other text in the response.<|eot_id|><|start_header_id|>assistant<|end_header_id|> ``` ##### Model Response Format ``` [get_user_info(user_id=7890, special='black')]<|eot_id|> ``` ##### Notes - The tool call format for the model is the same whether your function calls are provided in the system or user message. - While builtin tool calls end with a <|eom_id|>, notice the <|eot_id|> for zero shot tool calls. ### Builtin Tool Calling The three built-in tools (brave_search, wolfram_alpha, and code interpreter) can be turned on using the system prompt: - Brave Search: Tool call to perform web searches. - Wolfram Alpha: Tool call to perform complex mathematical calculations. - Code Interpreter: Enables the model to output python code. Here is an example of a conversation using brave search ##### Input Prompt Format ``` <|begin_of_text|><|start_header_id|>system<|end_header_id|> Environment: ipython Tools: brave_search, wolfram_alpha Cutting Knowledge Date: December 2023 Today Date: 21 September 2024 You are a helpful assistant. <|eot_id|><|start_header_id|>user<|end_header_id|> Search the web for the latest price of 1oz gold?<|eot_id|><|start_header_id|>assistant<|end_header_id|> ``` ##### Model Response Format ``` <|python_tag|>brave_search.call(query="latest price of 1oz gold")<|eom_id|> ``` - Just including Environment: ipython turns on code interpreter; therefore, you don't need to specify code interpretation on the Tools: line. The model can generate python code which is interpreted by the executor, with the result provided back to the model. - The message body of the assistant response starts with a special tag <|python_tag|> - As alluded to above, in such an environment, the model can generate <|eom_id|> instead of just the standard <|eot_id|> . The latter indicates the turn is finished, while the former indicates continued multi-step reasoning. That is, the model is expecting a continuation message with the output of the tool call. - The model tool call response is of the form `tool.call(query="...")` wher tool is `brave_search` or `wolfram_alpha` ## Builtin Code Interpreter Here is an actual example of model responding with code ##### Input Prompt Format ``` <|begin_of_text|><|start_header_id|>system<|end_header_id|> Environment: ipython<|eot_id|><|start_header_id|>user<|end_header_id|> Write code to check if number is prime, use that to see if the number 7 is prime<|eot_id|><|start_header_id|>assistant<|end_header_id|> ``` ##### Model Response Format ``` <|python_tag|>def is_prime(n): if n <= 1 return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True print(is_prime(7)) # Output: True<|eom_id|> ``` - Model starts with <|python_tag|> and continues writing python code that it needs to be executed - No explicit mention of code_interpreter in system prompt. `Environment: ipython` implicitly enables it. ## Built-in tools full interaction Here is a full interaction with the built-in tools including the tool response and the final assistant response. ##### Input Prompt Format ``` <|begin_of_text|><|start_header_id|>system<|end_header_id|> Environment: ipython Tools: brave_search, wolfram_alpha <|eot_id|><|start_header_id|>user<|end_header_id|> What is the 100th decimal of pi?<|eot_id|><|start_header_id|>assistant<|end_header_id|> <|python_tag|>wolfram_alpha.call(query="100th decimal of pi")<|eom_id|><|start_header_id|>ipython<|end_header_id|> { "queryresult": { "success": true, "inputstring": "100th decimal of pi", "pods": [ { "title": "Input interpretation", "subpods": [ { "title": "", "plaintext": "100th digit | π" } ] }, { "title": "Nearby digits", "subpods": [ { "title": "", "plaintext": "...86208998628034825342117067982148086513282306647093..." } ] }, { "title": "Result", "primary": true, "subpods": [ { "title": "", "plaintext": "7" } ] } ] } } <|eot_id|><|start_header_id|>assistant<|end_header_id|> ``` ##### Model Response Format ``` The 100th decimal of pi is 7.<|eot_id|> ``` - Note the `<|python_tag|>` in the assistant response. - Role is `tool` for the wolfram alpha response that is passed back to the model. - Final message from assistant has <|eot_id|> tag. ## Zero shot tool calling ## JSON based tool calling Llama models can now output custom tool calls from a single message to allow easier tool calling. The following prompts provide an example of how custom tools can be called from the output of the model. It's important to note that the model itself does not execute the calls; it provides structured output to facilitate calling by an executor. ##### Input Prompt Format ``` <|begin_of_text|><|start_header_id|>system<|end_header_id|> Environment: ipython Cutting Knowledge Date: December 2023 Today Date: 21 September 2024 You are a helpful assistant. <|eot_id|><|start_header_id|>user<|end_header_id|> Answer the user's question by making use of the following functions if needed. If none of the function can be used, please say so. Here is a list of functions in JSON format: { "type": "function", "function": { "name": "trending_songs", "description": "Returns the trending songs on a Music site", "parameters": { "type": "object", "properties": [ { "n": { "type": "object", "description": "The number of songs to return" } }, { "genre": { "type": "object", "description": "The genre of the songs to return" } } ], "required": ["n"] } } } Return function calls in JSON format.<|eot_id|><|start_header_id|>user<|end_header_id|> Use tools to get latest trending songs<|eot_id|><|start_header_id|>assistant<|end_header_id|> ``` ##### Model Response Format ``` <|python_tag|>{ "type": "function", "name": "trending_songs", "parameters": { "n": "10", "genre": "all" } }<|eom_id|> ``` - JSON format for providing tools needs name, description and parameters - Model responds with `<|python_tag|>` and `<|eom_id|>` as `Environment: ipython` was in the system prompt - Instructions for tools added as a user message - Only single tool calls are supported as of now ## Example of a user defined tool calling ## `` based tool calling Here is an example of how you could also write custom instructions for model to do zero shot tool calling. In this example, we define a custom tool calling format using the `` tag. ##### Input Prompt Format ``` <|begin_of_text|><|start_header_id|>system<|end_header_id|> Environment: ipython Cutting Knowledge Date: December 2023 Today Date: 21 September 2024 You are a helpful assistant. <|eot_id|><|start_header_id|>user<|end_header_id|> You have access to the following functions: Use the function 'trending_songs' to 'Returns the trending songs on a Music site': {"name": "trending_songs", "description": "Returns the trending songs on a Music site", "parameters": {"genre": {"description": "The genre of the songs to return", "param_type": "str", "required": false}, "n": {"description": "The number of songs to return", "param_type": "int", "required": true}}} Think very carefully before calling functions. If you choose to call a function ONLY reply in the following format with no prefix or suffix: {"example_name": "example_value"} Reminder: - If looking for real time information use relevant functions before falling back to brave_search - Function calls MUST follow the specified format, start with - Required parameters MUST be specified - Only call one function at a time - Put the entire function call reply on one line<|eot_id|><|start_header_id|>user<|end_header_id|> Use tools to get latest trending songs<|eot_id|><|start_header_id|>assistant<|end_header_id|> ``` ##### Model Response Format ``` {"n": 10}<|eot_id|> ``` - In this case, model does NOT respond with `<|python_tag|>` and ends with `<|eot_id|>` - Instructions for tools added as a user message Thank You!