Back to Graphrag

Retries

packages/graphrag-llm/notebooks/06_retries.ipynb

3.0.91.4 KB
Original Source

Retries

Retries are disabled by default. Retries can be enabled with the following example.

python
# Copyright (c) 2024 Microsoft Corporation.
# Licensed under the MIT License

import json
import logging
import os

from dotenv import load_dotenv
from graphrag_llm.completion import LLMCompletion, create_completion
from graphrag_llm.config import AuthMethod, ModelConfig, RetryConfig, RetryType

load_dotenv()

logging.basicConfig(level=logging.CRITICAL)


api_key = os.getenv("GRAPHRAG_API_KEY")
model_config = ModelConfig(
    model_provider="azure",
    model=os.getenv("GRAPHRAG_MODEL", "gpt-4o"),
    azure_deployment_name=os.getenv("GRAPHRAG_MODEL", "gpt-4o"),
    api_base=os.getenv("GRAPHRAG_API_BASE"),
    api_version=os.getenv("GRAPHRAG_API_VERSION", "2025-04-01-preview"),
    api_key=api_key,
    auth_method=AuthMethod.AzureManagedIdentity if not api_key else AuthMethod.ApiKey,
    retry=RetryConfig(
        type=RetryType.ExponentialBackoff, max_retries=7, base_delay=2.0, jitter=True
    ),
    # Internal option to test error handling and retries
    failure_rate_for_testing=0.5,  # type: ignore
)

llm_completion: LLMCompletion = create_completion(model_config)

response = llm_completion.completion(
    messages="What is the capital of France?",
)

print(f"Metrics for: {llm_completion.metrics_store.id}")
print(json.dumps(llm_completion.metrics_store.get_metrics(), indent=2))