File size: 756 Bytes
5d74609
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)