Skip to content

Commit

Permalink
fix: add retry to lightrag llm_func call (#572)
Browse files Browse the repository at this point in the history
* add retry to lightrag llm_func call

* fix: update logic for nanographrag

---------

Co-authored-by: Song Lin <song.lin@kirkland.com>
Co-authored-by: Tadashi <tadashi@cinnamon.is>
  • Loading branch information
3 people authored Dec 17, 2024
1 parent 1d3c4f4 commit a8b8fce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
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 (await model.ainvoke(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 = (await model.ainvoke(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
23 changes: 22 additions & 1 deletion libs/ktem/ktem/index/file/graph/nano_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 @@ -50,6 +56,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 (await model.ainvoke(input_messages)).text

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

output = (await model.ainvoke(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 a8b8fce

Please sign in to comment.