集成网关Helicone

Helicone 集成

Helicone 在被 Mintlify 收购 之后已进入维护模式。如果你想迁移,可以参考我们的 Helicone 到 Litefuse 迁移指南

本指南介绍如何把 LitefuseHelicone 集成。

什么是 Helicone? Helicone 是开源的 AI 网关,通过 OpenAI 兼容接口为你提供 100+ AI 模型的访问。它提供智能路由、自动故障转移、缓存、成本追踪等能力。

什么是 Litefuse? Litefuse 是开源的 AI Agent 可观测性和评估平台,帮助团队追踪 LLM 调用、监控性能并调试 AI 应用中的问题。

由于 Helicone 兼容 OpenAI,我们可以利用 Litefuse 与 OpenAI SDK 的原生集成,PythonTypeScript 都可用。

快速开始

  1. 在终端中安装下面的包(如果还没装):
pip install langfuse openai python-dotenv
  1. 然后在项目里创建 .env 文件并加入环境变量:
HELICONE_API_KEY=sk-helicone-... # Get it from your Helicone dashboard

LANGFUSE_SECRET_KEY=sk-lf-...
LANGFUSE_PUBLIC_KEY=pk-lf-...
LANGFUSE_BASE_URL=https://litefuse.cloud

示例 1:简单的 LLM 调用

我们使用 Litefuse 的 OpenAI SDK 包装器自动把 Helicone 调用记录为 Litefuse 中的 generation。

  • base_url 设置为 Helicone AI Gateway 的端点。
  • 你可以把 "gpt-4o" 替换为 Helicone 模型注册表 中的任意模型。
  • api_key 使用你的 Helicone API Key 来与各个模型提供商进行鉴权。
from langfuse.openai import openai
import os
from dotenv import load_dotenv
 
load_dotenv()
 
# Create an OpenAI client with Helicone's gateway endpoint
client = openai.OpenAI(
    api_key=os.getenv("HELICONE_API_KEY"),
    base_url="https://ai-gateway.helicone.ai/"
)
 
# Make a chat completion request
response = client.chat.completions.create(
    model="gpt-4o", # Or any other 100+ models
    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(response.choices[0].message.content)

示例 2:嵌套 LLM 调用

通过 @observe() 装饰器,我们可以捕获任意 Python 函数的执行细节,包括嵌套 LLM 调用、输入、输出和执行时间。这样能在极少代码改动下获得深入的可观测性。

  • @observe() 装饰器捕获函数的输入、输出和执行细节。
  • 嵌套函数 summarize_textanalyze_sentiment 也被装饰,从而构成 trace 的层级结构。
  • 函数中每次 LLM 调用都会被记录,提供详细的执行流程 trace。

通过 @observe() 装饰器,我们可以捕获任意 Python 函数的执行细节。 装饰器会捕获函数的输入、输出和执行细节。

from langfuse import observe
from langfuse.openai import openai
import os
from dotenv import load_dotenv
 
load_dotenv()
 
# Create an OpenAI client with Helicone's base URL
client = openai.OpenAI(
    base_url="https://ai-gateway.helicone.ai/",
    api_key=os.getenv("HELICONE_API_KEY")
)
 
@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="gpt-4o",
        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="gpt-4o",
        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)

示例 3:流式响应

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "Write a haiku about a robot."}
    ],
    stream=True,
    name="streaming-story"
)
 
print("🤖 Assistant (streaming):")
for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="", flush=True)
print("\n")

示例 4:多 provider 访问

Helicone 通过统一接口提供 100+ LLM provider 的访问。只需更换模型名即可使用不同 provider:

# Use Anthropic Claude
response = client.chat.completions.create(
    model="claude-3.5-sonnet-v2/anthropic",
    messages=[{"role": "user", "content": "Hello!"}]
)
 
# Use Gemini Pro if Anthropic's Claude Sonnet 3.5 is not available
response = client.chat.completions.create(
    model="claude-3.5-sonnet-v2/anthropic,gemini-2.5-flash-lite/google-ai-studio",
    messages=[{"role": "user", "content": "Hello!"}]
)

了解更多

这个页面对你有帮助吗?