Create a Simple AI Chatbot in Minutes with Google Gemini and Streamlit

Manul Thanura
4 min readMay 9, 2024

--

Create a Simple AI Chatbot in Minutes with Gemini and Streamlit

In this article, we’ll explore how to create a basic AI chatbot using Google’s Gemini API and the user-friendly Streamlit framework. This combination allows us to build an interactive chatbot in Python with minimal coding experience.

About the tools

About Gemini

Gemini is a family of multimodal large language models developed by Google DeepMind, serving as the successor to LaMDA and PaLM 2. Comprising Gemini Ultra, Gemini Pro, and Gemini Nano, it was announced on December 6, 2023, positioned as a competitor to OpenAI’s GPT-4. It powers the chatbot of the same name.

About Streamlit

Streamlit is an open-source Python framework for data scientists and AI/ML engineers to deliver dynamic data apps with only a few lines of code. Build and deploy powerful data apps in minutes.

Prerequisites

Let’s go

Step 01 — Set Gemini API

Simplify login to Google AI Studio using your Google account. If you need to know more about Google AI Studio refer Google AI Studio quickstart.

Google AI Studio

Once you’ve logged in successfully, click on the “Get API Key” button at the top left corner or on the model in the above image. Then, you will navigate to the following page.

API keys page
Click API key in a new project
API key generated

Yeah! That’s it with Google AI Studio. Just copy your API key (Or come back if you want it)

Step 02 — Local build

Create a GitHub repo (This is better than creating a folder and pushing it into GitHub) and clone it into your local machine.

git clone <Your URL>

Install the following library and tools, or create requirements.txt and install them in bulk.

pip install google-generativeai

pip install streamlit

requirements.txt (Optional)

streamlit
google-generativeai
python-dotenv
pip install -r requirements. txt

After installation, create a Python file (here, I create main.py)

import streamlit as st
import os
import google.generativeai as genai
# Initialize Gemini-Pro
genai.configure(api_key=st.secrets["GOOGLE_GEMINI_KEY"])
model = genai.GenerativeModel('gemini-pro')

The initialized Gemini-Pro code might be changed according to the use case. Since we use Streamlit, I will directly add the above code. Streamlit provides native file-based secrets management to easily store and securely access your secrets in your Streamlit app.

The full Python file looks like the following (main.py)

import streamlit as st
import os
import google.generativeai as genai

# Initialize Gemini-Pro
genai.configure(api_key=st.secrets["GOOGLE_GEMINI_KEY"])
model = genai.GenerativeModel('gemini-pro')

# Gemini uses 'model' for assistant; Streamlit uses 'assistant'
def role_to_streamlit(role):
if role == "model":
return "assistant"
else:
return role

# Add a Gemini Chat history object to Streamlit session state
if "chat" not in st.session_state:
st.session_state.chat = model.start_chat(history = [])

# Display Form Title
st.set_page_config(page_title="AskME")
st.title("AskME: The Knowledge Well")

# Display chat messages from history above current input box
for message in st.session_state.chat.history:
with st.chat_message(role_to_streamlit(message.role)):
st.markdown(message.parts[0].text)

# Accept user's next message, add to context, resubmit context to Gemini
if prompt := st.chat_input("I possess a well of knowledge. What would you like to know?"):
# Display user's last message
st.chat_message("user").markdown(prompt)

# Send user entry to Gemini and read the response
response = st.session_state.chat.send_message(prompt)

# Display last
with st.chat_message("assistant"):
st.markdown(response.text)

Now push your code into GitHub

Step 03 — Deploy to Streamlit

Log into Streamlit using your account

Log into Streamlit

Then, you will navigate to its dashboard. Click the “New app” button to deploy your Chatbot.

dashboard
Deploy an app

Fill in the above details and connect your GitHub account.

Step 04 — Secrets management

Once you successfully deploy your app it appears in the Streamlit dashboard.

Click the three-dot icon to set your app’s secrets (Gemini API key).

Then, click the settings that appear in the model.

Settings

Navigate to the Secrets tab and insert your Gemini API key in the following format.

Secrets

Now click the URL (Copy from the three-dot icon) and check your app is working properly.

My App

Cheers! Now you have your own chatbot🚀

Furthermore

You can use this technique to build various types of apps to build awesome projects. For your reference, I have included the link to my GitHub repo.

GitHub — https://github.com/manulthanura/askme

WebApp — https://askmebot.streamlit.app

Comment on your experience, share this with the world, and don’t forget to follow me😊

--

--