跳至主要內容

Langfuse SDK

適用於 Langfuse 的轉接端點 - 使用 LiteLLM Virtual Key 呼叫 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 端點

快速開始

我們來將一個 trace 記錄到 Langfuse。

  1. 將 Langfuse 公開/私有金鑰加入環境變數
export LANGFUSE_PUBLIC_KEY=""
export LANGFUSE_PRIVATE_KEY=""
  1. 啟動 LiteLLM Proxy
litellm

# RUNNING on http://0.0.0.0:4000
  1. 測試它!

我們來將一個 trace 記錄到 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")

進階 - 搭配 Virtual Key 使用

前置需求

使用這個方法,可避免將原始 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. 產生 virtual key
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 專案(依金鑰/團隊)

🚅
LiteLLM Enterprise
為正式環境打造的 SSO/SAML、稽核記錄、支出追蹤、多團隊管理與防護欄。
深入瞭解 →