1
00:00:03,469 --> 00:00:16,129
Okay, so in this chapter we are going to build a simple agent using language that is the problem we

2
00:00:16,129 --> 00:00:17,060
want to solve.

3
00:00:17,680 --> 00:00:26,770
We want our LM application to be able to decide which tool is the most suitable to answer the question

4
00:00:26,770 --> 00:00:27,850
we pose.

5
00:00:29,170 --> 00:00:36,190
And we want our application in this case, to be able to search on Google and to search for information

6
00:00:36,190 --> 00:00:38,200
on a specific URL.

7
00:00:38,200 --> 00:00:43,270
So we are going to build a very simple agent with these two tools.

8
00:00:43,570 --> 00:00:50,470
He's going to be able to search on Google, and he's going to be able to search for information on a

9
00:00:50,470 --> 00:00:51,910
specific URL.

10
00:00:52,610 --> 00:01:02,900
And as an agent, we want him to decide on autonomously which tool is he going to use, depending on

11
00:01:02,900 --> 00:01:04,519
the question we have.

12
00:01:04,550 --> 00:01:14,510
So the agent is going to decide if he wants to search on Google, or if he wants to search on a particular

13
00:01:14,510 --> 00:01:20,510
URL, depending on on the question we provide to to the application.

14
00:01:20,510 --> 00:01:29,180
So what we are going to do is we are going to create an agent that knows how to decide between two tools

15
00:01:29,180 --> 00:01:34,730
doing a search on Google or connecting to a specific web page.

16
00:01:34,970 --> 00:01:37,850
This is the process we are going to follow.

17
00:01:37,880 --> 00:01:41,240
We have the code here on the right.

18
00:01:41,660 --> 00:01:50,120
So first, as always, we load the OpenAI credentials from our dot env file.

19
00:01:50,120 --> 00:01:54,770
This is the confidential file we use to store our passwords, etc., etc..

20
00:01:56,910 --> 00:01:58,080
After that.

21
00:01:58,080 --> 00:02:06,060
What we do is load OpenAI, the module OpenAI, in order to create an instance of an LM.

22
00:02:06,690 --> 00:02:12,810
And once we have that, we are going to load the two.

23
00:02:13,900 --> 00:02:15,790
Tools we are going to use.

24
00:02:15,790 --> 00:02:19,360
The first tool we are going to use is Google Search.

25
00:02:19,900 --> 00:02:29,380
In order to make use of Google Search in your LM application, first you want to get a couple of private

26
00:02:29,380 --> 00:02:31,150
keys in Google.

27
00:02:31,150 --> 00:02:40,810
So here you have the links to go to Google in order to sign up to create your Google API key and your

28
00:02:40,810 --> 00:02:50,080
Google CC ID, and without any question you may have in the process of getting these keys, you know

29
00:02:50,080 --> 00:02:54,730
where to go ChatGPT, Google, StackOverflow, etc. it's very easy.

30
00:02:54,730 --> 00:02:56,590
It's free and it's very easy.

31
00:02:56,710 --> 00:03:03,250
Once you have these keys, you store these keys in your dot m file.

32
00:03:03,280 --> 00:03:09,790
And in this case, what we are doing here is we are getting these keys from our dot m file.

33
00:03:10,390 --> 00:03:20,290
Then in order to, uh, create a tool that is able to search on Google, we need to install the Google

34
00:03:20,290 --> 00:03:26,260
API, Python client, plugin or module to do that.

35
00:03:26,260 --> 00:03:32,290
If we want to do that from the notebook, we write this and execute this command.

36
00:03:32,530 --> 00:03:39,190
If we want to do it from the terminal, we execute this command from the terminal.

37
00:03:39,190 --> 00:03:41,350
It's different if you do it from the notebook.

38
00:03:41,350 --> 00:03:47,080
From the notebook you add the exclamation mark and from the terminal you just write this down.

39
00:03:47,080 --> 00:03:52,150
Since we have already installed this, we comment this and we don't want to do anything.

40
00:03:52,720 --> 00:03:57,310
So then we are going to create the variable.

41
00:03:57,310 --> 00:04:03,130
In this case, we name the variable search in order to make use of that functionality.

42
00:04:03,130 --> 00:04:08,770
So now we are able to use this functionality in the variable search.

43
00:04:09,070 --> 00:04:16,930
So first thing cover we now are able to search on Google from our LM application.

44
00:04:16,930 --> 00:04:22,780
The second functionality we want to be able to connect with an external URL.

