OpenAI 接口规范
支持两种 OpenAI 协议:经典的 Chat Completions(/v1/chat/completions)和新版 Responses(/v1/responses)。可直接使用 openai 官方 SDK,只需替换 Base URL 和 API Key。
| 项目 | 值 |
|---|---|
| Base URL | https://api.taiha.cn/v1 |
| API Key | 在控制台创建 |
| Model ID | 在模型广场查看 |
鉴权方式:所有协议统一使用
Authorization: Bearer <API Key>请求头。设置"stream": true可启用 SSE 流式输出。
文本生成模型根据自然语言提示词(Prompt)生成连贯、上下文相关的文本,支持聊天机器人、内容创作、文档摘要和代码生成等场景。 文本生成模型所需的输入可以是简单的关键词、一句话概述或更复杂的多步骤指令和上下文信息。常见应用场景:
- 内容创作:生成新闻文章、商品介绍及短视频脚本。
- 客户服务:构建全天候自动应答的聊天机器人,解答常见问题。
- 文本翻译:支持多语言之间的快速精准翻译。
- 摘要提炼:从长文、报告及邮件中提取关键信息。
- 法律文档编写:生成合同模板、法律意见书的基础框架。
基础对话
POST /v1/chat/completions
- cURL
- Python
curl -X POST 'https://api.taiha.cn/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <API Key>' \
-d '{
"model": "<Model ID>",
"messages": [{
"role": "user",
"content": "hello"
}]
}'
import requests
url = "https://api.taiha.cn/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer <API Key>",
}
payload = {
"model": "<Model ID>",
"messages": [{
"role": "user",
"content": "hello"
}]
}
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
result = response.json()
print(result)
if "choices" in result:
print("Assistant:", result["choices"][0]["message"]["content"])
多模态对话
- cURL
- Python
curl -X POST 'https://api.taiha.cn/v1/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <API Key>' \
-d '{
"model": "<Model ID>",
"messages": [{
"role": "user",
"content": [{
"type":"image_url",
"image_url": {
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250925/thtclx/input1.png"
}
}, {
"type": "video",
"video": {[
"https://img.alicdn.com/imgextra/i3/O1CN01K3SgGo1eqmlUgeE9b_!!6000000003923-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01BjZvwg1Y23CF5qIRB_!!6000000003000-0-tps-3840-2160.jpg",
]}
}, {
"type": "text",
"text": "这是什么"
}]
}]
}'
import requests
url = "https://api.taiha.cn/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer <API Key>",
}
payload = {
"model": "<Model ID>",
"messages": [{
"role": "user",
"content": [{
"type":"image_url",
"image_url": {
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250925/thtclx/input1.png"
}
}, {
"type": "video",
"video": {[
"https://img.alicdn.com/imgextra/i3/O1CN01K3SgGo1eqmlUgeE9b_!!6000000003923-0-tps-3840-2160.jpg",
"https://img.alicdn.com/imgextra/i4/O1CN01BjZvwg1Y23CF5qIRB_!!6000000003000-0-tps-3840-2160.jpg",
]}
}, {
"type": "text",
"text": "这是什么"
}]
}]
}
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
result = response.json()
print(result)
if "choices" in result:
print("Assistant:", result["choices"][0]["message"]["content"])
新版基础请求(Responses API)
POST /v1/responses
- cURL
- Python
curl -X POST 'https://api.taiha.cn/v1/responses' \
-H 'Authorization: Bearer <API Key>' \
-H 'Content-Type: application/json' \
-d '{
"model": "<Model ID>",
"input": [{
"role": "user",
"input": “hello”
}]
}'
import requests
url = "https://api.taiha.cn/v1/responses"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer <API Key>",
}
payload = {
"model": "<Model ID>",
"input": "hello"
}
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
result = response.json()
print(result)
常用参数
model string 必填 | 模型ID
string 必填 | 模型ID- 模型ID,请按照模型广场中模型所示名称填写,区分大小写。
- 您也可以通过请求端点models来获得所有可请求的模型列表。
messages array 必填 | 对话历史
array 必填 | 对话历史对话历史,见下方格式说明 role assistant 与模型的对话历史记录上下文,作用于模型短期记忆,便于模型分析推理 tool 工具调用结果,需带 string 选填 | 角色system 系统指令(人设、背景)user 用户指令string 可选 | 对话历史string 可选 | 工具调用结果tool_call_id
stream boolean 可选 | 流式输出开关
boolean 可选 | 流式输出开关流式输出(SSE),默认 false
max_tokens integer 可选 | 最大输出长度
integer 可选 | 最大输出长度最大输出长度,默认 4096,最大可输出长度请参考模型上下文长度
temperature number 可选 | 模型温度
number 可选 | 模型温度top_p number 可选 | 核采样
number 可选 | 核采样与 temperature 二选一调即可
tools array 可选 | 工具列表
array 可选 | 工具列表可提供模型调用的工具方法名称,具体使用方式请查看对应文档
response_format object 可选 | 输出格式
object 可选 | 输出格式可指定模型按照要求输出格式,如 {"type":"json_object"}
多模态 content(传数组时每块需指定 type):
text string 必填 | 输入指令
string 必填 | 输入指令可在该字段内要求模型根据需求推理,如参考图片1,并描述该图片中的内容
image_url array 可选 | 输入图像
array 可选 | 输入图像带 image_url.url(支持 URL 或 base64)
video_url array 可选 | 输入视频
array 可选 | 输入视频带 video_url.url(仅画面)
流式输出
设 stream: true,响应为 SSE,每行 data: {...},结束收到 data: [DONE]。
curl -X https://api.taiha.cn/v1/chat/completions \
-H "Authorization: Bearer <API Key>" \
-H "Content-Type: application/json" \
-d '{
"model": "<Model ID>",
"stream": true,
"messages": [{ "role": "user", "content": "hello" }]
}'
携带系统指令
input 传数组时可加入 system 角色:
curl -X POST 'https://api.taiha.cn/v1/chat/completions' \
-H'Content-Type: application/json' \
-H 'Authorization: Bearer <API Key>' \
-d '{
"model": "<Model ID>",
"messages": [{
"role": "system",
"content": "你是一个全能的AI助手,必须严格按照用户需求处理问题"
}, {
"role": "user",
"content": "帮我写一篇200字的文章"
}]
}'
搜索请求
原生模型搜索请求
POST /v1/responses
- cURL
- Python
curl -X POST 'https://api.taiha.cn/v1/responses' \
-H'Authorization: Bearer <API Key>' \
-H 'Content-Type: application/json' \
-d '{
"model": "<Model ID>",
"reasoning": {
"effort": "low"
},
"tools": [{
"type": "web_search"
}],
"tool_choice": "auto",
"input": "今天北京天气怎么样?"
}'
import requests
url = "https://api.taiha.cn/v1/responses"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer <API Key>",
}
payload = {
"model": "<Model ID>",
"reasoning": {
"effort": "low"
},
"tools": [{
"type": "web_search"
}],
"tool_choice": "auto",
"input": "今天北京天气怎么样?"
}
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
result = response.json()
print(result)
默认搜索请求
POST /v1/responses
- cURL
- Python
curl -X POST 'https://api.taiha.cn/v1/responses' \
-H 'Authorization: Bearer <API Key>' \
-H 'Content-Type: application/json' \
-d '{
"model": "<Model ID>",
"input": "今天北京天气怎么样?"
}'
import requests
url = "https://api.taiha.cn/v1/responses"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer <API Key>",
}
payload = {
"model": "<Model ID>",
"input": "今天北京天气怎么样?"
}
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
result = response.json()
print(result)
常见问题
Q:为什么输入Token数比我发送的文本Token数多?
在处理对话时,系统会使用对话模板(Chat Template)对输入的原始文本进行包装,添加角色标识、消息边界等控制标记。这些由系统添加的标记同样会计入Token。
{"role": "user", "content": "你好"},“你好” 在分词(Tokenize)后仅对应 1 个 Token,但系统处理时,实际输入完整文本为<|im_start|>user\n你好<|im_end|>\n<|im_start|>assistant\n<think>,分词后总Token数会增加到11个。Q:模型对话为何无法分析网页链接?
模型本身不具备直接访问网页链接的能力。您可以通过Function Calling、MCP等功能,或结合 Python 的 Beautiful Soup 等网页抓取工具提取网页内容后传入模型。
Q:官方网页端和API的回复为什么不一致?
网页端在API的基础上做了额外的工程优化,因此可以达到解析网页、联网搜索、画图、制作 PPT等功能,这些本身并不属于大模型API的能力,可以通过联网搜索、Function Calling、MCP等功能优化模型的效果。
Q:如何处理模型超时的情况?
使用流式输出可避免超时。流式输出在生成过程中逐步返回 token,无需等待完整响应。
Q:模型能直接生成 Word、Excel、PDF 或 PPT 格式的文件吗?
不能。文本生成模型仅输出纯文本内容。您需要通过代码或使用第三方库将文本转换为所需格式,或通过Agent的PPT自动生成应用等方式进行生成。