{ "cells": [ { "cell_type": "markdown", "id": "d75247ed", "metadata": {}, "source": [ "# Retries\n", "\n", "Retries are disabled by default. Retries can be enabled with the following example.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "299065b7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Metrics for: azure/gpt-4o\n", "{\n", " \"attempted_request_count\": 1,\n", " \"successful_response_count\": 1,\n", " \"failed_response_count\": 0,\n", " \"failure_rate\": 0.0,\n", " \"requests_with_retries\": 1,\n", " \"retries\": 2,\n", " \"retry_rate\": 0.6666666666666666,\n", " \"compute_duration_seconds\": 2.6571085453033447,\n", " \"compute_duration_per_response_seconds\": 2.6571085453033447,\n", " \"cache_hit_rate\": 0.0,\n", " \"streaming_responses\": 0,\n", " \"responses_with_tokens\": 1,\n", " \"prompt_tokens\": 14,\n", " \"completion_tokens\": 8,\n", " \"total_tokens\": 22,\n", " \"tokens_per_response\": 22.0,\n", " \"responses_with_cost\": 1,\n", " \"input_cost\": 3.5000000000000004e-05,\n", " \"output_cost\": 8e-05,\n", " \"total_cost\": 0.000115,\n", " \"cost_per_response\": 0.000115\n", "}\n" ] } ], "source": [ "# Copyright (c) 2024 Microsoft Corporation.\n", "# Licensed under the MIT License\n", "\n", "import json\n", "import logging\n", "import os\n", "\n", "from dotenv import load_dotenv\n", "from graphrag_llm.completion import LLMCompletion, create_completion\n", "from graphrag_llm.config import AuthMethod, ModelConfig, RetryConfig, RetryType\n", "\n", "load_dotenv()\n", "\n", "logging.basicConfig(level=logging.CRITICAL)\n", "\n", "\n", "api_key = os.getenv(\"GRAPHRAG_API_KEY\")\n", "model_config = ModelConfig(\n", " model_provider=\"azure\",\n", " model=os.getenv(\"GRAPHRAG_MODEL\", \"gpt-4o\"),\n", " azure_deployment_name=os.getenv(\"GRAPHRAG_MODEL\", \"gpt-4o\"),\n", " api_base=os.getenv(\"GRAPHRAG_API_BASE\"),\n", " api_version=os.getenv(\"GRAPHRAG_API_VERSION\", \"2025-04-01-preview\"),\n", " api_key=api_key,\n", " auth_method=AuthMethod.AzureManagedIdentity if not api_key else AuthMethod.ApiKey,\n", " retry=RetryConfig(\n", " type=RetryType.ExponentialBackoff, max_retries=7, base_delay=2.0, jitter=True\n", " ),\n", " # Internal option to test error handling and retries\n", " failure_rate_for_testing=0.5, # type: ignore\n", ")\n", "\n", "llm_completion: LLMCompletion = create_completion(model_config)\n", "\n", "response = llm_completion.completion(\n", " messages=\"What is the capital of France?\",\n", ")\n", "\n", "print(f\"Metrics for: {llm_completion.metrics_store.id}\")\n", "print(json.dumps(llm_completion.metrics_store.get_metrics(), indent=2))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }