WEBVTT

00:00.280 --> 00:00.880
Hey there.

00:00.920 --> 00:01.760
Eden here.

00:01.760 --> 00:06.640
And in the next two videos, we're going to be implementing our tool executor node.

00:07.080 --> 00:13.560
And this node is going to take as an input the I message, which is going to have the information of

00:13.560 --> 00:15.040
the wanted search queries.

00:15.440 --> 00:22.960
And it's going to run Tavileh to get us real time results and real time information from the web.

00:24.320 --> 00:29.040
Through this video, we'll have all the components for our graph ready and we'll build our graph.

00:29.360 --> 00:31.160
So let's go to the code.

00:31.840 --> 00:32.560
Alrighty.

00:32.560 --> 00:34.720
So let's go and create a new file.

00:35.040 --> 00:38.040
And I want to call it tool executor.

00:39.080 --> 00:43.320
Let's start by importing low dotenv and load the environment variables.

00:43.800 --> 00:47.720
And now I want to import from link chain Tavileh.

00:47.840 --> 00:54.040
I want to import the Tavileh search tool and make sure you have Tavileh installed.

00:54.040 --> 00:56.400
So you can do that by writing poetry.

00:56.440 --> 00:58.000
Add link chain Tavileh.

00:58.120 --> 01:01.720
And you want to make sure that you have the API key.

01:01.720 --> 01:05.920
So let me go and take that real quick and I'll log in.

01:11.760 --> 01:16.760
And let me go and copy one API key and let me put it in my env file.

01:16.760 --> 01:20.000
And by the way, don't worry about me exposing my API keys.

01:20.040 --> 01:22.640
I revoked them after I finished filming this video.

01:22.960 --> 01:29.320
And we want now to import the structured tool class from Link Chain, which is a component that's going

01:29.320 --> 01:34.240
to allow us to convert a Python function into a tool that can be used by Llms.

01:34.480 --> 01:40.480
So it's going to take that function and provide to the LLM a structured schema for the function, which

01:40.480 --> 01:43.080
will help the LLM understand how to use this tool.

01:43.760 --> 01:48.760
And then we're going to import the tool node class from Landgraaf.

01:49.240 --> 01:53.560
And this is a very cool class that saves us tons of work.

01:53.600 --> 01:56.760
And it's a node in the graph that we can invoke.

01:57.160 --> 02:01.360
And it's going to look in the state for the messages key.

02:01.600 --> 02:07.930
It's going to check the last message, and it's then going to see if there's any tool calls that were

02:07.930 --> 02:09.450
decided by the alarm.

02:09.570 --> 02:13.650
And if there are, it's going to execute those tools for us.

02:13.650 --> 02:15.770
And it can do this even in parallel.

02:16.290 --> 02:18.250
So this saves us tons of work.

02:18.250 --> 02:21.410
And trust me, before that we would need to do everything ourselves.

02:21.410 --> 02:25.010
And I actually did that in the original version of the course.

02:25.050 --> 02:30.090
And I'll even leave this implementation as optional in case you want to check out all the heavy lifting

02:30.090 --> 02:32.530
that tool node performs for us anyways.

02:32.530 --> 02:37.690
So this is going to be the node that is going to execute the tools, and we're going to instantiate

02:37.690 --> 02:39.970
it with the tools that it needs to run it.

02:39.970 --> 02:41.530
We're going to see it in this video.

02:41.530 --> 02:43.170
So it's going to be very clear.

02:44.050 --> 02:44.610
Cool.

02:44.650 --> 02:49.730
So let me just import the answer question and the revised answer classes.

02:49.970 --> 02:54.210
Let's go and initialize our search tool.

02:54.770 --> 02:58.610
So we'll start by creating an object of search.

02:58.970 --> 03:03.570
So we'll do that with max results equals to five simply to get five results.

03:03.930 --> 03:10.170
And it's going to give us back a link chain tool with the function of the search engine.

03:10.570 --> 03:14.970
And we don't want to use it as is, like we usually do.

03:15.010 --> 03:16.730
We want to do a cool trick here.

03:17.130 --> 03:23.930
So we want to take the original tool and its functionality, and we want to create from it two different

03:23.970 --> 03:24.490
tools.

03:24.530 --> 03:29.890
It's going to be two different tools with the same functionality of the search, but they're going to

03:29.890 --> 03:35.210
have different names because they serve different purposes in the application workflow.

