Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langgraph[patch]: ToolNode support for tools outputting msg #977

Merged
merged 7 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 12 additions & 26 deletions libs/langgraph/langgraph/prebuilt/tool_node.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import json
from typing import Any, Callable, Dict, Literal, Optional, Sequence, Union

from langchain_core.messages import AIMessage, AnyMessage, ToolCall, ToolMessage
Expand All @@ -11,20 +10,11 @@
from langgraph.utils import RunnableCallable


def str_output(output: Any) -> str:
if isinstance(output, str):
return output
else:
try:
return json.dumps(output)
except Exception:
return str(output)


class ToolNode(RunnableCallable):
"""A node that runs the tools requested in the last AIMessage. It can be used
either in StateGraph with a "messages" key or in MessageGraph. If multiple
tool calls are requested, they will be run in parallel. The output will be
"""A node that runs the tools called in the last AIMessage.

It can be used either in StateGraph with a "messages" key or in MessageGraph. If
multiple tool calls are requested, they will be run in parallel. The output will be
a list of ToolMessages, one for each tool call.

The `ToolNode` is roughly analogous to:
Expand Down Expand Up @@ -79,14 +69,13 @@ def _func(

def run_one(call: ToolCall):
try:
output = self.tools_by_name[call["name"]].invoke(call["args"], config)
input = {**call, **{"type": "tool_call"}}
return self.tools_by_name[call["name"]].invoke(input, config)
except Exception as e:
if not self.handle_tool_errors:
raise e
output = f"Error: {repr(e)}\n Please fix your mistakes."
return ToolMessage(
content=str_output(output), name=call["name"], tool_call_id=call["id"]
)
content = f"Error: {repr(e)}\n Please fix your mistakes."
return ToolMessage(content, name=call["name"], tool_call_id=call["id"])

with get_executor_for_config(config) as executor:
outputs = [*executor.map(run_one, message.tool_calls)]
Expand All @@ -112,16 +101,13 @@ async def _afunc(

async def run_one(call: ToolCall):
try:
output = await self.tools_by_name[call["name"]].ainvoke(
call["args"], config
)
input = {**call, **{"type": "tool_call"}}
return await self.tools_by_name[call["name"]].ainvoke(input, config)
except Exception as e:
if not self.handle_tool_errors:
raise e
output = f"Error: {repr(e)}\n Please fix your mistakes."
return ToolMessage(
content=str_output(output), name=call["name"], tool_call_id=call["id"]
)
content = f"Error: {repr(e)}\n Please fix your mistakes."
return ToolMessage(content, name=call["name"], tool_call_id=call["id"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we no lnoger passing content by keyword?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleaner this way (imo), messages accept content as pos arg


outputs = await asyncio.gather(*(run_one(call) for call in message.tool_calls))
if output_type == "list":
Expand Down
8 changes: 4 additions & 4 deletions libs/langgraph/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions libs/langgraph/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repository = "https://www.github.com/langchain-ai/langgraph"

[tool.poetry.dependencies]
python = ">=3.9.0,<4.0"
langchain-core = ">=0.2.15,<0.3"
langchain-core = ">=0.2.19,<0.3"


[tool.poetry.group.dev.dependencies]
Expand Down Expand Up @@ -61,7 +61,7 @@ omit = ["tests/*"]
[tool.pytest-watcher]
now = true
delay = 0.1
runner_args = ["-x", "--ff", "-vv", "--snapshot-update"]
runner_args = ["--ff", "-vv", "--snapshot-update"]
patterns = ["*.py"]

[build-system]
Expand Down
Loading
Loading