motleycrew.agents
Everything agent-related: wrappers, pre-made agents, output handlers etc.
Modules
- class motleycrew.agents.MotleyAgentAbstractParent
Bases:
Runnable,ABCAbstract class for describing agents.
Agents in motleycrew implement the Langchain Runnable interface.
- abstract property kv_store: dict
- abstract invoke(input: dict, config: RunnableConfig | None = None, **kwargs: Any) Any
Transform a single input into an output.
- Parameters:
input – The input to the Runnable.
config – A config to use when invoking the Runnable. The config supports standard keys like ‘tags’, ‘metadata’ for tracing purposes, ‘max_concurrency’ for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details.
- Returns:
The output of the Runnable.
- abstract as_tool(**kwargs) Any
Convert the agent to a tool to be used by other agents via delegation.
- Parameters:
kwargs – Additional arguments to pass to the tool. See
motleycrew.tools.tool.MotleyToolfor more details.
- class motleycrew.agents.MotleyAgentParent(prompt: str | ChatPromptTemplate | None = None, description: str | None = None, name: str | None = None, agent_factory: MotleyAgentFactory | None = None, tools: Sequence[MotleySupportedTool] | None = None, force_output_handler: bool = False, verbose: bool = False)
Bases:
MotleyAgentAbstractParent,ABCParent class for all motleycrew agents.
This class is abstract and should be subclassed by all agents in motleycrew.
In most cases, it’s better to use one of the specialized agent classes, such as LangchainMotleyAgent or LlamaIndexMotleyAgent, which provide various useful features, such as observability and output handling, out of the box.
If you need to create a custom agent, subclass this class and implement the invoke method.
- __init__(prompt: str | ChatPromptTemplate | None = None, description: str | None = None, name: str | None = None, agent_factory: MotleyAgentFactory | None = None, tools: Sequence[MotleySupportedTool] | None = None, force_output_handler: bool = False, verbose: bool = False)
- Parameters:
prompt –
Prompt for the agent.
If a string, it will be used as a prompt. If a string containing f-string-style placeholders, it will be used as a prompt template. If a ChatPromptTemplate, it will be used as a prompt template.
description –
Description of the agent. The description is only used for describing the agent’s purpose when giving it as a tool to other agents.
It is NOT included in the agent’s prompt.
name –
Name of the agent.
The name is used for identifying the agent when it is given as a tool to other agents, as well as for logging purposes.
It is NOT included in the agent’s prompt.
agent_factory –
Factory function to create the agent.
The factory function should accept a dictionary of tools and return the agent. It is usually called right before the agent is invoked for the first time.
See
motleycrew.common.types.MotleyAgentFactoryfor more details.tools – Tools to add to the agent.
force_output_handler – Whether to force the agent to return through an output handler. If True, at least one tool must have return_direct set to True.
verbose – Whether to log verbose output.
- property kv_store: dict
- compose_prompt(input: str | dict | List[BaseMessage] | None = None, as_messages: bool = False) str | list[BaseMessage]
Compose the agent’s prompt from the prompt prefix and the provided prompt.
- Parameters:
input – The input to the agent.
as_messages – Whether the prompt should be returned as a Langchain messages list instead of a single string.
- Returns:
The composed prompt.
- property agent
Getter for the inner agent that makes sure it’s already materialized. The inner agent should always be accessed via this property method.
- property is_materialized
Whether the agent is materialized.
- get_output_handlers()
Get all output handlers (tools with return_direct set to True).
- materialize()
Materialize the agent by creating the agent instance using the agent factory. This method should be called before invoking the agent for the first time.
- add_tools(tools: Sequence[MotleySupportedTool])
Add tools to the agent.
- Parameters:
tools – The tools to add to the agent.
- as_tool(**kwargs) MotleyTool
Convert the agent to a tool to be used by other agents via delegation.
- Parameters:
kwargs – Additional arguments to pass to the tool. See
motleycrew.tools.tool.MotleyToolfor more details.
- abstract invoke(input: dict, config: RunnableConfig | None = None, **kwargs: Any) Any
Transform a single input into an output.
- Parameters:
input – The input to the Runnable.
config – A config to use when invoking the Runnable. The config supports standard keys like ‘tags’, ‘metadata’ for tracing purposes, ‘max_concurrency’ for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details.
- Returns:
The output of the Runnable.
- class motleycrew.agents.LangchainMotleyAgent(description: str | None = None, name: str | None = None, prompt: str | ChatPromptTemplate | None = None, agent_factory: MotleyAgentFactory[AgentExecutor] | None = None, tools: Sequence[MotleySupportedTool] | None = None, force_output_handler: bool = False, chat_history: bool | GetSessionHistoryCallable = True, input_as_messages: bool = False, runnable_config: RunnableConfig | None = None, verbose: bool = False)
Bases:
MotleyAgentParent,LangchainOutputHandlingAgentMixinMotleyCrew wrapper for Langchain agents.
- __init__(description: str | None = None, name: str | None = None, prompt: str | ChatPromptTemplate | None = None, agent_factory: MotleyAgentFactory[AgentExecutor] | None = None, tools: Sequence[MotleySupportedTool] | None = None, force_output_handler: bool = False, chat_history: bool | GetSessionHistoryCallable = True, input_as_messages: bool = False, runnable_config: RunnableConfig | None = None, verbose: bool = False)
- Parameters:
description –
Description of the agent.
Unlike the prompt prefix, it is not included in the prompt. The description is only used for describing the agent’s purpose when giving it as a tool to other agents.
name –
Name of the agent. The name is used for identifying the agent when it is given as a tool to other agents, as well as for logging purposes.
It is not included in the agent’s prompt.
prompt –
Prompt to the agent.
If a string, it will be used as a prompt. If a string containing f-string-style placeholders, it will be used as a prompt template. If a ChatPromptTemplate, it will be used as a prompt template.
agent_factory –
Factory function to create the agent. The factory function should accept a dictionary of tools and return an AgentExecutor instance.
See
motleycrew.common.types.MotleyAgentFactoryfor more details.Alternatively, you can use the
from_agent()method to wrap an existing AgentExecutor.tools – Tools to add to the agent.
force_output_handler – Whether to force the agent to return through an output handler. If True, at least one tool must have return_direct set to True.
chat_history –
Whether to use chat history or not. If True, uses InMemoryChatMessageHistory. If a callable is passed, it is used to get the chat history by session_id.
See
langchain_core.runnables.history.RunnableWithMessageHistoryfor more details.input_as_messages – Whether the agent expects a list of messages as input instead of a single string.
runnable_config – Default Langchain config to use when invoking the agent. It can be used to add callbacks, metadata, etc.
verbose – Whether to log verbose output.
- materialize()
Materialize the agent and wrap it in RunnableWithMessageHistory if needed.
- invoke(input: str | dict | List[BaseMessage] | None = None, config: RunnableConfig | None = None, **kwargs: Any) Any
Transform a single input into an output.
- Parameters:
input – The input to the Runnable.
config – A config to use when invoking the Runnable. The config supports standard keys like ‘tags’, ‘metadata’ for tracing purposes, ‘max_concurrency’ for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details.
- Returns:
The output of the Runnable.
- async ainvoke(input: str | dict | List[BaseMessage] | None = None, config: RunnableConfig | None = None, **kwargs: Any) Any
Default implementation of ainvoke, calls invoke from a thread.
The default implementation allows usage of async code even if the Runnable did not implement a native async version of invoke.
Subclasses should override this method if they can run asynchronously.
- static from_agent(agent: AgentExecutor, description: str | None = None, prompt: str | ChatPromptTemplate | None = None, tools: Sequence[MotleySupportedTool] | None = None, runnable_config: RunnableConfig | None = None, verbose: bool = False) LangchainMotleyAgent
Create a LangchainMotleyAgent from a
langchain.agents.AgentExecutorinstance.Using this method, you can wrap an existing AgentExecutor without providing a factory function.
- Parameters:
agent – AgentExecutor instance to wrap.
prompt –
Prompt for the agent.
If a string, it will be used as a prompt. If a string containing f-string-style placeholders, it will be used as a prompt template. If a ChatPromptTemplate, it will be used as a prompt template.
description –
Description of the agent.
Unlike the prompt prefix, it is not included in the prompt. The description is only used for describing the agent’s purpose when giving it as a tool to other agents.
tools – Tools to add to the agent.
runnable_config – Default Langchain config to use when invoking the agent. It can be used to add callbacks, metadata, etc.
verbose – Whether to log verbose output.