﻿1
00:00:00,830 --> 00:00:06,220
We need the answer in natural language,
so we'll use the large language model to

2
00:00:06,230 --> 00:00:06,640
get it.

3
00:00:12,240 --> 00:00:13,850
I'm creating a new function.

4
00:00:14,240 --> 00:00:21,630
df askAndGetAnswer () The function will
have two parameters.

5
00:00:22,220 --> 00:00:26,130
The vector store and the question or the query.

6
00:00:27,680 --> 00:00:30,230
First, we'll retrieve the most relevant

7
00:00:30,240 --> 00:00:33,550
chunks of text from our vector database.

8
00:00:33,560 --> 00:00:37,690
Then, we'll feed those chunks to the LLM

9
00:00:37,700 --> 00:00:39,290
to get the final answer.

10
00:00:40,140 --> 00:00:42,930
In the previous section, I've already

11
00:00:42,940 --> 00:00:47,050
explained the code and now I'm just copy
pasting it here.

12
00:00:47,880 --> 00:00:51,570
This is the notebook from the previous section.

13
00:00:55,020 --> 00:00:55,310
Ok.

14
00:00:55,640 --> 00:00:56,830
This is the code.

15
00:00:58,940 --> 00:01:01,790
I'm copy pasting it in the function.

16
00:01:02,620 --> 00:01:05,330
And I'm indenting it correctly.

17
00:01:06,340 --> 00:01:08,210
I'm also running the chain.

18
00:01:08,680 --> 00:01:14,530
answer equals chain .run of q
And return answer.

19
00:01:16,490 --> 00:01:17,520
I'm running the set.

20
00:01:17,950 --> 00:01:19,060
Let's test it.

21
00:01:21,780 --> 00:01:22,630
So, q equals.

22
00:01:23,120 --> 00:01:24,830
What is the whole document about?

23
00:01:29,430 --> 00:01:37,280
And the answer equals askAndGetAnswer of
vector store and q

24
00:01:37,290 --> 00:01:40,720
And of course, print answer.

25
00:01:41,930 --> 00:01:43,300
I'm running the cell.

26
00:01:47,560 --> 00:01:52,030
And it says that the document is about
the US Constitution.

27
00:01:54,010 --> 00:01:54,660
Very well.

28
00:01:55,690 --> 00:01:58,140
What I'll do next is create a loop so

29
00:01:58,150 --> 00:02:02,500
that the user can send questions to the
application continuously.

30
00:02:04,170 --> 00:02:05,380
Import time.

31
00:02:05,970 --> 00:02:07,620
i equals one.

32
00:02:08,010 --> 00:02:10,420
This is a counter to display the question number.

33
00:02:10,890 --> 00:02:11,880
And the message.

34
00:02:13,230 --> 00:02:15,740
Write quit or exit to quit.

35
00:02:22,560 --> 00:02:23,750
And now the while loop.

36
00:02:23,980 --> 00:02:25,730
So, while true colon.

37
00:02:28,090 --> 00:02:29,380
This will be an infinite loop.

38
00:02:30,030 --> 00:02:34,920
And it will stop when the user enters
quit or exit.

39
00:02:36,030 --> 00:02:44,720
So, q equals input question and hash and
i in curly braces.

40
00:02:45,390 --> 00:02:48,020
And of course, this is an f string.

41
00:02:49,850 --> 00:02:51,140
Like this.

42
00:02:53,840 --> 00:02:55,650
i equals i plus one.

43
00:02:55,960 --> 00:02:57,490
I'm incrementing the counter.

44
00:02:58,960 --> 00:03:03,070
I'm checking if q equals exit or quit.

45
00:03:03,860 --> 00:03:06,130
And if it's the case, I'm breaking out.

46
00:03:06,800 --> 00:03:14,120
So, if q dot lower in quit and exit
.

47
00:03:14,130 --> 00:03:19,240
If it's the case, I'm printing a message quitting.

48
00:03:19,250 --> 00:03:21,860
Bye -bye.

49
00:03:21,870 --> 00:03:24,600
Time dot slip of two.

50
00:03:24,610 --> 00:03:27,000
I'm pausing for two seconds so that the

51
00:03:27,010 --> 00:03:30,060
user has enough time to read the message.

52
00:03:30,070 --> 00:03:33,720
And I'm breaking out of the while loop.

53
00:03:33,730 --> 00:03:39,960
If q is not quit or exit, I'm asking the question.