45
00:04:22,780 --> 00:04:32,590
In order to do that, we are going to load or import a module called Text Requests wrapper from the

46
00:04:32,590 --> 00:04:34,870
Lang Chain Utilities library.

47
00:04:35,110 --> 00:04:39,310
And once we have this module, we can create a new variable.

48
00:04:39,310 --> 00:04:40,840
We will call it request.

49
00:04:40,840 --> 00:04:45,460
You can call it whatever you want in order to make use of this new functionality.

50
00:04:45,460 --> 00:04:53,650
Once we have these two functionalities in our in our application, we are going to set the tools that

51
00:04:53,650 --> 00:04:55,900
we that will use our agent.

52
00:04:55,900 --> 00:04:59,920
So the first thing we do is we import the tool module.

53
00:04:59,920 --> 00:05:06,940
And with this tool module we are going to create a new variable called tools that is going to store

54
00:05:06,940 --> 00:05:08,770
our two tools okay.

55
00:05:08,770 --> 00:05:11,770
So this is the first tool which is the Google search.

56
00:05:11,770 --> 00:05:16,000
And this is the second tool which is the request what we have called requests.

57
00:05:16,000 --> 00:05:21,910
What is doing this is going to connect with a particular URL on the internet okay.

58
00:05:22,180 --> 00:05:31,960
So once we have the tools defined we are going to initialize and configure our agent okay.

59
00:05:31,960 --> 00:05:40,240
In order to do that we are going to load three more modules initialize agent agent type and load tools.

60
00:05:40,240 --> 00:05:43,030
And we are going to create our agent.

61
00:05:43,120 --> 00:05:47,860
So we are going to call our agent my agent you can call it whatever you want.

62
00:05:47,860 --> 00:05:52,000
And we are going to initialize it with this functionality.

63
00:05:52,000 --> 00:05:57,190
And we are going to tell them okay, you are going to use these tools we have created.

64
00:05:57,190 --> 00:06:02,680
We are going to use this LM instance we have created here.

65
00:06:04,850 --> 00:06:05,540
Okay.

66
00:06:05,870 --> 00:06:11,780
And, uh, this is the type of agent we want to use from long chain.

67
00:06:11,780 --> 00:06:19,040
As you will see, if you go to the long chain documentation, long chain provides a few, uh, types

68
00:06:19,040 --> 00:06:20,480
of agent you can use.

69
00:06:20,480 --> 00:06:26,870
This is by far the most popular, the one we use, like, 99% of the time.

70
00:06:26,870 --> 00:06:29,030
But you have other options as well.

71
00:06:29,480 --> 00:06:38,330
We are going to define this new agent with the option verbose as true because we want to see what is

72
00:06:38,330 --> 00:06:44,300
happening, you know, on the on the background whenever we execute a command we'll see how very soon.

73
00:06:44,300 --> 00:06:54,770
And this is very important, we are setting a max iterations to three because we want to avoid here

74
00:06:54,770 --> 00:06:56,540
one of the main.

75
00:06:58,600 --> 00:07:05,380
One of the most common troubles you are going to find when working with agents, which is that in some

76
00:07:05,380 --> 00:07:13,930
cases, if the agent is not able to find an answer from your questions right away, he's going to loop

77
00:07:13,930 --> 00:07:17,290
over and over trying to get the response.

78
00:07:17,290 --> 00:07:26,530
So if you do not limit the number of attempts your agent is going to make in order to solve your questions,

79
00:07:26,530 --> 00:07:34,450
your agent can be looping, you know, forever, and your OpenAI bill is going to be high.

80
00:07:34,480 --> 00:07:36,730
So be very careful with this.

81
00:07:36,910 --> 00:07:40,390
Okay, so this is the note we make here.

82
00:07:40,390 --> 00:07:46,840
Operations with agents are more expensive than regular operations in OpenAI.

83
00:07:46,960 --> 00:07:51,160
And especially if the number of iterations is high okay.

84
00:07:51,160 --> 00:07:52,930
So be careful with this.

85
00:07:53,380 --> 00:07:58,750
So once we have defined our agent we can start asking questions.

86
00:07:58,750 --> 00:08:04,180
And we will see how our agent decides which tool to use.

87
00:08:04,360 --> 00:08:15,310
So we are going to start asking our agent which team won the 2010 soccer World Championship.

88
00:08:16,790 --> 00:08:18,020
Is the question.

89
00:08:18,020 --> 00:08:23,060
And this is the way we run the question through our agent.

90
00:08:23,060 --> 00:08:23,780
Okay.

