1
00:00:00,630 --> 00:00:04,650
Hey, in the previous video, we built this guy, which has three widgets.

2
00:00:04,860 --> 00:00:07,400
This one here, this one here and this one here.

3
00:00:07,410 --> 00:00:10,170
So this was the front end, basically.

4
00:00:10,440 --> 00:00:13,170
Now we're ready to build the back end.

5
00:00:14,420 --> 00:00:19,370
So the front end was built by this class class chat bot to window write.

6
00:00:19,370 --> 00:00:21,170
We wrote this class in the previous video.

7
00:00:21,170 --> 00:00:22,210
Let me fold it.

8
00:00:22,220 --> 00:00:24,260
I'm not deleting it, just folding it.

9
00:00:24,800 --> 00:00:28,550
So I have a better view here in the script.

10
00:00:28,850 --> 00:00:32,870
Now the back end will be in this class.

11
00:00:33,080 --> 00:00:38,030
And before we begin, I think it makes sense that we refactor this class.

12
00:00:38,030 --> 00:00:39,350
Or right click here.

13
00:00:39,350 --> 00:00:45,560
I'll select the class, right click, go to refactor and then go to move.

14
00:00:46,460 --> 00:00:51,080
So I want to move this class to Bec and that PE basically.

15
00:00:53,400 --> 00:00:54,450
Add to it.

16
00:00:54,960 --> 00:00:58,140
So what I've just done is I deleted the class from here.

17
00:00:58,140 --> 00:01:01,080
You could do that manually as well, if you like.

18
00:01:01,920 --> 00:01:08,160
So if you do it manually, you would have to delete the class from here or cut it and paste it in this

19
00:01:08,160 --> 00:01:09,480
new file back.

20
00:01:09,480 --> 00:01:11,670
And that's P right.

21
00:01:12,640 --> 00:01:13,810
Well, that's what I did.

22
00:01:14,050 --> 00:01:18,230
So I use the refactor function in part to do this.

23
00:01:18,250 --> 00:01:22,690
This makes more sense because this is the code that builds the gooey.

24
00:01:22,750 --> 00:01:24,670
And this does the processing.

25
00:01:24,670 --> 00:01:27,070
So it makes sense to have them separate.

26
00:01:27,190 --> 00:01:33,130
So in this code now, we want to interact with the Open I API.

27
00:01:34,060 --> 00:01:40,000
The interaction happens with the Open I library, which is something you need to install.

28
00:01:40,000 --> 00:01:42,220
So install open eye.

29
00:01:42,520 --> 00:01:46,300
Once this is installed, then you need an API key.

30
00:01:47,650 --> 00:01:55,960
So for that you need to go to google, search for open ie API and you should go to open i dot com slash

31
00:01:55,960 --> 00:01:56,650
API.

32
00:01:58,210 --> 00:02:05,020
So this is the official website of Open AI, an organization that develops AI tools.

33
00:02:05,050 --> 00:02:07,750
So, Chad, Djibouti is one of those tools.

34
00:02:07,750 --> 00:02:13,540
And we will use The Da Vinci three model, which is a very advanced algorithm, and it's very similar

35
00:02:13,540 --> 00:02:15,640
to the Chad Djibouti algorithm.

36
00:02:16,000 --> 00:02:19,780
To be able to use the API, you need to sign up first for an account.

37
00:02:19,780 --> 00:02:23,800
It's free, and then go to log in to log in to that account.

38
00:02:24,370 --> 00:02:27,850
And so once you log in, you should see this page.

39
00:02:27,850 --> 00:02:31,390
So this is a platform that open A.I. dot com.

40
00:02:32,470 --> 00:02:34,840
So that's the overview page.

41
00:02:34,840 --> 00:02:39,580
And now you need to get the API for the chat bots.

42
00:02:39,580 --> 00:02:44,130
So you want to go to this personal and then you're going to go to view API keys.

43
00:02:44,140 --> 00:02:51,310
Here you'll see a list of API keys you have created and to create a new one, you go to create a new

44
00:02:51,310 --> 00:02:53,680
secret key and copy that.

45
00:02:54,100 --> 00:02:56,020
So that's your new key.

46
00:02:56,020 --> 00:03:07,600
Go to Python and then create a define init methods and to open AI dot.

47
00:03:08,670 --> 00:03:15,270
API underscore key is equal to to double quotes and paste the API key there.

48
00:03:15,480 --> 00:03:22,350
So open AI is the module we imported and API key is an attribute of that module.

49
00:03:22,350 --> 00:03:25,290
So we declare the API key in here.

50
00:03:26,070 --> 00:03:28,620
That's all we have to do in the init function.

51
00:03:28,620 --> 00:03:37,140
Then we want to create a GetResponse method which will be responsible for getting a response from the

52
00:03:37,140 --> 00:03:37,920
API.

53
00:03:38,700 --> 00:03:41,430
So let's create a response variable.

54
00:03:42,930 --> 00:03:53,550
To get a response, we'll do open AI, dot completion, dot create and inside this parentheses.

55
00:03:53,550 --> 00:04:00,120
Now this will be a long list of parameters that this create method gets.

56
00:04:01,140 --> 00:04:04,450
The first argument is the engine.

57
00:04:04,470 --> 00:04:06,420
This comes as a string.

58
00:04:08,310 --> 00:04:12,690
And this is the AI model we will use.

59
00:04:13,620 --> 00:04:15,030
Text Da Vinci.

60
00:04:15,920 --> 00:04:17,149
003.

61
00:04:17,149 --> 00:04:21,560
So text minus the Vinci, -0 zero three, a comma.

62
00:04:23,380 --> 00:04:26,180
Prompt is the second argument.