54
00:03:39,970 --> 00:03:45,320
Answer equals ask and get answer of

55
00:03:45,330 --> 00:03:48,560
vector store and q.

56
00:03:48,570 --> 00:03:53,210
And I'm printing the answer.

57
00:03:53,400 --> 00:03:59,310
So, answer colon and the answer in curly braces.

58
00:03:59,320 --> 00:04:03,950
And I'm adding a new line before the answer.

59
00:04:03,960 --> 00:04:06,250
This is for a better readability.

60
00:04:06,260 --> 00:04:09,950
And also 50 dashes after each answer.

61
00:04:09,960 --> 00:04:20,270
Backslash n dash times 50 and another
backslash n at the end.

62
00:04:20,520 --> 00:04:20,810
Ok.

63
00:04:20,820 --> 00:04:22,810
This is just for readability.

64
00:04:22,820 --> 00:04:25,090
For a better readability.

65
00:04:25,100 --> 00:04:27,750
I have loaded the U .S. Constitution.

66
00:04:27,820 --> 00:04:31,910
So, let's ask questions about the U .S. Constitution.

67
00:04:31,920 --> 00:04:35,730
We have in front of us the best expert on

68
00:04:35,740 --> 00:04:38,290
the U .S. Constitution.

69
00:04:38,300 --> 00:04:40,450
I'm running the cell.

70
00:04:40,460 --> 00:04:42,790
The first question.

71
00:04:42,800 --> 00:04:52,570
What is the first amendment of the U .S. Constitution?

72
00:04:52,580 --> 00:04:59,600
Let's see what it will answer.

73
00:04:59,610 --> 00:05:00,880
Very good.

74
00:05:00,890 --> 00:05:03,820
I've got a very good answer.

75
00:05:04,370 --> 00:05:04,820
Ok.

76
00:05:04,830 --> 00:05:06,900
Another question.

77
00:05:06,910 --> 00:05:14,660
Explain the concept of federalism as it

78
00:05:14,670 --> 00:05:27,750
is presented in the U .S. Constitution.

79
00:05:27,760 --> 00:05:30,170
And I've got the answer.

80
00:05:30,180 --> 00:05:33,610
It's about federalism.

81
00:05:33,620 --> 00:05:49,410
Now describe the Bill of Rights.

82
00:05:49,420 --> 00:05:51,250
Very good.

83
00:05:51,260 --> 00:05:53,590
Note that you can send more than one

84
00:05:53,600 --> 00:05:56,750
question to the LLM at the same time.

85
00:05:56,760 --> 00:06:00,010
For example.

86
00:06:01,400 --> 00:06:02,310
Q1.

87
00:06:02,320 --> 00:06:05,650
How does the Constitution address the

88
00:06:05,660 --> 00:06:10,990
issue of presidential succession?

89
00:06:11,000 --> 00:06:13,330
And Q2.

90
00:06:13,340 --> 00:06:16,030
The second question.

91
00:06:16,040 --> 00:06:20,310
Describe the Bill of Rights.

92
00:06:20,320 --> 00:06:24,150
And answer both questions.

93
00:06:24,160 --> 00:06:28,170
And I'm hitting enter.

94
00:06:28,180 --> 00:06:36,820
Depending on how busy the OpenAI's
backend is, you'll wait a few seconds or more.

95
00:06:36,830 --> 00:06:42,470
I've got the answers of both questions.

96
00:06:42,480 --> 00:06:44,570
A1 and A2.

97
00:06:44,580 --> 00:06:51,710
You can get accurate answers from the LLM
on any topic, even if that data wasn't

98
00:06:51,720 --> 00:06:53,690
included in the training.

99
00:06:53,700 --> 00:06:56,830
You can also use other languages.

100
00:06:56,840 --> 00:06:58,270
I'm quitting.

101
00:06:58,280 --> 00:07:01,210
Now I'm loading another document.

102
00:07:01,220 --> 00:07:07,410
This time I'll load a Wikipedia page in
Romanian, my native language.

103
00:07:07,420 --> 00:07:09,970
I use the Pinecone free tier.

104
00:07:09,980 --> 00:07:13,010
And I will delete the existing index.

105
00:07:13,020 --> 00:07:16,250
Because only one index is permitted.

106
00:07:16,440 --> 00:07:18,070
So, delete.

107
00:07:18,080 --> 00:07:20,410
Pinecone index.

