chat_agent
ChatAgentConfig
¶
Bases: AgentConfig
Configuration for ChatAgent
Attributes:
system_message: system message to include in message sequence
(typically defines role and task of agent).
Used only if task
is not specified in the constructor.
user_message: user message to include in message sequence.
Used only if task
is not specified in the constructor.
use_tools: whether to use our own ToolMessages mechanism
use_functions_api: whether to use functions/tools native to the LLM API
(e.g. OpenAI's function_call
or tool_call
mechanism)
use_tools_api: When use_functions_api
is True, if this is also True,
the OpenAI tool-call API is used, rather than the older/deprecated
function-call API. However the tool-call API has some tricky aspects,
hence we set this to False by default.
enable_orchestration_tool_handling: whether to enable handling of orchestration
tools, e.g. ForwardTool, DoneTool, PassTool, etc.
ChatAgent(config=ChatAgentConfig(), task=None)
¶
Bases: Agent
Chat Agent interacting with external env
(could be human, or external tools).
The agent (the LLM actually) is provided with an optional "Task Spec",
which is a sequence of LLMMessage
s. These are used to initialize
the task_messages
of the agent.
In most applications we will use a ChatAgent
rather than a bare Agent
.
The Agent
class mainly exists to hold various common methods and attributes.
One difference between ChatAgent
and Agent
is that ChatAgent
's
llm_response
method uses "chat mode" API (i.e. one that takes a
message sequence rather than a single message),
whereas the same method in the Agent
class uses "completion mode" API (i.e. one
that takes a single message).
config: settings for the agent
Source code in langroid/agent/chat_agent.py
task_messages: List[LLMMessage]
property
¶
The task messages are the initial messages that define the task of the agent. There will be at least a system message plus possibly a user msg. Returns: List[LLMMessage]: the task messages
init_state()
¶
Initialize the state of the agent. Just conversation state here, but subclasses can override this to initialize other state.
Source code in langroid/agent/chat_agent.py
from_id(id)
staticmethod
¶
Get an agent from its ID Args: agent_id (str): ID of the agent Returns: ChatAgent: The agent with the given ID
clone(i=0)
¶
Create i'th clone of this agent, ensuring tool use/handling is cloned. Important: We assume all member variables are in the init method here and in the Agent class. TODO: We are attempting to clone an agent after its state has been changed in possibly many ways. Below is an imperfect solution. Caution advised. Revisit later.
Source code in langroid/agent/chat_agent.py
clear_history(start=-2)
¶
Clear the message history, starting at the index start
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start |
int
|
index of first message to delete; default = -2 (i.e. delete last 2 messages, typically these are the last user and assistant messages) |
-2
|
Source code in langroid/agent/chat_agent.py
update_history(message, response)
¶
Update the message history with the latest user message and LLM response. Args: message (str): user message response: (str): LLM response
Source code in langroid/agent/chat_agent.py
tool_format_rules()
¶
Specification of tool formatting rules
(typically JSON-based but can be non-JSON, e.g. XMLToolMessage),
based on the currently enabled usable ToolMessage
s
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
formatting rules |
Source code in langroid/agent/chat_agent.py
tool_instructions()
¶
Instructions for tools or function-calls, for enabled and usable Tools. These are inserted into system prompt regardless of whether we are using our own ToolMessage mechanism or the LLM's function-call mechanism.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
concatenation of instructions for all usable tools |
Source code in langroid/agent/chat_agent.py
augment_system_message(message)
¶
Augment the system message with the given message. Args: message (str): system message
last_message_with_role(role)
¶
from message_history
, return the last message with role role
Source code in langroid/agent/chat_agent.py
nth_message_idx_with_role(role, n)
¶
Index of n
th message in message_history, with specified role.
(n is assumed to be 1-based, i.e. 1 is the first message with that role).
Return -1 if not found. Index = 0 is the first message in the history.
Source code in langroid/agent/chat_agent.py
update_last_message(message, role=Role.USER)
¶
Update the last message that has role role
in the message history.
Useful when we want to replace a long user prompt, that may contain context
documents plus a question, with just the question.
Args:
message (str): new message to replace with
role (str): role of message to replace
Source code in langroid/agent/chat_agent.py
unhandled_tools()
¶
The set of tools that are known but not handled. Useful in task flow: an agent can refuse to accept an incoming msg when it only has unhandled tools.
Source code in langroid/agent/chat_agent.py
enable_message(message_class, use=True, handle=True, force=False, require_recipient=False, include_defaults=True)
¶
Add the tool (message class) to the agent, and enable either - tool USE (i.e. the LLM can generate JSON to use this tool), - tool HANDLING (i.e. the agent can handle JSON from this tool),
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message_class |
Optional[Type[ToolMessage] | List[Type[ToolMessage]]]
|
The ToolMessage class OR List of such classes to enable, for USE, or HANDLING, or both. If this is a list of ToolMessage classes, then the remain args are applied to all classes. Optional; if None, then apply the enabling to all tools in the agent's toolset that have been enabled so far. |
required |
use |
bool
|
IF True, allow the agent (LLM) to use this tool (or all tools), else disallow |
True
|
handle |
bool
|
if True, allow the agent (LLM) to handle (i.e. respond to) this tool (or all tools) |
True
|
force |
bool
|
whether to FORCE the agent (LLM) to USE the specific
tool represented by |
False
|
require_recipient |
bool
|
whether to require that recipient be specified
when using the tool message (only applies if |
False
|
include_defaults |
bool
|
whether to include fields that have default values, in the "properties" section of the JSON format instructions. (Normally the OpenAI completion API ignores these fields, but the Assistant fn-calling seems to pay attn to these, and if we don't want this, we should set this to False.) |
True
|
Source code in langroid/agent/chat_agent.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 |
|
disable_message_handling(message_class=None)
¶
Disable this agent from RESPONDING to a message_class
(Tool). If
message_class
is None, then disable this agent from responding to ALL.
Args:
message_class: The ToolMessage class to disable; Optional.
Source code in langroid/agent/chat_agent.py
disable_message_use(message_class)
¶
Disable this agent from USING a message class (Tool).
If message_class
is None, then disable this agent from USING ALL tools.
Args:
message_class: The ToolMessage class to disable.
If None, disable all.
Source code in langroid/agent/chat_agent.py
disable_message_use_except(message_class)
¶
Disable this agent from USING ALL messages EXCEPT a message class (Tool) Args: message_class: The only ToolMessage class to allow
Source code in langroid/agent/chat_agent.py
truncate_message(idx, tokens=5, warning='...[Contents truncated!]')
¶
Truncate message at idx in msg history to tokens
tokens
Source code in langroid/agent/chat_agent.py
llm_response(message=None)
¶
Respond to a single user message, appended to the message history, in "chat" mode Args: message (str|ChatDocument): message or ChatDocument object to respond to. If None, use the self.task_messages Returns: LLM response as a ChatDocument object
Source code in langroid/agent/chat_agent.py
llm_response_async(message=None)
async
¶
Async version of llm_response
. See there for details.
Source code in langroid/agent/chat_agent.py
init_message_history()
¶
Initialize the message history with the system message and user message
Source code in langroid/agent/chat_agent.py
llm_response_messages(messages, output_len=None, tool_choice='auto')
¶
Respond to a series of messages, e.g. with OpenAI ChatCompletion Args: messages: seq of messages (with role, content fields) sent to LLM output_len: max number of tokens expected in response. If None, use the LLM's default max_output_tokens. Returns: Document (i.e. with fields "content", "metadata")
Source code in langroid/agent/chat_agent.py
llm_response_messages_async(messages, output_len=None, tool_choice='auto')
async
¶
Async version of llm_response_messages
. See there for details.
Source code in langroid/agent/chat_agent.py
llm_response_forget(message)
¶
LLM Response to single message, and restore message_history. In effect a "one-off" message & response that leaves agent message history state intact.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
str
|
user message |
required |
Returns:
Type | Description |
---|---|
ChatDocument
|
A Document object with the response. |
Source code in langroid/agent/chat_agent.py
llm_response_forget_async(message)
async
¶
Async version of llm_response_forget
. See there for details.
Source code in langroid/agent/chat_agent.py
chat_num_tokens(messages=None)
¶
Total number of tokens in the message history so far.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages |
Optional[List[LLMMessage]]
|
if provided, compute the number of tokens in this list of messages, rather than the current message history. |
None
|
Returns: int: number of tokens in message history
Source code in langroid/agent/chat_agent.py
message_history_str(i=None)
¶
Return a string representation of the message history Args: i: if provided, return only the i-th message when i is postive, or last k messages when i = -k. Returns: