跳到主内容

调用后规则

使用此功能根据 LLM API 调用的输出结果来使请求失败。

快速入门

步骤 1: 创建一个文件 (例如 post_call_rules.py)

def my_custom_rule(input): # receives the model response 
if len(input) < 5:
return {
"decision": False,
"message": "This violates LiteLLM Proxy Rules. Response too short"
}
return {"decision": True} # message not required since, request will pass

步骤 2. 指向你的代理

litellm_settings:
post_call_rules: post_call_rules.my_custom_rule

步骤 3. 启动并测试你的代理

$ litellm /path/to/config.yaml
curl --location 'http://0.0.0.0:4000/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-1234' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [{"role":"user","content":"What llm are you?"}],
"temperature": 0.7,
"max_tokens": 10,
}'

现在它将检查响应长度是否 > 5,如果失败,它会在最终失败前重试调用 3 次。

未能通过规则的响应

这是 LiteLLM 代理在规则失败时的响应

{
"error":
{
"message":"This violates LiteLLM Proxy Rules. Response too short",
"type":null,
"param":null,
"code":500
}
}