63
00:04:26,200 --> 00:04:27,910
This will be the user input.

64
00:04:29,970 --> 00:04:30,390
Right.

65
00:04:30,540 --> 00:04:37,350
And let's have this here as a parameter user input.

66
00:04:38,190 --> 00:04:48,000
So our other class, this class will call this chat bot class and the user will enter something in the

67
00:04:48,000 --> 00:04:51,930
text input field here in this class.

68
00:04:51,960 --> 00:04:52,690
Right?

69
00:04:52,710 --> 00:05:00,090
So the user will enter something in here and that string is going to be sent to this method.

70
00:05:00,570 --> 00:05:08,580
So similar in this code here in our guy, we will call this get response method method by providing

71
00:05:08,580 --> 00:05:12,930
the user string as the value of the user input variable.

72
00:05:12,930 --> 00:05:16,410
And so that value will be sent in the prompt.

73
00:05:18,030 --> 00:05:19,050
Then we need to.

74
00:05:21,250 --> 00:05:25,890
Provide a max tokens arguments set it to 4000.

75
00:05:25,900 --> 00:05:26,700
What is this?

76
00:05:26,710 --> 00:05:37,000
Well, if you want the bot to provide longer answers, you can set a maximum of 4080 tokens or tokens

77
00:05:37,000 --> 00:05:37,990
are words.

78
00:05:38,350 --> 00:05:43,750
So this means that the boat cannot provide more than 40 tokens.

79
00:05:44,170 --> 00:05:49,470
If you want to provide shorter answers, perhaps you can set it to 2000.

80
00:05:49,480 --> 00:05:52,390
So I would go for 3000.

81
00:05:52,420 --> 00:05:54,940
That would be a good value comma.

82
00:05:54,940 --> 00:06:00,550
And lastly, the temperature, I'll suggest a value of 0.5.

83
00:06:00,550 --> 00:06:04,900
You can set the value from 0 to 1 if you are closer to zero.

84
00:06:04,900 --> 00:06:12,190
So if you are giving a low temperature, that will produce more accurate answers, but they will be

85
00:06:12,190 --> 00:06:13,810
less diverse.

86
00:06:14,050 --> 00:06:22,000
So more rigid, if you provide a higher temperature, you will sacrifice a bit on accuracy, but the

87
00:06:22,000 --> 00:06:25,510
answers will be more diverse, if that makes sense.

88
00:06:25,810 --> 00:06:28,540
So 0.5 is a good value to have.

89
00:06:30,710 --> 00:06:33,570
So that's the create methods.

90
00:06:33,590 --> 00:06:34,850
It opens in here.

91
00:06:34,850 --> 00:06:35,960
It closes in here.

92
00:06:35,960 --> 00:06:40,290
And then after this, you need to extract the choices.

93
00:06:40,310 --> 00:06:41,560
This will give you a list.

94
00:06:41,570 --> 00:06:47,840
You get the first item of that list, and you extract the text out of that the text property and then

95
00:06:47,840 --> 00:06:49,850
return the response.

96
00:06:50,210 --> 00:06:51,050
And that's it.

97
00:06:51,050 --> 00:06:52,550
That's the complete class.

98
00:06:52,550 --> 00:06:54,020
So let's try it now.

99
00:06:54,290 --> 00:06:55,490
I'll go and do.

100
00:06:55,490 --> 00:07:06,050
If name is equal to main, instantiate the chat bot.

101
00:07:06,080 --> 00:07:09,410
It doesn't take any arguments, so just parentheses there.

102
00:07:11,360 --> 00:07:13,730
The response will be equal to chat bots.

103
00:07:13,730 --> 00:07:21,650
Dot get re spawns and get response will get an argument that is the user inputs.

104
00:07:21,650 --> 00:07:30,920
So for example, write a joke about birds and I'll print out the response here.

105
00:07:33,260 --> 00:07:41,990
Of course later on it is the chat bot window which will call this chat bot class and it will call that

106
00:07:41,990 --> 00:07:43,220
gets response methods.

107
00:07:43,220 --> 00:07:48,350
But for now, let's keep it here so we make sure that our chat bot class is working fine.

108
00:07:49,250 --> 00:07:52,640
So I just ran this backends py file.

109
00:07:52,940 --> 00:07:54,350
I've got an attribute error.

110
00:07:54,350 --> 00:07:56,420
Attribute error choice.

111
00:07:56,420 --> 00:08:01,010
So choice is not an attribute of its object.

112
00:08:01,010 --> 00:08:02,720
So actually it's choices.

113
00:08:03,830 --> 00:08:05,150
So let's run again.

114
00:08:06,950 --> 00:08:07,540
There we go.

115
00:08:07,550 --> 00:08:10,400
That's the response of the chat board.

116
00:08:12,020 --> 00:08:15,700
So the chat bot gave a joke in the form of a Q&A.

117
00:08:15,710 --> 00:08:19,910
So there's a question, Oh, what did the bird say when it flew into the world?

118
00:08:20,870 --> 00:08:23,320
Oh, I shouldn't have flown straight.

119
00:08:23,330 --> 00:08:24,430
That's the joke.

120
00:08:24,440 --> 00:08:26,060
So it seems to be working fine.

121
00:08:29,720 --> 00:08:33,230
That's how you create a chat bot with open AI.

122
00:08:33,890 --> 00:08:41,659
In the next video, we'll go ahead and connect this backend file with the main dot p file, So we connect

123
00:08:41,659 --> 00:08:48,560
the chat bot with the chat bot window so that our chat bot window will function like a real chat bot.

124
00:08:48,890 --> 00:08:49,490
Thanks.

125
00:08:49,490 --> 00:08:50,570
I'll see you in the next video.