108
00:07:20,420 --> 00:07:27,330
And I'm running the code.

109
00:07:27,660 --> 00:07:27,930
Good.

110
00:07:27,940 --> 00:07:30,130
It's deleting all indexes.

111
00:07:30,140 --> 00:07:32,530
And data equals.

112
00:07:32,540 --> 00:07:37,890
Load from Wikipedia.

113
00:07:37,900 --> 00:07:41,110
ChartGPT is the term.

114
00:07:41,120 --> 00:07:44,850
And RO the language.

115
00:07:44,860 --> 00:07:46,870
And chunks equals.

116
00:07:46,880 --> 00:07:48,890
Chunk data.

117
00:07:48,900 --> 00:07:50,430
Of data.

118
00:07:50,440 --> 00:07:54,710
And index name equals.

119
00:07:56,600 --> 00:07:57,450
ChartGPT.

120
00:07:57,460 --> 00:07:59,010
VectorStore equals.

121
00:07:59,020 --> 00:08:01,630
Insert or fetch embeddings.

122
00:08:01,640 --> 00:08:03,410
Of index name.

123
00:08:03,420 --> 00:08:04,190
And that's it.

124
00:08:04,200 --> 00:08:06,610
I'm running it.

125
00:08:06,620 --> 00:08:07,930
It will get the data.

126
00:08:07,940 --> 00:08:11,410
From Wikipedia.

127
00:08:11,420 --> 00:08:12,750
Chunk it.

128
00:08:12,760 --> 00:08:14,550
Create an index.

129
00:08:14,560 --> 00:08:16,130
Embed the chunks.

130
00:08:16,140 --> 00:08:19,610
And insert both the chunks and the

131
00:08:19,620 --> 00:08:27,000
embeddings into a Pinecone index.

132
00:08:27,010 --> 00:08:32,080
It's creating and initializing the new index.

133
00:08:33,250 --> 00:08:34,260
ChartGPT.

134
00:08:34,270 --> 00:08:38,940
I'm preparing the first question in Romanian.

135
00:08:38,950 --> 00:08:40,400
Q equals.

136
00:08:40,410 --> 00:08:45,900
What is ChartGPT?

137
00:08:45,910 --> 00:08:48,320
I'm waiting for it to finish.

138
00:08:48,330 --> 00:08:51,460
And answer.

139
00:08:52,270 --> 00:08:52,620
Equals.

140
00:08:52,630 --> 00:08:56,800
Ask and get answer.

141
00:08:56,810 --> 00:08:59,280
Of VectorStore.

142
00:08:59,290 --> 00:09:00,940
And Q.

143
00:09:00,950 --> 00:09:03,580
And I'm printing the answer.

144
00:09:03,590 --> 00:09:05,040
It's still running.

145
00:09:05,130 --> 00:09:09,180
So, I'm waiting a few more seconds.

146
00:09:09,190 --> 00:09:10,140
It's done.

147
00:09:10,150 --> 00:09:11,420
If you get an error.

148
00:09:11,430 --> 00:09:13,060
You can run the code again.

149
00:09:13,070 --> 00:09:16,760
Because Pinecone is overloaded sometimes.

150
00:09:16,770 --> 00:09:24,220
I'm running this cell.

151
00:09:24,230 --> 00:09:30,600
And I've got a pretty good answer in Romanian.

152
00:09:30,610 --> 00:09:41,700
When was GPT -4 launched?

153
00:09:41,710 --> 00:09:43,420
Very good.

154
00:09:43,430 --> 00:09:45,400
And the last question.

155
00:09:45,410 --> 00:09:49,200
What is InstructGPT?

156
00:09:49,210 --> 00:10:04,640
Of course, I'm writing all questions in Romanian.

157
00:10:04,650 --> 00:10:08,540
And I've got very good answers for each question.

158
00:10:08,550 --> 00:10:11,400
The LLM was able to answer a question

159
00:10:11,410 --> 00:10:15,820
from a new document that was not included
in its training data.

160
00:10:15,830 --> 00:10:20,800
We have a very useful application that
can be used for real businesses.

161
00:10:21,230 --> 00:10:24,280
However, it lacks memory or chart history.

162
00:10:24,290 --> 00:10:25,500
We'll take a break.

163
00:10:25,510 --> 00:10:30,600
And in the next lecture, I'll show you
how to add memory or chart history to

164
00:10:30,610 --> 00:10:31,140
this application.


