Skip to main content

👥📊 基于团队/密钥的日志记录

允许每个密钥/团队使用自己的 Langfuse 项目/自定义回调

这使你可以实现以下功能

团队 1 -> 日志记录到 Langfuse 项目 1 
团队 2 -> 日志记录到 Langfuse 项目 2
团队 3 -> 禁用日志记录(以符合 GDPR)

基于团队的日志记录

👉 教程 - 允许每个团队使用自己的 Langfuse 项目/自定义回调

日志记录/缓存

为特定团队 ID 开启/关闭日志记录和缓存。

示例:

此配置将根据团队 ID 将 langfuse 日志发送到两个不同的 langfuse 项目

litellm_settings:
default_team_settings:
- team_id: my-secret-project
success_callback: ["langfuse"]
langfuse_public_key: os.environ/LANGFUSE_PUB_KEY_1 # 项目 1
langfuse_secret: os.environ/LANGFUSE_PRIVATE_KEY_1 # 项目 1
- team_id: ishaans-secret-project
success_callback: ["langfuse"]
langfuse_public_key: os.environ/LANGFUSE_PUB_KEY_2 # 项目 2
langfuse_secret: os.environ/LANGFUSE_SECRET_2 # 项目 2

现在,当你为这个团队 ID 生成密钥

curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{"team_id": "ishaans-secret-project"}'

所有使用这些密钥的请求将记录数据到其团队特定的日志中。

[BETA] 通过 API 进行团队日志记录

info

✨ 这是一个仅限企业的功能 在这里开始使用企业版

为团队设置回调

1. 为团队设置回调

我们向 POST /team/{team_id}/callback 发出请求以添加回调

curl -X POST 'http:/localhost:4000/team/dbe2f686-a686-4896-864a-4c3924458709/callback' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-d '{
"callback_name": "langfuse",
"callback_type": "success",
"callback_vars": {
"langfuse_public_key": "pk",
"langfuse_secret_key": "sk_",
"langfuse_host": "https://cloud.langfuse.com"
}

}'
支持的值
字段支持的值说明
callback_name"langfuse", "gcs_bucket"目前仅支持 "langfuse", "gcs_bucket"
callback_type"success", "failure", "success_and_failure"
callback_vars回调设置的字典
    langfuse_public_key字符串对于 Langfuse 是必需的
    langfuse_secret_key字符串对于 Langfuse 是必需的
    langfuse_host字符串对于 Langfuse 是可选的(默认为 https://cloud.langfuse.com)
    gcs_bucket_name字符串对于 GCS 存储桶是必需的。GCS 存储桶的名称
    gcs_path_service_account字符串对于 GCS 存储桶是必需的。服务账户 json 的路径

2. 为团队创建密钥

为团队 dbe2f686-a686-4896-864a-4c3924458709 创建的所有密钥将在 步骤 1. 为团队设置回调 中指定的 Langfuse 项目中记录日志

curl --location 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"team_id": "dbe2f686-a686-4896-864a-4c3924458709"
}'

3. 为团队发起 /chat/completion 请求

curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-KbUuE0WNptC0jXapyMmLBA" \
-d '{
"model": "gpt-4",
"messages": [
{"role": "user", "content": "Hello, Claude gm!"}
]
}'

期望这将在 步骤 1. 为团队设置回调 中指定的 Langfuse 项目中记录日志

为团队禁用日志记录

要禁用特定团队的日志记录,可以使用以下端点:

POST /team/{team_id}/disable_logging

此端点将移除指定团队的所有成功和失败回调,从而有效地禁用日志记录。

步骤 1. 为团队禁用日志记录

curl -X POST 'http://localhost:4000/team/YOUR_TEAM_ID/disable_logging' \
-H 'Authorization: Bearer YOUR_API_KEY'

将 YOUR_TEAM_ID 替换为实际的团队 ID

响应 成功的请求将返回类似以下的响应:

{
"status": "success",
"message": "Logging disabled for team YOUR_TEAM_ID",
"data": {
"team_id": "YOUR_TEAM_ID",
"success_callbacks": [],
"failure_callbacks": []
}
}

步骤 2. 测试 - /chat/completions

使用为团队 = team_id 生成的密钥 - 你应该看不到在配置的成功回调(例如 Langfuse)中有任何日志

curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-KbUuE0WNptC0jXapyMmLBA" \
-d '{
"model": "gpt-4",
"messages": [
{"role": "user", "content": "你好,克劳德,早上好!"}
]
}'

调试 / 故障排除

  • 使用 GET /team/{team_id}/callback 检查团队的活动回调

使用此接口检查团队 team_id 的活跃成功/失败回调

curl -X GET 'http://localhost:4000/team/dbe2f686-a686-4896-864a-4c3924458709/callback' \
-H 'Authorization: Bearer sk-1234'

团队日志记录端点

[测试版] 基于密钥的日志记录

使用 /key/generate/key/update 端点为特定密钥添加日志回调。

info

✨ 这是一个仅限企业的功能 在此处开始使用企业版

基于密钥的日志记录工作原理:

  • 如果密钥未配置回调,它将使用 config.yaml 文件中指定的默认回调。
  • 如果密钥已配置回调,它将使用密钥中指定的回调。
curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"metadata": {
"logging": [{
"callback_name": "langfuse", # "otel", "gcs_bucket"
"callback_type": "success", # "success", "failure", "success_and_failure"
"callback_vars": {
"langfuse_public_key": "os.environ/LANGFUSE_PUBLIC_KEY", # [推荐] 引用代理环境中的密钥
"langfuse_secret_key": "os.environ/LANGFUSE_SECRET_KEY", # [推荐] 引用代理环境中的密钥
"langfuse_host": "https://cloud.langfuse.com"
}
}]
}
}'


帮助我们改进此功能,通过在此处提交 问题

检查密钥回调是否正确配置 /key/health

使用密钥调用 /key/health 以检查回调设置是否正确配置

在请求头中传递密钥

curl -X POST "http://localhost:4000/key/health" \
-H "Authorization: Bearer <your-key>" \
-H "Content-Type: application/json"

当日志回调正确设置时的响应:

一个密钥是健康的,当日志回调正确设置时。

{
"key": "healthy",
"logging_callbacks": {
"callbacks": [
"gcs_bucket"
],
"status": "healthy",
"details": "未触发任何日志记录异常,系统健康。请手动检查日志是否已发送到 ['gcs_bucket']"
}
}
优云智算