Skip to content
  • Auto
  • Light
  • Dark
Log in to API

Create

Create
chat.completions.create(CompletionCreateParams**kwargs) -> completion_messageCompletionMessageidstrmetricslistCreateChatCompletionResponse
post/chat/completions

Generate a chat completion for the given messages using the specified model.

Parameters
messagesiterable

List of messages in the conversation.

Hide ParametersShow Parameters
contentunionroleliteralUserMessage
contentunionroleliteralSystemMessage
contentunionroleliteraltool_call_idstrToolResponseMessage
roleliteralcontentContentstop_reasonliteraltool_callslistCompletionMessage
modelstr

The identifier of the model to use.

max_completion_tokensint
optional

The maximum number of tokens to generate.

minimum1
repetition_penaltyfloat
optional

Controls the likelyhood and generating repetitive responses.

minimum1
maximum2
response_formatResponseFormat
optional

An object specifying the format that the model must output. Setting to { "type": "json_schema", "json_schema": {...} } enables Structured Outputs which ensures the model will match your supplied JSON schema. If not specified, the default is {"type": "text"}, and model will return a free-form text response.

Hide ParametersShow Parameters
ResponseFormatJsonSchemaResponseFormatclass

Configuration for JSON schema-guided response generation.

Hide ParametersShow Parameters

The JSON schema the response should conform to.

Hide ParametersShow Parameters
namestr

The name of the response format.

schemaobject

The JSON schema the response should conform to. In a Python SDK, this is often a pydantic model.

typeliteral
Literal["json_schema"]

The type of response format being defined. Always json_schema.

Hide ParametersShow Parameters
"json_schema"
ResponseFormatTextResponseFormatclass

Configuration for text-guided response generation.

Hide ParametersShow Parameters
typeliteral
Literal["text"]

The type of response format being defined. Always text.

Hide ParametersShow Parameters
"text"
streamliteral
optional
Literal[false]

If True, generate an SSE event stream of the response. Defaults to False.

Hide ParametersShow Parameters
false
temperaturefloat
optional

Controls randomness of the response by setting a temperature. Higher value leads to more creative responses. Lower values will make the response more focused and deterministic.

minimum0
maximum1
tool_choiceToolChoiceUnionMember0literalToolChoice
optional

Controls which (if any) tool is called by the model. none means the model will not call any tool and instead generates a message. auto means the model can pick between generating a message or calling one or more tools. required means the model must call one or more tools. Specifying a particular tool via {"type": "function", "function": {"name": "my_function"}} forces the model to call that tool.

none is the default when no tools are present. auto is the default if tools are present.

Hide ParametersShow Parameters
ToolChoiceUnionMember0type
Literal["none", "auto", "required"]

none means the model will not call any tool and instead generates a message. auto means the model can pick between generating a message or calling one or more tools. required means the model must call one or more tools.

Hide ParametersShow Parameters
"none"
"auto"
"required"
ToolChoiceChatCompletionNamedToolChoiceclass

Specifies a tool the model should use. Use to force the model to call a specific function.

Hide ParametersShow Parameters
Hide ParametersShow Parameters
namestr

The name of the function to call.

typeliteral
Literal["function"]

The type of the tool. Currently, only function is supported.

Hide ParametersShow Parameters
"function"
toolsiterable
optional
functionToolFunctiontypeliteralIterable[Tool]

List of tool definitions available to the model

Hide ParametersShow Parameters
functionnamestrdescriptionstrparametersDict[str, object]strictboolToolFunction
Hide ParametersShow Parameters
namestr

The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.

descriptionstr
optional

A description of what the function does, used by the model to choose when and how to call the function.

parametersDict[str, object]
optional
Dict[str, object]

The parameters the functions accepts, described as a JSON Schema object. Omitting parameters defines a function with an empty parameter list.

strictbool
optional

Whether to enable strict schema adherence when generating the function call. If set to true, the model will follow the exact schema defined in the parameters field. Only a subset of JSON Schema is supported when strict is true. Learn more about Structured Outputs in the function calling guide.

typeliteral
Literal["function"]

The type of the tool. Currently, only function is supported.

Hide ParametersShow Parameters
"function"
top_kint
optional

Only sample from the top K options for each subsequent token.

minimum0
top_pfloat
optional

Controls diversity of the response by setting a probability threshold when choosing the next token.

minimum0
maximum1
userstr
optional

A unique identifier representing your application end-user for monitoring abuse.

Returns
completion_messageCompletionMessageidstrmetricslistCreateChatCompletionResponse
from llama_api_client import LlamaAPIClient

client = LlamaAPIClient(
    api_key="My API Key",
)
create_chat_completion_response = client.chat.completions.create(
    messages=[{
        "content": "string",
        "role": "user",
    }],
    model="model",
)
print(create_chat_completion_response.id)
200 Example
{
  "completion_message": {
    "role": "assistant",
    "content": "string",
    "stop_reason": "stop",
    "tool_calls": [
      {
        "id": "id",
        "function": {
          "arguments": "arguments",
          "name": "name"
        }
      }
    ]
  },
  "id": "id",
  "metrics": [
    {
      "metric": "metric",
      "value": 0,
      "unit": "unit"
    }
  ]
}