OpenAI 透传
/openai
的透传端点
概述
功能 | 支持 | 备注 |
---|---|---|
成本跟踪 | ❌ | 不支持 |
日志记录 | ✅ | 适用于所有集成 |
流式传输 | ✅ | 完全支持 |
何时使用?
- 对于 90% 的用例,您应该使用原生的 LiteLLM OpenAI 集成(
/chat/completions
,/embeddings
,/completions
,/images
,/batches
等) - 使用此透传功能调用 LiteLLM 尚未完全支持的较少使用或较新的 OpenAI 端点,例如
/assistants
,/threads
,/vector_stores
只需将 https://api.openai.com
替换为 LITELLM_PROXY_BASE_URL/openai
使用示例
Assistants API
创建 OpenAI 客户端
请确保执行以下操作
- 将
base_url
指向您的LITELLM_PROXY_BASE_URL/openai
- 使用您的
LITELLM_API_KEY
作为api_key
import openai
client = openai.OpenAI(
base_url="http://0.0.0.0:4000/openai", # <your-proxy-url>/openai
api_key="sk-anything" # <your-proxy-api-key>
)
创建 Assistant
# Create an assistant
assistant = client.beta.assistants.create(
name="Math Tutor",
instructions="You are a math tutor. Help solve equations.",
model="gpt-4o",
)
创建 Thread
# Create a thread
thread = client.beta.threads.create()
向 Thread 添加消息
# Add a message
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="Solve 3x + 11 = 14",
)
运行 Assistant
# Create a run to get the assistant's response
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
)
# Check run status
run_status = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
检索消息
# List messages after the run completes
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
删除 Assistant
# Delete the assistant when done
client.beta.assistants.delete(assistant.id)