Skip to content

聊天完成

通过统一 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);

请求参数

参数类型必需描述
modelstring要使用的模型 ID
messagesarray消息对象数组
temperaturenumber采样温度(0–2)。默认值:1
max_tokensinteger生成的最大令牌数
top_pnumber核采样参数
streamboolean是否流式传输响应

消息角色

消息必须包含 rolecontent

  • 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 值(较低用于事实性,较高用于创造性)
  • 包含系统消息以指导行为
  • 流式传输响应以获得更好的用户体验

注意

注意每个模型的令牌限制。较长的对话可能需要进行对话历史管理。