关于 portkey-python-sdk
portkey-python-sdk 是通过 Portkey AI Gateway 将请求路由到 200 多种 LLM 的 Python SDK。它基于 OpenAI SDK 接口,并提供回退、重试、负载均衡、缓存和可观测性控制。
主要功能
- 01统一访问 200 多种 LLM
- 02自动提供商回退与重试
- 03流量负载均衡
- 04语义及精确匹配缓存
- 05跟踪 ID 与请求元数据
适用场景
- 01在多个 LLM 提供商间路由请求
- 02提供商不可用时自动切换
- 03在配置的提供商间分配流量
- 04跟踪并检查生产请求
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ portkey-ai/ skills --skill portkey-python-sdk安装后在 Claude Code 中输入/portkey-python-sdk确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/.gitkeepreferences/.gitkeepADVANCED_FEATURES.mdAPI_REFERENCE.mdINTEGRATIONS.mdPROVIDERS.mdscripts/.gitkeep详细介绍
Portkey Python SDK
Portkey Python SDK 通过 Portkey AI Gateway,为 200 多个大模型提供统一接口。它基于 OpenAI SDK 构建,可无缝兼容,并增加了适用于生产环境的功能:自动故障转移、重试、负载均衡、语义缓存、护栏和全面的可观测性。
其他参考资料:
- API 参考 - 响应结构、错误处理
- 高级功能 - 工具调用、嵌入、音频、图像
- 框架集成 - LangChain、LlamaIndex、Strands、Google ADK
- 提供商配置 - Azure、AWS Bedrock、Vertex AI 设置
安装
pip install portkey-ai
# Or with poetry/uv
poetry add portkey-ai
uv add portkey-ai
快速开始
import os
from portkey_ai import Portkey
client = Portkey(
api_key=os.environ["PORTKEY_API_KEY"],
virtual_key="your-openai-virtual-key"
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
身份验证
API 密钥 + 虚拟密钥(推荐)
虚拟密钥可在 Portkey 的保险库中安全存储提供商 API 密钥:
import os
from portkey_ai import Portkey
client = Portkey(
api_key=os.environ["PORTKEY_API_KEY"], # From app.portkey.ai
virtual_key="openai-virtual-key-xxx" # From app.portkey.ai/virtual-keys
)
使用配置 ID
在控制面板中预先配置路由、故障转移和缓存:
client = Portkey(
api_key=os.environ["PORTKEY_API_KEY"],
config="pc-config-xxx" # Config ID from dashboard
)
聊天补全
基本请求
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing briefly."}
]
)
print(response.choices[0].message.content)
print(f"Tokens used: {response.usage.total_tokens}")
流式传输
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a short story"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
异步支持
import asyncio
from portkey_ai import AsyncPortkey
async def main():
client = AsyncPortkey(
api_key=os.environ["PORTKEY_API_KEY"],
virtual_key="openai-key"
)
response = await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
asyncio.run(main())
异步流式传输
async def stream_response():
client = AsyncPortkey(
api_key=os.environ["PORTKEY_API_KEY"],
virtual_key="openai-key"
)
stream = await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a poem"}],
stream=True
)
async for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
Gateway 功能
故障转移
提供商发生故障时自动切换:
client = Portkey(
api_key=os.environ["PORTKEY_API_KEY"],
config={
"strategy": {"mode": "fallback"},
"targets": [
{
"virtual_key": "openai-key",
"override_params": {"model": "gpt-4o"}
},
{
"virtual_key": "anthropic-key",
"override_params": {"model": "claude-3-5-sonnet-20241022"}
}
]
}
)
# If OpenAI fails, automatically tries Anthropic
response = client.chat.completions.create(
messages=[{"role": "user", "content": "Hello!"}]
)
负载均衡
在多个提供商之间分配流量:
client = Portkey(
api_key=os.environ["PORTKEY_API_KEY"],
config={
"strategy": {"mode": "loadbalance"},
"targets": [
{"virtual_key": "openai-key-1", "weight": 0.7},
{"virtual_key": "openai-key-2", "weight": 0.3}
]
}
)
自动重试
client = Portkey(
api_key=os.environ["PORTKEY_API_KEY"],
config={
"retry": {
"attempts": 3,
"on_status_codes": [429, 500, 502, 503, 504]
},
"virtual_key": "openai-key"
}
)
语义缓存
通过智能缓存降低成本和延迟:
client = Portkey(
api_key=os.environ["PORTKEY_API_KEY"],
config={
"cache": {
"mode": "semantic", # or "simple" for exact match
"max_age": 3600 # TTL in seconds
},
"virtual_key": "openai-key"
}
)
# Similar queries return cached responses
response1 = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "What is the capital of France?"}]
)
response2 = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Tell me France's capital"}]
) # Returns cached response
请求超时
client = Portkey(
api_key=os.environ["PORTKEY_API_KEY"],
virtual_key="openai-key",
request_timeout=30 # 30 seconds
)
可观测性
跟踪 ID
关联相关请求,便于调试:
import uuid
client = Portkey(
api_key=os.environ["PORTKEY_API_KEY"],
virtual_key="openai-key",
trace_id=str(uuid.uuid4())
)
自定义元数据
为请求添加可搜索的元数据:
client = Portkey(
api_key=os.environ["PORTKEY_API_KEY"],
virtual_key="openai-key",
metadata={
"user_id": "user-123",
"session_id": "session-456",
"environment": "production"
}
)
单请求选项
response = client.with_options(
trace_id="unique-trace-id",
metadata={"request_type": "summarization"}
).chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Summarize this..."}]
)
常见模式
多轮对话
messages = [
{"role": "system", "content": "You are a helpful coding assistant."},
{"role": "user", "content": "What is Python?"},
{"role": "assistant", "content": "Python is a high-level programming language..."},
{"role": "user", "content": "Show me a hello world example."}
]
response = client.chat.completions.create(model="gpt-4o", messages=messages)
JSON 输出
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "Extract as JSON with name and age fields."},
{"role": "user", "content": "John is 30 years old."}
],
response_format={"type": "json_object"}
)
# Returns: {"name": "John", "age": 30}
配置故障转移 + 缓存的生产环境设置
def create_production_client():
return Portkey(
api_key=os.environ["PORTKEY_API_KEY"],
config={
"strategy": {"mode": "fallback"},
"targets": [
{
"virtual_key": os.environ["OPENAI_VIRTUAL_KEY"],
"override_params": {"model": "gpt-4o"},
"retry": {"attempts": 2, "on_status_codes": [429, 500]}
},
{
"virtual_key": os.environ["ANTHROPIC_VIRTUAL_KEY"],
"override_params": {"model": "claude-3-5-sonnet-20241022"}
}
],
"cache": {"mode": "semantic", "max_age": 3600}
},
trace_id="production-session",
metadata={"environment": "production"}
)
最佳实践
- 使用环境变量 - 切勿将 API 密钥硬编码
- 实现故障转移 - 生产环境务必准备备用提供商
- 使用流式传输 - 长响应可获得更好的用户体验
- 添加跟踪 - 使用跟踪 ID 和元数据启用可观测性
- 启用缓存 - 使用语义缓存降低成本
- 处理错误 - 使用指数退避实现重试逻辑
资源
- 控制面板:app.portkey.ai
- 文档:docs.portkey.ai
- GitHub:github.com/portkey-ai/portkey-python-sdk
- Discord:portkey.ai/discord
评论
支持 Markdown