tool_message
langroid/agent/tool_message.py
Structured messages to an agent, typically from an LLM, to be handled by an agent. The messages could represent, for example: - information or data given to the agent - request for information or data from the agent - request to run a method of the agent
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) |
instructions()
classmethod
¶
langroid_tools_instructions()
classmethod
¶
Instructions on tool usage when use_tools == True, i.e.
when using langroid built-in tools
(as opposed to OpenAI-like function calls/tools).
Source code in langroid/agent/tool_message.py
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). Ignored in the default implementation, but can be used in subclasses. |
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
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 | |
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
handler_name(message_class, default)
¶
Name of the agent method that handles this tool class.
A tool may declare _handler = "some_method" to route itself to an
agent method whose name differs from the tool's request value. This
is a class-level declaration and must be resolved from the class:
ToolMessage sets extra="allow", so an LLM-supplied "_handler"
key in tool JSON lands on the instance, and reading it from there
would let a tool call redirect dispatch to an arbitrary agent method
(issue #1106).
Pydantic v2 represents a class-level underscore attribute as a
ModelPrivateAttr, so unwrap that to get the declared name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_class
|
Type[ToolMessage]
|
The tool class to read the declaration from. |
required |
default
|
str
|
Name to use when the class declares no usable |
required |
Returns:
| Type | Description |
|---|---|
str
|
The declared handler-method name, else |
Source code in langroid/agent/tool_message.py
remove_if_exists(k, d)
¶
format_schema_for_strict(schema)
¶
Recursively set additionalProperties to False and replace oneOf and allOf with anyOf, required for OpenAI structured outputs. Additionally, remove all defaults and set all fields to required. This may not be equivalent to the original schema.