forked from phoenix/litellm-mirror
fix langsmith clear logged queue on success
This commit is contained in:
parent
860516c843
commit
15277aff1c
1 changed files with 26 additions and 5 deletions
|
@ -9,9 +9,12 @@ 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
|
||||||
|
|
||||||
import litellm
|
import litellm
|
||||||
|
@ -55,6 +58,12 @@ 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")
|
||||||
|
@ -277,11 +286,23 @@ class LangsmithLogger(CustomLogger):
|
||||||
)
|
)
|
||||||
if len(self.log_queue) >= self.batch_size:
|
if len(self.log_queue) >= self.batch_size:
|
||||||
await self._async_send_batch()
|
await self._async_send_batch()
|
||||||
|
self.log_queue.clear()
|
||||||
except:
|
except:
|
||||||
verbose_logger.error(f"Langsmith Layer Error - {traceback.format_exc()}")
|
verbose_logger.error(f"Langsmith Layer Error - {traceback.format_exc()}")
|
||||||
|
|
||||||
|
@on_exception(
|
||||||
|
backoff.expo, (httpx.HTTPError, Exception), max_tries=3, on_backoff=on_backoff
|
||||||
|
)
|
||||||
async def _async_send_batch(self):
|
async def _async_send_batch(self):
|
||||||
|
"""
|
||||||
|
sends runs to /batch endpoint
|
||||||
|
|
||||||
|
Sends runs from self.log_queue
|
||||||
|
|
||||||
|
Returns: None
|
||||||
|
|
||||||
|
Raises: Does not raise an exception, will only verbose_logger.exception()
|
||||||
|
"""
|
||||||
import json
|
import json
|
||||||
|
|
||||||
if not self.log_queue:
|
if not self.log_queue:
|
||||||
|
@ -308,14 +329,14 @@ class LangsmithLogger(CustomLogger):
|
||||||
verbose_logger.debug(
|
verbose_logger.debug(
|
||||||
f"Batch of {len(self.log_queue)} runs successfully created"
|
f"Batch of {len(self.log_queue)} runs successfully created"
|
||||||
)
|
)
|
||||||
|
|
||||||
self.log_queue.clear()
|
|
||||||
except httpx.HTTPStatusError as e:
|
except httpx.HTTPStatusError as e:
|
||||||
verbose_logger.error(
|
verbose_logger.exception(
|
||||||
f"Langsmith HTTP Error: {e.response.status_code} - {e.response.text}"
|
f"Langsmith HTTP Error: {e.response.status_code} - {e.response.text}"
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
verbose_logger.error(f"Langsmith Layer Error - {traceback.format_exc()}")
|
verbose_logger.exception(
|
||||||
|
f"Langsmith Layer Error - {traceback.format_exc()}"
|
||||||
|
)
|
||||||
|
|
||||||
def get_run_by_id(self, run_id):
|
def get_run_by_id(self, run_id):
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue