本地调试
有两种方法可以进行本地调试——litellm.set_verbose=True
和通过传递自定义函数 completion(...logger_fn=<your_local_function>)
。警告:确保不要在生产环境中使用 set_verbose
。它记录 API 密钥,这些密钥可能会出现在日志文件中。
设置详细模式
这对于获取 litellm 正在执行的所有打印语句很有用。
import litellm
from litellm import completion
litellm.set_verbose=True # 👈 这是你需要做的唯一一行更改
## 设置环境变量
os.environ["OPENAI_API_KEY"] = "openai key"
os.environ["COHERE_API_KEY"] = "cohere key"
messages = [{ "content": "Hello, how are you?","role": "user"}]
# openai 调用
response = completion(model="gpt-3.5-turbo", messages=messages)
# cohere 调用
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}")
## 设置环境变量
os.environ["OPENAI_API_KEY"] = "openai key"
os.environ["COHERE_API_KEY"] = "cohere key"
messages = [{ "content": "Hello, how are you?","role": "user"}]
# openai 调用
response = completion(model="gpt-3.5-turbo", messages=messages, logger_fn=my_custom_logging_fn)
# cohere 调用
response = completion("command-nightly", messages, logger_fn=my_custom_logging_fn)
仍然有问题?
给我们发短信 @ +17708783106 或加入 Discord。
我们承诺以“lite”速度帮助你 ❤️