Vercel AI Gateway 集成
本指南介绍如何把 Litefuse 与 Vercel AI Gateway 集成。
Vercel AI Gateway 是 Vercel 提供的代理服务,把模型请求转发到各家 AI provider。它向多个 provider 提供统一 API,并能让你设置预算、监控用量、负载均衡请求以及管理 fallback。
由于 Vercel AI Gateway 使用 OpenAI API schema,可以利用 Litefuse 与 OpenAI SDK 的原生集成,Python 与 TypeScript 都可用。
快速开始
pip install langfuse openaiimport os
# Set your Litefuse API keys
LANGFUSE_SECRET_KEY="sk-lf-..."
LANGFUSE_PUBLIC_KEY="pk-lf-..."
LANGFUSE_BASE_URL="https://litefuse.cloud"
# LANGFUSE_BASE_URL="https://litefuse.cloud"
# Set your Vercel AI Gateway API key or OIDC token (Vercel AI Gateway uses the 'AI_GATEWAY_API_KEY' and 'VERCEL_OIDC_TOKEN' environment variables)
os.environ["OPENAI_API_KEY"] = "<YOUR_VERCEL_AI_GATEWAY_API_KEY_OR_OIDC_TOKEN>"示例 1:简单的 LLM 调用
由于 Vercel AI Gateway 提供 OpenAI 兼容 API,我们可以使用 Litefuse 的 OpenAI SDK 包装器自动把 Vercel AI Gateway 的调用记录为 Litefuse 中的 generation。
base_url设置为 Vercel AI Gateway 的 API 端点。- 你可以把
"anthropic/claude-4-sonnet"替换为 Vercel AI Gateway 上的任意模型。 default_headers可包含 Vercel AI Gateway 文档列出的可选 header。
# Import the Langfuse OpenAI SDK wrapper
from langfuse.openai import openai
# Create an OpenAI client with the Vercel AI Gateway base URL
client = openai.OpenAI(
base_url="https://ai-gateway.vercel.sh/v1",
default_headers={
"http-referer": "<YOUR_SITE_URL>", # Optional: Your site URL
"x-title": "<YOUR_SITE_NAME>", # Optional: Your site name
}
)
# Make a chat completion request
response = client.chat.completions.create(
model="anthropic/claude-4-sonnet",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me a fun fact about space."}
],
name="fun-fact-request" # Optional: Name of the generation in Litefuse
)
# Print the assistant's reply
print(response.choices[0].message.content)示例 2:嵌套 LLM 调用
我们可以捕获嵌套 LLM 调用的执行细节、输入、输出与执行时间。这样能在极少代码改动下获得深入的可观测性。
- 嵌套函数构成 trace 的层级结构。
- 函数中的每次 LLM 调用都会被记录,提供详细的执行流程 trace。
通过 @observe() 装饰器,我们可以捕获任意 Python 函数的执行细节。
@observe()装饰器捕获函数的输入、输出和执行细节。
from langfuse import observe
from langfuse.openai import openai
# Create an OpenAI client with the Vercel AI Gateway base URL
client = openai.OpenAI(
base_url="https://ai-gateway.vercel.sh/v1",
)
@observe() # This decorator enables tracing of the function
def analyze_text(text: str):
# First LLM call: Summarize the text
summary_response = summarize_text(text)
summary = summary_response.choices[0].message.content
# Second LLM call: Analyze the sentiment of the summary
sentiment_response = analyze_sentiment(summary)
sentiment = sentiment_response.choices[0].message.content
return {
"summary": summary,
"sentiment": sentiment
}
@observe() # Nested function to be traced
def summarize_text(text: str):
return client.chat.completions.create(
model="openai/gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You summarize texts in a concise manner."},
{"role": "user", "content": f"Summarize the following text:\n{text}"}
],
name="summarize-text"
)
@observe() # Nested function to be traced
def analyze_sentiment(summary: str):
return client.chat.completions.create(
model="openai/gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You analyze the sentiment of texts."},
{"role": "user", "content": f"Analyze the sentiment of the following summary:\n{summary}"}
],
name="analyze-sentiment"
)
# Example usage
text_to_analyze = "OpenAI's GPT-4 model has significantly advanced the field of AI, setting new standards for language generation."
analyze_text(text_to_analyze)了解更多
- Vercel AI Gateway 文档:https://vercel.com/docs/ai-gateway
- Litefuse OpenAI 集成:https://litefuse.ai/integrations/model-providers/openai-py
- Litefuse
@observe()装饰器:https://litefuse.ai/docs/sdk/python/decorators
这个页面对你有帮助吗?