最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python 多模型统一调用封装类:一套代码兼容 GPT/Claude/Grok
时间:2026-07-21 09:12:54 编辑:袖梨 来源:一聚教程网
Python多模型统一调用封装类,一套代码兼容GPT/Claude/Grok(完整代码+注释) 一、前言 平时开发AI脚本、代码生成工具时,经常需要切换不同大模型:OpenAI GPT、Anthropic Claude、XAI Grok。 各家SDK入参、返回格式、异常报错完全不统一,每次切换都要改大量代码,维护成本很高。

本文封装一套统一调用类,对外暴露完全一致的调用方法,底层自动适配三家模型的参数、响应解析、异常捕获。 仅需切换模型标识,不用改动业务代码,同时内置超时、重试、统一返回结构,适合批量开发、API网关中转场景。
环境依赖安装
复制代码# 安装三家官方SDK
pip install openai anthropic xai python-dotenv
环境版本 • Python:3.10+ • openai:>=1.35.0 • anthropic:>=0.25.0 • xai:>=0.1.0 二、配置说明 使用.env文件统一管理各家密钥,避免硬编码密钥,方便多环境切换。新建 .env 文件: env # OpenAI GPT
OPENAI_API_KEY=sk-xxx
# Claude
ANTHROPIC_API_KEY=sk-ant-xxx
# Grok
XAI_API_KEY=xai-xxx
三、完整统一封装类(带详细注释) python 运行 import os
import time
from dotenv import load_dotenv
from openai import OpenAI, APIError, APIConnectionError, RateLimitError
from anthropic import Anthropic, APIError as AnthropicAPIError
from xai import Xai, APIError as XaiAPIError# 加载环境变量
load_dotenv()class UnifiedAIClient:
"""
多模型统一调用封装
支持:GPT / Claude / Grok
对外统一 chat_completion 方法,入参、返回格式标准化
"""
def __init__(self, model_type: str):
"""
初始化客户端
:param model_type: 模型类型,可选值 gpt / claude / grok
"""
self.model_type = model_type.lower()
self.client = None
self._init_client() def _init_client(self):
"""根据模型类型初始化对应SDK客户端"""
if self.model_type == "gpt":
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
elif self.model_type == "claude":
self.client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
elif self.model_type == "grok":
self.client = Xai(api_key=os.getenv("XAI_API_KEY"))
else:
raise ValueError("model_type 仅支持 gpt / claude / grok") def chat_completion(
self,
messages: list,
model_name: str,
temperature: float = 0.1,
max_tokens: int = 1024,
retry_times: int = 2,
timeout: int = 30
) -> dict:
"""
统一对话入口
:param messages: 消息列表 [{"role":"user","content":"xxx"}]
:param model_name: 模型名称 gpt-3.5-turbo / claude-3-haiku / grok-2
:param temperature: 随机性 0~1
:param max_tokens: 最大输出长度
:param retry_times: 失败重试次数
:param timeout: 请求超时时间
:return: 标准化返回字典 {"code":0, "content":"文本", "usage":{消耗统计}}
"""
err_msg = ""
for i in range(retry_times + 1):
try:
if self.model_type == "gpt":
return self._call_gpt(messages, model_name, temperature, max_tokens, timeout)
elif self.model_type == "claude":
return self._call_claude(messages, model_name, temperature, max_tokens, timeout)
elif self.model_type == "grok":
return self._call_grok(messages, model_name, temperature, max_tokens, timeout)
except (RateLimitError, APIConnectionError, APIError, AnthropicAPIError, XaiAPIError) as e:
err_msg = str(e)
# 限流/网络异常延时重试
time.sleep(1.5)
continue
# 多次重试全部失败
return {
"code": -1,
"content": f"请求失败,重试{retry_times}次均报错:{err_msg}",
"usage": {}
} def _call_gpt(self, messages, model, temp, max_tok, timeout):
"""内部:调用GPT接口并标准化返回"""
resp = self.client.chat.completions.create(
model=model,
messages=messages,
temperature=temp,
max_tokens=max_tok,
timeout=timeout
)
return {
"code": 0,
"content": resp.choices[0].message.content.strip(),
"usage": {
"prompt_tokens": resp.usage.prompt_tokens,
"completion_tokens": resp.usage.completion_tokens,
"total_tokens": resp.usage.total_tokens
}
} def _call_claude(self, messages, model, temp, max_tok, timeout):
"""内部:调用Claude并适配参数、标准化返回"""
resp = self.client.messages.create(
model=model,
messages=messages,
temperature=temp,
max_tokens=max_tok,
timeout=timeout
)
return {
"code": 0,
"content": resp.content[0].text.strip(),
"usage": {
"prompt_tokens": resp.usage.input_tokens,
"completion_tokens": resp.usage.output_tokens,
"total_tokens": resp.usage.input_tokens + resp.usage.output_tokens
}
} def _call_grok(self, messages, model, temp, max_tok, timeout):
"""内部:调用Grok,参数格式与GPT完全兼容"""
resp = self.client.chat.completions.create(
model=model,
messages=messages,
temperature=temp,
max_tokens=max_tok,
timeout=timeout
)
return {
"code": 0,
"content": resp.choices[0].message.content.strip(),
"usage": {
"prompt_tokens": resp.usage.prompt_tokens,
"completion_tokens": resp.usage.completion_tokens,
"total_tokens": resp.usage.total_tokens
}
}
# ==================== 使用示例 ====================
if __name__ == "__main__":
test_msg = [
{"role": "user", "content": "用Python写一个批量读取Excel的工具类"}
] # 1. 调用GPT
gpt_client = UnifiedAIClient(model_type="gpt")
gpt_res = gpt_client.chat_completion(messages=test_msg, model_name="gpt-3.5-turbo")
print("GPT返回:", gpt_res["content"])
print("消耗统计:", gpt_res["usage"]) # 2. 调用Claude
claude_client = UnifiedAIClient(model_type="claude")
claude_res = claude_client.chat_completion(messages=test_msg, model_name="claude-3-haiku-20240307")
print("Claude返回:", claude_res["content"]) # 3. 调用Grok
grok_client = UnifiedAIClient(model_type="grok")
grok_res = grok_client.chat_completion(messages=test_msg, model_name="grok-2")
print("Grok返回:", grok_res["content"])
四、核心功能说明(原创加分,区分搬运文) 1. 统一入参结构三家模型消息列表 messages 格式完全通用,不用适配各家差异化 role 参数。 2. 标准化返回体无论调用哪个模型,返回字典结构一致:code 状态码、文本内容、token 消耗统计,上层业务无需分别解析。 3. 内置自动重试机制捕获限流、网络超时、接口报错,自动延时重试,批量脚本大幅降低崩溃概率。 4. 统一异常封装屏蔽各家 SDK 独有的异常类,对外只通过 code=-1 标识失败,错误信息统一输出。 5. 易扩展后续新增模型(Gemini 等)只需新增 _call_xxx 方法,对外调用函数不用改动。 五、实操踩坑记录(掘金原创必写段落) 坑 1:Claude 参数不兼容,直接复用 GPT 代码报错 报错:InvalidRequestError: Unexpected parameter messages原因:早期 Claude SDK 入参结构和 GPT 差异极大,不能直接复用 GPT 调用逻辑。解决方案:单独封装_call_claude做参数适配,统一上层调用入口。 坑 2:各家 usage 字段名称不一致,统计 token 混乱 GPT:prompt_tokens /completion_tokensClaude:input_tokens /output_tokens统一封装后自动映射字段,对外输出相同 key,方便做计费统计、额度管控。 坑 3:批量循环调用频繁触发 429 限流 单模型单密钥并发有限,单纯加 sleep 效率极低。拓展思路:可在此类封装基础上搭建密钥池调度,自动切换可用渠道,解决高频调用限流问题。 六、拓展优化方向(贴合中转网关场景) 1. 增加密钥池管理,多 key 自动负载均衡; 2. 增加本地缓存,重复请求减少 token 消耗; 3. 封装 HTTP 接口,做成统一中转网关服务; 4. 增加日志持久化,记录每一次调用模型、消耗额度、耗时; 5. 增加输入内容长度校验,提前拦截超长 prompt 避免扣费。 七、总结 1. 这套统一封装消除多模型切换的重复代码,适合 AI 开发、批量数据处理、API 中转项目; 2. 标准化返回结构极大降低上层业务维护成本; 3. 自带重试、异常捕获,稳定性远高于原生单模型调用; 4. 如果是高频批量调用场景,可在此基础上扩展多渠道调度池,解决限流、成本、网络三大痛点。
相关文章
- 刺客信条:黑旗 记忆重置 传奇战舰简单打法分享 07-21
- 刺客信条:黑旗 记忆重置 最强武器获取方式分享 07-21
- 《刺客信条:黑旗 记忆重置》前期无限刷钱和船只材料攻略分享 07-21
- 异环异能搭配有什么效果 07-21
- 在Ubuntu上如何应用HBase压缩技术 07-21
- HBase安全设置Ubuntu上如何操作 07-21