跳至主要内容

本地调试

有两种方法进行本地调试 - 使用 litellm._turn_on_debug() 和传入一个自定义函数 completion(...logger_fn=<your_local_function>)。警告:请确保不要在生产环境中使用 _turn_on_debug()。它会记录 API 密钥,这可能会出现在日志文件中。

设置详细模式

这有助于获取 litellm 执行所有操作的打印输出。

import litellm
from litellm import completion

litellm._turn_on_debug() # 👈 this is the 1-line change you need to make

## set ENV variables
os.environ["OPENAI_API_KEY"] = "openai key"
os.environ["COHERE_API_KEY"] = "cohere key"

messages = [{ "content": "Hello, how are you?","role": "user"}]

# openai call
response = completion(model="gpt-3.5-turbo", messages=messages)

# cohere call
response = completion("command-nightly", messages)

JSON 日志

如果您需要将日志存储为 JSON,只需设置 litellm.json_logs = True

我们目前仅将 litellm 发出的原始 POST 请求记录为 JSON -[查看代码].

在此分享反馈

日志函数

但有时您关心的只是确切地看到发送到您的 API 调用的内容以及返回的内容 - 例如,如果 API 调用失败,为什么会发生?设置了哪些确切的参数?

在这种情况下,LiteLLM 允许您传入一个自定义日志函数来查看/修改模型调用的输入/输出。

注意:我们期望您接受一个字典对象。

您的自定义函数

def my_custom_logging_fn(model_call_dict):
print(f"model call details: {model_call_dict}")

完整示例

from litellm import completion

def my_custom_logging_fn(model_call_dict):
print(f"model call details: {model_call_dict}")

## set ENV variables
os.environ["OPENAI_API_KEY"] = "openai key"
os.environ["COHERE_API_KEY"] = "cohere key"

messages = [{ "content": "Hello, how are you?","role": "user"}]

# openai call
response = completion(model="gpt-3.5-turbo", messages=messages, logger_fn=my_custom_logging_fn)

# cohere call
response = completion("command-nightly", messages, logger_fn=my_custom_logging_fn)

仍然遇到问题?

请发短信给我们:+17708783106 或加入Discord

我们承诺以极速 ('lite'ning speed) 帮助您 ❤️