Skip to content

Commit

Permalink
add retry to lightrag llm_func call
Browse files Browse the repository at this point in the history
  • Loading branch information
Song Lin committed Dec 16, 2024
1 parent c667bf9 commit d8f9166
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion libs/ktem/ktem/index/file/graph/lightrag_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
from ktem.embeddings.manager import embedding_models_manager as embeddings
from ktem.llms.manager import llms
from sqlalchemy.orm import Session
from tenacity import (
retry,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
)
from theflow.settings import settings

from kotaemon.base import Document, Param, RetrievedDocument
Expand Down Expand Up @@ -49,6 +55,17 @@


def get_llm_func(model):
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=4, max=10),
retry=retry_if_exception_type((Exception,)),
after=lambda retry_state: logging.warning(
f"LLM API call attempt {retry_state.attempt_number} failed. Retrying..."
),
)
async def _call_model(model, input_messages):
return model(input_messages).text

async def llm_func(
prompt, system_prompt=None, history_messages=[], **kwargs
) -> str:
Expand All @@ -70,7 +87,11 @@ async def llm_func(
if if_cache_return is not None:
return if_cache_return["return"]

output = model(input_messages).text
try:
output = await _call_model(model, input_messages)
except Exception as e:
logging.error(f"Failed to call LLM API after 3 retries: {str(e)}")
raise

print("-" * 50)
print(output, "\n", "-" * 50)
Expand Down

0 comments on commit d8f9166

Please sign in to comment.