use tenacity for langsmith

This commit is contained in:
Ishaan Jaff 2024-09-11 12:41:22 -07:00
parent 15277aff1c
commit 0a6a437e64

View file

@ -9,13 +9,16 @@ import uuid
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import Any, List, Optional, Union from typing import Any, List, Optional, Union
import backoff
import dotenv # type: ignore import dotenv # type: ignore
import httpx import httpx
import requests # type: ignore import requests # type: ignore
from backoff import on_exception
from backoff._typing import Details
from pydantic import BaseModel # type: ignore from pydantic import BaseModel # type: ignore
from tenacity import (
retry,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
)
import litellm import litellm
from litellm._logging import verbose_logger from litellm._logging import verbose_logger
@ -58,12 +61,6 @@ def is_serializable(value):
return not isinstance(value, non_serializable_types) return not isinstance(value, non_serializable_types)
def on_backoff(details: Details) -> None:
verbose_logger.warning(
f"Langsmith batch send failed. Retrying in {details['wait']} seconds. Attempt {details['tries']}/3"
)
class LangsmithLogger(CustomLogger): class LangsmithLogger(CustomLogger):
def __init__(self): def __init__(self):
self.langsmith_api_key = os.getenv("LANGSMITH_API_KEY") self.langsmith_api_key = os.getenv("LANGSMITH_API_KEY")
@ -290,8 +287,10 @@ class LangsmithLogger(CustomLogger):
except: except:
verbose_logger.error(f"Langsmith Layer Error - {traceback.format_exc()}") verbose_logger.error(f"Langsmith Layer Error - {traceback.format_exc()}")
@on_exception( @retry(
backoff.expo, (httpx.HTTPError, Exception), max_tries=3, on_backoff=on_backoff stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=4, max=10),
retry=retry_if_exception_type((httpx.HTTPStatusError, Exception)),
) )
async def _async_send_batch(self): async def _async_send_batch(self):
""" """