Skip to content

Commit

Permalink
implement retry
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalef committed Aug 26, 2024
1 parent 895afc7 commit 52f5f10
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
29 changes: 28 additions & 1 deletion graphiti_core/llm_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import typing
from abc import ABC, abstractmethod

import httpx
from diskcache import Cache
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential

Check failure on line 25 in graphiti_core/llm_client/client.py

View workflow job for this annotation

GitHub Actions / mypy

import-not-found

Cannot find implementation or library stub for module named "tenacity"

Check notice on line 25 in graphiti_core/llm_client/client.py

View workflow job for this annotation

GitHub Actions / mypy

Note

See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

from ..prompts.models import Message
from .config import LLMConfig
Expand All @@ -47,6 +49,23 @@ def __init__(self, config: LLMConfig | None, cache: bool = False):
def get_embedder(self) -> typing.Any:
pass

@staticmethod
def _is_server_error(exception):
return (
isinstance(exception, httpx.HTTPStatusError)
and 500 <= exception.response.status_code < 600
)

@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=4, max=10),
retry=retry_if_exception_type(httpx.HTTPStatusError),
)
async def _generate_response_with_retry(self, messages: list[Message]) -> dict[str, typing.Any]:
if self._is_server_error(httpx.HTTPStatusError):
raise
return await self._generate_response(messages)

@abstractmethod
async def _generate_response(self, messages: list[Message]) -> dict[str, typing.Any]:
pass
Expand All @@ -66,7 +85,15 @@ async def generate_response(self, messages: list[Message]) -> dict[str, typing.A
logger.debug(f'Cache hit for {cache_key}')
return cached_response

response = await self._generate_response(messages)
try:
response = await self._generate_response_with_retry(messages)
except httpx.HTTPStatusError as e:
error_type = 'server' if 500 <= e.response.status_code < 600 else 'client'
logger.error(f'Failed to generate response due to {error_type} error: {str(e)}')
raise
except Exception as e:
logger.error(f'Failed to generate response due to unexpected error: {str(e)}')
raise

if self.cache_enabled:
self.cache_dir.set(cache_key, response)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ diskcache = "^5.6.3"
arrow = "^1.3.0"
openai = "^1.38.0"
anthropic = "^0.34.1"
tenacity = "^9.0.0"

[tool.poetry.dev-dependencies]
pytest = "^8.3.2"
Expand Down

0 comments on commit 52f5f10

Please sign in to comment.