import logging import httpx from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type logger = logging.getLogger(__name__) RETRYABLE_EXCEPTIONS = ( httpx.TimeoutException, httpx.NetworkError, ) @retry( stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10), retry=retry_if_exception_type(RETRYABLE_EXCEPTIONS), before_sleep=lambda retry_state: logger.info(f"Retrying LLM call due to {retry_state.outcome.exception()}, attempt {retry_state.attempt_number + 1}...") ) def call_llm_with_retry(llm, program): """ Executes a guidance program with a given LLM, with retry logic. The program is called with the llm, i.e., program(llm). """ return program(llm)