Introduction
Tools are modular, reusable components meticulously designed to accomplish specific tasks or provide answers to distinct types of questions. By integrating these tools seamlessly into the system, users can effortlessly tap into a diverse range of functionalities and information sources to tackle challenges and generate meaningful responses. In this lesson, we will explore the various Tools in LangChain, uncovering their unique capabilities.
A few notable examples of tools in LangChain, without getting into technical details, are:
- Google Search: This tool uses the Google Search API to fetch relevant information from the web, which can be used to answer queries related to current events, facts, or any topic where a quick search can provide accurate results.
- Requests: This tool employs the popular Python library "requests" to interact with web services, access APIs, or obtain data from different online sources. It can be particularly useful for gathering structured data or specific information from a web service.
- Python REPL: The Python REPL (Read-Eval-Print Loop) tool allows users to execute Python code on-the-fly to perform calculations, manipulate data, or test algorithms. It serves as an interactive programming environment within the LangChain system.
- Wikipedia: The Wikipedia tool leverages the Wikipedia API to search and retrieve relevant articles, summaries, or specific information from the vast repository of knowledge on the Wikipedia platform.
- Wolfram Alpha: With this tool, users can tap into the powerful computational knowledge engine of Wolfram Alpha to answer complex questions, perform advanced calculations, or generate visual representations of data.
LangChain Agents and Toolkits
In LangChain, an Agent is a bot that acts using natural language instructions and can use tools to answer its queries. Based on user input, it is also used to determine which actions to take and in what order. An action can either be using a tool (such as a search engine or a calculator) and processing its output or returning a response to the user. Agents are powerful when used correctly, as they can dynamically call chains based on user input.
An agent has access to a suite of tools and can decide which of these tools to call, depending on the user input. Tools are functions that perform specific duties. To create an agent in LangChain, you can use the initialize_agent
function along with the load_tools
function to prepare the tools the agent can use.
For example, you can create a simple agent using the following code. It can use the SerpApi service to fetch Google search results or the Python requests wrapper when required. Remember to install the required packages with the following command: pip install langchain==0.1.4 deeplake==3.9.27 openai==1.10.0 tiktoken google-search-results
.
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain import OpenAI
llm = OpenAI(model_name="gpt-3.5-turbo-instruct", temperature=0)
tools = load_tools(['serpapi', 'requests_all'], llm=llm, serpapi_api_key="<YOUR_SERPAPI_API_KEY>")
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True, verbose=True)
You can use the run
method with a natural language input to run the agent.
text = agent.run("tell me what is midjourney?")
The agent will then use the tool to look for an answer to the query. We’ll see some output examples later in this lesson.
Google-Search
LLMs inherently possess knowledge only up to the point at which they were trained, leaving them unaware of any information beyond that timeframe. Integrating search engines as tools within the LangChain system presents a significant advantage. The LangChain library provides a Google Search API wrapper that you can use within your project. You can use this wrapper as a standalone utility or as a tool within an agent.
First, make sure you have an API Key and Custom Search Engine ID for the Google Search API. If you don’t already have a Custom Search Engine ID, the following tutorial is a helpful guide for generating one.
Also, getting Google Search API is straightforward. If you have Google Cloud Platform access, you’d just go to the credentials page and click Create credentials > API key.
Set them as environment variables like the following. Then, you can use GoogleSearchAPIWrapper
to receive k
top search results given a query.
import os
os.environ["OPENAI_API_KEY"] = "<YOUR-OPENAI-API-KEY>"
os.environ["GOOGLE_API_KEY"] = "<YOUR-GOOGLE-API-KEY>"
os.environ["GOOGLE_CSE_ID"] = "<YOUR-GOOGLE-CSE-ID>"
# As a standalone utility:
from langchain. utilities import GoogleSearchAPIWrapper
search = GoogleSearchAPIWrapper()
search.results("What is the capital of Spain?", 3)
[{'title': 'Madrid | History, Population, Climate, & Facts | Britannica',
'link': 'https://www.britannica.com/place/Madrid',
'snippet': "May 23, 2023 ... Madrid, city, capital of Spain and of Madrid provincia (province). Spain's arts and financial center, the city proper, and province form a\xa0..."},
{'title': 'Madrid - Eurocities',
'link': 'https://eurocities.eu/cities/madrid/',
'snippet': 'As the Spanish capital, Madrid is home to embassies and international organizations, major companies and financial institutions. It ranks first in Spain for the\xa0...'},
{'title': 'Madrid - Wikipedia',
'link': 'https://en.wikipedia.org/wiki/Madrid',
'snippet': 'Madrid is the capital and most populous city of Spain. The city has almost 3.6 million inhabitants and a metropolitan area population of approximately 6.7\xa0...'}]
In the LangChain library, using the available tools requires some necessary steps to be taken. First, you need to initialize an agent, which is the central manager for effectively using these tools. Then, we need to define the language model that we want the agent to use.
from langchain.llms import OpenAI
llm = OpenAI(model_name="gpt-3.5-turbo-instruct", temperature=0)
Now, we can initialize an agent and load the google-search
tool for it to use. The agent will load the search results and provide them to the llm
to answer our question. The ZERO_SHOT_REACT_DESCRIPTION
type gives the freedom to choose any of the defined tools to provide context for the model based on their description. (You can use different agent types, read more)
from langchain.agents import initialize_agent, load_tools, AgentType
tools = load_tools(["google-search"])
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
)
print( agent("What is the national drink in Spain?") )
> Entering new AgentExecutor chain...
I should research this online.
Action: Google Search
Action Input: National drink in Spain
Observation: Sangría is Spain's national drink, so, of course, it belongs on this list! Spain is famous for its wine, which is the base of this drink. Although there is nothing officially considered the national alcoholic drink of Spain, many people would say is the Spanish sherry wine (also known as vino de ... Apr 20, 2021 ... Spanish horchata is a very popular sweet, creamy drink made by mixing the milky juice of tiger nuts with white sugar. The drink must be ... "Very Old Rare Sherry", Pedro Ximenez by Garvey. Jerez de la Frontera (Andalusia, Spain), aged 30 years. Sherry is a national liquor of Spain. Nov 27, 2012 ... The fervor with which Spaniards drink gin & tonics is, to say the ... gin tonic is the national drink (and that ampersands are expendable). Jul 25, 2022 ... Orujo is an ancient Spanish pomace brandy that is enjoyed throughout the country, but it is usually associated with northern Spain—namely ... Even out of the country, people recognize the sherry wine as a very typical drink from Spain. Of course, when talking about the Spanish national drink, we could ... Horchata. A cooling creamy drink for summer, horchata (or orxata as it is also spelled in Valencian and Catalan) can be found in specialised ... Feb 22, 2023 ... Undoubtedly, sangria is one of the typical drinks of Spain. For some, it is even considered the national drink of Spain. Jul 10, 2022 ... But it couldn´t be another way, as wine is the national drink of Spain. You could probably not know that Spain is the second largest wine ...
Thought: I now know the final answer.
Final Answer: The national drink of Spain is Sherry wine.
> Finished chain.
{'input': 'What is the national drink in Spain?',
'output': 'The national drink of Spain is Sherry wine.'}
Requests
The internet is an extensive source of information that Large Language Models cannot access directly. LangChain offers a convenient wrapper built around the Python Requests module to facilitate seamless interaction between LLMs and this wealth of information. This wrapper accepts a URL as an input and efficiently retrieves data from the specified URL, allowing LLMs to obtain and process web-based content effortlessly.
In this example, we’ll set up a fake RESTful backend using mockapi.io. To do it, follow these steps:
- Go to mockapi.io and sign up for a free account.
- After signing up, log in to your account.
- Click on "New Project" (the "+" icon) and give your project a name. You don't need to fill in any optional fields.
- Once the project is created, click on it to view your unique API endpoint.
- Click on "New Resource" to create a new resource for your API. For example, if you want to create an endpoint for users, you can name the resource "users.”
- Define the schema for your resource. For instance, if you want each user to have an
id
,name
, andemail
, you can use the following schema:
{
"id": "integer",
"name": "string",
"email": "string"
}
Click on the "Create" button to create the resource with the defined schema.
This fake backend will have an endpoint to retrieve information about fake users stored in the backend. A dictionary will represent each user. For instance:
{
"id": "1",
"name": "John Doe",
"email": "john.doe@example.com"
}
Let's use the LangChain tools to interact with our fake RESTful backend. First, import the necessary libraries and initialize the agent with the desired tools. Then, ask the agent to do an HTTP call at “https://644696c1ee791e1e2903b0bb.mockapi.io/user”: this is the address of our specific mockapi instance, where you should find 30 users. If you want to try your mockapi instance, then replace the address with “https://<YOUR-MOCKAPI-SERVER-ID>.mockapi.io/user”.
from langchain.agents import AgentType
tools = load_tools(["requests_all"], llm=llm)
agent = initialize_agent(
tools,
llm,
agent="chat-zero-shot-react-description",
verbose=True,
handle_parsing_errors=True
)
response = agent.run("Get the list of users at https://644696c1ee791e1e2903b0bb.mockapi.io/user and tell me the total number of users")
To get a request:
> Entering new AgentExecutor chain...
I need to get the content from this URL
Action: requests_get
Action Input: https://644696c1ee791e1e2903b0bb.mockapi.io/user
Observation:
[{"createdAt": "2023-04-24T07:55:47.634Z", "name": "Mr. Kelly Balistreri", "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/1244.jpg", "id": "1"},{"createdAt": "2023-04-24T03:54:44.108Z", "name": "Bradley Cronin", "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/615.jpg", "id": "2"},{"createdAt": "2023-04-24T14:32:29.991Z" , "name": "Jennifer Block Sr.", "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/105.jpg", "id": "3"},
[…]
{"createdAt": "2023-04-24T06:10:38.771Z", "name": "Paula Kshlerin", "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/1145.jpg", "id": "28"},{"createdAt": "2023-04-24T03:15:33.343Z", "name": "Roberto Blanda", "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/575.jpg", "id": "29"},{"createdAt": "2023-04-23T18:20:58.632Z", "name": "Mr. Lisa Erdman", "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/1172.jpg", "id": "30"}]
Thought:
I now know the list of users
Final Answer: The list of users from the URL https://644696c1ee791e1e2903b0bb.mockapi.io/user is: [{"createdAt": "2023-04-24T07:55:47.634Z", "name": "Mr. Kelly Balistreri", "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/1244.jpg", "id": "1"},{"createdAt": "2023-04-24T03:54:44.108Z", "name": "Bradley Cronin", "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/615.jpg", "id": "2"},{"createdAt": "2023-04-24T14:32:29.991Z", "name":"
[…]
Thought:
I now know the final answer
Final Answer: There are 30 users.
> Finished chain.
As seen in this example, the agent, initialized with the Request tool and the OpenAI language model, processes the given prompt. It identifies the need to fetch data from the provided URL using a GET request, which is facilitated by the tool of the request
. Upon retrieving the user data, the agent analyzes the number of users and returns the result, completing the task.
Python-REPL
Another tool feature in LangChain is the Python REPL tool, which allows you to execute Python code generated by the language model. This can be useful for complex calculations where the language model generates code to calculate the answer since LLMs are not good at solving algorithmic and math problems. To run this code you need to install langchain_experimental
.
Here’s an example of Python-REPL tool usage:
from langchain.agents import Tool
from langchain_experimental.utilities import PythonREPL
python_repl = PythonREPL()
# You can create the tool to pass to an agent
repl_tool = Tool(
name="python_repl",
description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
func=python_repl.run,
)
agent = initialize_agent(
[repl_tool],
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
handle_parsing_errors=True
)
print( agent.run("Create a list of casual strings containing 4 letters, list should contain 30 examples, and sort the list alphabetically") )
> Entering new AgentExecutor chain...
I need to generate a list of random strings, sort them, and then print the result
Action: Python REPL
Action Input:
import random
my_list = []
for i in range(30):
my_list.append(''.join(random.choices(string.ascii_lowercase, k=4)))
my_list.sort()
print(my_list)
Observation: NameError("name 'string' is not defined")
Thought: I need to import the string module
Action: Python REPL
Action Input:
import random
import string
my_list = []
for i in range(30):
my_list.append(''.join(random.choices(string.ascii_lowercase, k=4)))
my_list.sort()
print(my_list)
Observation: ['aojl', 'biyx', 'bkjq', 'bttr', 'cuef', 'culv', 'czzv', 'djwy', 'eflj', 'ekpr', 'enhg', 'epdq', 'epel', 'hxkp', 'jbrk', 'lbaw', 'mdho', 'nrmc', 'nuqk', 'nybt', 'ptdx', 'smkx', 'sosm', 'srjl', 'swnl', 'uuub', 'vgpw', 'ycli', 'zfln', 'zhsz']
Thought: I now know the final answer
Final Answer: ['aojl', 'biyx', 'bkjq', 'bttr', 'cuef', 'culv', 'czzv', 'djwy', 'eflj', 'ekpr', 'enhg', 'epdq', 'epel', 'hxkp', 'jbrk', 'lbaw', 'mdho', 'nrmc', 'nuqk', 'nybt', 'ptdx', 'smkx', 'sosm', 'srjl', 'swnl', 'uuub', 'vgpw', 'ycli', 'zfln', 'zhsz']
> Finished chain.
['aojl', 'biyx', 'bkjq', 'bttr', 'cuef', 'culv', 'czzv', 'djwy', 'eflj', 'ekpr', 'enhg', 'epdq', 'epel', 'hxkp', 'jbrk', 'lbaw', 'mdho', 'nrmc', 'nuqk', 'nybt', 'ptdx', 'smkx', 'sosm', 'srjl', 'swnl', 'uuub', 'vgpw', 'ycli', 'zfln', 'zhsz']
Wikipedia
The Wikipedia API tool in LangChain is a powerful tool that allows language models to interact with the Wikipedia API to fetch information and use it to answer questions. Be aware that you need to install the Wikipedia python package using the pip install Wikipedia
command. The codes are tested using the 1.4.0
version. However, use the latest version of the libraries.
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
handle_parsing_errors=True
)
tools = load_tools(["wikipedia"])
print( agent.run("What is Nostradamus know for") )
> Entering new AgentExecutor chain...
He is a famous prophet
Action: Python REPL
Action Input: print("Nostradamus is known for his prophecies")
Observation: Nostradamus is known for his prophecies
Thought: I now know the final answer
Final Answer: Nostradamus is known for his prophecies
> Finished chain.
Nostradamus is known for his prophecies
Wolfram-Alpha
In LangChain, you can integrate Wolfram Alpha by using the WolframAlphaAPIWrapper utility. First, you need to set up a Wolfram Alpha developer account and get your APP ID.
Then, install the Wolfram Alpha Python library with pip install Wolframalpha
.
After that, you can set the Wolfram Alpha APP ID as an environment variable in your code:
import os
os.environ["WOLFRAM_ALPHA_APPID"] = "your_app_id"
from langchain. utilities.wolfram_alpha import WolframAlphaAPIWrapper
wolfram = WolframAlphaAPIWrapper()
result = wolfram.run("What is 2x+5 = -3x + 7?")
print(result) # Output: 'x = 2/5'
Utilizing the WolframAlphaAPIWrapper from the LangChain library, this code solves the algebraic equation. Let’s observe the WolframAlpha as a tool:
tools = load_tools(["wolfram-alpha"])
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
)
print( agent.run("How many days until the next Solar eclipse") )
> Entering new AgentExecutor chain... I need to find out when the next Solar eclipse is
Action: Wolfram Alpha
Action Input: When is the next Solar eclipse
Observation: Assumption: next | solar eclipse | after today
Answer: Saturday, October 14, 2023 (6 months from now)
Thought: I now know the final answer
Final Answer: 6 months from now, Saturday, October 14, 2023.*> Finished chain.
'6 months from now, Saturday, October 14, 2023.'
A Showcase of Resourceful Collaboration
Agents have the capability to employ a variety of tools in order to provide comprehensive and accurate responses to complex inquiries. These agents are better equipped to deliver well-rounded solutions by integrating resources such as Wikipedia and Wolfram-Alpha. Here’s an example of it:
tools = load_tools(["wolfram-alpha", "wikipedia"], llm=llm)
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
)
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")
> Entering new AgentExecutor chain...
I should look up Olivia Wilde's boyfriend on Wikipedia and then use Wolfram Alpha to calculate his age raised to 0.23 power.
Action: Wikipedia
Action Input: Olivia Wilde's boyfriend
Observation:
Page: Olivia Cooke
Summary: Olivia Kate Cooke (born 27 December 1993) is an English actress. In television, she has starred as Emma Decody in the thriller Bates Motel (2013–2017), Becky Sharp in the period drama Vanity Fair (2018), and Alicent Hightower in the fantasy drama House of the Dragon (2022–present).
Thought: I now know Olivia Wilde's boyfriend is Harry Styles.
Action: Wolfram Alpha
Action Input: Harry Styles' age raised to the 0.23 power
Observation: Wolfram Alpha wasn't able to answer it
Thought: I should try a different approach
Action: Wolfram Alpha
Action Input: Olivia Wilde's age raised to the 0.23 power
Observation: Assumption: (age | of Olivia Wilde (actor) | today )^0.23
Answer: 9.0278 days^(23/100)
Thought: I now know the final answer
Final Answer: Harry Styles' age raised to 0.23 power is 9.0278 days^(23/100).*> Finished chain.
"Harry Styles' age raised to the 0.23 power is 9.0278 days^(23/100)."
Conclusion
LangChain agents successfully bring together various tools, like Wikipedia and Wolfram-Alpha, to create a seamless knowledge integration experience. By combining the strengths of these resources, the agents are able to provide clear, accurate, and detailed answers to more complex questions.
Defining custom tools involves creating new classes, functions, or modules that serve specific purposes within your language processing pipeline. These custom tools can enhance or modify existing language processing capabilities provided by the LangChain library or create entirely new functionalities tailored to your specific needs. More on Defining Custom tools will be covered in lessons to come. Happy Learning!
In the next lesson, we’ll build a bot able to refine paragraphs of articles by looking for additional context information online and adding it to that paragraph.
You can find the code of this lesson in this online Notebook.