Skip to main content

调用后规则

使用此功能可以根据LLM API调用的输出来拒绝请求。

快速开始

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

def my_custom_rule(input): # 接收模型响应
if len(input) < 5:
return {
"decision": False,
"message": "这违反了LiteLLM代理规则。响应太短"
}
return {"decision": True} # 由于请求将通过,因此不需要消息

第二步:指向你的代理

litellm_settings:
post_call_rules: post_call_rules.my_custom_rule

第三步:启动并测试你的代理

$ 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":"你是哪个LLM?"}],
"temperature": 0.7,
"max_tokens": 10,
}'

现在,这将检查响应是否大于长度5,如果失败,它将在失败前重试3次。

失败的响应

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

{
"error":
{
"message":"这违反了LiteLLM代理规则。响应太短",
"type":null,
"param":null,
"code":500
}
}
优云智算