타임아웃과 Rate Limit 커스터마이징

타임아웃과 Rate Limit 커스터마이징

LLM 호출은 네트워크 상에서 일어나는 작업이라서 자주 실패할 수 있어요. Ragas의 컬렉션 API를 쓸 때는 LLM 클라이언트에 타임아웃과 재시도 횟수를 직접 설정해서 안정성을 잡을 수 있어요. 이번에는 그 방법을 하나씩 살펴볼게요.

출처: 문서

본문

llm_factory 로 LLM을 만들 때 client 파라미터를 넘기면, 그 클라이언트에 이미 설정된 타임아웃과 재시도 정책을 그대로 사용해요. 즉 LLM 클라이언트 단에서 제어를 하는 방식이죠.

OpenAI 클라이언트 설정

from openai import AsyncOpenAI
from ragas.llms import llm_factory
from ragas.metrics.collections import Faithfulness

# Configure timeout and retries on the client
client = AsyncOpenAI(
    timeout=60.0,        # 60 second timeout
    max_retries=5,       # Retry up to 5 times on failures
)

llm = llm_factory("gpt-4o-mini", client=client)

# Use with metrics
scorer = Faithfulness(llm=llm)
result = scorer.score(
    user_input="When was the first super bowl?",
    response="The first superbowl was held on Jan 15, 1967",
    retrieved_contexts=[
        "The First AFL–NFL World Championship Game was an American football game played on January 15, 1967, at the Los Angeles Memorial Coliseum in Los Angeles."
    ]
)

사용 가능한 옵션

Parameter Default Description
timeout 600.0 Request timeout in seconds
max_retries 2 Number of retry attempts for failed requests

세밀한 타임아웃 제어

타임아웃을 더 구체적으로 나눠서 제어하고 싶을 때는 httpx.Timeout 을 사용할 수 있어요. 연결, 읽기, 쓰기 각각의 타임아웃을 따로 잡아두는 방식이죠.

import httpx
from openai import AsyncOpenAI

client = AsyncOpenAI(
    timeout=httpx.Timeout(
        60.0,           # Total timeout
        connect=5.0,    # Connection timeout
        read=30.0,      # Read timeout
        write=10.0,     # Write timeout
    ),
    max_retries=3,
)

Provider 문서

각 LLM 프로바이더마다 클라이언트 설정 옵션이 달라요. 다음 SDK 문서를 참고하면 자신의 프로바이더에 맞는 설정을 찾을 수 있어요.

  • OpenAI Python SDK
  • Anthropic Python SDK

레거시 Metrics API

아래 예시는 RunConfig 를 사용하는 레거시 메트릭 API 패턴이에요. 새 프로젝트를 시작한다면 위에서 본 것처럼 클라이언트 레벨에서 설정하는 컬렉션 기반 API를 권장해요.

폐기 예정 타임라인

이 API는 버전 0.4에서 deprecated 되고 버전 1.0에서 제거될 예정이에요. 컬렉션 기반 API로 마이그레이션해 주세요.

RunConfig 파라미터

from ragas.run_config import RunConfig

run_config = RunConfig(
    timeout=180,        # Max seconds per operation (default: 180)
    max_retries=10,     # Retry attempts (default: 10)
    max_wait=60,        # Max seconds between retries (default: 60)
    max_workers=16,     # Concurrent workers (default: 16)
    log_tenacity=False, # Log retry attempts (default: False)
    seed=42,            # Random seed (default: 42)
)

evaluate와 함께 사용하기

from langchain_openai import ChatOpenAI
from ragas.llms import LangchainLLMWrapper
from ragas import EvaluationDataset, SingleTurnSample, evaluate
from ragas.metrics import Faithfulness
from ragas.run_config import RunConfig

# Legacy LLM setup
llm = LangchainLLMWrapper(ChatOpenAI(model="gpt-4o"))

# Configure run settings
run_config = RunConfig(max_workers=64, timeout=60)

# Use with evaluate
results = evaluate(
    dataset=eval_dataset,
    metrics=[Faithfulness(llm=llm)],
    run_config=run_config,
)

더 알아보기 (Learn more)