더북(TheBook)

test_tenacity.py

import os
from dotenv import load_dotenv
from openai import OpenAI, RateLimitError, APIError, AuthenticationError
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type

# .env 파일 로드
load_dotenv()
# 환경 변수에서 API 키 읽기
api_key = os.getenv("OPENAI_API_KEY")
# OpenAI 클라이언트 생성
client = OpenAI(api_key=api_key)
@retry( ---------------------------------------------------------- ➊
    retry=retry_if_exception_type((RateLimitError, APIError)), --- ➋
    stop=stop_after_attempt(3), ---------------------------------- ➌
    wait=wait_exponential(multiplier=1, min=2, max=60) ----------- ➍
)