Core[major]: Base Tracer to propagate raw output from tool for on_tool_end (#18932)

This PR completes work for PR #18798 to expose raw tool output in
on_tool_end.

Affected APIs:
* astream_log
* astream_events
* callbacks sent to langsmith via langsmith-sdk
* Any other code that relies on BaseTracer!

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
Mohammad Mohtashim 2024-04-02 05:24:46 +04:00 committed by GitHub
parent 2ae6dcdf01
commit 9ae2df36fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 82 additions and 2 deletions

View File

@ -984,7 +984,6 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
Args: Args:
output (Any): The output of the tool. output (Any): The output of the tool.
""" """
output = str(output)
handle_event( handle_event(
self.handlers, self.handlers,
"on_tool_end", "on_tool_end",

View File

@ -506,7 +506,6 @@ class BaseTracer(BaseCallbackHandler, ABC):
def on_tool_end(self, output: Any, *, run_id: UUID, **kwargs: Any) -> Run: def on_tool_end(self, output: Any, *, run_id: UUID, **kwargs: Any) -> Run:
"""End a trace for a tool run.""" """End a trace for a tool run."""
output = str(output)
tool_run = self._get_run(run_id, run_type="tool") tool_run = self._get_run(run_id, run_type="tool")
tool_run.outputs = {"output": output} tool_run.outputs = {"output": output}
tool_run.end_time = datetime.now(timezone.utc) tool_run.end_time = datetime.now(timezone.utc)

View File

@ -52,6 +52,88 @@ async def _collect_events(events: AsyncIterator[StreamEvent]) -> List[StreamEven
return events_ return events_
async def test_event_stream_with_simple_function_tool() -> None:
"""Test the event stream with a function and tool"""
def foo(x: int) -> dict:
"""Foo"""
return {"x": 5}
@tool
def get_docs(x: int) -> List[Document]:
"""Hello Doc"""
return [Document(page_content="hello")]
chain = RunnableLambda(foo) | get_docs
events = await _collect_events(chain.astream_events({}, version="v1"))
assert events == [
{
"event": "on_chain_start",
"run_id": "",
"name": "RunnableSequence",
"tags": [],
"metadata": {},
"data": {"input": {}},
},
{
"event": "on_chain_start",
"name": "foo",
"run_id": "",
"tags": ["seq:step:1"],
"metadata": {},
"data": {},
},
{
"event": "on_chain_stream",
"name": "foo",
"run_id": "",
"tags": ["seq:step:1"],
"metadata": {},
"data": {"chunk": {"x": 5}},
},
{
"event": "on_chain_end",
"name": "foo",
"run_id": "",
"tags": ["seq:step:1"],
"metadata": {},
"data": {"input": {}, "output": {"x": 5}},
},
{
"event": "on_tool_start",
"name": "get_docs",
"run_id": "",
"tags": ["seq:step:2"],
"metadata": {},
"data": {"input": {"x": 5}},
},
{
"event": "on_tool_end",
"name": "get_docs",
"run_id": "",
"tags": ["seq:step:2"],
"metadata": {},
"data": {"input": {"x": 5}, "output": [Document(page_content="hello")]},
},
{
"event": "on_chain_stream",
"run_id": "",
"tags": [],
"metadata": {},
"name": "RunnableSequence",
"data": {"chunk": [Document(page_content="hello")]},
},
{
"event": "on_chain_end",
"name": "RunnableSequence",
"run_id": "",
"tags": [],
"metadata": {},
"data": {"output": [Document(page_content="hello")]},
},
]
async def test_event_stream_with_single_lambda() -> None: async def test_event_stream_with_single_lambda() -> None:
"""Test the event stream with a tool.""" """Test the event stream with a tool."""