agent
Agent(config=AgentConfig())
¶
Bases: ABC
An Agent is an abstraction that encapsulates mainly two components:
- a language model (LLM)
- a vector store (vecdb)
plus associated components such as a parser, and variables that hold information about any tool/function-calling messages that have been defined.
Source code in langroid/agent/base.py
indent: str
property
writable
¶
Indentation to print before any responses from the agent's entities.
init_state()
¶
entity_responders()
¶
Sequence of (entity, response_method) pairs. This sequence is used
in a Task
to respond to the current pending message.
See Task.step()
for details.
Returns:
Sequence of (entity, response_method) pairs.
Source code in langroid/agent/base.py
entity_responders_async()
¶
Async version of entity_responders
. See there for details.
Source code in langroid/agent/base.py
enable_message_handling(message_class=None)
¶
Enable an agent to RESPOND (i.e. handle) a "tool" message of a specific type
from LLM. Also "registers" (i.e. adds) the message_class
to the
self.llm_tools_map
dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message_class |
Optional[Type[ToolMessage]]
|
The message class to enable; Optional; if None, all known message classes are enabled for handling. |
None
|
Source code in langroid/agent/base.py
disable_message_handling(message_class=None)
¶
Disable a message class from being handled by this Agent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message_class |
Optional[Type[ToolMessage]]
|
The message class to disable. If None, all message classes are disabled. |
None
|
Source code in langroid/agent/base.py
sample_multi_round_dialog()
¶
Generate a sample multi-round dialog based on enabled message classes. Returns: str: The sample dialog string.
Source code in langroid/agent/base.py
create_agent_response(content=None, content_any=None, tool_messages=[], oai_tool_calls=None, oai_tool_choice='auto', oai_tool_id2result=None, function_call=None, recipient='')
¶
Template for agent_response.
Source code in langroid/agent/base.py
agent_response(msg=None)
¶
Response from the "agent itself", typically (but not only)
used to handle LLM's "tool message" or function_call
(e.g. OpenAI function_call
).
Args:
msg (str|ChatDocument): the input to respond to: if msg is a string,
and it contains a valid JSON-structured "tool message", or
if msg is a ChatDocument, and it contains a function_call
.
Returns:
Optional[ChatDocument]: the response, packaged as a ChatDocument
Source code in langroid/agent/base.py
process_tool_results(results, id2result, tool_calls=None)
¶
Process results from a response, based on whether they are results of OpenAI tool-calls from THIS agent, so that we can construct an appropriate LLMMessage that contains tool results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results |
str
|
A possible string result from handling tool(s) |
required |
id2result |
OrderedDict[str, str] | None
|
A dict of OpenAI tool id -> result, if there are multiple tool results. |
required |
tool_calls |
List[OpenAIToolCall] | None
|
List of OpenAI tool-calls that the results are a response to. |
None
|
Return
- str: The response string
- Dict[str,str]|None: A dict of OpenAI tool id -> result, if there are multiple tool results.
- str|None: tool_id if there was a single tool result
Source code in langroid/agent/base.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 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 |
|
response_template(e, content=None, content_any=None, tool_messages=[], oai_tool_calls=None, oai_tool_choice='auto', oai_tool_id2result=None, function_call=None, recipient='')
¶
Template for response from entity e
.
Source code in langroid/agent/base.py
create_user_response(content=None, content_any=None, tool_messages=[], oai_tool_calls=None, oai_tool_choice='auto', oai_tool_id2result=None, function_call=None, recipient='')
¶
Template for user_response.
Source code in langroid/agent/base.py
user_response(msg=None)
¶
Get user response to current message. Could allow (human) user to intervene with an actual answer, or quit using "q" or "x"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str | ChatDocument
|
the string to respond to. |
None
|
Returns:
Type | Description |
---|---|
Optional[ChatDocument]
|
(str) User response, packaged as a ChatDocument |
Source code in langroid/agent/base.py
llm_can_respond(message=None)
¶
Whether the LLM can respond to a message. Args: message (str|ChatDocument): message or ChatDocument object to respond to.
Returns:
Source code in langroid/agent/base.py
can_respond(message=None)
¶
Whether the agent can respond to a message. Used in Task.py to skip a sub-task when we know it would not respond. Args: message (str|ChatDocument): message or ChatDocument object to respond to.
Source code in langroid/agent/base.py
create_llm_response(content=None, content_any=None, tool_messages=[], oai_tool_calls=None, oai_tool_choice='auto', oai_tool_id2result=None, function_call=None, recipient='')
¶
Template for llm_response.
Source code in langroid/agent/base.py
llm_response_async(msg=None)
async
¶
Asynch version of llm_response
. See there for details.
Source code in langroid/agent/base.py
llm_response(msg=None)
¶
LLM response to a prompt. Args: msg (str|ChatDocument): prompt string, or ChatDocument object
Returns:
Type | Description |
---|---|
Optional[ChatDocument]
|
Response from LLM, packaged as a ChatDocument |
Source code in langroid/agent/base.py
has_tool_message_attempt(msg)
¶
Check whether msg contains a Tool/fn-call attempt (by the LLM).
CAUTION: This uses self.get_tool_messages(msg) which as a side-effect may update msg.tool_messages when msg is a ChatDocument, if there are any tools in msg.
Source code in langroid/agent/base.py
has_only_unhandled_tools(msg)
¶
Does the msg have at least one tool, and ALL tools are disabled for handling by this agent?
Source code in langroid/agent/base.py
get_tool_messages(msg, all_tools=False)
¶
Get ToolMessages recognized in msg, handle-able by this agent. NOTE: as a side-effect, this will update msg.tool_messages when msg is a ChatDocument and msg contains tool messages.
If all_tools is True: - return all tools, i.e. any tool in self.llm_tools_known, whether it is handled by this agent or not; - otherwise, return only the tools handled by this agent.
Source code in langroid/agent/base.py
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 |
|
get_formatted_tool_messages(input_str)
¶
Returns ToolMessage objects (tools) corresponding to tool-formatted substrings, if any. ASSUMPTION - These tools are either ALL JSON-based, or ALL XML-based (i.e. not a mix of both). Terminology: a "formatted tool msg" is one which the LLM generates as part of its raw string output, rather than within a JSON object in the API response (i.e. this method does not extract tools/fns returned by OpenAI's tools/fns API or similar APIs).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_str |
str
|
input string, typically a message sent by an LLM |
required |
Returns:
Type | Description |
---|---|
List[ToolMessage]
|
List[ToolMessage]: list of ToolMessage objects |
Source code in langroid/agent/base.py
get_function_call_class(msg)
¶
From ChatDocument (constructed from an LLM Response), get the ToolMessage
corresponding to the function_call
if it exists.
Source code in langroid/agent/base.py
get_oai_tool_calls_classes(msg)
¶
From ChatDocument (constructed from an LLM Response), get
a list of ToolMessages corresponding to the tool_calls
, if any.
Source code in langroid/agent/base.py
tool_validation_error(ve)
¶
Handle a validation error raised when parsing a tool message, when there is a legit tool name used, but it has missing/bad fields. Args: tool (ToolMessage): The tool message that failed validation ve (ValidationError): The exception raised
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The error message to send back to the LLM |
Source code in langroid/agent/base.py
handle_message(msg)
¶
Handle a "tool" message either a string containing one or more
valid "tool" JSON substrings, or a
ChatDocument containing a function_call
attribute.
Handle with the corresponding handler method, and return
the results as a combined string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str | ChatDocument
|
The string or ChatDocument to handle |
required |
Returns:
Type | Description |
---|---|
None | str | OrderedDict[str, str] | ChatDocument
|
The result of the handler method can be:
- None if no tools successfully handled, or no tools present
- str if langroid-native JSON tools were handled, and results concatenated,
OR there's a SINGLE OpenAI tool-call.
(We do this so the common scenario of a single tool/fn-call
has a simple behavior).
- Dict[str, str] if multiple OpenAI tool-calls were handled
(dict is an id->result map)
- ChatDocument if a handler returned a ChatDocument, intended to be the
final response of the |
Source code in langroid/agent/base.py
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 |
|
handle_message_fallback(msg)
¶
Fallback method for the "no-tools" scenario. This method can be overridden by subclasses, e.g., to create a "reminder" message when a tool is expected but the LLM "forgot" to generate one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str | ChatDocument
|
The input msg to handle |
required |
Returns: Any: The result of the handler method
Source code in langroid/agent/base.py
to_ChatDocument(msg, orig_tool_name=None, chat_doc=None, author_entity=Entity.AGENT)
¶
Convert result of a responder (agent_response or llm_response, or task.run()), or tool handler, or handle_message_fallback, to a ChatDocument, to enable handling by other responders/tasks in a task loop possibly involving multiple agents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
Any
|
The result of a responder or tool handler or task.run() |
required |
orig_tool_name |
str
|
The original tool name that generated the response, if any. |
None
|
chat_doc |
ChatDocument
|
The original ChatDocument object that |
None
|
author_entity |
Entity
|
The intended author of the result ChatDocument |
AGENT
|
Source code in langroid/agent/base.py
from_ChatDocument(msg, output_type)
¶
Extract a desired output_type from a ChatDocument object.
We use this fallback order:
- if msg.content_any
exists and matches the output_type, return it
- if msg.content
exists and output_type is str return it
- if output_type is a ToolMessage, return the first tool in msg.tool_messages
- if output_type is a list of ToolMessage,
return all tools in msg.tool_messages
- search for a tool in msg.tool_messages
that has a field of output_type,
and if found, return that field value
- return None if all the above fail
Source code in langroid/agent/base.py
handle_tool_message(tool, chat_doc=None)
¶
Respond to a tool request from the LLM, in the form of an ToolMessage object.
Args:
tool: ToolMessage object representing the tool request.
chat_doc: Optional ChatDocument object containing the tool request.
This is passed to the tool-handler method only if it has a chat_doc
argument.
Returns:
Source code in langroid/agent/base.py
update_token_usage(response, prompt, stream, chat=True, print_response_stats=True)
¶
Updates response.usage
obj (token usage and cost fields).the usage memebr
It updates the cost after checking the cache and updates the
tokens (prompts and completion) if the response stream is True, because OpenAI
doesn't returns these fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
LLMResponse
|
LLMResponse object |
required |
prompt |
str | List[LLMMessage]
|
prompt or list of LLMMessage objects |
required |
stream |
bool
|
whether to update the usage in the response object if the response is not cached. |
required |
chat |
bool
|
whether this is a chat model or a completion model |
True
|
print_response_stats |
bool
|
whether to print the response stats |
True
|
Source code in langroid/agent/base.py
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 |
|
ask_agent(agent, request, no_answer=NO_ANSWER, user_confirm=True)
¶
Send a request to another agent, possibly after confirming with the user.
This is not currently used, since we rely on the task loop and
RecipientTool
to address requests to other agents. It is generally best to
avoid using this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent |
Agent
|
agent to ask |
required |
request |
str
|
request to send |
required |
no_answer |
str
|
expected response when agent does not know the answer |
NO_ANSWER
|
user_confirm |
bool
|
whether to gate the request with a human confirmation |
True
|
Returns:
Name | Type | Description |
---|---|---|
str |
Optional[str]
|
response from agent |
Source code in langroid/agent/base.py
AgentConfig
¶
Bases: BaseSettings
General config settings for an LLM agent. This is nested, combining configs of various components.
ChatDocument(**data)
¶
Bases: Document
Represents a message in a conversation among agents. All responders of an agent have signature ChatDocument -> ChatDocument (modulo None, str, etc), and so does the Task.run() method.
Attributes:
Name | Type | Description |
---|---|---|
oai_tool_calls |
Optional[List[OpenAIToolCall]]
|
Tool-calls from an OpenAI-compatible API |
oai_tool_id2results |
Optional[OrderedDict[str, str]]
|
Results of tool-calls from OpenAI (dict is a map of tool_id -> result) |
oai_tool_choice |
ToolChoiceTypes | Dict[str, Dict[str, str] | str]
|
ToolChoiceTypes | Dict[str, str]: Param controlling how the LLM should choose tool-use in its response (auto, none, required, or a specific tool) |
function_call |
Optional[LLMFunctionCall]
|
Function-call from an OpenAI-compatible API (deprecated by OpenAI, in favor of tool-calls) |
tool_messages |
List[ToolMessage]
|
Langroid ToolMessages extracted from
- |
metadata |
ChatDocMetaData
|
Metadata for the message, e.g. sender, recipient. |
attachment |
None | ChatDocAttachment
|
Any additional data attached. |
Source code in langroid/agent/chat_document.py
delete_id(id)
staticmethod
¶
Remove ChatDocument with given id from ObjectRegistry, and all its descendants.
Source code in langroid/agent/chat_document.py
get_tool_names()
¶
Get names of attempted tool usages (JSON or non-JSON) in the content
of the message.
Returns:
List[str]: list of attempted tool names
(We say "attempted" since we ONLY look at the request
component of the
tool-call representation, and we're not fully parsing it into the
corresponding tool message class)
Source code in langroid/agent/chat_document.py
log_fields()
¶
Fields for logging in csv/tsv logger Returns: List[str]: list of fields
Source code in langroid/agent/chat_document.py
pop_tool_ids()
¶
from_LLMResponse(response, displayed=False)
staticmethod
¶
Convert LLMResponse to ChatDocument. Args: response (LLMResponse): LLMResponse to convert. displayed (bool): Whether this response was displayed to the user. Returns: ChatDocument: ChatDocument representation of this LLMResponse.
Source code in langroid/agent/chat_document.py
to_LLMMessage(message, oai_tools=None)
staticmethod
¶
Convert to list of LLMMessage, to incorporate into msg-history sent to LLM API. Usually there will be just a single LLMMessage, but when the ChatDocument contains results from multiple OpenAI tool-calls, we would have a sequence LLMMessages, one per tool-call result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
str | ChatDocument
|
Message to convert. |
required |
oai_tools |
Optional[List[OpenAIToolCall]]
|
Tool-calls currently awaiting response, from the ChatAgent's latest message. |
None
|
Returns: List[LLMMessage]: list of LLMMessages corresponding to this ChatDocument.
Source code in langroid/agent/chat_document.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
|
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:
Source code in langroid/agent/chat_agent.py
ToolMessage
¶
Bases: ABC
, BaseModel
Abstract Class for a class that defines the structure of a "Tool" message from an LLM. Depending on context, "tools" are also referred to as "plugins", or "function calls" (in the context of OpenAI LLMs). Essentially, they are a way for the LLM to express its intent to run a special function or method. Currently these "tools" are handled by methods of the agent.
Attributes:
Name | Type | Description |
---|---|---|
request |
str
|
name of agent method to map to. |
purpose |
str
|
purpose of agent method, expressed in general terms. (This is used when auto-generating the tool instruction to the LLM) |
examples()
classmethod
¶
Examples to use in few-shot demos with formatting instructions. Each example can be either: - just a ToolMessage instance, e.g. MyTool(param1=1, param2="hello"), or - a tuple (description, ToolMessage instance), where the description is a natural language "thought" that leads to the tool usage, e.g. ("I want to find the square of 5", SquareTool(num=5)) In some scenarios, including such a description can significantly enhance reliability of tool use. Returns:
Source code in langroid/agent/tool_message.py
usage_examples(random=False)
classmethod
¶
Instruction to the LLM showing examples of how to use the tool-message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
random |
bool
|
whether to pick a random example from the list of examples.
Set to |
False
|
Returns: str: examples of how to use the tool/function-call
Source code in langroid/agent/tool_message.py
get_value_of_type(target_type)
¶
Try to find a value of a desired type in the fields of the ToolMessage.
Source code in langroid/agent/tool_message.py
default_value(f)
classmethod
¶
Returns the default value of the given field, for the message-class Args: f (str): field name
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
default value of the field, or None if not set or if the field does not exist. |
Source code in langroid/agent/tool_message.py
format_instructions(tool=False)
classmethod
¶
Default Instructions to the LLM showing how to use the tool/function-call. Works for GPT4 but override this for weaker LLMs if needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool |
bool
|
instructions for Langroid-native tool use? (e.g. for non-OpenAI LLM) (or else it would be for OpenAI Function calls) |
False
|
Returns: str: instructions on how to use the message
Source code in langroid/agent/tool_message.py
group_format_instructions()
staticmethod
¶
Template for instructions for a group of tools. Works with GPT4 but override this for weaker LLMs if needed.
Source code in langroid/agent/tool_message.py
llm_function_schema(request=False, defaults=True)
classmethod
¶
Clean up the schema of the Pydantic class (which can recursively contain other Pydantic classes), to create a version compatible with OpenAI Function-call API.
Adapted from this excellent library: https://github.com/jxnl/instructor/blob/main/instructor/function_calls.py
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
bool
|
whether to include the "request" field in the schema. (we set this to True when using Langroid-native TOOLs as opposed to OpenAI Function calls) |
False
|
defaults |
bool
|
whether to include fields with default values in the schema, in the "properties" section. |
True
|
Returns:
Name | Type | Description |
---|---|---|
LLMFunctionSpec |
LLMFunctionSpec
|
the schema as an LLMFunctionSpec |
Source code in langroid/agent/tool_message.py
simple_schema()
classmethod
¶
Return a simplified schema for the message, with only the request and required fields. Returns: Dict[str, Any]: simplified schema
Source code in langroid/agent/tool_message.py
Task(agent=None, name='', llm_delegate=False, single_round=False, system_message='', user_message='', restart=True, default_human_response=None, interactive=True, only_user_quits_root=True, erase_substeps=False, allow_null_result=False, max_stalled_steps=5, default_return_type=None, done_if_no_response=[], done_if_response=[], config=TaskConfig(), **kwargs)
¶
A Task
wraps an Agent
object, and sets up the Agent
's goals and instructions.
A Task
maintains two key variables:
self.pending_message
, which is the message awaiting a response, andself.pending_sender
, which is the entity that sent the pending message.
The possible responders to self.pending_message
are the Agent
's own "native"
responders (agent_response
, llm_response
, and user_response
), and
the run()
methods of any sub-tasks. All responders have the same type-signature
(somewhat simplified):
The main top-level method in the Task
class is run()
, which repeatedly calls
step()
until done()
returns true. The step()
represents a "turn" in the
conversation: this method sequentially (in round-robin fashion) calls the responders
until it finds one that generates a valid response to the pending_message
(as determined by the valid()
method). Once a valid response is found,
step()
updates the pending_message
and pending_sender
variables,
and on the next iteration, step()
re-starts its search for a valid response
from the beginning of the list of responders (the exception being that the
human user always gets a chance to respond after each non-human valid response).
This process repeats until done()
returns true, at which point run()
returns
the value of result()
, which is the final result of the task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent |
Agent
|
agent associated with the task |
None
|
name |
str
|
name of the task |
''
|
llm_delegate |
bool
|
Whether to delegate "control" to LLM; conceptually,
the "controlling entity" is the one "seeking" responses to its queries,
and has a goal it is aiming to achieve, and decides when a task is done.
The "controlling entity" is either the LLM or the USER.
(Note within a Task there is just one
LLM, and all other entities are proxies of the "User" entity).
See also: |
False
|
single_round |
bool
|
If true, task runs until one message by "controller"
(i.e. LLM if |
False
|
system_message |
str
|
if not empty, overrides agent's system_message |
''
|
user_message |
str
|
if not empty, overrides agent's user_message |
''
|
restart |
bool
|
if true, resets the agent's message history at every run. |
True
|
default_human_response |
str | None
|
default response from user; useful for
testing, to avoid interactive input from user.
[Instead of this, setting |
None
|
default_return_type |
Optional[type]
|
if not None, extracts a value of this type from the result of self.run() |
None
|
interactive |
bool
|
if true, wait for human input after each non-human
response (prevents infinite loop of non-human responses).
Default is true. If false, then |
True
|
only_user_quits_root |
bool
|
if true, when interactive=True, only user can quit the root task (Ignored when interactive=False). |
True
|
erase_substeps |
bool
|
if true, when task completes, erase intermediate
conversation with subtasks from this agent's |
False
|
allow_null_result |
bool
|
If true, create dummy NO_ANSWER response when no valid response is found
in a step.
Optional, default is False.
Note: In non-interactive mode, when this is set to True,
you can have a situation where an LLM generates (non-tool) text,
and no other responders have valid responses, and a "Null result"
is inserted as a dummy response from the User entity, so the LLM
will now respond to this Null result, and this will continue
until the LLM emits a DONE signal (if instructed to do so),
otherwise langroid detects a potential infinite loop after
a certain number of such steps (= |
False
|
max_stalled_steps |
int
|
task considered done after this many consecutive steps with no progress. Default is 3. |
5
|
done_if_no_response |
List[Responder]
|
consider task done if NULL response from any of these responders. Default is empty list. |
[]
|
done_if_response |
List[Responder]
|
consider task done if NON-NULL response from any of these responders. Default is empty list. |
[]
|
Source code in langroid/agent/task.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
|
clone(i)
¶
Returns a copy of this task, with a new agent.
Source code in langroid/agent/task.py
kill_session(session_id='')
classmethod
¶
Kill the session with the given session_id.
kill()
¶
add_sub_task(task)
¶
Add a sub-task (or list of subtasks) that this task can delegate (or fail-over) to. Note that the sequence of sub-tasks is important, since these are tried in order, as the parent task searches for a valid response (unless a sub-task is explicitly addressed).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task |
Task | List[Task] | Tuple[Task, TaskConfig] | List[Tuple[Task, TaskConfig]]
|
A task, or list of tasks, or a tuple of task and task config, or a list of tuples of task and task config. These tasks are added as sub-tasks of the current task. The task configs (if any) dictate how the tasks are run when invoked as sub-tasks of other tasks. This allows users to specify behavior applicable only in the context of a particular task-subtask combination. |
required |
Source code in langroid/agent/task.py
init(msg=None)
¶
Initialize the task, with an optional message to start the conversation.
Initializes self.pending_message
and self.pending_sender
.
Args:
msg (str|ChatDocument): optional message to start the conversation.
Returns:
Type | Description |
---|---|
ChatDocument | None
|
the initialized |
ChatDocument | None
|
Currently not used in the code, but provided for convenience. |
Source code in langroid/agent/task.py
reset_all_sub_tasks()
¶
Recursively reset message history & state of own agent and those of all sub-tasks.
run(msg=None, turns=-1, caller=None, max_cost=0, max_tokens=0, session_id='', allow_restart=True, return_type=None)
¶
Synchronous version of run_async()
.
See run_async()
for details.
Source code in langroid/agent/task.py
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
|
run_async(msg=None, turns=-1, caller=None, max_cost=0, max_tokens=0, session_id='', allow_restart=True, return_type=None)
async
¶
Loop over step()
until task is considered done or turns
is reached.
Runs asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
Any
|
initial user-role message to process; if None,
the LLM will respond to its initial |
None
|
turns |
int
|
number of turns to run the task for; default is -1, which means run until task is done. |
-1
|
caller |
Task | None
|
the calling task, if any |
None
|
max_cost |
float
|
max cost allowed for the task (default 0 -> no limit) |
0
|
max_tokens |
int
|
max tokens allowed for the task (default 0 -> no limit) |
0
|
session_id |
str
|
session id for the task |
''
|
allow_restart |
bool
|
whether to allow restarting the task |
True
|
return_type |
Optional[Type[T]]
|
desired final result type |
None
|
Returns:
Type | Description |
---|---|
Optional[ChatDocument | T]
|
Optional[ChatDocument]: valid result of the task. |
Source code in langroid/agent/task.py
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 |
|
step(turns=-1)
¶
Synchronous version of step_async()
. See step_async()
for details.
TODO: Except for the self.response() calls, this fn should be identical to
step_async()
. Consider refactoring to avoid duplication.
Source code in langroid/agent/task.py
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 |
|
step_async(turns=-1)
async
¶
A single "turn" in the task conversation: The "allowed" responders in this
turn (which can be either the 3 "entities", or one of the sub-tasks) are
tried in sequence, until a valid response is obtained; a valid
response is one that contributes to the task, either by ending it,
or producing a response to be further acted on.
Update self.pending_message
to the latest valid response (or NO_ANSWER
if no valid response was obtained from any responder).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
turns |
int
|
number of turns to process. Typically used in testing where there is no human to "quit out" of current level, or in cases where we want to limit the number of turns of a delegated agent. |
-1
|
Returns (ChatDocument|None):
Updated self.pending_message
. Currently the return value is not used
by the task.run()
method, but we return this as a convenience for
other use-cases, e.g. where we want to run a task step by step in a
different context.
Source code in langroid/agent/task.py
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 |
|
response(e, turns=-1)
¶
Sync version of response_async()
. See response_async()
for details.
Source code in langroid/agent/task.py
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 |
|
response_async(e, turns=-1)
async
¶
Get response to self.pending_message
from a responder.
If response is valid (i.e. it ends the current turn of seeking
responses):
-then return the response as a ChatDocument object,
-otherwise return None.
Args:
e (Responder): responder to get response from.
turns (int): number of turns to run the task for.
Default is -1, which means run until task is done.
Returns:
Type | Description |
---|---|
Optional[ChatDocument]
|
Optional[ChatDocument]: response to |
Optional[ChatDocument]
|
valid, None otherwise |
Source code in langroid/agent/task.py
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 |
|
result(status=None)
¶
Get result of task. This is the default behavior. Derived classes can override this.
Note the result of a task is returned as if it is from the User entity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
status |
StatusCode
|
status of the task when it ended |
None
|
Returns: ChatDocument: result of task
Source code in langroid/agent/task.py
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 |
|
done(result=None, r=None)
¶
Check if task is done. This is the default behavior. Derived classes can override this. Args: result (ChatDocument|None): result from a responder r (Responder|None): responder that produced the result Not used here, but could be used by derived classes. Returns: bool: True if task is done, False otherwise StatusCode: status code indicating why task is done
Source code in langroid/agent/task.py
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 |
|
valid(result, r)
¶
Is the result from a Responder (i.e. an entity or sub-task) such that we can stop searching for responses in this step?
Source code in langroid/agent/task.py
log_message(resp, msg=None, mark=False)
¶
Log current pending message, and related state, for lineage/debugging purposes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resp |
Responder
|
Responder that generated the |
required |
msg |
ChatDocument
|
Message to log. Defaults to None. |
None
|
mark |
bool
|
Whether to mark the message as the final result of
a |
False
|
Source code in langroid/agent/task.py
set_color_log(enable=True)
¶
Flag to enable/disable color logging using rich.console.
In some contexts, such as Colab notebooks, we may want to disable color logging
using rich.console, since those logs show up in the cell output rather than
in the log file. Turning off this feature will still create logs, but without
the color formatting from rich.console
Args:
enable (bool): value of self.color_log
to set to,
which will enable/diable rich logging