跳到主要内容

xAI

https://docs.xai.ac.cn/docs

提示

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

API 密钥

# env variable
os.environ['XAI_API_KEY']

示例用法

LiteLLM python SDK 用法 - 非流式
from litellm import completion
import os

os.environ['XAI_API_KEY'] = ""
response = completion(
model="xai/grok-3-mini-beta",
messages=[
{
"role": "user",
"content": "What's the weather like in Boston today in Fahrenheit?",
}
],
max_tokens=10,
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)

示例用法 - 流式

LiteLLM python SDK 用法 - 流式
from litellm import completion
import os

os.environ['XAI_API_KEY'] = ""
response = completion(
model="xai/grok-3-mini-beta",
messages=[
{
"role": "user",
"content": "What's the weather like in Boston today in Fahrenheit?",
}
],
stream=True,
max_tokens=10,
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 python SDK 用法 - 视觉
import os 
from litellm import completion

os.environ["XAI_API_KEY"] = "your-api-key"

response = completion(
model="xai/grok-2-vision-latest",
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://science.nasa.gov/wp-content/uploads/2023/09/web-first-images-release.png",
"detail": "high",
},
},
{
"type": "text",
"text": "What's in this image?",
},
],
},
],
)

与 LiteLLM 代理服务器一起使用

以下是使用 LiteLLM 代理服务器调用 XAI 模型的方法

  1. 修改 config.yaml 文件

    model_list:
    - model_name: my-model
    litellm_params:
    model: xai/<your-model-name> # add xai/ prefix to route as XAI 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)

推理用法

LiteLLM 支持 xAI 模型的推理用法。

使用 xai/grok-3-mini-beta 进行推理
import litellm
response = litellm.completion(
model="xai/grok-3-mini-beta",
messages=[{"role": "user", "content": "What is 101*3?"}],
reasoning_effort="low",
)

print("Reasoning Content:")
print(response.choices[0].message.reasoning_content)

print("\nFinal Response:")
print(completion.choices[0].message.content)

print("\nNumber of completion tokens (input):")
print(completion.usage.completion_tokens)

print("\nNumber of reasoning tokens (input):")
print(completion.usage.completion_tokens_details.reasoning_tokens)

示例响应

Reasoning Content:
Let me calculate 101 multiplied by 3:
101 * 3 = 303.
I can double-check that: 100 * 3 is 300, and 1 * 3 is 3, so 300 + 3 = 303. Yes, that's correct.

Final Response:
The result of 101 multiplied by 3 is 303.

Number of completion tokens (input):
14

Number of reasoning tokens (input):
310