Skip to content

Llama 3.3 70b: Chat-Based Completion API ​

The Chat-Based Completion API allows you to generate conversational responses based on a series of input messages. The API also provides endpoints to list available models.

Endpoints ​

1. Create Chat Completion ​

POST /v1/chat/completions

Description ​

Generates a conversational response based on the input messages.

Request Body ​

json
{
  "model": "meta/llama-3.3-70b-instruct",
  "messages": [
    {
      "role": "user",
      "content": "What is the capital of Switzerland?"
    }
  ],
  "temperature": 0.7,
  "max_completion_tokens": 100,
  "stream" : "false"
}
FieldTypeRequiredDescription
modelstringYesID of the model to use. Only meta/llama-3.3-70b-instruct is available.
messagesarrayYesList of messages in the conversation. Each message must include a role and content.
temperaturenumberNoSampling temperature (0-2). Higher values make output more random. Default: 1.
max_completion_tokensintegerNoMaximum number of tokens to generate for the completion.
streambooleanNoIf true, the response will be streamed in chunks. Default: false.

Response (When stream is false) ​

json
{
  "id": "chat-abcdefg1234567",
  "object": "chat.completion",
  "created": 1743765837,
  "model": "meta/llama-3.3-70b-instruct",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The capital of Switzerland is Bern."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 17,
    "completion_tokens": 7,
    "total_tokens": 24
  }
}
FieldTypeDescription
idstringUnique identifier for the chat completion.
objectstringAlways "chat.completion".
createdintegerUnix timestamp of when the chat completion was created.
modelstringThe model used to generate the completion.
choicesarrayList of generated responses. Each choice includes the message and finish_reason.
usageobjectToken usage details, including prompt_tokens, completion_tokens, and total_tokens.

Response (When stream is true) ​

The response is streamed in chunks. Each chunk contains a part of the generated message.

First Chunk ​
json
{
  "id": "chat-abcdefg1234567",
  "object": "chat.completion.chunk",
  "created": 1743765837,
  "model": "meta/llama-3.3-70b-instruct",
  "choices": [
    {
      "index": 0,
      "delta": {
        "role": "assistant",
        "content": "The capital of"
      },
      "finish_reason": null
    }
  ]
}
Subsequent Chunk ​
json
{
  "id": "chat-abcdefg1234567",
  "object": "chat.completion.chunk",
  "created": 1743765838,
  "model": "meta/llama-3.3-70b-instruct",
  "choices": [
    {
      "index": 0,
      "delta": {
        "content": " Switzerland is Bern."
      },
      "finish_reason": null
    }
  ]
}
Final Chunk ​
json
{
  "id": "chat-abcdefg1234567",
  "object": "chat.completion.chunk",
  "created": 1743765839,
  "model": "meta/llama-3.3-70b-instruct",
  "choices": [
    {
      "index": 0,
      "delta": {},
      "finish_reason": "stop"
    }
  ]
}
FieldTypeDescription
idstringUnique identifier for the chat completion.
objectstringAlways "chat.completion.chunk" for streaming responses.
createdintegerUnix timestamp of when the chunk was created.
modelstringThe model used to generate the completion.
choicesarrayList of incremental responses. Each choice includes the delta and finish_reason.
deltaobjectContains the incremental part of the response.
finish_reasonstring/nullIndicates why the response generation stopped.

2. List Models ​

GET /v1/models

Description ​

Lists all available models and their metadata.

Response ​

json
{
  "object": "list",
  "data": [
    {
      "id": "meta/llama-3.3-70b-instruct",
      "object": "model",
      "created": 1672531200,
      "owned_by": "SwissAI"
    }
  ]
}
FieldTypeDescription
idstringModel identifier.
objectstringAlways "model".
createdintegerUnix timestamp of model creation.
owned_bystringOrganization owning the model.

Example Code Snippets ​

Note:
The production URL for this API is available in the corresponding plan for this model in the Digital Marketplace. To retrieve the URL:

  1. Navigate to the Digital Marketplace.
  2. Go to the Documentation section.
  3. Select the plan associated with the Llama 3.3 70b model.
  4. Locate the production URL provided in the plan details.
  5. Set up the environment variable API_URL to the production URL retrieved.

How to retrieve the production URL

1. Using curl ​

Create Chat Completion ​

bash
curl -X POST $API_URL/v1/chat/completions \
-H "Authorization: Bearer  $YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "model": "meta/llama-3.3-70b-instruct",
  "messages": [
    {
      "role": "user",
      "content": "What is the capital of Switzerland?"
    }
  ],
  "temperature": 0.7,
  "max_completion_tokens": 100
}'

List Models ​

bash
curl -X GET $API_URL/v1/models \
-H "Authorization: Bearer YOUR_API_KEY"

2. Using Python (OpenAI Client) ​

Create Chat Completion ​

python
import os
import openai

client = openai.OpenAI(
    api_key=os.getenv("YOUR_API_KEY"),
    base_url=os.getenv("API_URL") + "/v1",
)

response = client.chat.completions.create(
    model="meta/llama-3.3-70b-instruct",
    messages=[
        {"role": "user", "content": "What is the capital of Switzerland?"}
    ],
    temperature=0.7,
    max_completion_tokens=100,
    stream = True
)

for chunk in response:
    print(chunk)
    print(chunk.choices[0].delta)
    print("****************")

List Models ​

python
import os
import openai

client = openai.OpenAI(
    api_key=os.getenv("YOUR_API_KEY"),
    base_url=os.getenv("API_URL") + "/v1",
)

response = client.models.list()
print(response)

Replace YOUR_API_KEY with your actual API key to authenticate the requests. You can follow the procedure in Inference Endpoints Overview