91
00:08:24,350 --> 00:08:24,950
And.

92
00:08:25,540 --> 00:08:30,520
We are going to store this response into this new variable we create.

93
00:08:30,820 --> 00:08:39,700
So here as we have defined variables through here, we can see what are the operations our agent is

94
00:08:39,700 --> 00:08:41,559
doing in the background.

95
00:08:41,830 --> 00:08:42,940
So.

96
00:08:44,020 --> 00:08:53,500
Our agent reads the question, and once he reads the question, he needs to decide which of the two

97
00:08:53,500 --> 00:08:58,060
tools is more, uh, adequate for him to use.

98
00:08:58,090 --> 00:09:06,130
So in this case, you can see that our agent is thinking and is is thinking, okay, I should look up

99
00:09:06,130 --> 00:09:07,540
the result online.

100
00:09:07,540 --> 00:09:11,800
So I'm going to use Google Search in order to solve this question.

101
00:09:12,130 --> 00:09:19,360
So what he is doing here is querying Google asking this to Google okay Google.

102
00:09:20,300 --> 00:09:23,570
Who is the world champion in in this year.

103
00:09:23,600 --> 00:09:23,990
Okay.

104
00:09:23,990 --> 00:09:30,080
So the first response gives the answer to our agent.

105
00:09:30,080 --> 00:09:32,780
So once he.

106
00:09:34,140 --> 00:09:36,030
Process this information.

107
00:09:36,030 --> 00:09:37,830
He knows the final answer.

108
00:09:37,830 --> 00:09:40,920
So here you have the thought of our agent.

109
00:09:40,920 --> 00:09:42,900
I know the final answer.

110
00:09:44,330 --> 00:09:48,020
So he has the final answer set.

111
00:09:48,320 --> 00:09:54,800
If we print the response, we now see how our agent is answering.

112
00:09:54,800 --> 00:09:58,790
Spain won the 2010 soccer World Championship.

113
00:09:59,660 --> 00:10:09,500
If we make another type of question asking for example, okay, go to this particular URL and tell me.

114
00:10:10,330 --> 00:10:14,140
What are the comments in this web page about?

115
00:10:14,140 --> 00:10:16,000
Okay, so.

116
00:10:16,850 --> 00:10:17,750
The.

117
00:10:18,660 --> 00:10:19,530
Agent.

118
00:10:19,710 --> 00:10:27,900
Once he starts processing our question, he immediately guessed that the best way to solve this question

119
00:10:27,900 --> 00:10:29,970
is to use the second tool.

120
00:10:30,000 --> 00:10:32,250
The tool we have named request.

121
00:10:32,250 --> 00:10:40,410
And as you know, the tool that allows our agent to connect with one particular URL, one particular

122
00:10:40,410 --> 00:10:42,720
web page on the internet.

123
00:10:42,720 --> 00:10:51,420
So he's connecting with this web page and he is downloading all the HTML, all the code that is in the

124
00:10:51,420 --> 00:10:53,340
background of this web page.

125
00:10:53,340 --> 00:10:54,600
And he's processing.

126
00:10:54,720 --> 00:10:57,300
He's learning from all this code.

127
00:10:57,330 --> 00:11:05,760
As you know, a web page on the background is HTML, but this HTML is wrapping all the text that is

128
00:11:05,760 --> 00:11:06,840
in a web page.

129
00:11:06,840 --> 00:11:11,430
So our agent is reading the website, the website, it is this web page.

130
00:11:11,430 --> 00:11:18,390
And you will see that after reading the information in this web page he says, I can see the comments

131
00:11:18,390 --> 00:11:22,170
in the web page which will give me the subjects.

132
00:11:22,320 --> 00:11:26,190
So he he processes the information, blah blah blah.

133
00:11:26,190 --> 00:11:33,840
And immediately he finds out that the subjects in the comments of the web page are related to OpenAI's

134
00:11:33,840 --> 00:11:34,950
embedding API.

135
00:11:34,980 --> 00:11:35,880
La la la la la.

136
00:11:35,910 --> 00:11:43,620
Okay, so if we print the response now we know, uh, what our agent has found found out, right?

137
00:11:43,620 --> 00:11:45,900
So as you have seen.

138
00:11:46,840 --> 00:11:53,590
We have learned how to build a simple agent, and we have also learned.

139
00:11:53,590 --> 00:12:00,940
Start learning about the limitations of agents and how you have to be careful with them, especially

140
00:12:00,940 --> 00:12:04,180
when you are working in a professional environment.

