from cerebras.cloud.sdk import Cerebras
import os 

client = Cerebras(api_key=os.environ.get("CEREBRAS_API_KEY"),)

chat_completion = client.chat.completions.create(
    model="llama3.1-8b",
    messages=[
        {"role": "user", "content": "Hello!",}
    ],
)
print(chat_completion)
{
  "id": "chatcmpl-292e278f-514e-4186-9010-91ce6a14168b",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "Hello! How can I assist you today?",
        "role": "assistant"
      }
    }
  ],
  "created": 1723733419,
  "model": "llama3.1-8b",
  "system_fingerprint": "fp_70185065a4",
  "object": "chat.completion",
  "usage": {
    "prompt_tokens": 12,
    "completion_tokens": 10,
    "total_tokens": 22
  },
  "time_info": {
    "queue_time": 0.000073161,
    "prompt_time": 0.0010744798888888889,
    "completion_time": 0.005658071111111111,
    "total_time": 0.022224903106689453,
    "created": 1723733419
  }
}
messages
object[]
required

A list of messages comprising the conversation so far.

Note: System prompts must be passed to the messages parameter as a string. Support for other object types will be added in future releases.

model
string
required

Available options: llama3.1-8b, llama-3.3-70b

max_completion_tokens
integer | null

The maximum number of tokens that can be generated in the completion. The total length of input tokens and generated tokens is limited by the model’s context length.

response_format
object | null

Controls the format of the model response. The primary option is structured outputs with schema enforcement, which ensures the model returns valid JSON adhering to your defined schema structure.

Setting to { "type": "json_schema", "json_schema": { "name": "schema_name", "strict": true, "schema": {...} } } enforces schema compliance. The schema must follow standard JSON Schema format with the following properties:

Note: Structured outputs with JSON schema is currently in beta. Visit our page on Structured Outputs for more information.

seed
integer | null

If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result. Determinism is not guaranteed.

stop
string | null

Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.

stream
boolean | null

If set, partial message deltas will be sent.

temperature
number | null

What sampling temperature to use, between 0 and 1.5. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or top_p but not both.

top_p
number | null

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So, 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.

tool_choice
string | object

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.

tools
object | null

A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for.

Specifying tools consumes prompt tokens in the context. If too many are given, the model may perform poorly or you may hit context length limitations

user
string | null

A unique identifier representing your end-user, which can help to monitor and detect abuse.

logprobs
bool

Whether to return log probabilities of the output tokens or not.

Default: False

top_logprobs
integer | null

An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs must be set to true if this parameter is used.

from cerebras.cloud.sdk import Cerebras
import os 

client = Cerebras(api_key=os.environ.get("CEREBRAS_API_KEY"),)

chat_completion = client.chat.completions.create(
    model="llama3.1-8b",
    messages=[
        {"role": "user", "content": "Hello!",}
    ],
)
print(chat_completion)
{
  "id": "chatcmpl-292e278f-514e-4186-9010-91ce6a14168b",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "Hello! How can I assist you today?",
        "role": "assistant"
      }
    }
  ],
  "created": 1723733419,
  "model": "llama3.1-8b",
  "system_fingerprint": "fp_70185065a4",
  "object": "chat.completion",
  "usage": {
    "prompt_tokens": 12,
    "completion_tokens": 10,
    "total_tokens": 22
  },
  "time_info": {
    "queue_time": 0.000073161,
    "prompt_time": 0.0010744798888888889,
    "completion_time": 0.005658071111111111,
    "total_time": 0.022224903106689453,
    "created": 1723733419
  }
}

Was this page helpful?