Skip to main content

馃 Create a custom agent without using abstractions

The create_tool_calling_agent constructor in LangChain makes it easy to create tool-calling agents by abstracting away the individual steps involved in creating the agent.

As a challenge, try creating a tool-calling agent without using the create_tool_calling_agent constructor.

To do this, fill in any <CODE_BLOCK_N> placeholders and run the cells under the 馃 Create a custom agent without using abstractions section in the notebook.

The answers for code blocks in this section are as follows:

CODE_BLOCK_21

Answer
prompt = ChatPromptTemplate.from_messages(
[
("system", system_message),
("human", "{input}"),
MessagesPlaceholder("agent_scratchpad"),
]
)

llm_with_tools = llm.bind_tools(tools)

agent = (
RunnablePassthrough.assign(
agent_scratchpad=lambda x: format_to_tool_messages(x["intermediate_steps"])
)
| prompt
| llm_with_tools
| ToolsAgentOutputParser()
)

agent_executor = AgentExecutor(
agent=agent, tools=tools, verbose=True, handle_parsing_errors=True
)