1 00:00:00,760 --> 00:00:03,970 In this video, we ll talk about language chain tools. 2 00:00:04,720 --> 00:00:08,810 Language chain tools are like specialized apps for LLM. 3 00:00:09,700 --> 00:00:15,470 They are tiny code modules that allow it to access information and services. 4 00:00:17,260 --> 00:00:24,090 These tools connect your LLM to search engines, databases, APIs, and more, 5 00:00:24,480 --> 00:00:27,230 expanding its knowledge and capabilities. 6 00:00:28,120 --> 00:00:30,470 Think of them as building blocks for 7 00:00:30,480 --> 00:00:33,990 creating smarter, more versatile language models. 8 00:00:35,120 --> 00:00:38,130 So whether your AI needs real -time 9 00:00:38,140 --> 00:00:44,190 weather updates, wants to search Wikipedia or even translate languages, it 10 00:00:44,200 --> 00:00:46,910 can use these tools to get the job done. 11 00:00:48,280 --> 00:00:52,330 Now, let s explore some interesting and 12 00:00:52,340 --> 00:00:52,970 useful tools. 13 00:00:53,920 --> 00:00:57,070 The first one we ll discuss is a search tool. 14 00:00:57,980 --> 00:01:04,210 There are several search tools available, such as Google Search, DuckDuckGo, or 15 00:01:04,220 --> 00:01:11,230 Taveli AI, which is optimized for LLMs and provides an API for AI developers. 16 00:01:12,540 --> 00:01:19,030 In this video, we ll focus on DuckDuckGo search tool to avoid the hassle of 17 00:01:19,040 --> 00:01:22,590 setting up Google API keys or other components. 18 00:01:24,920 --> 00:01:28,010 First, let s install DuckDuckSearch. 19 00:01:35,850 --> 00:01:48,340 Let s import the necessary library from langchain tools import DuckDuckGoSearchRun. 20 00:01:50,680 --> 00:01:58,870 I am creating an object of type DuckDuckGoSearchRun, search equals, and I 21 00:01:58,880 --> 00:02:01,030 am calling the constructor of the class. 22 00:02:03,870 --> 00:02:06,420 I am running the tool by calling search 23 00:02:06,430 --> 00:02:10,000 .invoke with the prompt string as an argument. 24 00:02:10,730 --> 00:02:16,300 Output equals, search .invoke, and the 25 00:02:16,310 --> 00:02:20,810 prompt Where was Freddie Mercury born? 26 00:02:24,470 --> 00:02:25,780 And print output. 27 00:02:26,690 --> 00:02:34,020 I am running the code, and I ve got the result. 28 00:02:35,410 --> 00:02:38,700 It searched the web using this service 29 00:02:38,710 --> 00:02:42,080 and gave me this result. 30 00:02:42,710 --> 00:02:45,320 You can ignore this warning for now. 31 00:02:46,070 --> 00:02:49,960 It s about running DuckDuckGo in the asynchronous mode. 32 00:02:51,730 --> 00:02:57,440 The tool has some attributes that can be printed using the . notation. 33 00:02:58,310 --> 00:03:01,560 search .name will print the name of the tool. 34 00:03:03,130 --> 00:03:07,880 search .description will print its description. 35 00:03:10,630 --> 00:03:16,660 To get more additional information, such as the link, use DuckDuckGo search results. 36 00:03:18,850 --> 00:03:29,530 I am importing the class from linkchain .tools import DuckDuckGo search results, 37 00:03:30,860 --> 00:03:41,320 and I am creating an object, and output equals, search .run, and the prompt. 38 00:03:41,750 --> 00:03:55,160 The prompt will be Freddie Mercury and Quinn, and I am printing the output. 39 00:03:56,290 --> 00:03:57,520 I am running the code. 40 00:03:59,010 --> 00:04:02,100 As you can see, output is a string 41 00:04:02,110 --> 00:04:06,820 representation of a list of dictionaries with the following keys. 42 00:04:20,630 --> 00:04:27,260 If you want to have much more control over the search results, you can directly 43 00:04:27,270 --> 00:04:34,140 pass a custom DuckDuckGo search API wrapper to DuckDuckGo search results. 44 00:04:35,130 --> 00:04:47,280 From linkchain .community .utilities import DuckDuckGo search API wrapper, and 45 00:04:47,290 --> 00:04:54,040 I am creating a wrapper object, wrapper equals DuckDuckGo search API 46 00:04:54,050 --> 00:05:02,720 wrapper of region equals, and the country code, for example, de -de from 47 00:05:02,730 --> 00:05:13,790 Deutschland or Germany, max underline results equals 3, and the safe search 48 00:05:13,800 --> 00:05:27,280 equals moderate, and the search object, DuckDuckGo search results of API wrapper 49 00:05:27,290 --> 00:05:42,460 equals wrapper, and the source equals news, output equals search .run, and 50 00:05:42,470 --> 00:05:45,780 Berlin will be the argument or the prompt. 51 00:05:46,890 --> 00:05:50,980 I am running the code and printing the 52 00:05:50,990 --> 00:05:59,850 output, and by the way, if you want to get rid of the warnings, you can do 53 00:05:59,860 --> 00:06:09,570 import warnings and warnings .filterWarnings of ignore as a string. 54 00:06:11,020 --> 00:06:16,830 I am running it, now the warnings, which are not errors, will be ignored. 55 00:06:22,390 --> 00:06:26,020 Let us display the output in a more user -friendly way. 56 00:06:26,770 --> 00:06:32,440 I will use regular expression to extract the snippet, the link, and the title. 57 00:06:33,570 --> 00:06:39,120 I am importing the regular expression module, import re. 58 00:06:40,550 --> 00:06:48,870 Next, I will define the pattern for the regular expression, the pattern includes 59 00:06:48,880 --> 00:06:55,710 3 groups, each group corresponds to the snippet, title, and link in the string, 60 00:06:56,660 --> 00:06:59,010 snippet, title, and link. 61 00:06:59,020 --> 00:07:03,890 Next, I will call the findAll function to 62 00:07:03,900 --> 00:07:12,610 find all matches of the pattern in the string, matches equals re .findAll of 63 00:07:12,620 --> 00:07:25,980 pattern output and the re .thatAll, the re .thatAll flag is used so that the 64 00:07:25,990 --> 00:07:30,000 .character matches any character including a newline. 65 00:07:31,470 --> 00:07:39,260 Now, let us loop through the matches for snippet, title, and link in matches, 66 00:07:39,510 --> 00:07:43,300 colon, and the for loop. 67 00:07:44,050 --> 00:07:47,400 Each match is a tuple containing the 68 00:07:47,410 --> 00:07:49,380 snippet, the title, and the link. 69 00:07:49,810 --> 00:07:52,480 I will unpack the tuple into variables. 70 00:07:52,490 --> 00:08:00,480 I am printing the snippet, the title, and the link using an F string. 71 00:08:01,990 --> 00:08:05,840 I am also printing a separator for readability. 72 00:08:10,300 --> 00:08:11,130 I am running the code. 73 00:08:12,600 --> 00:08:15,850 Sorry, I have mistyped pattern and snippet. 74 00:08:18,560 --> 00:08:22,190 As you can see, it returned 3 results in 75 00:08:22,200 --> 00:08:23,770 German about Berlin. 76 00:08:23,780 --> 00:08:27,630 Let us go ahead and talk about another 77 00:08:27,640 --> 00:08:29,990 useful tool, the Wikipedia tool. 78 00:08:31,400 --> 00:08:35,470 I am installing it, pip install -q -wikipedia. 79 00:08:38,700 --> 00:08:41,690 I am importing the necessary modules. 80 00:08:42,700 --> 00:08:45,290 From the linkchain -community -tools, I 81 00:08:45,300 --> 00:08:50,650 am importing Wikipedia -Query -Run and from the linkchain -community -utilities, 82 00:08:50,660 --> 00:08:54,630 I am importing the Wikipedia -API -Wrapper class. 83 00:08:56,360 --> 00:08:58,830 Now, we initialize the tool. 84 00:08:59,380 --> 00:09:02,110 This is where we can configure it as we want. 85 00:09:02,880 --> 00:09:16,740 API -Wrapper equals Wikipedia -API -Wrapper of top -k results equals 1 and 86 00:09:16,750 --> 00:09:21,420 the doc -content -chars -max equals 100. 87 00:09:24,500 --> 00:09:27,970 Now, I am creating an object of type 88 00:09:27,980 --> 00:09:29,190 Wikipedia -Query -Run. 89 00:09:30,350 --> 00:09:37,880 Wiki equals Wikipedia -Query -Run of API 90 00:09:37,890 --> 00:09:40,580 -Wrapper equals API -Wrapper. 91 00:09:41,890 --> 00:09:48,340 Let us use 500 instead of 100 and we can 92 00:09:48,350 --> 00:09:56,160 call this tool with a dictionary as input, wiki .invoke and a dictionary, 93 00:09:58,180 --> 00:10:01,690 query, colon, lama -index. 94 00:10:05,030 --> 00:10:05,520 I am running. 95 00:10:07,670 --> 00:10:12,800 By the way, lama -index is a data framework that helps build smart 96 00:10:12,810 --> 00:10:17,560 applications by connecting LLMs with your specific information. 97 00:10:19,150 --> 00:10:26,130 Very well, I got some information about the lama -index from Wikipedia. 98 00:10:26,860 --> 00:10:32,170 Note that the output is cut off at 500 characters. 99 00:10:33,080 --> 00:10:37,430 I am replacing 500 with 5000 to get the 100 00:10:37,440 --> 00:10:38,130 entire content. 101 00:10:42,680 --> 00:10:46,350 You can also call this tool with a string 102 00:10:46,360 --> 00:10:51,970 as an argument, wiki .invoke of Google -Gemini. 103 00:11:00,970 --> 00:11:03,740 That is it about the doc -doc -go and 104 00:11:03,750 --> 00:11:04,800 Wikipedia tools. 105 00:11:05,470 --> 00:11:06,560 Thank you, bye -bye.