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.
# Load API keys from .env file
from dotenv import load_dotenv
load_dotenv(override=True)
True
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 @tool decorator for defining tools, and provide clear docstrings.
import requests
from bs4 import BeautifulSoup
from langchain_core.tools import tool
# Define the tools
@tool
def get_word_length(word: str) -> int:
"""Return the length of the given text"""
return len(word)
@tool
def add_function(a: float, b: float) -> float:
"""Add two numbers together"""
return a + b
@tool
def bbc_news_crawl(news_url: str) -> str:
"""Crawl a news article from BBC"""
response = requests.get(news_url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, "html.parser")
# Extract the desired information from the article
article = soup.find("article")
if article:
title = article.find("h1").get_text() # Extract the title
content_list = [
tag.get_text()
for tag in article.find_all(["h2", "p"])
if (tag.name == "h2" and "sc-518485e5-0" in tag.get("class", []))
or (tag.name == "p" and "sc-eb7bd5f6-0" in tag.get("class", []))
] # Extract the content
content = "\n\n".join(content_list)
else:
print(f"HTTP request failed. Response code: {response.status_code}")
return f"{title}\n\n----------\n\n{content}"
tools = [get_word_length, add_function, bbc_news_crawl]
Binding Tools
Now, let's use the bind_tools function to associate the defined tools with a specific LLM.
from langchain_openai import ChatOpenAI
# Create a model
llm = ChatOpenAI(model="gpt-4o", temperature=0)
# Tool binding
llm_with_tools = llm.bind_tools(tools)
Let's check the results!
The results are stored in tool_calls . Let's print tool_calls .
[Note]
name indicates the name of the tool.
args contains the arguments that were passed to the tool.
# Execution result
llm_with_tools.invoke(
"What is the length of the given text 'LangChain OpenTutorial'?"
).tool_calls
Next, we will connect llm_with_tools with JsonOutputToolsParser to parse tool_calls and review the results.
[Note]
type indicates the type of the tool.
args contains the arguments that were passed to the tool.
from langchain_core.output_parsers.openai_tools import JsonOutputToolsParser
# Tool Binding + Tool Parser
chain = llm_with_tools | JsonOutputToolsParser(tools=tools)
# Execution Result
tool_call_results = chain.invoke(
"What is the length of the given text 'LangChain OpenTutorial'?"
)
print(tool_call_results)
The execute_tool_calls function identifies the appropriate tool, passes the corresponding args , and then executes the tool.
def execute_tool_calls(tool_call_results):
"""
Function to execute the tool call results.
:param tool_call_results: List of the tool call results
:param tools: List of available tools
"""
# Iterate over the list of the tool call results
for tool_call_result in tool_call_results:
# Tool name (function name)
tool_name = tool_call_result["type"]
# Tool arguments
tool_args = tool_call_result["args"]
# Find the tool that matches the name and execute it
# Use the next() function to find the first matching tool
matching_tool = next((tool for tool in tools if tool.name == tool_name), None)
if matching_tool:
# Execute the tool
result = matching_tool.invoke(tool_args)
print(
f"[Executed Tool] {tool_name} [Args] {tool_args}\n[Execution Result] {result}"
)
else:
print(f"Warning: Unable to find the tool corresponding to {tool_name}.")
# Execute the tool calls
execute_tool_calls(tool_call_results)
# Execution Result 3
chain.invoke("Crawl the news article: https://www.bbc.com/news/articles/cew52g8p2lko")
[Executed Tool] bbc_news_crawl [Args] {'news_url': 'https://www.bbc.com/news/articles/cew52g8p2lko'}
[Execution Result] New AI hub 'to create 1,000 jobs' on Merseyside
----------
A new Artificial Intelligence (AI) hub planned for Merseyside is set to create 1,000 jobs over the next three years, the government said.
Prime Minister Sir Keir Starmer said he wanted to make the UK one of the world's AI "super powers" as a way of boosting economic growth and improving public services.
Global IT company Kyndryl announced it was going to create the new tech hub in the Liverpool City Region.
Metro Mayor Steve Rotheram welcomed the investment, saying it would be "hugely beneficial" to the area.
'International investment'
In a speech setting out the government's AI ambitions, Starmer spoke of its "vast potential" for rejuvenating public services.
The government said its AI Opportunities Action Plan was backed by leading tech firms, some of which have committed £14bn towards various projects including growth zones, creating 13,250 jobs.
Mr Rotheram told BBC Radio Merseyside: "I went over last year to speak to [Kyndryl] face-to-face in New York.
"To have that come to fruition so quickly is hugely beneficial to the workforce in the Liverpool City Region."
He said attracting the world's largest IT infrastructure services provider was "testament to what we can achieve when local ambition is matched by national support to help attract international investment".
The Labour mayor said the Liverpool City Region was "leading the way in the UK's AI revolution".
He added: "As a region with a proud history of innovation we're ready to seize the opportunities that AI and digital technology can bring; not just to boost our economy but to improve lives, develop skills, tackle inequality, and ensure no-one is left behind."
The BBC has asked the Department for Science, Innovation and Technology for more details about Merseyside's AI hub plans.
Listen to the best of BBC Radio Merseyside on Sounds and follow BBC Merseyside on Facebook, X, and Instagram and watch BBC North West Tonight on BBC iPlayer.
Binding tools with Agent and AgentExecutor
bind_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 AgentExecutor will be covered in detail in the next chapter .
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI
# Create an Agent prompt
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are very powerful assistant, but don't know current events",
),
("user", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
]
)
# Create a model
llm = ChatOpenAI(model="gpt-4o", temperature=0)
from langchain.agents import AgentExecutor, create_tool_calling_agent
# Use the tools defined previously
tools = [get_word_length, add_function, bbc_news_crawl]
# Create an Agent
agent = create_tool_calling_agent(llm, tools, prompt)
# Create an AgentExecutor
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
handle_parsing_errors=True,
)
Let's calculate the length of a word.
# Execute the Agent
result = agent_executor.invoke(
{"input": "What is the length of the given text 'LangChain OpenTutorial'?"}
)
# Execution Result
print(result["output"])
> Entering new AgentExecutor chain...
Invoking: `get_word_length` with `{'word': 'LangChain OpenTutorial'}`
22The length of the text "LangChain OpenTutorial" is 22 characters.
> Finished chain.
The length of the text "LangChain OpenTutorial" is 22 characters.
Let's calculate the sum of two numbers.
# Execute the Agent
result = agent_executor.invoke({"input": "Calculate the result of 114.5 + 121.2"})
# Execution Result
print(result["output"])
print("\n==========\n")
print(114.5 + 121.2)
> Entering new AgentExecutor chain...
Invoking: `add_function` with `{'a': 114.5, 'b': 121.2}`
235.7The result of 114.5 + 121.2 is 235.7.
> Finished chain.
The result of 114.5 + 121.2 is 235.7.
==========
235.7
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.
# Execute the Agent
result = agent_executor.invoke(
{"input": "Calculate the result of 114.5 + 121.2 + 34.2 + 110.1"}
)
# Execution Result
print(result["output"])
print("\n==========\n")
print(114.5 + 121.2 + 34.2 + 110.1)
> Entering new AgentExecutor chain...
Invoking: `add_function` with `{'a': 114.5, 'b': 121.2}`
235.7
Invoking: `add_function` with `{'a': 235.7, 'b': 34.2}`
269.9
Invoking: `add_function` with `{'a': 34.2, 'b': 110.1}`
144.3
Invoking: `add_function` with `{'a': 269.9, 'b': 110.1}`
380.0The result of adding 114.5, 121.2, 34.2, and 110.1 is 380.0.
> Finished chain.
The result of adding 114.5, 121.2, 34.2, and 110.1 is 380.0.
==========
380.0
Finally, let's try using a tool to summarize a news article.
# Execute the Agent
result = agent_executor.invoke(
{
"input": "Summarize the news article: https://www.bbc.com/news/articles/cew52g8p2lko"
}
)
# Execution Result
print(result["output"])
> Entering new AgentExecutor chain...
Invoking: `bbc_news_crawl` with `{'news_url': 'https://www.bbc.com/news/articles/cew52g8p2lko'}`
New AI hub 'to create 1,000 jobs' on Merseyside
----------
A new Artificial Intelligence (AI) hub planned for Merseyside is set to create 1,000 jobs over the next three years, the government said.
Prime Minister Sir Keir Starmer said he wanted to make the UK one of the world's AI "super powers" as a way of boosting economic growth and improving public services.
Global IT company Kyndryl announced it was going to create the new tech hub in the Liverpool City Region.
Metro Mayor Steve Rotheram welcomed the investment, saying it would be "hugely beneficial" to the area.
'International investment'
In a speech setting out the government's AI ambitions, Starmer spoke of its "vast potential" for rejuvenating public services.
The government said its AI Opportunities Action Plan was backed by leading tech firms, some of which have committed £14bn towards various projects including growth zones, creating 13,250 jobs.
Mr Rotheram told BBC Radio Merseyside: "I went over last year to speak to [Kyndryl] face-to-face in New York.
"To have that come to fruition so quickly is hugely beneficial to the workforce in the Liverpool City Region."
He said attracting the world's largest IT infrastructure services provider was "testament to what we can achieve when local ambition is matched by national support to help attract international investment".
The Labour mayor said the Liverpool City Region was "leading the way in the UK's AI revolution".
He added: "As a region with a proud history of innovation we're ready to seize the opportunities that AI and digital technology can bring; not just to boost our economy but to improve lives, develop skills, tackle inequality, and ensure no-one is left behind."
The BBC has asked the Department for Science, Innovation and Technology for more details about Merseyside's AI hub plans.
Listen to the best of BBC Radio Merseyside on Sounds and follow BBC Merseyside on Facebook, X, and Instagram and watch BBC North West Tonight on BBC iPlayer.A new Artificial Intelligence (AI) hub is planned for Merseyside, expected to create 1,000 jobs over the next three years. Prime Minister Sir Keir Starmer aims to position the UK as a global AI "superpower" to boost economic growth and improve public services. The global IT company Kyndryl will establish the tech hub in the Liverpool City Region. Metro Mayor Steve Rotheram praised the investment, highlighting its benefits for the area. The government's AI Opportunities Action Plan, supported by leading tech firms, has secured £14 billion for various projects, including growth zones, creating 13,250 jobs. Rotheram emphasized the region's leadership in the UK's AI revolution and its readiness to leverage AI and digital technology for economic and social benefits.
> Finished chain.
A new Artificial Intelligence (AI) hub is planned for Merseyside, expected to create 1,000 jobs over the next three years. Prime Minister Sir Keir Starmer aims to position the UK as a global AI "superpower" to boost economic growth and improve public services. The global IT company Kyndryl will establish the tech hub in the Liverpool City Region. Metro Mayor Steve Rotheram praised the investment, highlighting its benefits for the area. The government's AI Opportunities Action Plan, supported by leading tech firms, has secured £14 billion for various projects, including growth zones, creating 13,250 jobs. Rotheram emphasized the region's leadership in the UK's AI revolution and its readiness to leverage AI and digital technology for economic and social benefits.
Set up the environment. You may refer to for more details.