Hi.
In this lesson you will learn how to connect Google Assistant to your chatbot.
So let's see the documentation.
You can find the link in the resources for this lesson.
And here what we will do step by step.
First we will install Google AI library.
It's an npm package.
Then we will import the Google generative AI.
And we create an instance of this generative AI and provide an API key.
Then we create an instance of the model and provide the model name as Gamini.
And then we create a chat as a history.
We will pass an empty array because we won't have any history at the beginning.
And then we will store the result of a AI response into a result variable.
And to get it we will call chat dot, send message and provide a content here.
And also we will use await operator because all API calls are asynchronous.
And this statement help us to wait till the API call will be finished.
So let's switch to Visual Studio Code and implement it.
First I will stop the server.
I will use Ctrl C and I will install the library.
I also use the save flag and flag to see the updated packages in the package.json file.
It's a file where we can see all our dependencies and not only okay, it's installed.
And now let's open app.js file and let's import the Google generative AI class.
And now we create a instance of this class Google AI equal new.
And we will provide an environment variable.
As you remember from the previous lesson we will use usAt import dot meta dot environment.
And then we pass the name of our variable.
I will copy paste it here.
This is a way how we can access environment variables using white.
Yep.
Okay.
Now let's create a um Gamini uh model.
So I will use Google I and uh, please generate me model.
And as a name I will provide the.
Gamini 1.5 slash just B string.
Okay.
And now I will create the chat instance.
I will use Gamini and the method start chat and provide history as an empty array.
Okay, great.
Uh, now we need to call this That instance inside of the handle content sent because we first saved
the user message the request.
And now we need to send this request to the, um, Google Gemini.
I recommend you for all API requests use try catch uh, construction uh intersection.
We will try to make API request if it will be succeed.
It's fine if everything works, but if it if it fail, uh, you will catch errors here in this section,
and your application won't broke.
So let's, uh, store the result into a result variable, and I will call chat, send message and pass
the content.
Also this is asynchronous operation.
So we will use await statement.
And it always used in pair with async as a function prefix.
Uh.
Okay.
What is wrong with expression?
Only allowed with an async.
Ah, sorry, I pasted async to the wrong to the wrong function, but now it's fine.
Okay, now we need to store a result.
We will use the same way as we did here to pass a new content to the messages array, but I suggest
don't duplicate the code and create a new method which we call add message and we will provide here
a message.
As a message, we will provide the content together with row.
So and I copy paste it here.
And instead of the content and the row object I pass message.
And now we can easier to add new messages with less code.
So anything we will do here we will pass a pro as an Assistant.
And as the content we will pass.
Let's check how to get the content.
Result response dot text.
Okay, a result response dot text and it is an A function.
I will call it.
Okay.
And in case of any errors we need to display something to user that sorry it doesn't work.
Something bad happened.
Uh.
And let's provide it.
Uh, let's pass through a system.
Might be later.
Will style provide a different style for these messages?
And let's paste message something like, um.
Sorry.
Um, I couldn't, uh, process your request.
Uh, please try again.
Okay.
So nice.
And looks finished.
Yeah.
Let's now switch to our application.
We of course need to run it and run.
Dev.
Uh, let's type some message.
Um, okay.
Um, do you remember I promised you to fix the color of the text?
It happens because I'm using the dark mode right now.
And please comment this line, but don't remove it because we will need it later when we will add some
feature to our chatbot to look nicer.
And now we see.
We see, um, all the text.
Okay, uh, let's say hello.
How you can assist assist me.
And let's wait a bit and we get a response from generative AI.
So congratulations.
Um, and see you in the next lesson.