In this video, we ll talk about language chain tools.
Language chain tools are like specialized apps for LLM.
They are tiny code modules that allow it to access information and services.
These tools connect your LLM to search engines, databases, APIs, and more,
expanding its knowledge and capabilities.
Think of them as building blocks for
creating smarter, more versatile language models.
So whether your AI needs real -time
weather updates, wants to search Wikipedia or even translate languages, it
can use these tools to get the job done.
Now, let s explore some interesting and
useful tools.
The first one we ll discuss is a search tool.
There are several search tools available, such as Google Search, DuckDuckGo, or
Taveli AI, which is optimized for LLMs and provides an API for AI developers.
In this video, we ll focus on DuckDuckGo search tool to avoid the hassle of
setting up Google API keys or other components.
First, let s install DuckDuckSearch.
Let s import the necessary library from langchain tools import DuckDuckGoSearchRun.
I am creating an object of type DuckDuckGoSearchRun, search equals, and I
am calling the constructor of the class.
I am running the tool by calling search
.invoke with the prompt string as an argument.
Output equals, search .invoke, and the
prompt Where was Freddie Mercury born?
And print output.
I am running the code, and I ve got the result.
It searched the web using this service
and gave me this result.
You can ignore this warning for now.
It s about running DuckDuckGo in the asynchronous mode.
The tool has some attributes that can be printed using the . notation.
search .name will print the name of the tool.
search .description will print its description.
To get more additional information, such as the link, use DuckDuckGo search results.
I am importing the class from linkchain .tools import DuckDuckGo search results,
and I am creating an object, and output equals, search .run, and the prompt.
The prompt will be Freddie Mercury and Quinn, and I am printing the output.
I am running the code.
As you can see, output is a string
representation of a list of dictionaries with the following keys.
If you want to have much more control over the search results, you can directly
pass a custom DuckDuckGo search API wrapper to DuckDuckGo search results.
From linkchain .community .utilities import DuckDuckGo search API wrapper, and
I am creating a wrapper object, wrapper equals DuckDuckGo search API
wrapper of region equals, and the country code, for example, de -de from
Deutschland or Germany, max underline results equals 3, and the safe search
equals moderate, and the search object, DuckDuckGo search results of API wrapper
equals wrapper, and the source equals news, output equals search .run, and
Berlin will be the argument or the prompt.
I am running the code and printing the
output, and by the way, if you want to get rid of the warnings, you can do
import warnings and warnings .filterWarnings of ignore as a string.
I am running it, now the warnings, which are not errors, will be ignored.
Let us display the output in a more user -friendly way.
I will use regular expression to extract the snippet, the link, and the title.
I am importing the regular expression module, import re.
Next, I will define the pattern for the regular expression, the pattern includes
3 groups, each group corresponds to the snippet, title, and link in the string,
snippet, title, and link.
Next, I will call the findAll function to
find all matches of the pattern in the string, matches equals re .findAll of
pattern output and the re .thatAll, the re .thatAll flag is used so that the
.character matches any character including a newline.
Now, let us loop through the matches for snippet, title, and link in matches,
colon, and the for loop.
Each match is a tuple containing the
snippet, the title, and the link.
I will unpack the tuple into variables.
I am printing the snippet, the title, and the link using an F string.
I am also printing a separator for readability.
I am running the code.
Sorry, I have mistyped pattern and snippet.
As you can see, it returned 3 results in
German about Berlin.
Let us go ahead and talk about another
useful tool, the Wikipedia tool.
I am installing it, pip install -q -wikipedia.
I am importing the necessary modules.
From the linkchain -community -tools, I
am importing Wikipedia -Query -Run and from the linkchain -community -utilities,
I am importing the Wikipedia -API -Wrapper class.
Now, we initialize the tool.
This is where we can configure it as we want.
API -Wrapper equals Wikipedia -API -Wrapper of top -k results equals 1 and
the doc -content -chars -max equals 100.
Now, I am creating an object of type
Wikipedia -Query -Run.
Wiki equals Wikipedia -Query -Run of API
-Wrapper equals API -Wrapper.
Let us use 500 instead of 100 and we can
call this tool with a dictionary as input, wiki .invoke and a dictionary,
query, colon, lama -index.
I am running.
By the way, lama -index is a data framework that helps build smart
applications by connecting LLMs with your specific information.
Very well, I got some information about the lama -index from Wikipedia.
Note that the output is cut off at 500 characters.
I am replacing 500 with 5000 to get the
entire content.
You can also call this tool with a string
as an argument, wiki .invoke of Google -Gemini.
That is it about the doc -doc -go and
Wikipedia tools.
Thank you, bye -bye.