|
|
|
@ -10,7 +10,7 @@ from uuid import UUID
|
|
|
|
|
from tenacity import RetryCallState
|
|
|
|
|
|
|
|
|
|
from langchain.callbacks.base import BaseCallbackHandler
|
|
|
|
|
from langchain.callbacks.tracers.schemas import Run, RunTypeEnum
|
|
|
|
|
from langchain.callbacks.tracers.schemas import Run
|
|
|
|
|
from langchain.load.dump import dumpd
|
|
|
|
|
from langchain.schema.document import Document
|
|
|
|
|
from langchain.schema.output import ChatGeneration, LLMResult
|
|
|
|
@ -110,7 +110,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
start_time=start_time,
|
|
|
|
|
execution_order=execution_order,
|
|
|
|
|
child_execution_order=execution_order,
|
|
|
|
|
run_type=RunTypeEnum.llm,
|
|
|
|
|
run_type="llm",
|
|
|
|
|
tags=tags or [],
|
|
|
|
|
)
|
|
|
|
|
self._start_trace(llm_run)
|
|
|
|
@ -130,7 +130,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
|
|
|
|
|
run_id_ = str(run_id)
|
|
|
|
|
llm_run = self.run_map.get(run_id_)
|
|
|
|
|
if llm_run is None or llm_run.run_type != RunTypeEnum.llm:
|
|
|
|
|
if llm_run is None or llm_run.run_type != "llm":
|
|
|
|
|
raise TracerException("No LLM Run found to be traced")
|
|
|
|
|
llm_run.events.append(
|
|
|
|
|
{
|
|
|
|
@ -182,7 +182,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
|
|
|
|
|
run_id_ = str(run_id)
|
|
|
|
|
llm_run = self.run_map.get(run_id_)
|
|
|
|
|
if llm_run is None or llm_run.run_type != RunTypeEnum.llm:
|
|
|
|
|
if llm_run is None or llm_run.run_type != "llm":
|
|
|
|
|
raise TracerException("No LLM Run found to be traced")
|
|
|
|
|
llm_run.outputs = response.dict()
|
|
|
|
|
for i, generations in enumerate(response.generations):
|
|
|
|
@ -210,7 +210,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
|
|
|
|
|
run_id_ = str(run_id)
|
|
|
|
|
llm_run = self.run_map.get(run_id_)
|
|
|
|
|
if llm_run is None or llm_run.run_type != RunTypeEnum.llm:
|
|
|
|
|
if llm_run is None or llm_run.run_type != "llm":
|
|
|
|
|
raise TracerException("No LLM Run found to be traced")
|
|
|
|
|
llm_run.error = repr(error)
|
|
|
|
|
llm_run.end_time = datetime.utcnow()
|
|
|
|
@ -246,7 +246,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
execution_order=execution_order,
|
|
|
|
|
child_execution_order=execution_order,
|
|
|
|
|
child_runs=[],
|
|
|
|
|
run_type=RunTypeEnum.chain,
|
|
|
|
|
run_type="chain",
|
|
|
|
|
tags=tags or [],
|
|
|
|
|
)
|
|
|
|
|
self._start_trace(chain_run)
|
|
|
|
@ -259,7 +259,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
if not run_id:
|
|
|
|
|
raise TracerException("No run_id provided for on_chain_end callback.")
|
|
|
|
|
chain_run = self.run_map.get(str(run_id))
|
|
|
|
|
if chain_run is None or chain_run.run_type != RunTypeEnum.chain:
|
|
|
|
|
if chain_run is None or chain_run.run_type != "chain":
|
|
|
|
|
raise TracerException("No chain Run found to be traced")
|
|
|
|
|
|
|
|
|
|
chain_run.outputs = outputs
|
|
|
|
@ -279,7 +279,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
if not run_id:
|
|
|
|
|
raise TracerException("No run_id provided for on_chain_error callback.")
|
|
|
|
|
chain_run = self.run_map.get(str(run_id))
|
|
|
|
|
if chain_run is None or chain_run.run_type != RunTypeEnum.chain:
|
|
|
|
|
if chain_run is None or chain_run.run_type != "chain":
|
|
|
|
|
raise TracerException("No chain Run found to be traced")
|
|
|
|
|
|
|
|
|
|
chain_run.error = repr(error)
|
|
|
|
@ -316,7 +316,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
execution_order=execution_order,
|
|
|
|
|
child_execution_order=execution_order,
|
|
|
|
|
child_runs=[],
|
|
|
|
|
run_type=RunTypeEnum.tool,
|
|
|
|
|
run_type="tool",
|
|
|
|
|
tags=tags or [],
|
|
|
|
|
)
|
|
|
|
|
self._start_trace(tool_run)
|
|
|
|
@ -327,7 +327,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
if not run_id:
|
|
|
|
|
raise TracerException("No run_id provided for on_tool_end callback.")
|
|
|
|
|
tool_run = self.run_map.get(str(run_id))
|
|
|
|
|
if tool_run is None or tool_run.run_type != RunTypeEnum.tool:
|
|
|
|
|
if tool_run is None or tool_run.run_type != "tool":
|
|
|
|
|
raise TracerException("No tool Run found to be traced")
|
|
|
|
|
|
|
|
|
|
tool_run.outputs = {"output": output}
|
|
|
|
@ -347,7 +347,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
if not run_id:
|
|
|
|
|
raise TracerException("No run_id provided for on_tool_error callback.")
|
|
|
|
|
tool_run = self.run_map.get(str(run_id))
|
|
|
|
|
if tool_run is None or tool_run.run_type != RunTypeEnum.tool:
|
|
|
|
|
if tool_run is None or tool_run.run_type != "tool":
|
|
|
|
|
raise TracerException("No tool Run found to be traced")
|
|
|
|
|
|
|
|
|
|
tool_run.error = repr(error)
|
|
|
|
@ -386,7 +386,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
child_execution_order=execution_order,
|
|
|
|
|
tags=tags,
|
|
|
|
|
child_runs=[],
|
|
|
|
|
run_type=RunTypeEnum.retriever,
|
|
|
|
|
run_type="retriever",
|
|
|
|
|
)
|
|
|
|
|
self._start_trace(retrieval_run)
|
|
|
|
|
self._on_retriever_start(retrieval_run)
|
|
|
|
@ -402,7 +402,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
if not run_id:
|
|
|
|
|
raise TracerException("No run_id provided for on_retriever_error callback.")
|
|
|
|
|
retrieval_run = self.run_map.get(str(run_id))
|
|
|
|
|
if retrieval_run is None or retrieval_run.run_type != RunTypeEnum.retriever:
|
|
|
|
|
if retrieval_run is None or retrieval_run.run_type != "retriever":
|
|
|
|
|
raise TracerException("No retriever Run found to be traced")
|
|
|
|
|
|
|
|
|
|
retrieval_run.error = repr(error)
|
|
|
|
@ -418,7 +418,7 @@ class BaseTracer(BaseCallbackHandler, ABC):
|
|
|
|
|
if not run_id:
|
|
|
|
|
raise TracerException("No run_id provided for on_retriever_end callback.")
|
|
|
|
|
retrieval_run = self.run_map.get(str(run_id))
|
|
|
|
|
if retrieval_run is None or retrieval_run.run_type != RunTypeEnum.retriever:
|
|
|
|
|
if retrieval_run is None or retrieval_run.run_type != "retriever":
|
|
|
|
|
raise TracerException("No retriever Run found to be traced")
|
|
|
|
|
retrieval_run.outputs = {"documents": documents}
|
|
|
|
|
retrieval_run.end_time = datetime.utcnow()
|
|
|
|
|