Sentry - 记录 LLM 异常
提示
这是社区维护的内容,如果您遇到错误,请提交 issue https://github.com/BerriAI/litellm
Sentry 提供生产环境的错误监控。LiteLLM 可以通过此集成添加面包屑并向 Sentry 发送异常。
跟踪以下异常:
- litellm.completion() - 适用于 100+ LLM 的 completion()
- litellm.acompletion() - 异步 completion()
- 流式 completion() 和 acompletion() 调用
用法
设置 SENTRY_DSN 和 回调
import litellm, os
os.environ["SENTRY_DSN"] = "your-sentry-url"
litellm.failure_callback=["sentry"]
使用 completion 的 Sentry 回调
import litellm
from litellm import completion
litellm.input_callback=["sentry"] # adds sentry breadcrumbing
litellm.failure_callback=["sentry"] # [OPTIONAL] if you want litellm to capture -> send exception to sentry
import os
os.environ["SENTRY_DSN"] = "your-sentry-url"
os.environ["OPENAI_API_KEY"] = "your-openai-key"
# set bad key to trigger error
api_key="bad-key"
response = completion(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hey!"}], stream=True, api_key=api_key)
print(response)
从 Sentry 日志记录中删除消息和响应内容
设置 litellm.turn_off_message_logging=True
这将阻止消息和响应被记录到 Sentry,但请求元数据仍会被记录。