UnionLLM
用一套 OpenAI 风格的 model / messages 调用方式,访问国内外的大模型和 Agent 平台的 Python 库。
UnionLLM 负责选择 provider、适配请求与响应格式,并尽量把结果统一成 OpenAI 风格的 ModelResponse。它是一个 Python 库,不是 HTTP 代理服务。
0.3.0 起,LiteLLM 改为可选依赖。 Azure OpenAI、xAI、OpenRouter、Groq 和自定义 OpenAI 兼容端点改为直接用 openai SDK 调用,支持 openai 3.x 与 anthropic 1.x。从 0.2.x 升级前请阅读迁移指南。
特点
- 一套调用方式:
unionchat(provider=..., model=..., messages=...),流式与非流式都返回 OpenAI 风格的对象。 - 国内模型直接适配:智谱、Moonshot(Kimi)、通义千问、DeepSeek、豆包、文心、讯飞星火、MiniMax、百川、零一万物、阶跃星辰、天工。
- 海外模型用官方 SDK:OpenAI、Anthropic、Gemini、Azure OpenAI 与 Azure 上的 Claude。
- Agent 平台:Coze、Dify、FastGPT。
- 其他 provider 可选 LiteLLM:Bedrock、Vertex AI、Mistral、Ollama 等,按需安装。
安装
python -m pip install unionllm
需要 LiteLLM 路由时:
python -m pip install "unionllm[litellm]"
Python 要求 3.10–3.13。LiteLLM 要求 openai 2.x,详见迁移指南。
快速开始
export ZHIPU_API_KEY="..."
from unionllm import unionchat
response = unionchat(
provider="zhipuai",
model="glm-4",
messages=[{"role": "user", "content": "你好"}],
)
print(response.choices[0].message.content)
流式调用返回可迭代的 chunk:
for chunk in unionchat(
provider="openai",
model="gpt-5.4-mini",
messages=[{"role": "user", "content": "用一句话介绍 UnionLLM"}],
stream=True,
):
if chunk.choices and getattr(chunk.choices[0].delta, "content", None):
print(chunk.choices[0].delta.content, end="")
需要复用配置时,使用 UnionLLM 客户端:
from unionllm import UnionLLM
client = UnionLLM(provider="deepseek")
response = client.completion(
model="deepseek-chat",
messages=[{"role": "user", "content": "你好"}],
)
调用约定、异步、流式与错误处理见入门指南;更多示例见 examples/。
支持范围
| 类别 | provider 取值 |
实现 |
|---|---|---|
| 国内模型 | zhipuai、moonshot、qwen、deepseek、doubao、wenxin、xunfei、xunfei_http、minimax、baichuan、lingyi、stepfun、tiangong |
各自的适配器 |
| 海外模型 | openai、anthropic、gemini |
官方 SDK |
| Azure | azure |
Claude 走 AnthropicFoundry,其他模型走 openai SDK 的 AzureOpenAI |
| OpenAI 兼容 | xai、openrouter、groq,以及 custom_llm_provider="openai" 加 api_base 的自定义端点 |
openai SDK |
| Agent 平台 | coze、dify、fastgpt |
各自的适配器 |
| LiteLLM(可选) | bedrock、vertex_ai、mistral、ollama、together_ai、perplexity、cohere 等 |
需安装 unionllm[litellm] |
“支持”不代表能力相同。多模态、工具调用、推理参数、缓存和流式 usage 都取决于具体适配器。每个 provider 的环境变量、默认地址和特有参数见 Provider 矩阵。
兼容性
| 依赖 | 范围 |
|---|---|
| Python | 3.10–3.13 |
| openai | 2.x、3.x |
| anthropic | 0.x、1.x、2.x |
| litellm(可选) | >=1.75,<1.95,要求 openai 2.x |
以上组合在仓库测试中验证,测试使用模拟的上游响应;Azure Claude 另外在 anthropic 0.x、1.x、2.x 下用真实请求验证过。
文档
- 入门指南:安装、
unionchat与UnionLLM、流式、异步、响应与错误。 - Provider 矩阵:所有路由、环境变量、特有参数。
- 从 0.2.x 迁移到 0.3.0:LiteLLM 改为可选后的变化与检查清单。
- 架构说明:请求路径、目录分工与统一层的边界。
- 贡献指南:本地测试与提交要求。
- 变更日志
- 项目治理与改进计划
边界
- 公开 API 只有
unionchat与UnionLLM,入口在unionllm/main.py。 - 适配器不支持的参数可能被过滤、改写或忽略,不要假设任意 OpenAI 参数在所有 provider 上都生效。
- Agent 平台的
model只是占位值,实际调用什么由平台上的应用配置决定。 - 本仓库只包含 Python 库,不包含服务端、命令行工具或文档站点。
贡献
欢迎提交 issue 和 PR。提交前请阅读贡献指南,并至少运行:
python -m pip install -e . -r requirements-dev.txt
python -m pytest --ignore=tests/my
许可证:MIT。
Metadata
Release files for unionllm 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| unionllm-0.3.0.tar.gz | 73.9 kB | Without provenance | Details for unionllm-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| unionllm-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for unionllm-0.3.0-py3-none-any.whl |
Total release size: 169.7 kB
Release files / unionllm-0.3.0.tar.gz
| Download URL | unionllm-0.3.0.tar.gz |
|---|---|
| Size | 73.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e29325e5d74d49c1783a159a2a6160e0348bb8ac7a86e74df49289499571ecc3
|
|
BLAKE2b-256 checksum How to use checksums |
3c5f1f68a06bc38412ecc4c59ffdd099cebc579b9b4b70a675348f4282e53425
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.13
|
Release files / unionllm-0.3.0-py3-none-any.whl
| Download URL | unionllm-0.3.0-py3-none-any.whl |
|---|---|
| Size | 95.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9a40a7d2f250070a94fb72d819d9574cd823be384a0ca8396ae37f21c01bedef
|
|
BLAKE2b-256 checksum How to use checksums |
9b910cd47ecc08a1c2c951f67d6047c114968bedb87584a9c03723bbb1d224b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.13
|
Security for unionllm 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.