Chat Completions 对话补全
POST /v1/chat/completions
基于消息列表创建模型响应。这是最常用的 AI 接口,支持流式输出(SSE)、多轮对话、函数调用(Function Calling)、工具使用(Tool Use)、Vision 多模态等高级功能。完全兼容 OpenAI Chat Completions API 格式。
| Header | Type | Description |
|---|---|---|
| Authorization | string | Bearer sk-your-api-key |
| Content-Type | string | application/json |
请求体参数 (Request Body)
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| model | string | Yes | - | 模型 ID,如 gpt-5.4, claude-opus-4-7, gemini-2.5-flash |
| messages | array | Yes | - | 对话消息列表,包含 role 和 content 字段 |
| stream | boolean | No | false | 是否流式输出(SSE),设为 true 时返回 Server-Sent Events 流 |
| temperature | number | No | 1 | 采样温度,0-2。值越高输出越随机,越低越确定 |
| max_tokens | integer | No | - | 生成 token 最大数量 |
| top_p | number | No | 1 | 核采样参数,0-1。与 temperature 二选一 |
| presence_penalty | number | No | 0 | 存在惩罚,-2 到 2。正值鼓励新话题 |
| frequency_penalty | number | No | 0 | 频率惩罚,-2 到 2。降低重复内容 |
| stop | string / array | No | null | 生成停止序列,最多 4 个 |
| tools | array | No | - | 模型可调用的工具列表(Function Calling) |
| tool_choice | string / object | No | auto | 工具选择策略:auto / none / required |
| response_format | object | No | - | 指定响应格式,如 {"type": "json_object"} |
| n | integer | No | 1 | 为每个 prompt 生成多少个 completion |
| logprobs | boolean | No | false | 是否返回 log probabilities |
Messages 结构说明
system - 系统指令,定义助手行为user - 用户消息,支持文本或多模态assistant - 助手回复(多轮上下文)tool - 工具调用结果代码示例
curl -X POST "https://api.xidao.online/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-api-key-here" \
-d '{
"model": "gpt-5.4",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello! Please introduce yourself."}
],
"temperature": 0.7,
"max_tokens": 1024,
"stream": false
}'
响应示例 (Response)
{
"id": "chatcmpl-xidao-abc123",
"object": "chat.completion",
"created": 1700000000,
"model": "gpt-5.4",
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "Hello! I'm an AI assistant..."},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 19, "completion_tokens": 26, "total_tokens": 45}
}
Embeddings 文本嵌入
POST /v1/embeddings
将文本转换为高维向量表示(浮点数数组)。用于语义搜索、RAG(检索增强生成)、文本聚类、相似度计算、推荐系统等场景。
请求体参数
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| model | string | Yes | - | text-embedding-3-small(1536维) · text-embedding-3-large(3072维) · text-embedding-ada-002(1532维) |
| input | string / array | Yes | - | 输入文本或文本数组(最多 2048 个输入,每个最多 8192 tokens) |
| dimensions | integer | No | - | 输出向量维度(仅 text-embedding-3 系列) |
| encoding_format | string | No | float | float 或 base64 |
代码示例
from openai import OpenAI
client = OpenAI(api_key="sk-your-key", base_url="https://api.xidao.online/v1")
response = client.embeddings.create(
model="text-embedding-3-small",
input="Hello, world!"
)
vector = response.data[0].embedding
print(f"Dimension: {len(vector)}") # 1536
响应示例
{"object":"list","data":[{"object":"embedding","index":0,"embedding":[0.0023,-0.0091,...]}],"model":"text-embedding-3-small","usage":{"prompt_tokens":4,"total_tokens":4}}
Audio 音频处理
STT + TTS
音频处理接口包含两个核心功能:语音转文字(Speech-to-Text, STT)和文字转语音(Text-to-Speech, TTS)。支持 Whisper 语音识别和 TTS-1/TTS-1-HD 语音合成。
POST /v1/audio/transcriptions
请求参数
| Parameter | Type | Required | Description |
|---|---|---|---|
| file | file | Yes | 音频文件(mp3, mp4, wav, webm 等) |
| model | string | Yes | whisper-1 |
| language | string | No | ISO-639-1 语言代码(如 zh, en),留空自动检测 |
| response_format | string | No | json/text/srt/verbose_json |
curl -X POST "https://api.xidao.online/v1/audio/transcriptions" \
-H "Authorization: Bearer sk-your-api-key" \
-F file="@recording.mp3" \
-F model="whisper-1" \
-F language="zh"
POST /v1/audio/speech
请求参数
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| model | string | Yes | - | tts-1 或 tts-1-hd |
| input | string | Yes | - | 要转换的文本(最大 4096 字符) |
| voice | string | Yes | - | 声音选择 |
| response_format | string | No | mp3 | mp3/opus/aac/flac/wav |
| speed | number | No | 1.0 | 语速:0.25 ~ 4.0 |
from openai import OpenAI
client = OpenAI(api_key="sk-your-key", base_url="https://api.xidao.online/v1")
response = client.audio.speech.create(model="tts-1-hd", voice="alloy", input="你好,欢迎使用!", response_format="mp3")
with open("output.mp3", "wb") as f:
for chunk in response.iter_bytes(chunk_size=1024): f.write(chunk)
Images 图像生成
POST /v1/images/generations
根据文本描述生成图像。支持 DALL·E 3、Midjourney、Stable Diffusion、Flux 等模型。
代码示例
from openai import OpenAI
client = OpenAI(api_key="sk-your-key", base_url="https://api.xidao.online/v1")
response = client.images.generate(model="dall-e-3", prompt="A sunset over ocean", n=1, size="1024x1024", quality="hd")
print(response.data[0].url)
Completions 文本补全
POST /v1/completions
传统的文本补全接口。给定提示文本,模型继续生成后续文本。推荐优先使用 Chat Completions。
curl -X POST "https://api.xidao.online/v1/completions" \
-H "Authorization: Bearer sk-your-api-key" \
-d '{"model":"gpt-5.4","prompt":"The quick brown fox","max_tokens":50}'
Moderations 内容审查
POST /v1/moderations
curl -X POST "https://api.xidao.online/v1/moderations" \
-H "Authorization: Bearer sk-your-api-key" \
-d '{"input":"Test content"}'
Rerank 文档重排序
POST /v1/rerank
import requests
response = requests.post(
"https://api.xidao.online/v1/rerank",
headers={"Authorization": "Bearer sk-your-api-key"},
json={
"model": "rerank-v1",
"query": "What is machine learning?",
"documents": [
"Machine learning is a subset of artificial intelligence.",
"Python is a popular programming language.",
"Deep learning uses neural networks with multiple layers."
],
"top_n": 3
}
)
results = response.json()
for r in results["results"]:
print(f"Doc {r['index']}: {r['relevance_score']:.4f}")
Anthropic Messages (Claude 原生格式)
POST /v1/messages · Base URL: https://api.xidao.online
| Header | Value | Description |
|---|---|---|
| x-api-key | sk-xxxxxx | API Key |
| anthropic-version | 2023-06-01 | API Version |
curl -X POST "https://api.xidao.online/v1/messages" \
-H "x-api-key: sk-your-api-key" \
-H "anthropic-version: 2023-06-01" \
-d '{"model":"claude-opus-4-7","max_tokens":1024,"messages":[{"role":"user","content":"Hello!"}]}'
Gemini Native (Google 原生格式)
OpenAI 兼容格式调用 Gemini
方式一:OpenAI 兼容格式 (推荐)
from openai import OpenAI
client = OpenAI(api_key="sk-your-key", base_url="https://api.xidao.online/v1")
resp = client.chat.completions.create(model="gemini-2.5-flash", messages=[{"role":"user","content":"Hello!"}])
print(resp.choices[0].message.content)
Realtime 实时语音对话
WebSocket · wss://api.xidao.online/v1/realtime
基于 WebSocket 的实时双向语音对话接口,适用于实时语音助手、客服机器人等场景。
import asyncio, websockets, json
async def chat():
async with websockets.connect("wss://api.xidao.online/v1/realtime?model=gpt-5.4-realtime",
additional_headers={"Authorization":"Bearer sk-your-key"}) as ws:
await ws.send(json.dumps({"type":"session.update","session":{"modalities":["text","audio"]}}))
async for msg in ws: print(json.loads(msg).get("type"))
asyncio.run(chat())
Videos 视频生成
POST /v1/videos/generations
根据文本描述生成视频。支持 Sora、Kling、Runway Gen-3 等模型。
curl -X POST "https://api.xidao.online/v1/videos/generations" \
-H "Authorization: Bearer sk-your-api-key" \
-d '{"model":"sora","prompt":"A cat in garden","duration":10}'
Models 模型列表
GET /v1/models
curl "https://api.xidao.online/v1/models" -H "Authorization: Bearer sk-your-api-key"
Error Codes 错误码
| Status | Error Type | Meaning | Solution |
|---|---|---|---|
| 400 | invalid_request_error | Invalid request parameters | Check request format and fields |
| 401 | authentication_error | Invalid or missing API key | Verify your API key |
| 402 | insufficient_quota | Insufficient balance | Top up your balance |
| 404 | not_found | Model not found | Check model ID spelling |
| 429 | rate_limit_exceeded | Rate limit exceeded | Reduce frequency, retry later |
| 500 | server_error | Internal server error | Retry or contact support |