聊天完成
通过统一 API 使用各种 AI 模型创建对话式响应。
创建聊天完成
POST https://ciyuanx.io/v1/chat/completions
bash
curl https://ciyuanx.io/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-4.1",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
],
"temperature": 0.7
}'python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://ciyuanx.io/v1",
)
response = client.chat.completions.create(
model="gpt-4.1",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"},
],
temperature=0.7,
)
print(response.choices[0].message.content)typescript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "YOUR_API_KEY",
baseURL: "https://ciyuanx.io/v1",
});
const response = await client.chat.completions.create({
model: "gpt-4.1",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What is the capital of France?" },
],
temperature: 0.7,
});
console.log(response.choices[0].message.content);请求参数
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
model | string | 是 | 要使用的模型 ID |
messages | array | 是 | 消息对象数组 |
temperature | number | 否 | 采样温度(0–2)。默认值:1 |
max_tokens | integer | 否 | 生成的最大令牌数 |
top_p | number | 否 | 核采样参数 |
stream | boolean | 否 | 是否流式传输响应 |
消息角色
消息必须包含 role 和 content:
system— 设置助手的行为/个性user— 来自最终用户的消息assistant— AI 的先前响应
流式响应
启用流式传输以增量接收响应:
python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://ciyuanx.io/v1",
)
stream = client.chat.completions.create(
model="gpt-4.1",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")typescript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "YOUR_API_KEY",
baseURL: "https://ciyuanx.io/v1",
});
const stream = await client.chat.completions.create({
model: "gpt-4.1",
messages: [{ role: "user", content: "Tell me a story" }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) process.stdout.write(content);
}最佳实践
优化你的请求
- 设置
max_tokens以限制成本 - 使用合适的
temperature值(较低用于事实性,较高用于创造性) - 包含系统消息以指导行为
- 流式传输响应以获得更好的用户体验
注意
注意每个模型的令牌限制。较长的对话可能需要进行对话历史管理。