How to Integrate Google Gemini AI into Your Angular App [Step-by-Step Guide]
Introduction
Integrating AI-powered features into modern web applications has become important to deliver smarter, more responsive user experiences. This project demonstrates how to integrate Google Gemini AI with an Angular 18 application, containing features such as dynamic chat conversations, text and contextually aware responses. Gemini is a multimodal AI model that generates text, as well as other types of information like code.
Prerequisites
Node.js v20.19.2
Angular cli v18.2.20
Before we begin, ensure Node.js and Angular cli is installed.
Step 1: Create a new angular application
Run the following command to generate a new Angular app:
ng new ai-powered-app
cd ai-powered-app
Note: During setup, choose the default configuration (no routing, CSS for styling, unless otherwise needed).
After creation serve the project using command
npm start or ng serve
Step 2: Remove the Default App Content
By default, Angular includes sample HTML in the AppComponent. To prepare for a routed layout:
Open app.component.ts.
Replace the existing template code with the following:
<router-outlet></router-outlet>
This enables Angular’s router to display components based on the current route.
Step 3: Generate Project Components
Use Angular CLI commands to scaffold the necessary components. Open a terminal in your project directory and run the following:
Run ‘ng g c components/layout’
Run ‘ng g c components/sidebar’
Run ‘ng g c components/bubble’
These components will be used to structure your application page and layout.
LayoutComponent is created as a parent component for chats and will contain BubbleComponent and SidebarComponent as child component.
Step 4: Configure Application Routing
Update your app.routes.ts file to define application routes. This includes a default redirect and a route for the layout component.
Angular Material UI is added to enhance UI components in chat bot. In the process run the command to install angular material. This will find the compatible version of Angular Material for Angular v18.2.20
Run ‘ng add @angular/material’
Next, choose any of the themes for example the Azure/Blue theme, also include animations and typography.
After this restart the app once to update angular.json and to add Bootstrap v5.3 using cdn links add these tags to attach css and javascript bundle files in index.html.
Now let’s start working on the layout design of the chatbot first. On the left side the sidebar will be created and the chat section on the right side.
A component selector of the sidebar component is added in the layout component, shown with a condition of opening of sidebar depending on the innerwidth of the window as it will be hidden if inner width is less than 992px. Here 992px will be the breakpoint to create a responsive screen. That's why we have added styles for left and right sections dynamically based on screen width.
Also, Event emitters are used to open and close the sidebar which is called from the sidebar component as shown below. The flag for showing sidebar is passed to sidebar.component.
The list section in the sidebar component is created for a list of history of chat conversations and a bottom section for new chat features and name of user.
Following functions are added in class LayoutComponent and add NgStyle and class SidebarComponent to imports array. Also, import HostListener to check the resizing of the window. Also, added a check for window width in ngOnInit to show the sidebar.
The toggleSidebar function is created to open and close the sidebar.
The leftStyle function is created for styling of sidebar in case of screen width less than or more than 992px and is sidebar open or not.
The rightStyle function is created for styling of the chat section in case of screen width less than or more than 992px and is sidebar open or not.
After creating the sidebar, let's focus on the chats section on the right side in layout.component.ts file.
Additions in layout.component.html
Add these divs in the chat-section.
First is the header section, containing a button for opening the sidebar and search bar for searching the chat messages.
Next in the chat section, the component selector of bubble component is added to pass the chat message and a spinner is added to show in waiting for response from gemini.
At Last, a prompt section is added to write text or upload images in a prompt to send to gemini.
Add MatIconModule, MatCardModule, BubbleComponent, MatButtonModule in imports array and a function to provide search bar width according to screen width.
Add MatCardModule, MatIconModule, MatButtonModule in imports array. The Updated design looks like
Step 10: Installation and Setup
To integrate gemini into the angular app run this command to install @google/genai
Run ‘npm install @google/genai’
Create environments using command
Run ‘ng g environments’
Now add googleAiApiKey in the environments file. To get this googleAiApiKey create a google account and open https://aistudio.google.com/apikey this link and create API key.
Now, to send the prompt message and get a response from gemini, first set up a generate-result service to create functions for sending prompt and receiving responses.
Create the following functions in in generate-result.service.ts
The sendQuestion is created to send a prompt to gemini and will get a response using result$.
Create an instance of this service, messages array to store chat messages, a variable for prompt question, file and filename to store file data url and file’s name.
On clicking the image icon, the onFileSelection function is executed, the file is uploaded and read as a data url. The send button is only enabled if a prompt is written.
The sendPrompt function is called to send the prompt to gemini and get the response in the constructor. Adding the prompt and response in messages array. And Scrolling to bottom after getting the response.
Add the variable promptQuestion to the input box using ngModel and call function sendPrompt to submit the prompt. The onFileSelection function added on change of file input.
To show the prompt and response in chat, let’s add the bubble component selector in for loop on messages array, showing the message sender icons depending upon the message sender in the array. Also, making the spinner work when we are waiting for a response from Gemini.
To create new chat feature and chat history, add a chat index in generate-result.service.ts
chatIndex:number = -1
Modifications in layout.component.ts
Add a history array to store history of chat conversations and to be stored in local storage. Get the previous history stored in localstorage in ngOnInit.
Update pushToMessage function to get the history array updated on every prompt and response and update the index of chat history on which user is working. This will be used by the openChat function to open a particular chat.
The createNewChat function assigns an empty array to messages to store new chat and changes the chatIndex to -1. The createNewChat function and openChat function will be called from the sidebar function using the event emitter.
Search functionality is used to search any of the chat conversations.
Modifications in layout.component.html
To create search functionality do the following changes in the header section, add a searchText variable in the input box and function search to search button.
Create the functions search which searches the text from the array and scrolls to that particular message.
searchText: any = ''
search() {
if (this.messages.length > 0) {
let filteredArray = this.messages.filter((x: any, i) => {
x['index'] = i
return x.message.toLowerCase().includes(this.searchText.trim())
})
if (filteredArray.length > 0) {
let obj: any = filteredArray[0]
let ele: any = document.getElementById('id_' + obj.index)
if (!!ele) {
ele.scrollIntoView({ behavior: 'smooth', block: 'start' })
}
}
}
else {
this.searchText = ""
}
}
The final design after functionality implementation looks like:
Conclusion :
Integrating Google Gemini AI into and using angular’s 18 architecture, adding RxJS patterns like switchMap and shareReplay, ensures data handling, while Angular Material and Bootstrap provides better UX and responsive UI. Using best ways possible in functionality and design, AI-Powered apps are created for upcoming innovations.
Get in touch with Prishusoft – your trusted partner for custom software development. Whether you need a powerful web application or a sleek mobile app, our expert team is here to turn your ideas into reality.