rewind_tool
langroid/agent/tools/rewind_tool.py
The rewind_tool is used to rewind to the nth previous Assistant message
and replace it with a new content. This is useful in several scenarios and
- saves token-cost + inference time,
- reduces distracting clutter in chat history, which helps improve response quality.
This is intended to mimic how a human user might use a chat interface, where they go down a conversation path, and want to go back in history to "edit and re-submit" a previous message, to get a better response.
See usage examples in tests/main/test_rewind_tool.py.
RewindTool
¶
Bases: ToolMessage
Used by LLM to rewind (i.e. backtrack) to the nth 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 nth 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
prune_messages(agent, idx)
¶
Clear the message history of agent, starting at index idx,
taking care to first clear all dependent messages (possibly from other agents'
message histories) that are linked to the message at idx, via the child_id field
of the metadata field of the ChatDocument linked from the message at idx.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
ChatAgent
|
The agent whose message history is to be pruned. |
required |
idx
|
int
|
The index from which to start clearing the message history. |
required |
Returns:
| Type | Description |
|---|---|
ChatDocument | None
|
The parent ChatDocument of the ChatDocument linked from the message at |
ChatDocument | None
|
if it exists, else None. |