02. Tool Binding (Binding Tools)
In order for the LLM model to be able to call the tool, it is necessary to pass the tool schema to the model when making a chat request.
LangChain Chat Model supporting tool calling function .bind_tools() Implement a method to receive a list of LangChain tool objects, Pydantic classes or JSON schemas and bind to the chat model in the expected format by provider.
Subsequent calls to the bound Chat Model include a tool schema in every call to the model API.
Copy
# Configuration file for managing API KEY as environment variable
from dotenv import load_dotenv
# Load API KEY information
load_dotenv()Copy
True Copy
# LangSmith Set up tracking. https://smith.langchain.com
# !pip install -qU langchain-teddynote
from langchain_teddynote import logging
# Enter a project name.
logging.langsmith("CH15-Bind-Tools")Copy
Tool definition to bind to LLM
Defines the tool for experimentation.
get_word_length: A function that returns the length of a wordadd_function: A function that adds two numbersnaver_news_crawl: A function that crawls a Naver news article to return the text content
Reference
When defining tools
@toolDefine tools using decorators.docstring is recommended to write in English whenever possible.
Copy
Binding tools to LLM with bind_tools()
llm model bind_tools() Use to bind the tool.
Copy
Check the results.
result tool_calls Stored in. therefore, .tool_calls You can check the tool call result by checking.
Reference
nameMeans the name of the silver tool.argsmeans the factor passed to the tool.
Copy
Copy
Next llm_with_tools Wow JsonOutputToolsParser By connecting tool_calls Parsing to confirm the result.
Copy
The results of the run are as follows.
Reference
type: The name of the toolargs: Factors passed to the tool
Copy
Copy
Find and run tools that match tool names.
Copy
Copy
execute_tool_calls The function finds the tool and passes args to run the tool.
In other words, type Meaning the name of the silver tool args means the factor passed to the tool.
Copy
Copy
bind_tools + Parser + Execution
This time, a series of processes are run at once.
llm_with_tools: Model with tools boundJsonOutputToolsParser: Parser parsing tool call resultsexecute_tool_calls: Function running tool call result
Flow cleanup One. Binding tools to the model 2. Parsing tool call results 3. Run tool call results
Copy
Copy
Copy
Copy
Copy
Copy
Copy
replaced by bind_tools > Agent & AgentExecutor
bind_tools() Provides a schema (tool) that can be used for models.
AgentExecutor It actually creates an execution loop for llm calls, routing, running, model re-calling, etc. with the right tools.
Reference
AgentWowAgentExecutorThe following chapters are covered in detail.
Copy
Copy
Copy
Copy
Copy
Copy
It doesn't end with a single run, but the model goes through the process of checking their results and calling themselves again.
Copy
Copy
This time, we will crawl the news results and make a request to summarize.
Copy
Copy
Last updated