OpenAI接口
兼容 OpenAI 的聊天、图像、音频等接口
本文档描述了通用AI接口规范,兼容OpenAI、Claude、Gemini、Deepseek等多种主流大模型,支持聊天、图像生成、音频处理等功能。
Completions
文本聊天
与大语言模型进行文本对话。
URL
https://api.xty.app/v1/chat/completions
方法
POST
请求参数
| 参数名 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| model | string | 是 | 使用的模型ID |
| messages | array | 是 | 包含对话内容的消息数组 |
| temperature | number | 否 | 采样温度,介于0-2之间 |
| top_p | number | 否 | 核采样,在0-1之间 |
| n | integer | 否 | 为每个输入消息生成的完成数量 |
| stream | boolean | 否 | 是否流式返回部分结果 |
| stop | string/array | 否 | 停止生成的序列,最多4个序列 |
| max_tokens | integer | 否 | 生成的最大token数量 |
| presence_penalty | number | 否 | 存在惩罚,介于-2.0和2.0之间 |
| frequency_penalty | number | 否 | 频率惩罚,介于-2.0和2.0之间 |
| logit_bias | object | 否 | 修改特定词元出现的可能性,将词元ID映射到-100到100之间的偏差值 |
| user | string | 否 | 最终用户的唯一标识符 |
| response_format | object | 否 | 指定模型输出的格式,可选值:"text"、"json_object"或带有schema的JSON格式 |
| seed | integer | 否 | 用于确定性生成的种子值,使重复请求能返回相似结果 |
| tools | array | 否 | 模型可以调用的工具列表,目前仅支持函数,最多支持128个函数 |
| tool_choice | string/object | 否 | 控制模型如何调用工具:"none"、"auto"、"required"或指定工具 |
| parallel_tool_calls | boolean | 否 | 是否启用工具使用期间的并行函数调用,默认为true |
| logprobs | boolean | 否 | 是否返回输出词元的概率对数,默认为false |
| top_logprobs | integer | 否 | 每个词元位置返回的最可能词元数量(0-20) |
| functions | array | 否 | 【已废弃,请使用tools】模型可能生成JSON输入的函数列表 |
| function_call | string/object | 否 | 【已废弃,请使用tool_choice】控制模型是否/如何调用函数 |
识图聊天
分析和理解图像内容,并进行文本对话。
- URL:
https://api.xty.app/v1/chat/completions - 方法: POST
- 说明: 通过提供图像URL或Base64编码的图像数据,模型可以分析图像并回答相关问题
请求参数
| 参数名 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| model | string | 是 | 支持视觉能力的模型ID,如gpt-4o |
| messages | array | 是 | 包含对话内容和图像内容的消息数组 |
| temperature | number | 否 | 采样温度,介于0-2之间 |
| max_tokens | integer | 否 | 生成的最大token数量 |
消息格式 (带图像)
{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "这张图片是什么?"
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg"
}
}
]
}
]
}请求示例
curl https://api.xty.app/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "这张图片中有什么?"
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg"
}
}
]
}
],
"max_tokens": 300
}'文本补全
创建文本补全。
- URL:
https://api.xty.app/v1/completions - 方法: POST
- 说明: 基于提供的提示创建文本完成
请求参数
| 参数名 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| model | string | 是 | 使用的模型ID |
| prompt | string/array | 是 | 要完成的提示 |
| max_tokens | integer | 否 | 生成的最大token数量 |
| temperature | number | 否 | 采样温度,介于0-2之间 |
| top_p | number | 否 | 核采样,在0-1之间 |
| n | integer | 否 | 生成的完成数量 |
| stream | boolean | 否 | 是否流式返回部分结果 |
| logprobs | integer | 否 | 返回的对数概率数量 |
| stop | string/array | 否 | 停止生成的序列 |
请求示例
curl https://api.xty.app/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "text-davinci-003",
"prompt": "写一首关于人工智能的诗",
"max_tokens": 100,
"temperature": 0.7
}'Responses
openai最新的接口格式。
- URL:
https://api.xty.app/v1/responses - 方法: POST
- 说明: 创建模型响应,支持多种输入格式和输出格式
请求参数
| 参数名 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| model | string | 是 | 使用的模型ID |
| input | array/string | 是 | 输入内容,可以是字符串或包含多种类型内容的数组 |
| response_format | object | 否 | 指定响应的格式,例如text类型输出 |
| temperature | number | 否 | 采样温度,介于0-2之间 |
| top_p | number | 否 | 核采样,在0-1之间 |
| max_tokens | integer | 否 | 生成的最大token数量 |
| seed | integer | 否 | 用于确定性生成的种子值 |
| tools | array | 否 | 模型可以调用的工具列表 |
| tool_choice | string/object | 否 | 控制模型如何调用工具 |
| user | string | 否 | 最终用户的唯一标识符 |
请求示例 (文本输入)
curl https://api.xty.app/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-4o",
"input": "为什么天空是蓝色的?",
"temperature": 0.7
}'请求示例 (多模态输入)
curl https://api.xty.app/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-4o",
"input": [
{
"type": "text",
"text": "这张图片中有什么?"
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg"
}
}
],
"max_tokens": 300
}'响应示例
{
"id": "resp_abc123",
"object": "response",
"created": 1698936542,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "天空呈现蓝色是因为阳光(包含各种颜色的光)穿过大气层时,空气分子会散射光线。蓝色光波较短,散射效果更强,因此我们从各个方向看到的天空主要是蓝色的。这种现象称为瑞利散射。在日出或日落时,阳光需要穿过更多的大气层,蓝光被散射得更多,因此我们看到的是红色和橙色。"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 140,
"total_tokens": 150
}
}音频接口
音频转文本
将语音音频转换为文本。
- URL:
https://api.xty.app/v1/audio/transcriptions - 方法: POST
- 说明: 将音频文件转录为文本
请求参数
| 参数名 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| file | file | 是 | 要转录的音频文件 |
| model | string | 是 | 使用的模型ID |
| prompt | string | 否 | 可以帮助音频模型识别音频中单词的可选文本 |
| language | string | 否 | 音频语言,ISO-639-1格式 |
| response_format | string | 否 | 响应格式,可选值:json, text, srt, verbose_json, vtt |
| temperature | number | 否 | 采样温度,介于0-1之间 |
请求示例
curl https://api.xty.app/v1/audio/transcriptions \
-H "Authorization: Bearer YOUR_API_KEY" \
-F file=@/path/to/audio.mp3 \
-F model="whisper-1" \
-F language="zh"创建翻译
将语音音频翻译为英文文本。
- URL:
https://api.xty.app/v1/audio/translations - 方法: POST
- 说明: 将非英语音频翻译成英文文本
请求参数
| 参数名 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| file | file | 是 | 要翻译的音频文件 |
| model | string | 是 | 使用的模型ID |
| prompt | string | 否 | 可以帮助音频模型识别音频中单词的可选文本 |
| response_format | string | 否 | 响应格式,可选值:json, text, srt, verbose_json, vtt |
| temperature | number | 否 | 采样温度,介于0-1之间 |
请求示例
curl https://api.xty.app/v1/audio/translations \
-H "Authorization: Bearer YOUR_API_KEY" \
-F file=@/path/to/audio.mp3 \
-F model="whisper-1"文本转音频
将文本转换为逼真的口语音频。
- URL:
https://api.xty.app/v1/audio/speech - 方法: POST
- 说明: 生成逼真的文本到语音音频
请求参数
| 参数名 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| model | string | 是 | 使用的TTS模型ID |
| input | string | 是 | 要转换为音频的文本 |
| voice | string | 是 | 要使用的声音,可选:alloy, echo, fable, onyx, nova, shimmer |
| response_format | string | 否 | 音频格式,可选:mp3, opus, aac, flac |
| speed | number | 否 | 语音速度,0.25-4.0之间 |
请求示例
curl https://api.xty.app/v1/audio/speech \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "tts-1",
"input": "你好,这是一段测试语音。",
"voice": "alloy",
"response_format": "mp3",
"speed": 1.0
}' \
--output speech.mp3图像
创建图像
根据文本提示生成图像。
- URL:
https://api.xty.app/v1/images/generations - 方法: POST
- 说明: 根据文本提示创建图像
请求参数
| 参数名 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| model | string | 是 | 使用的模型ID,如"dall-e-3"、"dall-e-2"或"gpt-image-1" |
| prompt | string | 是 | 描述要生成的图像的文本提示,最大长度为1000个字符 |
| n | integer | 否 | 要生成的图像数量。DALL-E 3只支持n=1,DALL-E 2支持1-10之间的值 |
| size | string | 否 | 图像尺寸。DALL-E 3支持"1024x1024"、"1792x1024"、"1024x1792";DALL-E 2支持"256x256"、"512x512"、"1024x1024";GPT-Image-1支持"1024x1024"(方形)、"1536x1024"(横向)、"1024x1536"(纵向)或"auto"(默认) |
| quality | string | 否 | 图像质量,DALL-E 3可选值:"standard"(默认)、"hd"(仅DALL-E 3支持);GPT-Image-1可选值:"low"、"medium"、"high"、"auto"(默认) |
| style | string | 否 | 图像风格,可选值:"vivid"(默认,生动逼真)、"natural"(自然,照片般的图像) |
| response_format | string | 否 | 响应格式,可选值:"url"(默认)、"b64_json"(Base64编码的JSON字符串) |
| user | string | 否 | 最终用户的唯一标识符 |
| background | string | 否 | 仅GPT-Image-1支持,背景类型,可选值:"transparent"(透明背景,仅PNG和WebP格式支持)、"opaque"(不透明背景,默认) |
| output_compression | integer | 否 | 仅GPT-Image-1支持,输出图像的压缩级别(1-100),默认为100 |
| output_format | string | 否 | 仅GPT-Image-1支持,输出图像格式,可选值:"png"、"jpeg"、"webp" |
GPT-Image-1请求示例
curl https://api.xty.app/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-image-1",
"prompt": "一只穿着宇航服的猫咪在太空中漂浮,背景是星空和行星",
"quality": "high",
"size": "1024x1024",
"output_format": "png"
}'