Skip to main content
W&B Inference 오류를 적절히 처리하고 애플리케이션의 안정성을 유지하려면 다음 모범 사례를 따르세요.

1. 항상 오류 처리를 구현하세요

API 호출을 try-except 블록으로 감싸세요:
import openai

try:
    response = client.chat.completions.create(
        model="meta-llama/Llama-3.1-8B-Instruct",
        messages=messages
    )
except Exception as e:
    print(f"Error: {e}")
    # 오류를 적절히 처리하세요

2. 지수 백오프를 적용한 재시도 로직 사용하기

import time
from typing import Optional

def call_inference_with_retry(
    client, 
    messages, 
    model: str,
    max_retries: int = 3,
    base_delay: float = 1.0
) -> Optional[str]:
    for attempt in range(max_retries):
        try:
            response = client.chat.completions.create(
                model=model,
                messages=messages
            )
            return response.choices[0].message.content
        except Exception as e:
            if attempt == max_retries - 1:
                raise
            
            # 지수 백오프로 지연 시간 계산
            delay = base_delay * (2 ** attempt)
            print(f"시도 {attempt + 1} 실패, {delay}초 후 재시도...")
            time.sleep(delay)
    
    return None

3. 사용량을 모니터링하세요

  • W&B Billing 페이지에서 크레딧 사용량을 추적하세요
  • 한도에 도달하기 전에 알림을 설정하세요
  • 애플리케이션에서 API 사용량을 기록하세요

4. 특정 오류 코드 처리

def handle_inference_error(error):
    error_str = str(error)
    
    if "401" in error_str:
        # 인증 실패
        raise ValueError("API 키와 프로젝트 설정을 확인하세요")
    elif "402" in error_str:
        # 크레딧 부족
        raise ValueError("크레딧이 부족합니다")
    elif "429" in error_str:
        # 요청 속도 제한 초과
        return "retry"
    elif "500" in error_str or "503" in error_str:
        # 서버 오류
        return "retry"
    else:
        # 알 수 없는 오류
        raise

5. 적절한 타임아웃 설정

사용 사례에 맞게 적절한 타임아웃을 설정하세요:
# 응답 시간이 긴 경우
client = openai.OpenAI(
    base_url='https://api.inference.wandb.ai/v1',
    api_key="your-api-key",
    timeout=60.0  # 60초 타임아웃
)

추가 팁

  • 디버깅할 수 있도록 오류를 타임스탬프와 함께 로깅합니다
  • 더 나은 동시성 처리를 위해 비동기 오퍼레이션을 사용합니다
  • 프로덕션 시스템에 서킷 브레이커를 구현합니다
  • 필요한 경우 응답을 캐시해 API 호출 수를 줄입니다

Inference