Chat Completions

对话生成 API 详细说明

Chat Completions API

生成对话式 AI 响应。

请求

POST https://api.atdak.com/v1/chat/completions

请求体

{
  "model": "atdak-gpt-4",
  "messages": [
    {"role": "system", "content": "你是一个有帮助的助手"},
    {"role": "user", "content": "你好"}
  ],
  "temperature": 0.7,
  "max_tokens": 2048,
  "stream": false
}

参数说明

参数类型必填说明
modelstring模型名称
messagesarray对话消息列表
temperaturenumber采样温度,0-2,默认 1
max_tokensinteger最大生成 token 数
streamboolean是否流式输出
top_pnumber核采样参数
frequency_penaltynumber频率惩罚,-2 到 2
presence_penaltynumber存在惩罚,-2 到 2

Message 对象

字段类型说明
rolestring角色:system, user, assistant
contentstring消息内容

响应

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1701234567,
  "model": "atdak-gpt-4",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "你好!有什么可以帮助你的吗?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 15,
    "completion_tokens": 12,
    "total_tokens": 27
  }
}

支持的模型

模型上下文长度说明
atdak-gpt-4128K最强综合能力
atdak-gpt-4-mini128K高性价比
atdak-claude-3200K超长上下文
atdak-llama-70b32K开源模型

流式输出

设置 stream: true 开启流式输出:

curl https://api.atdak.com/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "atdak-gpt-4",
    "messages": [{"role": "user", "content": "你好"}],
    "stream": true
  }'

流式响应格式:

data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":"你"}}]}
data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":"好"}}]}
data: [DONE]

代码示例

Python

from atdak import AtdakClient

client = AtdakClient()

# 普通调用
response = client.chat.completions.create(
    model="atdak-gpt-4",
    messages=[
        {"role": "user", "content": "写一首关于春天的诗"}
    ]
)
print(response.choices[0].message.content)

# 流式调用
stream = client.chat.completions.create(
    model="atdak-gpt-4",
    messages=[{"role": "user", "content": "你好"}],
    stream=True
)
for chunk in stream:
    print(chunk.choices[0].delta.content, end="")