Chat Completion API
创建对话式AI应用的核心接口,支持多轮对话、系统提示、函数调用等高级功能
POST稳定版本支持流式
端点
POST
https://api.n1n.ai/v1/chat/completions请求头
| Header | 值 | 说明 |
|---|---|---|
| Content-Type | application/json | 请求体格式 |
| Authorization | Bearer YOUR_API_KEY | 身份认证令牌 |
请求参数
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
| model | string | 模型ID,如 gpt-3.5-turbo、gpt-4 | |
| messages | array | 对话消息数组,包含历史对话记录 | |
| temperature | number | 否 | 采样温度,范围0-2,默认1。值越高输出越随机 |
| max_tokens | integer | 否 | 生成的最大token数量 |
| stream | boolean | 否 | 是否启用流式响应,默认false |
| top_p | number | 否 | 核采样参数,范围0-1,默认1 |
| n | integer | 否 | 生成的回复数量,默认1 |
| stop | array | 否 | 停止生成的标记序列,最多4个 |
| presence_penalty | number | 否 | 存在惩罚,范围-2到2,默认0 |
| frequency_penalty | number | 否 | 频率惩罚,范围-2到2,默认0 |
消息格式
messages数组中的每个消息对象包含以下字段:
| 字段 | 类型 | 说明 |
|---|---|---|
| role | string | 角色类型:system、user、assistant |
| content | string | 消息内容 |
| name | string | (可选)发送者名称 |
消息数组示例
[
{
"role": "system",
"content": "你是一个有用的AI助手,擅长回答各种问题。"
},
{
"role": "user",
"content": "什么是机器学习?"
},
{
"role": "assistant",
"content": "机器学习是人工智能的一个分支..."
},
{
"role": "user",
"content": "能给我一个简单的例子吗?"
}
]请求示例
基础请求
cURL
curl https://api.n1n.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "你好,今天天气怎么样?"
}
],
"temperature": 0.7
}'流式响应请求
Python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.n1n.ai/v1"
)
# 流式响应
stream = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "写一个关于春天的诗"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")响应格式
成功响应
{
"id": "chatcmpl-123456",
"object": "chat.completion",
"created": 1677652288,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "作为AI助手,我无法获取实时天气信息。建议您查看天气预报网站或应用..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 13,
"completion_tokens": 28,
"total_tokens": 41
}
}流式响应格式
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"content":"作为"},"finish_reason":null}]}
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"content":"AI"},"finish_reason":null}]}
data: [DONE]错误处理
401 Unauthorized
API密钥无效或缺失
{"error": {"message": "Invalid API key", "type": "invalid_request_error", "code": "invalid_api_key"}}
429 Too Many Requests
请求频率超过限制
{"error": {"message": "Rate limit exceeded", "type": "rate_limit_error", "code": "rate_limit_exceeded"}}
400 Bad Request
请求参数错误
{"error": {"message": "Invalid model specified", "type": "invalid_request_error", "code": "model_not_found"}}
最佳实践
推荐做法
- 合理设置temperature参数:创意任务使用0.7-1.0,精确任务使用0-0.3
- 使用系统消息定义AI的角色和行为规范
- 保留对话历史以维持上下文连贯性
- 使用max_tokens限制响应长度,控制成本
- 对长对话使用流式响应,提升用户体验
- 实现错误重试机制,提高服务稳定性