Bind Tools
Author: Jaemin Hong
Peer Review: Hye-yoon Jeong, JoonHo Kim
Proofread : Chaeyoon Kim
This is a part of LangChain Open Tutorial
Overview
bind_tools is a powerful function in LangChain for integrating custom tools with LLMs, enabling enriched AI workflows.
This tutorial will show you how to create, bind tools, parse and execute outputs, and integrate them into an AgentExecutor .
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
You can alternatively set API keys such as OPENAI_API_KEY in a .env file and load them.
[Note] This is not necessary if you've already set the required API keys in previous steps.
Creating Tools
Let's define tools for experimentation:
get_word_length: Returns the length of a word.add_function: Adds two numbers.bbc_news_crawl: Crawls BBC news and extracts main content.
[Note]
Use the
@tooldecorator for defining tools, and provide clear docstrings.
Binding Tools
Now, let's use the bind_tools function to associate the defined tools with a specific LLM.
Let's check the results!
The results are stored in tool_calls . Let's print tool_calls .
[Note]
nameindicates the name of the tool.argscontains the arguments that were passed to the tool.
Next, we will connect llm_with_tools with JsonOutputToolsParser to parse tool_calls and review the results.
[Note]
typeindicates the type of the tool.argscontains the arguments that were passed to the tool.
Execute the corresponding tool.
The execute_tool_calls function identifies the appropriate tool, passes the corresponding args , and then executes the tool.
Binding tools with Parser to Execute
This time, we will combine the entire process of binding tools, parsing the results, and executing the tool calls into a single step.
llm_with_tools: The LLM model with bound tools.JsonOutputToolsParser: The parser that processes the results of tool calls.execute_tool_calls: The function that executes the results of tool calls.
[Flow Summary]
Bind tools to the model.
Parse the results of tool calls.
Execute the results of tool calls.
Binding tools with Agent and AgentExecutor
AgentExecutorbind_tools provides schemas (tools) that can be used by the model.
AgentExecutor creates an execution loop for tasks such as invoking the LLM, routing to the appropriate tool, executing it, and re-invoking the model.
[Note]
Agent and
AgentExecutorwill be covered in detail in the next chapter .
Let's calculate the length of a word.
Let's calculate the sum of two numbers.
Let's add more than two numbers.
In this scenario, you can observe that the agent is capable of verifying its own intermediate results and repeating the process if necessary to arrive at the correct final answer.
Finally, let's try using a tool to summarize a news article.
Last updated