项目文件夹

文件
wehub-resource-sync 81ef904570
Check Markdown links / linkChecker (push) Has been cancelled
docs: make Chinese README the default
2026-07-13 10:47:22 +00:00

344 行
22 KiB
Markdown

此文件含有模棱两可的 Unicode 字符
此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
<!-- WEHUB_ZH_README -->
> [!NOTE]
> 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
> [English](./README.en.md) · [原始项目](https://github.com/Portkey-AI/gateway) · [上游 README](https://github.com/Portkey-AI/gateway/blob/HEAD/README.md)
> 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
<p align="right">
<strong>English</strong> | <a href="./.github/README.cn.md">中文</a> | <a href="./.github/README.jp.md">日本語</a>
</p>
> [!IMPORTANT]
> :rocket: Gateway 2.0(预发布版)Portkey 的核心企业级网关将随 2.0 版本发布并入开源项目。你可以在此试用预发布分支 [此处](https://github.com/portkey-ai/gateway/tree/2.0.0).
> 在我们的 [**A 轮融资公告**](https://portkey.wiki/rohit-a). 中了解更多 Portkey 的未来规划
<div align="center">
🆕 **[Portkey Models](https://github.com/Portkey-AI/models)** - 覆盖 40+ 提供商、2,300+ 模型的开源 LLM 定价。[Explore →](https://portkey.ai/models)
# AI Gateway
#### 通过 1 个快速友好的 API 路由至 250+ 个 LLM
<img src="https://cfassets.portkey.ai/sdk.gif" width="550px" alt="Portkey AI Gateway 演示:展示 LLM 路由能力" style="margin-left:-35px">
[文档](https://portkey.wiki/gh-1) | [企业版](https://portkey.wiki/gh-2) | [托管网关](https://portkey.wiki/gh-3) | [更新日志](https://portkey.wiki/gh-4) | [API 参考](https://portkey.wiki/gh-5)
[![License](https://img.shields.io/github/license/Ileriayo/markdown-badges)](./LICENSE)
[![Discord](https://img.shields.io/discord/1143393887742861333)](https://portkey.wiki/gh-6)
[![Twitter](https://img.shields.io/twitter/url/https/twitter/follow/portkeyai?style=social&label=Follow%20%40PortkeyAI)](https://portkey.wiki/gh-7)
[![npm version](https://badge.fury.io/js/%40portkey-ai%2Fgateway.svg)](https://portkey.wiki/gh-8)
[![Better Stack Badge](https://uptime.betterstack.com/status-badges/v1/monitor/q94g.svg)](https://portkey.wiki/gh-9)
<a href="https://us-east-1.console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/quickcreate?stackName=portkey-gateway&templateURL=https://portkey-gateway-ec2-quicklaunch.s3.us-east-1.amazonaws.com/portkey-gateway-ec2-quicklaunch.template.yaml"><img src="https://img.shields.io/badge/Deploy_to_EC2-232F3E?style=for-the-badge&logo=amazonwebservices&logoColor=white" alt="部署到 AWS EC2" width="105"/></a> [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/Portkey-AI/gateway)
</div>
<br/>
[**AI Gateway**](https://portkey.wiki/gh-10) 专为快速、可靠、安全地路由至 1600+ 语言、视觉、音频和图像模型而设计。这是一款轻量级、开源且企业就绪(enterprise-ready)的解决方案,可让你在 2 分钟内集成任意语言模型。
- [x] **极速**(延迟 <1ms),体积小巧(122kb
- [x] **久经考验**,每天处理超过 100 亿 token
- [x] **企业就绪**,具备增强的安全性、可扩展性和自定义部署能力
<br>
#### AI Gateway 能做什么?
- 在 2 分钟内集成任意 LLM - [快速开始](#quickstart-2-mins)
- 通过 **[自动重试](https://portkey.wiki/gh-11)** 和 **[故障回退](https://portkey.wiki/gh-12)** 防止服务中断
- 借助 **[负载均衡](https://portkey.wiki/gh-13)** 和 **[条件路由](https://portkey.wiki/gh-14)** 扩展 AI 应用
- 使用 **[防护栏(guardrails](https://portkey.wiki/gh-15)** 保护 AI 部署
- 借助 **[多模态能力](https://portkey.wiki/gh-16)** 超越纯文本场景
- 探索 **[智能体工作流(agentic workflow](https://portkey.wiki/gh-17)** 集成
- 使用 **[MCP Gateway](https://portkey.ai/docs/product/mcp-gateway)** 以企业级认证和可观测性管理 MCP 服务器
<br><br>
> [!TIP]
> 为本仓库加 Star 可帮助更多开发者发现 AI Gateway 🙏🏻
>
> ![star-2](https://github.com/user-attachments/assets/53597dce-6333-4ecc-a154-eb05532954e4)
>
<br>
<br>
## 快速开始(2 分钟)
### 1. 配置 AI Gateway
```bash
# Run the gateway locally (needs Node.js and npm)
npx @portkey-ai/gateway
```
> Gateway 运行在 `http://localhost:8787/v1`
>
> Gateway 控制台运行在 `http://localhost:8787/public/`
<sup>
部署指南:
&nbsp; <a href="https://portkey.wiki/gh-18"><img height="12" width="12" src="https://cfassets.portkey.ai/logo/dew-color.svg" /> Portkey Cloud(推荐)</a>
&nbsp; <a href="./docs/installation-deployments.md#docker"><img height="12" width="12" src="https://cdn.simpleicons.org/docker/3776AB" /> Docker</a>
&nbsp; <a href="./docs/installation-deployments.md#nodejs-server"><img height="12" width="12" src="https://cdn.simpleicons.org/node.js/3776AB" /> Node.js</a>
&nbsp; <a href="./docs/installation-deployments.md#cloudflare-workers"><img height="12" width="12" src="https://cdn.simpleicons.org/cloudflare/3776AB" /> Cloudflare</a>
&nbsp; <a href="./docs/installation-deployments.md#replit"><img height="12" width="12" src="https://cdn.simpleicons.org/replit/3776AB" /> Replit</a>
&nbsp; <a href="./docs/installation-deployments.md"> 其他...</a>
</sup>
### 2. 发起首次请求
<!-- <details open>
<summary>Python Example</summary> -->
```python
# pip install -qU portkey-ai
from portkey_ai import Portkey
# OpenAI compatible client
client = Portkey(
provider="openai", # or 'anthropic', 'bedrock', 'groq', etc
Authorization="sk-***" # the provider API key
)
# Make a request through your AI Gateway
client.chat.completions.create(
messages=[{"role": "user", "content": "What's the weather like?"}],
model="gpt-4o-mini"
)
```
<sup>支持的库:
&nbsp; [<img height="12" width="12" src="https://cdn.simpleicons.org/javascript/3776AB" /> JS](https://portkey.wiki/gh-19)
&nbsp; [<img height="12" width="12" src="https://cdn.simpleicons.org/python/3776AB" /> Python](https://portkey.wiki/gh-20)
&nbsp; [<img height="12" width="12" src="https://cdn.simpleicons.org/gnubash/3776AB" /> REST](https://portkey.sh/gh-84)
&nbsp; [<img height="12" width="12" src="https://cdn.simpleicons.org/openai/3776AB" /> OpenAI SDKs](https://portkey.wiki/gh-21)
&nbsp; [<img height="12" width="12" src="https://cdn.simpleicons.org/langchain/3776AB" /> Langchain](https://portkey.wiki/gh-22)
&nbsp; [LlamaIndex](https://portkey.wiki/gh-23)
&nbsp; [Autogen](https://portkey.wiki/gh-24)
&nbsp; [CrewAI](https://portkey.wiki/gh-25)
&nbsp; [More..](https://portkey.wiki/gh-26)
</sup>
在 Gateway 控制台(`http://localhost:8787/public/`)中,你可以在一处查看所有本地日志。
<img src="https://github.com/user-attachments/assets/362bc916-0fc9-43f1-a39e-4bd71aac4a3a" width="400" />
### 3. 路由与防护栏(Guardrails
LLM 网关中的 `Configs` 允许你创建路由规则、增强可靠性并配置防护栏。
```python
config = {
"retry": {"attempts": 5},
"output_guardrails": [{
"default.contains": {"operator": "none", "words": ["Apple"]},
"deny": True
}]
}
# Attach the config to the client
client = client.with_options(config=config)
client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Reply randomly with Apple or Bat"}]
)
# This would always response with "Bat" as the guardrail denies all replies containing "Apple". The retry config would retry 5 times before giving up.
```
<div align="center">
<img src="https://portkey.ai/blog/content/images/size/w1600/2024/11/image-15.png" width=600 title="请求经由 Portkey AI Gateway 的流程,包含重试与防护栏" alt="请求经由 Portkey AI Gateway 的流程,包含重试与防护栏"/>
</div>
你还可以在 AI Gateway 中通过配置实现更多功能。[跳转到示例 →](https://portkey.wiki/gh-27)
<br/>
### 企业版(私有部署)
<sup>
[<img height="12" width="12" src="https://cfassets.portkey.ai/amazon-logo.svg" /> AWS](https://portkey.wiki/gh-28)
&nbsp; [<img height="12" width="12" src="https://cfassets.portkey.ai/azure-logo.svg" /> Azure](https://portkey.wiki/gh-29)
&nbsp; [<img height="12" width="12" src="https://cdn.simpleicons.org/googlecloud/3776AB" /> GCP](https://portkey.wiki/gh-30)
&nbsp; [<img height="12" width="12" src="https://cdn.simpleicons.org/redhatopenshift/3776AB" /> OpenShift](https://portkey.wiki/gh-31)
&nbsp; [<img height="12" width="12" src="https://cdn.simpleicons.org/kubernetes/3776AB" /> Kubernetes](https://portkey.wiki/gh-85)
</sup>
</sup>
LLM Gateway 的[企业版](https://portkey.wiki/gh-86) 提供面向**组织管理**、**治理**、**安全**及[更多能力](https://portkey.wiki/gh-87) 的开箱即用高级功能。[查看功能对比 →](https://portkey.wiki/gh-32)
各支持平台的企业部署架构可在此查看 — [**企业私有云部署**](https://portkey.ai/docs/self-hosting/hybrid-deployments/architecture)
<a href="https://portkey.sh/demo-13"><img src="https://portkey.ai/blog/content/images/2024/08/Get-API-Key--5-.png" height=50 alt="预约企业 AI Gateway 演示" /></a><br/>
<br>
## MCP Gateway
[MCP Gateway](https://portkey.ai/docs/product/mcp-gateway) 为你的组织提供集中式控制平面,用于管理 MCPModel Context Protocol,模型上下文协议)服务器。
- **Authentication(身份认证)** — 在网关层提供统一认证。用户只需认证一次;你的 MCP 服务器将收到已验证的请求
- **Access Control(访问控制)** — 控制哪些团队和用户可以访问哪些服务器与工具。可即时撤销访问权限
- **Observability(可观测性)** — 记录每一次工具调用的完整上下文:谁调用了什么、参数、响应、延迟
- **Identity Forwarding(身份转发)** — 自动将用户身份(邮箱、团队、角色)转发至 MCP 服务器
可与 Claude Desktop、Cursor、VS Code 及任何兼容 MCP 的客户端配合使用。[立即开始 →](https://portkey.ai/docs/product/mcp-gateway/quickstart)
<br>
## 核心功能
### 可靠路由
- <a href="https://portkey.wiki/gh-37">**Fallbacks(降级回退)**</a>:通过 LLM Gateway 在请求失败时回退到另一供应商或模型。你可指定触发回退的错误类型,从而提升应用的可靠性。
- <a href="https://portkey.wiki/gh-38">**Automatic Retries(自动重试)**</a>:自动重试失败的请求,最多 5 次。采用指数退避策略分散重试间隔,避免网络过载。
- <a href="https://portkey.wiki/gh-39">**Load Balancing(负载均衡)**</a>:按权重将 LLM 请求分发到多个 API 密钥或 AI 供应商,确保高可用与最佳性能。
- <a href="https://portkey.wiki/gh-40">**Request Timeouts(请求超时)**</a>:通过细粒度请求超时管理难以控制的 LLM 与延迟,自动终止超过指定时长的请求。
- <a href="https://portkey.wiki/gh-41">**Multi-modal LLM Gateway(多模态 LLM Gateway**</a>:调用多家供应商的视觉、音频(文本转语音与语音转文本)及图像生成模型 — 均使用熟悉的 OpenAI 签名
- <a href="https://portkey.wiki/gh-42">**Realtime APIs(实时 API**</a>:通过集成的 WebSocket 服务器调用 OpenAI 推出的实时 API。
### 安全与准确性
- <a href="https://portkey.wiki/gh-88">**Guardrails(护栏)**</a>:校验 LLM 输入与输出是否符合你指定的检查项。可从 40+ 预置护栏中选择,确保符合安全与准确性标准。你可以<a href="https://portkey.wiki/gh-43">自带护栏</a>,或从我们的<a href="https://portkey.wiki/gh-44">众多合作伙伴</a>中选择。
- [**Secure Key Management(安全密钥管理)**](https://portkey.wiki/gh-45): 使用你自己的密钥,或即时生成虚拟密钥。
- [**Role-based access control(基于角色的访问控制)**](https://portkey.wiki/gh-46): 为用户、工作区与 API 密钥提供细粒度访问控制。
- <a href="https://portkey.wiki/gh-47">**Compliance & Data Privacy(合规与数据隐私)**</a>:该 AI Gateway 符合 SOC2、HIPAA、GDPR 与 CCPA 合规要求。
### 成本管理
- [**Smart caching(智能缓存)**](https://portkey.wiki/gh-48): 缓存 LLM 响应以降低成本并改善延迟。支持简单缓存与语义*缓存。
- [**Usage analytics(用量分析)**](https://portkey.wiki/gh-49): 监控并分析 AI 与 LLM 用量,包括请求量、延迟、成本与错误率。
- [**Provider optimization*(供应商优化)**](https://portkey.wiki/gh-89): 根据使用模式与定价模型自动切换到最具成本效益的供应商。
### 协作与工作流
- <a href="https://portkey.ai/docs/integrations/agents">**Agents SupportAgent 支持)**</a>:与主流 Agent 框架无缝集成,构建复杂 AI 应用。该 Gateway 可与 [Autogen](https://portkey.wiki/gh-50), [CrewAI](https://portkey.wiki/gh-51), [LangChain](https://portkey.wiki/gh-52), [LlamaIndex](https://portkey.wiki/gh-53), [Phidata](https://portkey.wiki/gh-54), [Control Flow](https://portkey.wiki/gh-55), 乃至 [Custom Agents](https://portkey.wiki/gh-56). 无缝集成
- [**Prompt Template Management*(提示词模板管理)**](https://portkey.wiki/gh-57): 通过通用提示词 Playground 协作创建、管理与版本化提示词模板。
<br/><br/>
<sup>
*&nbsp;在托管版与企业版中可用
</sup>
<br>
## Portkey Models
面向 40+ 供应商的开源 LLM 定价数据库 — 供 Gateway 用于成本追踪。
[GitHub](https://github.com/Portkey-AI/models) | [Model Explorer](https://portkey.ai/models)
<br>
## Cookbooks
### ☄️ 热门
- 通过 AI Gateway 使用 [Nvidia NIM](/cookbook/providers/nvidia.ipynb) 模型
- 使用 Portkey 监控 [CrewAI Agents](/cookbook/monitoring-agents/CrewAI_with_Telemetry.ipynb)
- 使用 AI Gateway 对比 [Top 10 LMSYS Models](/cookbook/use-cases/LMSYS%20Series/comparing-top10-LMSYS-models-with-Portkey.ipynb)。
### 🚨 最新
* [使用 Nemotron 创建合成数据集](/cookbook/use-cases/Nemotron_GPT_Finetuning_Portkey.ipynb)
* [将 LLM Gateway 与 Vercel 的 AI SDK 配合使用](/cookbook/integrations/vercel-ai.md)
* [使用 Portkey 的 LLM Gateway 监控 Llama Agents](/cookbook/monitoring-agents/Llama_Agents_with_Telemetry.ipynb)
[查看全部 Cookbooks →](https://portkey.wiki/gh-58)
<br/><br/>
## 支持的供应商
探索 Gateway 与 [45+ 供应商](https://portkey.wiki/gh-59) 及 [8+ Agent 框架](https://portkey.wiki/gh-90). 的集成
| | Provider | Support | Stream |
| -------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | ------- | ------ |
| <img src="docs/images/openai.png" width=35 /> | [OpenAI](https://portkey.wiki/gh-60) | ✅ | ✅ |
| <img src="docs/images/azure.png" width=35> | [Azure OpenAI](https://portkey.wiki/gh-61) | ✅ | ✅ |
| <img src="docs/images/anyscale.png" width=35> | [Anyscale](https://portkey.wiki/gh-62) | ✅ | ✅ |
| <img src="https://upload.wikimedia.org/wikipedia/commons/2/2d/Google-favicon-2015.png" width=35> | [Google Gemini](https://portkey.wiki/gh-63) | ✅ | ✅ |
| <img src="docs/images/anthropic.png" width=35> | [Anthropic](https://portkey.wiki/gh-64) | ✅ | ✅ |
| <img src="docs/images/cohere.png" width=35> | [Cohere](https://portkey.wiki/gh-65) | ✅ | ✅ |
| <img src="https://assets-global.website-files.com/64f6f2c0e3f4c5a91c1e823a/654693d569494912cfc0c0d4_favicon.svg" width=35> | [Together AI](https://portkey.wiki/gh-66) | ✅ | ✅ |
| <img src="https://www.perplexity.ai/favicon.svg" width=35> | [Perplexity](https://portkey.wiki/gh-67) | ✅ | ✅ |
| <img src="https://docs.mistral.ai/img/favicon.ico" width=35> | [Mistral](https://portkey.wiki/gh-68) | ✅ | ✅ |
| <img src="https://docs.nomic.ai/img/nomic-logo.png" width=35> | [Nomic](https://portkey.wiki/gh-69) | ✅ | ✅ |
| <img src="https://files.readme.io/d38a23e-small-studio-favicon.png" width=35> | [AI21](https://portkey.wiki/gh-91) | ✅ | ✅ |
| <img src="https://platform.stability.ai/small-logo-purple.svg" width=35> | [Stability AI](https://portkey.wiki/gh-71) | ✅ | ✅ |
| <img src="https://deepinfra.com/_next/static/media/logo.4a03fd3d.svg" width=35> | [DeepInfra](https://portkey.sh/gh-92) | ✅ | ✅ |
| <img src="https://ollama.com/public/ollama.png" width=35> | [Ollama](https://portkey.wiki/gh-72) | ✅ | ✅ |
| <img src="https://novita.ai/favicon.ico" width=35> | [Novita AI](https://portkey.wiki/gh-73) | ✅ | ✅ | `/chat/completions`, `/completions` |
> [在此查看 200+ 款受支持模型的完整列表](https://portkey.wiki/gh-74)
<br>
<br>
## 智能体(Agents
Gateway 可与主流智能体框架无缝集成。[在此阅读文档](https://portkey.wiki/gh-75).
| 框架 | 调用 200+ 款 LLM | 高级路由(Advanced Routing | 缓存(Caching | 日志与追踪*Logging & Tracing | 可观测性*Observability | 提示词管理*Prompt Management |
|------------------------------|--------|-------------|---------|------|---------------|-------------------|
| [Autogen](https://portkey.wiki/gh-93) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| [CrewAI](https://portkey.wiki/gh-94) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| [LangChain](https://portkey.wiki/gh-95) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| [Phidata](https://portkey.wiki/gh-96) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| [Llama Index](https://portkey.wiki/gh-97) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| [Control Flow](https://portkey.wiki/gh-98) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| [Build Your Own Agents](https://portkey.wiki/gh-99) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| <img src="https://io.net/favicon.ico" width=35> | [IO Intelligence](https://io.net/intelligence) | ✅ | ✅ |
<br>
*功能适用于[托管应用](https://portkey.wiki/gh-76). 详细文档请[点击此处](https://portkey.wiki/gh-100).
## Gateway 企业版
让你的 AI 应用更<ins>可靠</ins>、更具<ins>前向兼容性</ins>,同时确保完全的<ins>数据安全</ins>与<ins>隐私</ins>保护。
✅&nbsp; 安全密钥管理(Secure Key Management)—— 用于基于角色的访问控制与追踪 <br>
✅&nbsp; 简单与语义缓存(Simple & Semantic Caching)—— 更快响应重复查询并节省成本 <br>
✅&nbsp; 访问控制与入站规则(Access Control & Inbound Rules)—— 控制哪些 IP 与地理区域可连接到你的部署 <br>
✅&nbsp; PII 脱敏(PII Redaction)—— 自动从请求中移除敏感数据,防止无意泄露 <br>
✅&nbsp; SOC2、ISO、HIPAA、GDPR 合规 —— 遵循最佳安全实践 <br>
✅&nbsp; 专业支持 —— 并提供功能优先级排序 <br>
[预约通话,讨论企业版部署](https://portkey.sh/demo-13)
<br>
## 贡献
最简单的贡献方式是挑选带有 `good first issue` 标签的 issue 💪。贡献指南请见[此处](/.github/CONTRIBUTING.md)。
Bug 报告?[在此提交](https://portkey.wiki/gh-78) | 功能请求?[在此提交](https://portkey.wiki/gh-78)
### 加入社区入门
每周五(太平洋时间上午 8 点)参加我们的 AI Engineering Hours,你可以:
- 结识其他贡献者与社区成员
- 学习 Gateway 高级功能与实现模式
- 分享经验并获得帮助
- 了解最新开发优先级
[参加下一场活动 →](https://portkey.wiki/gh-101) | [会议纪要](https://portkey.wiki/gh-102)
<br>
## 社区
加入我们在全球不断壮大的社区,获取 AI 相关帮助、灵感与讨论。
- 浏览我们的官方[博客](https://portkey.wiki/gh-78)
- 在 [Discord](https://portkey.wiki/community) 与我们聊天
- 在 [Twitter](https://portkey.wiki/gh-79) 关注我们
- 在 [LinkedIn](https://portkey.wiki/gh-80) 与我们连接
- 阅读[日语](./.github/README.jp.md)版文档
- 在 [YouTube](https://portkey.wiki/gh-103) 访问我们
- 加入我们的[开发者社区](https://portkey.wiki/gh-82)
<!-- - 在 [Stack Overflow](https://stackoverflow.com/questions/tagged/portkey) 上搜索 #portkey 标签的问题 -->
![Rubeus Social Share (4)](https://github.com/Portkey-AI/gateway/assets/971978/89d6f0af-a95d-4402-b451-14764c40d03f)