跳到主要内容

Cerebras

https://inference-docs.cerebras.ai/api-reference/chat-completions

提示

我们支持所有 Cerebras 模型,只需在发送 litellm 请求时将 model=cerebras/<any-model-on-cerebras> 设置为前缀即可。

API 密钥

# env variable
os.environ['CEREBRAS_API_KEY']

使用示例

from litellm import completion
import os

os.environ['CEREBRAS_API_KEY'] = ""
response = completion(
model="cerebras/llama3-70b-instruct",
messages=[
{
"role": "user",
"content": "What's the weather like in Boston today in Fahrenheit? (Write in JSON)",
}
],
max_tokens=10,

# The prompt should include JSON if 'json_object' is selected; otherwise, you will get error code 400.
response_format={ "type": "json_object" },
seed=123,
stop=["\n\n"],
temperature=0.2,
top_p=0.9,
tool_choice="auto",
tools=[],
user="user",
)
print(response)

使用示例 - 流式

from litellm import completion
import os

os.environ['CEREBRAS_API_KEY'] = ""
response = completion(
model="cerebras/llama3-70b-instruct",
messages=[
{
"role": "user",
"content": "What's the weather like in Boston today in Fahrenheit? (Write in JSON)",
}
],
stream=True,
max_tokens=10,

# The prompt should include JSON if 'json_object' is selected; otherwise, you will get error code 400.
response_format={ "type": "json_object" },
seed=123,
stop=["\n\n"],
temperature=0.2,
top_p=0.9,
tool_choice="auto",
tools=[],
user="user",
)

for chunk in response:
print(chunk)

与 LiteLLM 代理服务器一起使用

以下是如何使用 LiteLLM 代理服务器调用 Cerebras 模型

  1. 修改 config.yaml

    model_list:
    - model_name: my-model
    litellm_params:
    model: cerebras/<your-model-name> # add cerebras/ prefix to route as Cerebras provider
    api_key: api-key # api key to send your model
  1. 启动代理

    $ litellm --config /path/to/config.yaml
  2. 发送请求到 LiteLLM 代理服务器

    import openai
    client = openai.OpenAI(
    api_key="sk-1234", # pass litellm proxy key, if you're using virtual keys
    base_url="http://0.0.0.0:4000" # litellm-proxy-base url
    )

    response = client.chat.completions.create(
    model="my-model",
    messages = [
    {
    "role": "user",
    "content": "what llm are you"
    }
    ],
    )

    print(response)