使用硅基流动进行多模型对比
本文介绍了一个基于硅基流动API的多模型对话系统实现方案。该方案通过调用硅基流动平台的17个不同大语言模型,能够将用户问题同时发送给所有模型,并将每个模型的回答分别保存为txt文件。系统支持模型名称安全转换、推理模型参数优化等功能,可实现最多6个模型的并行对话(免费用户限制为2个)。使用前需配置API密钥环境变量,系统会自动处理模型响应并保存思维链和最终回答内容。该方案可作为ChatHub等聚合插
·
zh
ChatHub是一个是一个流行的大语言模型聚合插件,特点是可以同时和多个模型聊天,方便对比回答。ChatHub 在全球拥有数十万活跃用户。
方便之处在于多模型对比功能:
你可以重复上面的步骤在 ChatHub 添加其他模型,然后你就可以使用 ChatHub 的 All-in-One 功能同时和多个模型聊天(最多可以同时和 6 个模型对话)。
但实际使用下来,免费用户仅能使用两个模型同时对话,故,进行简单的替代:
功能:
把你输入的问题发给硅基流动上的一串模型,并把每个模型的回答分别写进对应的 txt 文件里
预先准备:
1、准备硅基流动api:https://cloud.siliconflow.cn/i/ccbITHS9
2、设置环境变量:在终端输入:
3、修改代码中的MODEL列表
Linux / macOS(bash/zsh)
export SILICONFLOW_API_KEY="你的硅基流动API密钥"
Windows PowerShell:
setx SILICONFLOW_API_KEY "你的硅基流动API密钥"
import os
import re
import requests
from typing import List
# 硅基流动 Chat Completions 的接口地址
# 官方文档参考:https://docs.siliconflow.cn (chat.completions):contentReference[oaicite:0]{index=0}
API_URL = "https://api.siliconflow.cn/v1/chat/completions"
# 从环境变量中读取 API Key,避免把密钥写死在代码里
API_KEY = os.getenv("SILICONFLOW_API_KEY")
if not API_KEY:
raise RuntimeError(
"请先在环境变量中设置 SILICONFLOW_API_KEY,例如:\n"
" Linux / macOS: export SILICONFLOW_API_KEY='你的API密钥'\n"
" Windows PowerShell: setx SILICONFLOW_API_KEY \"你的API密钥\""
)
HEADERS = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
}
# 你要求的所有模型列表
MODELS: List[str] = [
"deepseek-ai/DeepSeek-R1",
"deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
"deepseek-ai/DeepSeek-R1-Distill-Qwen-14B",
"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
"ascend-tribe/pangu-pro-moe",
"baidu/ERNIE-4.5-300B-A47B",
"MiniMaxAl/MiniMax-M2",
"moonshotai/Kimi-K2-Instruct-0905",
"Qwen/Qwen2.5-72B-Instruct",
"Qwen/Qwen3-0mni-30B-A3B-Instruct",
"Qwen/Qwen3-235B-A22B-Thinking-2507",
"Qwen/Qwen3-32B",
"Qwen/Qwen3-8B",
"Qwen/QwQ-32B",
"tencent/Hunyuan-MT-7B",
"THUDM/GLM-Z1-Rumination-32B-0414",
"zai-org/GLM-4.5",
]
def sanitize_model_name(model: str) -> str:
"""将模型名称转换为安全的文件名."""
# 只保留字母、数字、点、横线、下划线,其余的都替换为下划线
return re.sub(r"[^a-zA-Z0-9_.-]", "_", model)
def build_payload(model: str, question: str) -> dict:
"""根据模型构建请求体,可以对推理模型单独调参."""
# 判断是否是“推理模型”(DeepSeek-R1 / Thinking / Rumination / QwQ 等):contentReference[oaicite:1]{index=1}
is_reasoning_model_keywords = ["DeepSeek-R1", "Thinking", "Rumination", "QwQ"]
is_reasoning_model = any(k in model for k in is_reasoning_model_keywords)
payload = {
"model": model,
"messages": [
{"role": "user", "content": question},
],
"max_tokens": 2048,
"temperature": 0.6 if is_reasoning_model else 0.7,
"top_p": 0.95,
# 如果你想玩更多参数,可以在这里继续加
}
# 某些推理模型/平台支持通过 extra_body 控制是否返回 reasoning_content,
# 在硅基流动上如果文档提到支持,可以打开下面注释:
# payload["extra_body"] = {"return_reasoning": True}
return payload
def call_model(model: str, question: str) -> str:
"""调用单个模型并返回要写入文件的文本内容."""
payload = build_payload(model, question)
resp = requests.post(API_URL, headers=HEADERS, json=payload, timeout=600)
try:
resp.raise_for_status()
except requests.HTTPError as e:
return f"调用模型 {model} 出错:{e}\n响应内容:{resp.text}"
data = resp.json()
try:
message = data["choices"][0]["message"]
except (KeyError, IndexError, TypeError) as e:
return f"解析返回结果失败:{e}\n原始响应:{data}"
# chat.completions 返回结构示例参考官方文档和示例:contentReference[oaicite:2]{index=2}
content = message.get("content", "") or ""
# 推理模型特有字段:reasoning_content(思维链):contentReference[oaicite:3]{index=3}
reasoning = message.get("reasoning_content")
# 如果有思维链,就一起写到文件里
if reasoning:
return (
"=== reasoning_content(思维链) ===\n"
+ reasoning
+ "\n\n=== content(最终回答) ===\n"
+ content
)
else:
return content
def ask_all_models(question: str, output_dir: str = "outputs") -> None:
"""把问题发给所有模型,并将结果分别写入对应 txt 文件."""
os.makedirs(output_dir, exist_ok=True)
for model in MODELS:
print(f"正在调用模型:{model}")
result_text = call_model(model, question)
filename = sanitize_model_name(model) + ".txt"
file_path = os.path.join(output_dir, filename)
with open(file_path, "w", encoding="utf-8") as f:
f.write(result_text)
print(f" -> 已保存到 {file_path}")
if __name__ == "__main__":
user_question = input("请输入你的问题:\n> ").strip()
if not user_question:
print("问题不能为空。")
else:
ask_all_models(user_question)
print("全部模型调用完成。")
运行结果:

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)