tools
AddRecipientTool
¶
Bases: ToolMessage
Used by LLM to add a recipient to the previous message, when it has forgotten to specify a recipient. This avoids having to re-generate the previous message (and thus saves token-cost and time).
response(agent)
¶
Returns:
Type | Description |
---|---|
ChatDocument
|
with content set to self.content and metadata.recipient set to self.recipient. |
Source code in langroid/agent/tools/recipient_tool.py
RecipientTool
¶
Bases: ToolMessage
Used by LLM to send a message to a specific recipient.
Useful in cases where an LLM is talking to 2 or more agents (or an Agent and human user), and needs to specify which agent (task) its message is intended for. The recipient name should be the name of a task (which is normally the name of the agent that the task wraps, although the task can have its own name).
To use this tool/function-call, LLM must generate a JSON structure
with these fields:
{
"request": "recipient_message", # also the function name when using fn-calling
"intended_recipient": content
will be sent to the intended_recipient
task.
create(recipients, default='')
classmethod
¶
Create a restricted version of RecipientTool that only allows certain recipients, and possibly sets a default recipient.
Source code in langroid/agent/tools/recipient_tool.py
instructions()
classmethod
¶
Generate instructions for using this tool/function. These are intended to be appended to the system message of the LLM.
Source code in langroid/agent/tools/recipient_tool.py
response(agent)
¶
When LLM has correctly used this tool, construct a ChatDocument with an explicit recipient, and make it look like it is from the LLM.
Returns:
Type | Description |
---|---|
ChatDocument
|
with content set to self.content and metadata.recipient set to self.intended_recipient. |
Source code in langroid/agent/tools/recipient_tool.py
handle_message_fallback(agent, msg)
staticmethod
¶
Response of agent if this tool is not used, e.g.
the LLM simply sends a message without using this tool.
This method has two purposes:
(a) Alert the LLM that it has forgotten to specify a recipient, and prod it
to use the add_recipient
tool to specify just the recipient
(and not re-generate the entire message).
(b) Save the content of the message in the agent's content
field,
so the agent can construct a ChatDocument with this content once LLM
later specifies a recipient using the add_recipient
tool.
This method is used to set the agent's handle_message_fallback() method.
Returns:
Type | Description |
---|---|
str
|
reminder to LLM to use the |
Source code in langroid/agent/tools/recipient_tool.py
RewindTool
¶
Bases: ToolMessage
Used by LLM to rewind (i.e. backtrack) to the n
th Assistant message
and replace with a new msg.
response(agent)
¶
Define the tool-handler method for this tool here itself, since it is a generic tool whose functionality should be the same for any agent.
When LLM has correctly used this tool, rewind this agent's
message_history
to the n
th assistant msg, and replace it with content
.
We need to mock it as if the LLM is sending this message.
Within a multi-agent scenario, this also means that any other messages dependent
on this message will need to be invalidated --
so go down the chain of child messages and clear each agent's history
back to the msg_idx
corresponding to the child message.
Returns:
Type | Description |
---|---|
ChatDocument
|
with content set to self.content. |
Source code in langroid/agent/tools/rewind_tool.py
AgentDoneTool
¶
Bases: ToolMessage
Tool for AGENT entity (i.e. agent_response or downstream tool handling fns) to signal the current task is done.
DoneTool
¶
Bases: ToolMessage
Tool for Agent Entity (i.e. agent_response) or LLM entity (i.e. llm_response) to signal the current task is done, with some content as the result.
ForwardTool
¶
Bases: PassTool
Tool for forwarding the received msg (ChatDocument) to another agent or entity. Similar to PassTool, but with a specified recipient agent.
response(agent, chat_doc)
¶
When this tool is enabled for an Agent, this will result in a method
added to the Agent with signature:
forward_tool(self, tool: ForwardTool, chat_doc: ChatDocument) -> ChatDocument:
Source code in langroid/agent/tools/orchestration.py
PassTool
¶
Bases: ToolMessage
Tool for "passing" on the received msg (ChatDocument), so that an as-yet-unspecified agent can handle it. Similar to ForwardTool, but without specifying the recipient agent.
response(agent, chat_doc)
¶
When this tool is enabled for an Agent, this will result in a method
added to the Agent with signature:
forward_tool(self, tool: PassTool, chat_doc: ChatDocument) -> ChatDocument:
Source code in langroid/agent/tools/orchestration.py
SendTool
¶
Bases: ToolMessage
Tool for agent or LLM to send content to a specified agent. Similar to RecipientTool.
AgentSendTool
¶
Bases: ToolMessage
Tool for Agent (i.e. agent_response) to send content or tool_messages to a specified agent. Similar to SendTool except that AgentSendTool is only usable by agent_response (or handler of another tool), to send content or tools to another agent. SendTool does not allow sending tools.
DonePassTool
¶
Bases: PassTool
Tool to signal DONE, AND Pass incoming/current msg as result. Similar to PassTool, except we append a DoneTool to the result tool_messages.
ResultTool
¶
Bases: ToolMessage
Class to use as a wrapper for sending arbitrary results from an Agent's
agent_response or tool handlers, to:
(a) trigger completion of the current task (similar to (Agent)DoneTool), and
(b) be returned as the result of the current task, i.e. this tool would appear
in the resulting ChatDocument's tool_messages
list.
See test_tool_handlers_and_results in test_tool_messages.py, and
examples/basic/tool-extract-short-example.py.
Note
- when defining a tool handler or agent_response, you can directly return ResultTool(field1 = val1, ...), where the values can be arbitrary data structures, including nested Pydantic objs, or you can define a subclass of ResultTool with the fields you want to return.
- This is a special ToolMessage that is NOT meant to be used or handled by an agent.
- AgentDoneTool is more restrictive in that you can only send a
content
ortools
in the result.
FinalResultTool
¶
Bases: ToolMessage
Class to use as a wrapper for sending arbitrary results from an Agent's
agent_response or tool handlers, to:
(a) trigger completion of the current task as well as all parent tasks, and
(b) be returned as the final result of the root task, i.e. this tool would appear
in the final ChatDocument's tool_messages
list.
See test_tool_handlers_and_results in test_tool_messages.py, and
examples/basic/chat-tool-function.py.
Note
- when defining a tool handler or agent_response, you can directly return FinalResultTool(field1 = val1, ...), where the values can be arbitrary data structures, including nested Pydantic objs, or you can define a subclass of FinalResultTool with the fields you want to return.
- This is a special ToolMessage that is NOT meant to be used by an agent's llm_response, but only by agent_response or tool handlers.
- A subclass of this tool can be defined, with specific fields, and
with _allow_llm_use = True, to allow the LLM to generate this tool,
and have the effect of terminating the current and all parent tasks,
with the tool appearing in the final ChatDocument's
tool_messages
list. See examples/basic/multi-agent-return-result.py.