MCPcopy Index your code
hub / github.com/unclecode/crawl4ai / perform_completion_with_backoff

Function perform_completion_with_backoff

crawl4ai/utils.py:794–842  ·  view source on GitHub ↗
(
    provider, 
    prompt_with_variables, 
    api_token, 
    json_response = False, 
    base_url=None,
    **kwargs
    )

Source from the content-addressed store, hash-verified

792
793# Function to perform the completion with exponential backoff
794def perform_completion_with_backoff(
795 provider,
796 prompt_with_variables,
797 api_token,
798 json_response = False,
799 base_url=None,
800 **kwargs
801 ):
802 from litellm import completion
803 from litellm.exceptions import RateLimitError
804 max_attempts = 3
805 base_delay = 2 # Base delay in seconds, you can adjust this based on your needs
806
807 extra_args = {}
808 if json_response:
809 extra_args["response_format"] = { "type": "json_object" }
810
811 if kwargs.get("extra_args"):
812 extra_args.update(kwargs["extra_args"])
813
814 for attempt in range(max_attempts):
815 try:
816 response =completion(
817 model=provider,
818 messages=[
819 {"role": "user", "content": prompt_with_variables}
820 ],
821 temperature=0.01,
822 api_key=api_token,
823 base_url=base_url,
824 **extra_args
825 )
826 return response # Return the successful response
827 except RateLimitError as e:
828 print("Rate limit error:", str(e))
829
830 # Check if we have exhausted our max attempts
831 if attempt < max_attempts - 1:
832 # Calculate the delay and wait
833 delay = base_delay * (2 ** attempt) # Exponential backoff formula
834 print(f"Waiting for {delay} seconds before retrying...")
835 time.sleep(delay)
836 else:
837 # Return an error response after exhausting all retries
838 return [{
839 "index": 0,
840 "tags": ["error"],
841 "content": ["Rate limit error. Please try again later."]
842 }]
843
844def extract_blocks(url, html, provider = DEFAULT_PROVIDER, api_token = None, base_url = None):
845 # api_token = os.getenv('GROQ_API_KEY', None) if not api_token else api_token

Callers 2

extract_blocksFunction · 0.85
extractMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…