WEBVTT

00:00.440 --> 00:01.240
Alrighty.

00:01.280 --> 00:04.800
In this video, we'll implement the web searching node.

00:05.440 --> 00:12.160
We'll be leveraging the search engine, which is highly optimized to downstream search results, into

00:12.200 --> 00:13.920
an LLM based application.

00:14.520 --> 00:20.560
And to use Tavileh, we need to make sure we have in our dot env file the Tavileh API key.

00:21.800 --> 00:25.880
Let's go to the code and let's add a new file under node.

00:25.880 --> 00:28.240
And we want to call it web search.

00:29.520 --> 00:33.800
And here we want to start with the imports to import typing.

00:34.080 --> 00:41.680
We want to import the Lang document class because we want to convert the search results into a Lang

00:41.960 --> 00:42.760
document.

00:43.080 --> 00:51.400
And finally we want to import the search class, which is a link chain tool that runs the search engine

00:51.400 --> 00:52.120
on the queries.

00:52.120 --> 00:53.080
We will provide it.

00:54.000 --> 00:54.760
All right.

00:54.760 --> 00:58.240
So let's now also import the graph state.

00:58.760 --> 01:05.720
And finally, we want to create an object of the Search results tool.

01:06.280 --> 01:09.400
Let's initialize it with max results equals to three.

01:09.400 --> 01:12.200
So we'll get at most three results.

01:12.360 --> 01:14.320
And let's define our function.

01:14.320 --> 01:15.640
We'll call it web search.

01:15.640 --> 01:22.520
And it will receive the state and return a dictionary will print something just for debugging.

01:23.080 --> 01:27.680
And we'll extract the question and documents from the graph state.

01:27.720 --> 01:33.320
Now notice because we execute the web search node only after we grade the documents.

01:33.600 --> 01:35.840
So it's after we filter them out.

01:35.840 --> 01:39.480
So we're not supposed to have any non-relevant documents.

01:39.640 --> 01:45.400
So all of the documents we're going to have in the documents list are going to be relevant for our query.

01:45.960 --> 01:46.400
All right.

01:46.400 --> 01:48.440
We're going to debug this file soon.

01:48.440 --> 01:54.960
So I'm going to import the environment variables and make sure that I have my API key in my env file.

01:55.680 --> 01:58.360
Let's add some if name equals main.

01:59.160 --> 02:05.120
And we're going to plug in the question to the agent memory and the documents to be none.

02:05.120 --> 02:07.760
So we don't want to put anything at the moment.

02:07.760 --> 02:12.200
This is the scenario where we didn't find any relevant documents at all.

02:12.960 --> 02:13.720
All right.

02:13.720 --> 02:21.720
So now we want to invoke the search tool, and we want to invoke it with the query to be our question.

02:23.280 --> 02:24.600
So let's debug it.

02:24.600 --> 02:27.680
And we want to examine the results we get from Tavelli.

02:28.240 --> 02:34.800
So because we limited the max results to be three, we're supposed to have here only three search results.

02:34.800 --> 02:38.080
And we can see it right here we have a list of three elements.

02:38.280 --> 02:42.760
And every element is a dictionary with the content key and the URL key.

02:43.120 --> 02:49.240
And what we want to do is to take all the content from all the elements of this list, and to combine

02:49.240 --> 02:52.480
them into one document of length chain.

02:52.720 --> 02:54.640
So that's what we're going to do next.

02:54.880 --> 02:55.680
Alrighty.

02:56.040 --> 02:58.040
So let's go back to the code.

03:01.560 --> 03:05.120
And let's stop debugging and let's add it.

03:05.120 --> 03:08.320
So we want to iterate through every element on the list.

03:08.320 --> 03:17.000
And we want to use the join function with backslash n to take the elements in the key of content and

03:17.000 --> 03:19.080
to simply join everything together.

03:19.680 --> 03:22.080
So we get only one huge string.

03:22.320 --> 03:22.920
All right.

03:22.960 --> 03:24.640
Now let's run this in debug.

03:24.640 --> 03:27.160
And let's see that we get indeed this string.

03:30.560 --> 03:33.520
And I'm going now to evaluate this expression.

03:35.920 --> 03:38.480
And we can see we have one big string.

03:39.040 --> 03:40.920
Now we want to take this string.

03:40.920 --> 03:47.800
And from it we want to create a long chain document where its content is going to be this big string.

03:49.440 --> 03:52.680
So I want to create a new variable called web results.

03:52.680 --> 03:54.640
And it's going to be a document.

03:54.640 --> 03:58.760
And in the page content I'm going to give the joint result.

03:58.760 --> 04:04.680
And if we currently have documents in our state then those are relevant documents.

04:04.680 --> 04:10.400
So we want to append to this document, list the document that is containing the web search.

04:10.720 --> 04:16.600
And if not, then this means that we didn't find any document that's relevant, and we simply want to

04:16.640 --> 04:21.280
put in the documents a list with one element, which is the web results.

04:21.520 --> 04:26.240
And finally, we want to return and update the state of our graph execution.

04:26.480 --> 04:29.520
So in the documents key we're going to put the documents.

04:29.520 --> 04:32.600
And in the question we're simply going to keep the original question.

04:33.200 --> 04:35.520
And that's it for the web searching node.

04:35.760 --> 04:37.040
Very straightforward.

04:37.680 --> 04:43.000
And the last thing I want to do in this video is to simply rename the file name.

04:43.360 --> 04:46.040
So I will go and rename it.

04:46.040 --> 04:48.560
And I simply want to add here an underscore.

04:48.560 --> 04:50.880
So I'll call it web underscore search.
