In this tutorial, we are going to find out step by step how to build an assistant that you can chat face to face via text or voice message. Using React Native and one of the most popular language models today is Gemini AI.
Prerequisites:
- Node.js and npm: Ensure that Node.js and npm are installed on your machine. Otherwise, you can follow here downloading-and-installing-node-js-and-npm
- Code editor: You can use any editor you love but in this tutorial, I use Visual Studio Code
- Gemini AI key: Obtain your Gemini AI key by signing up at https://ai.google.dev/
Create a new React Native project
Let’s begin by creating a new project with React Native using the following command:
1 | npx react-native init ReactNativeVoiceAssistant |
To set up your environment, run, and build a React Native project, you can follow the instructions outlined in the React Native Get Started guide.
Installing dependencies
Next, we will install some packages for this project. Run the following commands one after another:
1 | // for interaction with Rest API |
I will not explain this detail here, you can find more documentation on their official sites.
Implementing
Creating folders structure
In the root, create some folders: src/screens
, src/navigations
, src/services
and src/styles
. The screens
folder contain all code related to Welcome Screen and ChatScreen, navigations
folder for code related to navigation between screens, services
folder for code related to logic accessing the rest API, styles
for stylesheets.
Creating service for accessing the Gemini AI rest API
First, create a .env
file in the root folder and store your API key here. Please remember not to publish your API key.
1 | API_KEY=YourApiKeyHere |
Next, create the file src/services/AiService.js
. The following code will read variables from .env
and call the rest API via Axios.
1 | import { API_KEY, API_URL } from "@env"; |
Creating WelcomeScreen
We need to create a src/screens/WelcomeScreen.tsx
file for logic code and a src/styles/WelcomeStyle.ts
file for the stylesheet.
1 | // Import necessary components and libraries |
1 | import { StyleSheet } from "react-native"; |
Creating ChatScreen
We also create two files the same as WelcomeScreen. Put the following code to src/screens/ChatScreen.tsx
file:
1 | // Import necessary components and libraries |
This code put into src/styles/ChatStyle.ts
1 | import { StyleSheet } from "react-native"; |
Creating navigation
Create a src/navigations/AppNavigation.tsx
file to handle navigation between screens in the application.
1 | import { NavigationContainer } from "@react-navigation/native"; |
Next, we include the AppNavigation component in App.tsx
file by replacing content with the following code.
1 | import React from "react"; |
Run your application
Run on ios:
1 | npx react-native run-ios |
Run on android:
1 | npx react-native run-android |
Demo
Click on the following video to watch a demo talk with AI