SiliconFlow 文本对话
以下是 SiliconFlow 文本对话接口的详细参数说明及示例,供你撰写文档参考:
一、接口请求体(Body)参数详解
1. model(必需)
- 类型:枚举字符串
- 描述:指定使用的模型名称。模型会定期更新、上下线或调整服务功能,变更会通过公告等方式通知。
- 示例值:
Qwen/QwQ-32BTHUDM/GLM-Z1-32B-0414deepseek-ai/DeepSeek-R1等(具体可选模型见官方文档列表)
2. messages(必需)
- 类型:消息列表
- 描述:包含对话历史的消息列表,用于构建上下文。
- 子参数:
role(必需):消息作者角色,可选值为system(系统)、user(用户)、assistant(助手)。content(必需):消息内容文本。- 示例值:
[
{"role": "system", "content": "你是一位技术专家。"},
{"role": "user", "content": "2025年中国大模型产业会面临哪些机遇和挑战?"},
{"role": "assistant", "content": "这里将生成助手的回答内容。"}
]
3. stream
- 类型:布尔值
- 描述:若设置为
true,则以流式传输方式逐段返回生成的 tokens,最后以data: [DONE]结束。 - 默认值:
false - 示例值:
true
4. max_tokens
- 类型:整数
- 描述:设置生成 tokens 的最大数量,控制生成内容长度,范围为
1 <= max_tokens <= 16384。 - 示例值:
1024
5. stop
- 类型:字符串列表
- 描述:指定最多 4 个停止序列,当生成内容遇到这些序列时停止。
- 示例值:
["\n", "."]
6. temperature
- 类型:数字
- 描述:控制生成内容的随机性。值越低(接近 0),生成结果越确定;值越高(接近 1 或以上),结果越随机。
- 默认值:
1.0 - 示例值:
0.7
7. top_p
- 类型:数字
- 描述: nucleus采样参数,动态调整每个预测标记的选择范围。值越小,生成结果越聚焦于高概率选项。
- 默认值:
1.0 - 示例值:
0.8
8. n
- 类型:整数
- 描述:指定返回的生成内容数量(并行生成多个响应)。
- 默认值:
1 - 示例值:
3
9. response_format
- 类型:对象
- 描述:指定模型输出格式。
- 子参数:
type(必需):响应格式类型,目前只支持"text"。- 示例值:
{"type": "text"}
10. tools
- 类型:工具列表
- 描述:指定模型可调用的工具或函数。
- 子参数:
type(必需):工具类型,目前只支持"function"。function(必需):函数详细信息。name(必需):函数名称,遵循命名规则。description(必需):函数描述,帮助模型判断调用时机。parameters:函数参数的 JSON Schema 描述。strict:布尔值,是否严格遵循参数模式,默认为false。
- 示例值:
[
{
"type": "function",
"function": {
"name": "analyze_market_trends",
"description": "分析市场趋势并提供洞察",
"parameters": {
"type": "object",
"properties": {
"industry": {"type": "string"},
"year": {"type": "integer"}
}
}
}
}
]
二、完整请求体示例
{
"model": "Qwen/QwQ-32B", // 使用的模型
"messages": [ // 对话消息列表
{"role": "system", "content": "你是一位技术专家。"},
{"role": "user", "content": "2025年中国大模型产业会面临哪些机遇和挑战?"}
],
"stream": true, // 是否流式响应
"max_tokens": 2048, // 最大生成tokens数
"temperature": 0.7, // 随机性控制
"top_p": 0.8, // nucleus采样参数
"n": 2, // 返回结果数量
"response_format": {"type": "text"}, // 响应格式
"tools": [ // 可用工具
{
"type": "function",
"function": {
"name": "analyze_market_trends",
"description": "分析市场趋势并提供洞察",
"parameters": {
"type": "object",
"properties": {
"industry": {"type": "string"},
"year": {"type": "integer"}
}
}
}
}
]
}
好的,以下是带有详细中文注释的 Python 代码示例,展示如何使用 SiliconFlow 的接口:
1. 基础请求示例
import requests
# 设置接口的URL
url = "https://api.siliconflow.cn/v1/chat/completions"
# 设置请求头,包含认证信息和内容类型
headers = {
"Authorization": "Bearer YOUR_API_KEY", # 替换为你的API密钥
"Content-Type": "application/json" # 指定发送的数据格式为JSON
}
# 设置请求体,包含模型名称和消息内容
data = {
"model": "Qwen/QwQ-32B", # 使用的模型名称
"messages": [
{"role": "user", "content": "2025年中国大模型产业会面临哪些机遇和挑战?"} # 用户发送的消息内容
]
}
# 发送POST请求
response = requests.post(url, headers=headers, json=data)
# 打印返回结果
print(response.json())
2. 流式响应示例
import requests
url = "https://api.siliconflow.cn/v1/chat/completions"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
# 设置流式响应为True,这样可以逐段接收模型返回的内容
data = {
"model": "Qwen/QwQ-32B",
"messages": [
{"role": "user", "content": "2025年中国大模型产业会面临哪些机遇和挑战?"}
],
"stream": True # 启用流式响应
}
# 发送请求并逐段处理返回内容
response = requests.post(url, headers=headers, json=data, stream=True)
for chunk in response.iter_lines():
if chunk:
print(chunk.decode()) # 打印每一部分内容
3. 限制生成的 token 数量
import requests
url = "https://api.siliconflow.cn/v1/chat/completions"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
# 设置最大token数量,控制生成内容的长度
data = {
"model": "Qwen/QwQ-32B",
"messages": [
{"role": "user", "content": "2025年中国大模型产业会面临哪些机遇和挑战?"}
],
"max_tokens": 1024 # 最大生成token数量,避免内容过长
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
4. 设置停止序列
import requests
url = "https://api.siliconflow.cn/v1/chat/completions"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
# 设置停止序列,当模型生成到这些序列时停止
data = {
"model": "Qwen/QwQ-32B",
"messages": [
{"role": "user", "content": "2025年中国大模型产业会面临哪些机遇和挑战?"}
],
"stop": ["\n", "."] # 遇到换行符或句号时停止生成
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
5. 调整随机性
import requests
url = "https://api.siliconflow.cn/v1/chat/completions"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
# 使用top_p参数调整生成内容的随机性
data = {
"model": "Qwen/QwQ-32B",
"messages": [
{"role": "user", "content": "2025年中国大模型产业会面临哪些机遇和挑战?"}
],
"top_p": 0.7 # 设置随机性参数,值越小结果越确定
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
6. 指定响应格式
import requests
url = "https://api.siliconflow.cn/v1/chat/completions"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
# 指定响应格式为纯文本(其他选项可能包括JSON等)
data = {
"model": "Qwen/QwQ-32B",
"messages": [
{"role": "user", "content": "2025年中国大模型产业会面临哪些机遇和挑战?"}
],
"response_format": {"type": "text"} # 指定返回格式为文本
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
7. 使用工具或函数
import requests
url = "https://api.siliconflow.cn/v1/chat/completions"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
# 定义可以使用的工具(目前仅支持函数调用)
data = {
"model": "Qwen/QwQ-32B",
"messages": [
{"role": "user", "content": "2025年中国大模型产业会面临哪些机遇和挑战?"}
],
"tools": [
{
"type": "function", # 工具类型为函数
"function": {
"name": "analyze_market_trends", # 函数名称
"description": "分析市场趋势并提供洞察", # 函数描述
"parameters": { # 函数参数定义
"type": "object",
"properties": {
"industry": {"type": "string"}, # 行业参数
"year": {"type": "integer"} # 年份参数
}
}
}
}
]
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
8. 多轮对话示例
import requests
url = "https://api.siliconflow.cn/v1/chat/completions"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
# 模拟多轮对话,包含系统消息、用户消息和助手消息
data = {
"model": "Qwen/QwQ-32B",
"messages": [
{"role": "system", "content": "你是一个大型语言模型领域的专家。"}, # 系统预设角色
{"role": "user", "content": "2025年中国大模型产业会面临哪些机遇和挑战?"}, # 用户问题
{"role": "assistant", "content": "这个领域正在快速增长,但也面临激烈竞争和监管挑战。"} # 助手回答
]
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
以上示例展示了如何利用 Python 调用 SiliconFlow 的接口进行各种文本对话任务,你可以根据实际需求替换模型名称、调整参数,并扩展功能。