Chat Completions
POST /v1/chat/completions 用于根据消息列表生成模型回复。
请求
POST https://ai.agriwis.com/api/v1/chat/completions
Authorization: Bearer $SHUNONG_API_KEY
Content-Type: application/json
{
"model": "auto",
"messages": [
{"role": "system", "content": "你是一个简洁的助手。"},
{"role": "user", "content": "什么是 API?"}
],
"temperature": 0.7,
"max_tokens": 1024,
"stream": false
}
常用参数
| 参数 | 类型 | 说明 |
|---|---|---|
model |
string | 要使用的模型名称。 |
messages |
array | 按时间顺序排列的消息列表。 |
temperature |
number | 采样随机性,值越高输出越多样。 |
max_tokens |
integer | 限制本次生成的最大输出长度。 |
stream |
boolean | 是否以流式方式返回,默认为 false。 |
stop |
string / array | 触发后停止生成的字符串。 |
tools |
array | 可供模型选择的工具定义。 |
tool_choice |
string / object | 控制是否以及调用哪个工具。 |
extra_body |
object | 扩展参数,如 {"thinking": {"type": "enabled"}} 开启深度思考。 |
Python 示例
response = client.chat.completions.create(
model="auto",
messages=[{"role": "user", "content": "写一个标题"}],
temperature=0.7,
max_tokens=256,
)
message = response.choices[0].message
print(message.content)
响应结构
{
"id": "chatcmpl-example",
"object": "chat.completion",
"model": "auto",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "API 是应用程序编程接口。"},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 12,
"completion_tokens": 10,
"total_tokens": 22
}
}
finish_reason 表示生成结束原因,常见值包括 stop、length 和 tool_calls。