create_tool_calling_agent#
- langchain.agents.tool_calling_agent.base.create_tool_calling_agent(llm: ~langchain_core.language_models.base.BaseLanguageModel, tools: ~typing.Sequence[~langchain_core.tools.base.BaseTool], prompt: ~langchain_core.prompts.chat.ChatPromptTemplate, *, message_formatter: ~typing.Callable[[~typing.Sequence[~typing.Tuple[~langchain_core.agents.AgentAction, str]]], ~typing.List[~langchain_core.messages.base.BaseMessage]] = <function format_to_tool_messages>) Runnable [source]#
Create an agent that uses tools.
- Parameters:
llm (BaseLanguageModel) – LLM to use as the agent.
tools (Sequence[BaseTool]) – Tools this agent has access to.
prompt (ChatPromptTemplate) – The prompt to use. See Prompt section below for more on the expected input variables.
message_formatter (Callable[[Sequence[Tuple[AgentAction, str]]], List[BaseMessage]]) – Formatter function to convert (AgentAction, tool output) tuples into FunctionMessages.
- Returns:
A Runnable sequence representing an agent. It takes as input all the same input variables as the prompt passed in does. It returns as output either an AgentAction or AgentFinish.
- Return type:
Example
from langchain.agents import AgentExecutor, create_tool_calling_agent, tool from langchain_anthropic import ChatAnthropic from langchain_core.prompts import ChatPromptTemplate prompt = ChatPromptTemplate.from_messages( [ ("system", "You are a helpful assistant"), ("placeholder", "{chat_history}"), ("human", "{input}"), ("placeholder", "{agent_scratchpad}"), ] ) model = ChatAnthropic(model="claude-3-opus-20240229") @tool def magic_function(input: int) -> int: """Applies a magic function to an input.""" return input + 2 tools = [magic_function] agent = create_tool_calling_agent(model, tools, prompt) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) agent_executor.invoke({"input": "what is the value of magic_function(3)?"}) # Using with chat history from langchain_core.messages import AIMessage, HumanMessage agent_executor.invoke( { "input": "what's my name?", "chat_history": [ HumanMessage(content="hi! my name is bob"), AIMessage(content="Hello Bob! How can I assist you today?"), ], } )
Prompt:
- The agent prompt must have an agent_scratchpad key that is a
MessagesPlaceholder
. Intermediate agent actions and tool output messages will be passed in here.
Examples using create_tool_calling_agent