|
|
|
@ -16,6 +16,16 @@ from langchain.env import get_runtime_environment
|
|
|
|
|
from langchain.schema import BaseMessage, messages_to_dict
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
_LOGGED = set()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def log_error_once(method: str, exception: Exception) -> None:
|
|
|
|
|
"""Log an error once."""
|
|
|
|
|
global _LOGGED
|
|
|
|
|
if (method, type(exception)) in _LOGGED:
|
|
|
|
|
return
|
|
|
|
|
_LOGGED.add((method, type(exception)))
|
|
|
|
|
logger.error(exception)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LangChainTracer(BaseTracer):
|
|
|
|
@ -76,11 +86,21 @@ class LangChainTracer(BaseTracer):
|
|
|
|
|
extra = run_dict.get("extra", {})
|
|
|
|
|
extra["runtime"] = get_runtime_environment()
|
|
|
|
|
run_dict["extra"] = extra
|
|
|
|
|
try:
|
|
|
|
|
run = self.client.create_run(**run_dict, session_name=self.session_name)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# Errors are swallowed by the thread executor so we need to log them here
|
|
|
|
|
log_error_once("post", e)
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
def _update_run_single(self, run: Run) -> None:
|
|
|
|
|
"""Update a run."""
|
|
|
|
|
try:
|
|
|
|
|
self.client.update_run(run.id, **run.dict())
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# Errors are swallowed by the thread executor so we need to log them here
|
|
|
|
|
log_error_once("patch", e)
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
def _on_llm_start(self, run: Run) -> None:
|
|
|
|
|
"""Persist an LLM run."""
|
|
|
|
|