跳到主要内容

Chat 模型接口

本文档介绍了如何使用 Chat 模型 API 接口。我们提供了多种高质量的语言模型,包括 GPT-4、Gemini、DeepSeek、Qwen 等系列,您可以根据实际需求选择合适的模型。

接口地址

POST https://api.yunyx.top/v1/chat/completions

请求参数

参数名类型必选说明
modelstring模型名称,可选值参考下方支持的模型列表
messagesarray对话消息数组
temperaturefloat温度参数,控制输出的随机性,取值范围 0-2,默认 1
top_pfloat核采样参数,取值范围 0-1,默认 0.9
max_tokensinteger最大输出长度,默认 4096
streamboolean是否启用流式输出,默认 false
toolsarray工具函数数组,参考下方 Function Calling 部分

messages 数组格式

每个消息对象包含以下字段:

字段类型说明
rolestring角色,可选值:system/user/assistant/tool
contentstring消息内容

支持的模型

  • GPT-4 系列

    • chatgpt-4o-latest
    • gpt-4o
    • gpt-4o-mini
  • Gemini 系列

    • gemini-1.5-pro-latest
    • gemini-1.5-pro
    • gemini-1.0-pro
  • DeepSeek 系列

    • DeepSeek-V2.5
    • deepseek-chat
    • deepseek-reasoner
  • Qwen 系列

    • QVQ-72B-Preview
    • Qwen2.5-72B-Instruct
    • Qwen2.5-Coder-32B-Instruct
  • 其他模型

    • glm-4-9b-chat
    • llama-3.2-90b-vision
    • llama-3.1-70b

示例代码

Python

import requests

url = "https://api.yunyx.top/v1/chat/completions"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
data = {
"model": "gemini-1.5-pro-latest",
"messages": [
{"role": "user", "content": "你好,请介绍一下你自己。"}
],
"temperature": 0.7
}

response = requests.post(url, headers=headers, json=data)
print(response.json())

Node.js

const axios = require('axios');

const url = 'https://api.yunyx.top/v1/chat/completions';
const headers = {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
};
const data = {
model: 'gemini-1.5-pro-latest',
messages: [
{role: 'user', content: '你好,请介绍一下你自己。'}
],
temperature: 0.7
};

axios.post(url, data, {headers})
.then(response => console.log(response.data))
.catch(error => console.error(error));

流式输出示例

Python

import requests

url = "https://api.yunyx.top/v1/chat/completions"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
data = {
"model": "gemini-1.5-pro-latest",
"messages": [
{"role": "user", "content": "你好,请介绍一下你自己。"}
],
"stream": True
}

response = requests.post(url, headers=headers, json=data, stream=True)
for line in response.iter_lines():
if line:
print(line.decode())

Node.js

const axios = require('axios');

const url = 'https://api.yunyx.top/v1/chat/completions';
const headers = {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
};
const data = {
model: 'gemini-1.5-pro-latest',
messages: [
{role: 'user', content: '你好,请介绍一下你自己。'}
],
stream: true
};

axios.post(url, data, {
headers,
responseType: 'stream'
})
.then(response => {
response.data.on('data', chunk => {
console.log(chunk.toString());
});
})
.catch(error => console.error(error));

Function Calling

Function Calling 让模型能够调用外部工具,来增强自身能力。通过定义工具函数,模型可以根据对话内容自动选择和调用合适的工具来完成任务。

支持的模型

  • GPT-4 系列(全系列支持)

    • chatgpt-4o-latest
    • gpt-4o
    • gpt-4o-mini
  • Gemini 系列(全系列支持)

    • gemini-1.5-pro-latest
    • gemini-1.5-pro
    • gemini-1.0-pro
  • DeepSeek 系列

    • deepseek-chat(注意:当前版本 Function Calling 功能效果不稳定,会出现循环调用、空回复的情况。官方正在积极修复中,预计将在下一个版本中得到修复)

请求参数

在请求中添加 tools 参数来定义可用的工具函数:

{
"model": "gemini-1.5-pro-latest",
"messages": [
{"role": "user", "content": "杭州今天的天气怎么样?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定位置的天气信息",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市名称,例如:杭州"
}
},
"required": ["location"]
}
}
}
]
}

示例代码

Python

from openai import OpenAI
import json

# 初始化客户端
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.yunyx.top"
)

# 定义工具函数
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定位置的天气信息",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市名称,例如:杭州"
}
},
"required": ["location"]
}
}
}
]

def send_messages(messages):
response = client.chat.completions.create(
model="gemini-1.5-pro-latest",
messages=messages,
tools=tools
)
return response.choices[0].message

# 开始对话
messages = [{"role": "user", "content": "杭州今天的天气怎么样?"}]
print(f"用户> {messages[0]['content']}")

# 获取模型响应
message = send_messages(messages)
messages.append(message)

# 处理函数调用
if message.tool_calls:
tool = message.tool_calls[0]
# 这里实现实际的天气查询逻辑
weather_info = "24℃" # 示例返回值

# 将函数执行结果返回给模型
messages.append({
"role": "tool",
"tool_call_id": tool.id,
"content": weather_info
})

# 获取最终响应
final_message = send_messages(messages)
print(f"助手> {final_message.content}")

Node.js

const { OpenAI } = require('openai');

// 初始化客户端
const client = new OpenAI({
apiKey: 'YOUR_API_KEY',
baseURL: 'https://api.yunyx.top'
});

// 定义工具函数
const tools = [
{
type: 'function',
function: {
name: 'get_weather',
description: '获取指定位置的天气信息',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: '城市名称,例如:杭州'
}
},
required: ['location']
}
}
}
];

async function sendMessages(messages) {
const response = await client.chat.completions.create({
model: 'gemini-1.5-pro-latest',
messages: messages,
tools: tools
});
return response.choices[0].message;
}

async function chat() {
try {
// 开始对话
const messages = [{ role: 'user', content: '杭州今天的天气怎么样?' }];
console.log(`用户> ${messages[0].content}`);

// 获取模型响应
const message = await sendMessages(messages);
messages.push(message);

// 处理函数调用
if (message.tool_calls) {
const tool = message.tool_calls[0];
// 这里实现实际的天气查询逻辑
const weatherInfo = '24℃'; // 示例返回值

// 将函数执行结果返回给模型
messages.push({
role: 'tool',
tool_call_id: tool.id,
content: weatherInfo
});

// 获取最终响应
const finalMessage = await sendMessages(messages);
console.log(`助手> ${finalMessage.content}`);
}
} catch (error) {
console.error('Error:', error);
}
}

chat();

执行流程说明

  1. 用户发送询问天气的消息
  2. 模型识别需要获取天气信息,调用 get_weather 函数
  3. 程序执行 get_weather 函数获取天气数据
  4. 将天气数据返回给模型
  5. 模型生成最终的自然语言响应

错误码说明

错误码说明
400请求参数错误
401认证失败
402余额不足
403访问被拒绝
404模型不存在
429请求过于频繁
500服务器内部错误

注意事项

  1. 请合理设置 temperature 和 top_p 参数,这会影响模型输出的质量和多样性
  2. 建议使用流式输出获得更好的交互体验
  3. 请妥善保管 API Key,不要泄露给他人
  4. 如遇到问题,请参考错误码说明或联系客服
  5. Function Calling 功能仅在支持的模型中可用
  6. GPT-4 系列和 Gemini 系列的 Function Calling 功能稳定可靠
  7. DeepSeek Chat 模型的 Function Calling 功能当前版本不稳定,建议等待后续版本更新
  8. 工具函数定义需要符合 OpenAPI Schema 规范
  9. 建议在函数描述中使用中文,可以提高模型理解和调用的准确性
  10. 模型不会实际执行函数,具体的函数实现需要由开发者提供
  11. 处理函数调用时要注意异常处理,确保程序的健壮性