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 typing import Any, List, Optional, Union
import backoff
import dotenv # type: ignore
import httpx
import requests # type: ignore
from backoff import on_exception
from backoff._typing import Details
from pydantic import BaseModel # type: ignore
from tenacity import (
retry,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
)
import litellm
from litellm._logging import verbose_logger
@ -58,12 +61,6 @@ def is_serializable(value):
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):
def __init__(self):
self.langsmith_api_key = os.getenv("LANGSMITH_API_KEY")
@ -290,8 +287,10 @@ class LangsmithLogger(CustomLogger):
except:
verbose_logger.error(f"Langsmith Layer Error - {traceback.format_exc()}")
@on_exception(
backoff.expo, (httpx.HTTPError, Exception), max_tries=3, on_backoff=on_backoff
@retry(
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):
"""