tools package¶
Submodules¶
tools.argref module¶
- tools.argref.argref_by_name(fxn_requires_state: bool = False, prefix: str = '', return_direct: bool = False, type_check: bool = False, args_to_skip: set[str] | None = None)[source]¶
Decorator to allow args to be a string key into a refs dict instead of the full object.
This can prevent LLM-powered tool selections from getting confused by full objects, instead it enables them to work using named references. If a reference is not found, it will fallback on passing the original argument unless it is the first argument. If the first argument str is not found in the state object, it will raise an error.
- Parameters:
fxn_requires_state – Whether to pass the state object to the decorated function.
prefix – A prefix to add to the generated reference ID.
return_direct – Whether to return the result directly or update the state object.
type_check – Whether to type-check arguments with respect to the wrapped function’s type annotations.
args_to_skip – If provided, a set of argument names that should not be referenced by name.
- Example 1:
>>> @argref_by_name() >>> def my_func(foo: float): ...
- Example 2:
>>> def my_func(foo: float, bar: float) -> list[float]: ... return [foo, bar] >>> wrapped_fxn = argref_by_name()(my_func) >>> # Equivalent to my_func(state.refs["foo"]) >>> wrapped_fxn("foo", state=state)
Working with lists: - If you return a list, the decorator will create a new reference for each item in the list. - If you pass multiple args that are strings, the decorator will assume those are the keys. - If you need to pass a string, then use a keyword argument.
- Example 1:
>>> @argref_by_name() >>> def my_func(foo: float, bar: float) -> list[float]: ... return [foo, bar]
- Example 2:
>>> def my_func(foo: float, bar: float) -> list[float]: ... return [foo, bar] >>> wrapped_fxn = argref_by_name()(my_func) >>> # Returns a multiline string with the new references >>> # Equivalent to my_func(state.refs["a"], state.refs["b"]) >>> wrapped_fxn("a", "b", state=state)
tools.base module¶
- class tools.base.FunctionInfo(*, name: str, description: str, parameters: Parameters)[source]¶
Bases:
BaseModel
Function-level (not arg-level) information.
Matches LiteLLM’s desired “tools” schema, and resembles inspect.Signature.
- description: str¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'description': FieldInfo(annotation=str, required=True), 'name': FieldInfo(annotation=str, required=True), 'parameters': FieldInfo(annotation=Parameters, required=True)}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- name: str¶
- parameters: Parameters¶
- class tools.base.Parameters(*, type: Literal['object'] = 'object', properties: Annotated[dict[str, dict[str, Any]], PlainSerializer(func=dict_serialize_exclude_none, return_type=PydanticUndefined, when_used=always)], required: list[str], **extra_data: Any)[source]¶
Bases:
BaseModel
Matches LiteLLM’s desired “tools” schema.
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {'extra': 'allow'}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'properties': FieldInfo(annotation=dict[str, dict[str, Any]], required=True, metadata=[PlainSerializer(func=<function dict_serialize_exclude_none>, return_type=PydanticUndefined, when_used='always')]), 'required': FieldInfo(annotation=list[str], required=True), 'type': FieldInfo(annotation=Literal['object'], required=False, default='object')}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- properties: Annotated[dict[str, dict[str, Any]], PlainSerializer(func=dict_serialize_exclude_none, return_type=PydanticUndefined, when_used=always)]¶
- required: list[str]¶
- type: Literal['object']¶
- class tools.base.Tool(tool_fn: ~collections.abc.Callable[[...], ~typing.Any] | ~collections.abc.Callable[[...], ~collections.abc.Awaitable[~typing.Any]] = <function Tool.<lambda>>, *, type: ~typing.Literal['function'] = 'function', function: ~tools.base.FunctionInfo)[source]¶
Bases:
BaseModel
- classmethod from_function(function: Callable[[...], Any] | Callable[[...], Awaitable[Any]], docstring_style: DocstringStyle = DocstringStyle.AUTO, allow_empty_param_descriptions: bool = False, types_in_param_descriptions: bool = False, **formats) Tool [source]¶
Hydrate this class via inspection from a free function with a docstring.
- info: FunctionInfo¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {'populate_by_name': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'info': FieldInfo(annotation=FunctionInfo, required=True, alias='function', alias_priority=2, description="The serialization alias of 'function' is to match LiteLLM structure on serialization, and the validation alias enables deserialization."), 'type': FieldInfo(annotation=Literal['function'], required=False, default='function')}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- type: Literal['function']¶
- class tools.base.ToolCall(*, id: str, type: Literal['function'] = 'function', function: ToolCallFunction)[source]¶
Bases:
BaseModel
- classmethod from_tool(tool: Tool, *args, id: str | None = None, **kwargs) Self [source]¶
Create a ToolCall from a Tool and arguments.
The *args is packaged into the ToolCallFunction’s arguments dict with best effort. **kwargs is what is passed to toolcall because we have to use named parameters.
- function: ToolCallFunction¶
- id: str¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'function': FieldInfo(annotation=ToolCallFunction, required=True), 'id': FieldInfo(annotation=str, required=True), 'type': FieldInfo(annotation=Literal['function'], required=False, default='function')}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- type: Literal['function']¶
- class tools.base.ToolCallFunction(*, arguments: dict[str, Any], name: str)[source]¶
Bases:
BaseModel
- arguments: dict[str, Any]¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'arguments': FieldInfo(annotation=dict[str, Any], required=True), 'name': FieldInfo(annotation=str, required=True)}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- name: str¶
- class tools.base.ToolRequestMessage(*, role: Literal['assistant'] = 'assistant', content: str | None = None, content_is_json_str: bool = False, info: dict | None = None, function_call: None = None, tool_calls: list[ToolCall] = None)[source]¶
Bases:
Message
- content: str | None¶
- function_call: None¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'content': FieldInfo(annotation=Union[str, NoneType], required=False, default=None), 'content_is_json_str': FieldInfo(annotation=bool, required=False, default=False, description='Whether the content is JSON-serialized (e.g., for multiple modalities).', exclude=True, repr=False), 'function_call': FieldInfo(annotation=NoneType, required=False, default=None), 'info': FieldInfo(annotation=Union[dict, NoneType], required=False, default=None, description='Optional metadata about the message.', exclude=True, repr=False), 'role': FieldInfo(annotation=Literal['assistant'], required=False, default='assistant', description='Matching LiteLLM structure.'), 'tool_calls': FieldInfo(annotation=list[ToolCall], required=False, default_factory=list, description='List of ToolCalls to make concurrently and independently.')}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- role: Literal['assistant']¶
- class tools.base.ToolResponseMessage(*, role: Literal['tool'] = 'tool', content: str, content_is_json_str: bool = False, info: dict | None = None, name: str, tool_call_id: str)[source]¶
Bases:
Message
- content: str¶
- classmethod from_request(request: ToolRequestMessage, contents: Iterable[str]) list[Self] [source]¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'content': FieldInfo(annotation=str, required=True, description='Response message content, required to be a string by OpenAI/Anthropic.'), 'content_is_json_str': FieldInfo(annotation=bool, required=False, default=False, description='Whether the content is JSON-serialized (e.g., for multiple modalities).', exclude=True, repr=False), 'info': FieldInfo(annotation=Union[dict, NoneType], required=False, default=None, description='Optional metadata about the message.', exclude=True, repr=False), 'name': FieldInfo(annotation=str, required=True, description='Name of the tool that was called.'), 'role': FieldInfo(annotation=Literal['tool'], required=False, default='tool', description='Matching LiteLLM structure.'), 'tool_call_id': FieldInfo(annotation=str, required=True, description='Propagated from ToolCall.id, enabling matching response with ToolRequestMessage.')}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- name: str¶
- role: Literal['tool']¶
- tool_call_id: str¶
- tools.base.dict_serialize_exclude_none(value: dict[str, dict[str, Any]], info: FieldSerializationInfo) dict[str, dict[str, Any]] [source]¶
Work around Pydantic not applying exclude_none to dict serializations.
- tools.base.wraps_doc_only(wrapped)[source]¶
A decorator to copy only the docstring from the wrapped function.
You cannot use functools wraps directly because it will set the __wrapped__ attribute, which causes inspect.signature to inspect the wrapped function instead of the wrapper.
- Usage:
- def my_documented_function(foo):
‘’’This is a function that does something with foo.’’’ pass
@wraps_doc_only(my_documented_function) def my_other_function(foo, state):
pass
In this example, the second function can have different arguments, types, etc. and only the docstring will be copied over.
tools.server module¶
- async tools.server.make_tool_server(environment_factory: Callable, name: str = 'Aviary Tool Server', env_path: Path | None = None)[source]¶
Create a FastAPI server for the provided environment.
This function exposes one endpoint per tool and endpoints to create/view/delete environments. In contrast to other environment servers that expose an action endpoint, this one exposes all tools individually.
This is only for debugging tools and not intended as a strategy for working with environments. Most environments have side-effects from using tools that occur in the step function. This bypasses that and allows you to call tools directly.
- Parameters:
environment_factory – A callable that returns an environment instance.
name – The name of the server. Defaults to Aviary Tool Server.
env_path – The path to the directory to store environments
tools.utils module¶
- class tools.utils.EvalAnswerMode(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
StrEnum
- CONTAINS = 'contains'¶
- EXACT = 'exact'¶
- LLM = 'llm'¶
- LLM_SCORE = 'llm-score'¶
- class tools.utils.ToolSelector(model_name: str = 'gpt-4o', acompletion: Callable[..., Awaitable[ModelResponse]] | None = None, accum_messages: bool = False)[source]¶
Bases:
object
Simple entity to select a tool based on messages.
- TOOL_CHOICE_REQUIRED: ClassVar[str] = 'required'¶
- class tools.utils.ToolSelectorLedger(*, tools: list[Tool] = None, messages: list[ToolRequestMessage | ToolResponseMessage | Message] = None)[source]¶
Bases:
BaseModel
Simple ledger to record tools and messages.
- messages: list[ToolRequestMessage | ToolResponseMessage | Message]¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'messages': FieldInfo(annotation=list[Union[ToolRequestMessage, ToolResponseMessage, Message]], required=False, default_factory=list), 'tools': FieldInfo(annotation=list[Tool], required=False, default_factory=list)}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- async tools.utils.eval_answer(proposed: str, correct: str, question: str | None = None, eval_mode: EvalAnswerMode = EvalAnswerMode.CONTAINS, llm_eval_config: dict | None = None) float [source]¶
Evaluate a proposed answer against a correct answer.
Will return 0 or 1, except for llm-score which should be between 0 and 1
Module contents¶
- class tools.FunctionInfo(*, name: str, description: str, parameters: Parameters)[source]¶
Bases:
BaseModel
Function-level (not arg-level) information.
Matches LiteLLM’s desired “tools” schema, and resembles inspect.Signature.
- description: str¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'description': FieldInfo(annotation=str, required=True), 'name': FieldInfo(annotation=str, required=True), 'parameters': FieldInfo(annotation=Parameters, required=True)}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- name: str¶
- parameters: Parameters¶
- class tools.Parameters(*, type: Literal['object'] = 'object', properties: Annotated[dict[str, dict[str, Any]], PlainSerializer(func=dict_serialize_exclude_none, return_type=PydanticUndefined, when_used=always)], required: list[str], **extra_data: Any)[source]¶
Bases:
BaseModel
Matches LiteLLM’s desired “tools” schema.
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {'extra': 'allow'}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'properties': FieldInfo(annotation=dict[str, dict[str, Any]], required=True, metadata=[PlainSerializer(func=<function dict_serialize_exclude_none>, return_type=PydanticUndefined, when_used='always')]), 'required': FieldInfo(annotation=list[str], required=True), 'type': FieldInfo(annotation=Literal['object'], required=False, default='object')}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- properties: Annotated[dict[str, dict[str, Any]], PlainSerializer(func=dict_serialize_exclude_none, return_type=PydanticUndefined, when_used=always)]¶
- required: list[str]¶
- type: Literal['object']¶
- class tools.Tool(tool_fn: ~collections.abc.Callable[[...], ~typing.Any] | ~collections.abc.Callable[[...], ~collections.abc.Awaitable[~typing.Any]] = <function Tool.<lambda>>, *, type: ~typing.Literal['function'] = 'function', function: ~tools.base.FunctionInfo)[source]¶
Bases:
BaseModel
- classmethod from_function(function: Callable[[...], Any] | Callable[[...], Awaitable[Any]], docstring_style: DocstringStyle = DocstringStyle.AUTO, allow_empty_param_descriptions: bool = False, types_in_param_descriptions: bool = False, **formats) Tool [source]¶
Hydrate this class via inspection from a free function with a docstring.
- info: FunctionInfo¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {'populate_by_name': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'info': FieldInfo(annotation=FunctionInfo, required=True, alias='function', alias_priority=2, description="The serialization alias of 'function' is to match LiteLLM structure on serialization, and the validation alias enables deserialization."), 'type': FieldInfo(annotation=Literal['function'], required=False, default='function')}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- type: Literal['function']¶
- class tools.ToolCall(*, id: str, type: Literal['function'] = 'function', function: ToolCallFunction)[source]¶
Bases:
BaseModel
- classmethod from_tool(tool: Tool, *args, id: str | None = None, **kwargs) Self [source]¶
Create a ToolCall from a Tool and arguments.
The *args is packaged into the ToolCallFunction’s arguments dict with best effort. **kwargs is what is passed to toolcall because we have to use named parameters.
- function: ToolCallFunction¶
- id: str¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'function': FieldInfo(annotation=ToolCallFunction, required=True), 'id': FieldInfo(annotation=str, required=True), 'type': FieldInfo(annotation=Literal['function'], required=False, default='function')}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- type: Literal['function']¶
- class tools.ToolCallFunction(*, arguments: dict[str, Any], name: str)[source]¶
Bases:
BaseModel
- arguments: dict[str, Any]¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'arguments': FieldInfo(annotation=dict[str, Any], required=True), 'name': FieldInfo(annotation=str, required=True)}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- name: str¶
- class tools.ToolRequestMessage(*, role: Literal['assistant'] = 'assistant', content: str | None = None, content_is_json_str: bool = False, info: dict | None = None, function_call: None = None, tool_calls: list[ToolCall] = None)[source]¶
Bases:
Message
- content: str | None¶
- content_is_json_str: bool¶
- function_call: None¶
- info: dict | None¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'content': FieldInfo(annotation=Union[str, NoneType], required=False, default=None), 'content_is_json_str': FieldInfo(annotation=bool, required=False, default=False, description='Whether the content is JSON-serialized (e.g., for multiple modalities).', exclude=True, repr=False), 'function_call': FieldInfo(annotation=NoneType, required=False, default=None), 'info': FieldInfo(annotation=Union[dict, NoneType], required=False, default=None, description='Optional metadata about the message.', exclude=True, repr=False), 'role': FieldInfo(annotation=Literal['assistant'], required=False, default='assistant', description='Matching LiteLLM structure.'), 'tool_calls': FieldInfo(annotation=list[ToolCall], required=False, default_factory=list, description='List of ToolCalls to make concurrently and independently.')}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- role: Literal['assistant']¶
- class tools.ToolResponseMessage(*, role: Literal['tool'] = 'tool', content: str, content_is_json_str: bool = False, info: dict | None = None, name: str, tool_call_id: str)[source]¶
Bases:
Message
- content: str¶
- content_is_json_str: bool¶
- classmethod from_request(request: ToolRequestMessage, contents: Iterable[str]) list[Self] [source]¶
- info: dict | None¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'content': FieldInfo(annotation=str, required=True, description='Response message content, required to be a string by OpenAI/Anthropic.'), 'content_is_json_str': FieldInfo(annotation=bool, required=False, default=False, description='Whether the content is JSON-serialized (e.g., for multiple modalities).', exclude=True, repr=False), 'info': FieldInfo(annotation=Union[dict, NoneType], required=False, default=None, description='Optional metadata about the message.', exclude=True, repr=False), 'name': FieldInfo(annotation=str, required=True, description='Name of the tool that was called.'), 'role': FieldInfo(annotation=Literal['tool'], required=False, default='tool', description='Matching LiteLLM structure.'), 'tool_call_id': FieldInfo(annotation=str, required=True, description='Propagated from ToolCall.id, enabling matching response with ToolRequestMessage.')}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- name: str¶
- role: Literal['tool']¶
- tool_call_id: str¶
- class tools.ToolSelector(model_name: str = 'gpt-4o', acompletion: Callable[..., Awaitable[ModelResponse]] | None = None, accum_messages: bool = False)[source]¶
Bases:
object
Simple entity to select a tool based on messages.
- TOOL_CHOICE_REQUIRED: ClassVar[str] = 'required'¶
- class tools.ToolSelectorLedger(*, tools: list[Tool] = None, messages: list[ToolRequestMessage | ToolResponseMessage | Message] = None)[source]¶
Bases:
BaseModel
Simple ledger to record tools and messages.
- messages: list[ToolRequestMessage | ToolResponseMessage | Message]¶
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}¶
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'messages': FieldInfo(annotation=list[Union[ToolRequestMessage, ToolResponseMessage, Message]], required=False, default_factory=list), 'tools': FieldInfo(annotation=list[Tool], required=False, default_factory=list)}¶
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- tools.argref_by_name(fxn_requires_state: bool = False, prefix: str = '', return_direct: bool = False, type_check: bool = False, args_to_skip: set[str] | None = None)[source]¶
Decorator to allow args to be a string key into a refs dict instead of the full object.
This can prevent LLM-powered tool selections from getting confused by full objects, instead it enables them to work using named references. If a reference is not found, it will fallback on passing the original argument unless it is the first argument. If the first argument str is not found in the state object, it will raise an error.
- Parameters:
fxn_requires_state – Whether to pass the state object to the decorated function.
prefix – A prefix to add to the generated reference ID.
return_direct – Whether to return the result directly or update the state object.
type_check – Whether to type-check arguments with respect to the wrapped function’s type annotations.
args_to_skip – If provided, a set of argument names that should not be referenced by name.
- Example 1:
>>> @argref_by_name() >>> def my_func(foo: float): ...
- Example 2:
>>> def my_func(foo: float, bar: float) -> list[float]: ... return [foo, bar] >>> wrapped_fxn = argref_by_name()(my_func) >>> # Equivalent to my_func(state.refs["foo"]) >>> wrapped_fxn("foo", state=state)
Working with lists: - If you return a list, the decorator will create a new reference for each item in the list. - If you pass multiple args that are strings, the decorator will assume those are the keys. - If you need to pass a string, then use a keyword argument.
- Example 1:
>>> @argref_by_name() >>> def my_func(foo: float, bar: float) -> list[float]: ... return [foo, bar]
- Example 2:
>>> def my_func(foo: float, bar: float) -> list[float]: ... return [foo, bar] >>> wrapped_fxn = argref_by_name()(my_func) >>> # Returns a multiline string with the new references >>> # Equivalent to my_func(state.refs["a"], state.refs["b"]) >>> wrapped_fxn("a", "b", state=state)
- async tools.eval_answer(proposed: str, correct: str, question: str | None = None, eval_mode: EvalAnswerMode = EvalAnswerMode.CONTAINS, llm_eval_config: dict | None = None) float [source]¶
Evaluate a proposed answer against a correct answer.
Will return 0 or 1, except for llm-score which should be between 0 and 1
- tools.wraps_doc_only(wrapped)[source]¶
A decorator to copy only the docstring from the wrapped function.
You cannot use functools wraps directly because it will set the __wrapped__ attribute, which causes inspect.signature to inspect the wrapped function instead of the wrapper.
- Usage:
- def my_documented_function(foo):
‘’’This is a function that does something with foo.’’’ pass
@wraps_doc_only(my_documented_function) def my_other_function(foo, state):
pass
In this example, the second function can have different arguments, types, etc. and only the docstring will be copied over.