跳到主要内容

Langfuse SDK

Langfuse 透传端点 - 使用 LiteLLM 虚拟密钥调用 Langfuse 端点。

只需将 https://us.cloud.langfuse.com 替换为 LITELLM_PROXY_BASE_URL/langfuse 🚀

示例用法

from langfuse import Langfuse

langfuse = Langfuse(
host="http://localhost:4000/langfuse", # your litellm proxy endpoint
public_key="anything", # no key required since this is a pass through
secret_key="LITELLM_VIRTUAL_KEY", # no key required since this is a pass through
)

print("sending langfuse trace request")
trace = langfuse.trace(name="test-trace-litellm-proxy-passthrough")
print("flushing langfuse request")
langfuse.flush()

print("flushed langfuse request")

支持 所有 Langfuse 端点。

查看所有 Langfuse 端点

快速入门

让我们向 Langfuse 记录一个跟踪。

  1. 将 Langfuse 公钥/私钥添加到环境变量
export LANGFUSE_PUBLIC_KEY=""
export LANGFUSE_PRIVATE_KEY=""
  1. 启动 LiteLLM 代理
litellm

# RUNNING on http://0.0.0.0:4000
  1. 测试一下!

让我们向 Langfuse 记录一个跟踪!

from langfuse import Langfuse

langfuse = Langfuse(
host="http://localhost:4000/langfuse", # your litellm proxy endpoint
public_key="anything", # no key required since this is a pass through
secret_key="anything", # no key required since this is a pass through
)

print("sending langfuse trace request")
trace = langfuse.trace(name="test-trace-litellm-proxy-passthrough")
print("flushing langfuse request")
langfuse.flush()

print("flushed langfuse request")

高级 - 与虚拟密钥一起使用

前提条件

使用此方法可以避免向开发者提供原始的 Google AI Studio 密钥,但仍然让他们能够使用 Google AI Studio 端点。

用法

  1. 设置环境变量
export DATABASE_URL=""
export LITELLM_MASTER_KEY=""
export LANGFUSE_PUBLIC_KEY=""
export LANGFUSE_PRIVATE_KEY=""
litellm

# RUNNING on http://0.0.0.0:4000
  1. 生成虚拟密钥
curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{}'

预期响应

{
...
"key": "sk-1234ewknldferwedojwojw"
}
  1. 测试一下!
from langfuse import Langfuse

langfuse = Langfuse(
host="http://localhost:4000/langfuse", # your litellm proxy endpoint
public_key="anything", # no key required since this is a pass through
secret_key="sk-1234ewknldferwedojwojw", # no key required since this is a pass through
)

print("sending langfuse trace request")
trace = langfuse.trace(name="test-trace-litellm-proxy-passthrough")
print("flushing langfuse request")
langfuse.flush()

print("flushed langfuse request")

高级 - 记录到不同的 Langfuse 项目(按密钥/团队)