Skip to content

Chat Completions API 完整指南

DeepSeek 支持 OpenAI 兼容 Chat Completions。Xikapi 示例统一使用:

text
https://xikapi.com/v1/chat/completions

基础调用

bash
curl https://xikapi.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XIKAPI_API_KEY" \
  -d '{
    "model": "deepseek-v4.5-chat",
    "messages": [
      { "role": "system", "content": "You are a helpful assistant." },
      { "role": "user", "content": "Explain in three sentences what DeepSeek is best suited for" }
    ]
  }'

JavaScript

javascript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.XIKAPI_API_KEY,
  baseURL: "https://xikapi.com/v1",
});

const completion = await client.chat.completions.create({
  model: "deepseek-v4.5-chat",
  messages: [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: "Introduce the DeepSeek-compatible calling method" },
  ],
});

console.log(completion.choices[0].message.content);

Python

python
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_XIKAPI_API_KEY",
    base_url="https://xikapi.com/v1",
)

completion = client.chat.completions.create(
    model="deepseek-v4.5-chat",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Introduce the DeepSeek-compatible calling method"},
    ],
)

print(completion.choices[0].message.content)

多轮对话

json
{
  "model": "deepseek-v4.5-chat",
  "messages": [
    { "role": "user", "content": "My system needs knowledge-base Q&A" },
    { "role": "assistant", "content": "Start with document chunking and retrieval." },
    { "role": "user", "content": "What are good document chunking practices?" }
  ]
}

流式输出

json
{
  "model": "deepseek-v4.5-chat",
  "stream": true,
  "messages": [
    { "role": "user", "content": "Write a short article about API integration" }
  ]
}

接入建议

  • API Key 只放在服务端
  • 显式设置输出长度和超时
  • 推理模型要单独设置更长超时
  • 流式输出要处理异常中断和重试