03:35.610 --> 03:41.530
So we're going to have an answer question tool, which is going to be used during the initial research

03:41.530 --> 03:44.410
phase when the agent is first answering the question.

03:44.610 --> 03:50.450
And then we want to have this revised answer tool which is used during the revision phase when the agent

03:50.450 --> 03:53.250
is improving its answer based on the reflection.

03:53.490 --> 03:56.890
So both tools are going to run the search.

03:57.810 --> 04:02.610
And that's why we need those objects the answer question object and the revised answer object.

04:02.610 --> 04:06.410
Because we want to get their names in order to label those tools.

04:07.170 --> 04:14.210
But theoretically we can use one tool, but having separate names in two separate tools allows the system

04:14.210 --> 04:19.770
to clearly track which stage of the research process triggered the search initial research versus the

04:19.770 --> 04:20.930
revision research.

04:21.090 --> 04:24.610
So it's going to help us in debugging and evaluating the response.

04:24.890 --> 04:25.370
All right.

04:25.370 --> 04:28.130
So let me create a new function.

04:28.130 --> 04:34.770
And I'm going to call this function run queries which is going to receive as an input the search queries

04:34.770 --> 04:36.130
which is a list of strings.

04:36.530 --> 04:40.450
And in its description it's going to have run the generated queries.

04:40.730 --> 04:45.450
And let's also add the quarks here in case the LLM is going to fail some other values.

04:45.450 --> 04:47.490
So we won't get an error in that case.

04:47.890 --> 04:51.330
And the implementation here is going to be very straightforward.

04:51.330 --> 04:55.610
We're going to run the Tavileh tool with those queries.

04:55.610 --> 04:57.410
So we're going to iterate over them.

04:57.410 --> 05:02.610
And we're going to run the query with the batch function which is going to run them concurrently.

05:03.090 --> 05:03.530
All right.

05:03.530 --> 05:08.930
So now we want to create from this function we want to create two different tools with different names.

05:09.170 --> 05:14.090
So one tool is going to be answer a question and the other tool is going to be revised answer.

05:14.370 --> 05:19.090
And they're both going to run the search the run queries function.

05:19.090 --> 05:22.450
And the structured tool class is going to help us achieve that.

05:23.250 --> 05:26.290
And they're all going to run in the tool mode.

05:26.290 --> 05:29.690
So the tool mode is going to run two different tools.

05:29.930 --> 05:36.090
And one tool is going to be the search tool that originated from the first creation of the research.

05:36.250 --> 05:41.130
And the other tool is going to be a search query that originated from the reflection revision.

05:41.130 --> 05:42.970
And that's the cool trick over here.

05:43.090 --> 05:44.250
So let's go and do that.

05:44.250 --> 05:46.610
Let's go create an object of tool node.

05:46.810 --> 05:51.450
And in order to do that we'll need to supply the list of tools that it's going to run.

05:51.610 --> 05:53.050
So let's create the first tool.

05:53.050 --> 05:55.410
So we'll take the structured tool class.

05:55.410 --> 06:01.450
And it's going to have a method from function which is going to receive a function and convert it into

06:01.450 --> 06:05.450
a tool with schema and description and all of that that we saw before.

06:05.650 --> 06:08.460
And another argument is going to be the name of the tool.

06:08.500 --> 06:10.820
So here the name is going to be the name of the class.

06:11.380 --> 06:16.260
And we want to do another tool which is going to run the same function.

06:16.260 --> 06:20.620
But it's going to have a different name of the revised answer class.

06:20.780 --> 06:21.780
And that's it.

06:22.020 --> 06:22.900
We are done.

06:22.940 --> 06:26.140
Here we have all the moving parts in order to create the graph.

06:26.340 --> 06:30.300
Now I remind you this tool is going to examine the state.

06:30.300 --> 06:32.380
It's going to check the last message.

06:32.380 --> 06:35.860
And if there is a tool call it's going to execute the relevant tool call.

06:37.460 --> 06:44.580
All right let me run some formatting over here and let me go and add everything we did right now.

06:44.580 --> 06:45.620
And let's commit it.

06:46.220 --> 06:52.540
So I'm going to commit this to the branch of slash projects slash reflection agent.

06:54.900 --> 07:01.260
And if you want to see the code you can select in the repo a branch of project slash reflection agent.

07:01.700 --> 07:06.780
And you can check out the commit here, which has all the code that we did in this video.

07:06.780 --> 07:09.620
And it's going to be linked in the videos resources.
