규칙
규칙 (Rules)
LLM API 호출의 입력 또는 출력을 기준으로 요청을 실패시키는 기능이에요. 사전 호출(pre_call) 또는 사후 호출(post_call) 규칙으로 모델 응답을 검사하고 요청 성공 여부를 결정할 수 있어요.
출처: 문서
본문
LLM API 호출의 입력 또는 출력을 기준으로 요청을 실패시키는 데 사용해요.
import litellm
import os
# set env vars
os.environ["OPENAI_API_KEY"] = "your-api-key"
os.environ["OPENROUTER_API_KEY"] = "your-api-key"
def my_custom_rule(input): # receives the model response
if "i don't think i can answer" in input: # trigger fallback if the model refuses to answer
return False
return True
litellm.post_call_rules = [my_custom_rule] # have these be functions that can be called to fail a call
response = litellm.completion(model="gpt-5.6-luna", messages=[{"role": "user",
"content": "Hey, how's it going?"}], fallbacks=["openrouter/gryphe/mythomax-l2-13b"])
사용 가능한 엔드포인트
litellm.pre_call_rules = []- API 호출을 만들기 전에 반복할 함수 목록. 각 함수는 True(호출 허용) 또는 False(호출 실패)를 반환해야 해요.litellm.post_call_rules = []- API 호출을 만들기 전에 반복할 함수 목록. 각 함수는 True(호출 허용) 또는 False(호출 실패)를 반환해야 해요.
규칙의 예상 형식
def my_custom_rule(input: str) -> bool: # receives the model response
if "i don't think i can answer" in input: # trigger fallback if the model refuses to answer
return False
return True
입력
input: str: 사용자 입력 또는 LLM 응답.
출력
bool: True(호출 허용) 또는 False(호출 실패) 반환.
규칙 예시
예시 1: 사용자 입력이 너무 길면 실패
import litellm
import os
# set env vars
os.environ["OPENAI_API_KEY"] = "your-api-key"
def my_custom_rule(input): # receives the model response
if len(input) > 10: # fail call if too long
return False
return True
litellm.pre_call_rules = [my_custom_rule] # have these be functions that can be called to fail a call
response = litellm.completion(model="gpt-5.6-luna", messages=[{"role": "user", "content": "Hey, how's it going?"}])
예시 2: LLM이 응답을 거부하면 검열 없는 모델로 폴백
import litellm
import os
# set env vars
os.environ["OPENAI_API_KEY"] = "your-api-key"
os.environ["OPENROUTER_API_KEY"] = "your-api-key"
def my_custom_rule(input): # receives the model response
if "i don't think i can answer" in input: # trigger fallback if the model refuses to answer
return False
return True
litellm.post_call_rules = [my_custom_rule] # have these be functions that can be called to fail a call
response = litellm.completion(model="gpt-5.6-luna", messages=[{"role": "user",
"content": "Hey, how's it going?"}], fallbacks=["openrouter/gryphe/mythomax-l2-13b"])