How to build a chatbot in 5 minutes with Gemini API

Roaming Roadster
3 min readMar 15, 2024

The world of chatbots is booming, offering engaging and informative interactions across various platforms. In this tutorial, I will show you how to build a chatbot with Google Gemini API in the fastest and easiest way.

Environment Setup

Before we start, we need to set up the environment. In this article, I will use python as the programming language because a lot of machine learning software and libraries and written in python. If you want to use other programming languages, you can check out the official Google Gemini docs.

  1. create python virtual environment

It is a good practice to create a virtual environment before working on a python project. In this way, you don’t need to fix up dependencies with other projects.

python3 -m venv .venv
source .venv/bin/activate

2. Install Dependencies

  • Install the Python SDK for the Gemini API (contained in the google-generativeai package) using pip

pip install -q -U google-generativeai

Obtain an API Key

You need an API key to access the Gemini API. Go to MakerSuite at Google and register an account to get a free Gemini API key. Clicked on Get API Key on the left panel to generate a key. You can pass the API key as a envionrment variable to your chatbot.

Build Your Chatbot in Python

  1. We’ll need libraries like generative-ai to interact with the API.
import google.generativeai as genai

2. Initialize the Gemini API with your API key.

GOOGLE_API_KEY=os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)

Then you can pass your API key to your bot:

GOOGLE_API_KEY=<YOUR_KEY> python gemini_bot.py

3. Interact with the Gemini model to generate responses.

Gemini offers various models, such as gemini-pro or gemini-pro-vision.

  • gemini-pro: optimized for text-only prompts.
  • gemini-pro-vision: optimized for text-and-images prompts.

You can see the available models by

for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
print(m.name)

Here are the models available currently:

models/gemini-1.0-pro
models/gemini-1.0-pro-001
models/gemini-1.0-pro-latest
models/gemini-1.0-pro-vision-latest
models/gemini-pro
models/gemini-pro-vision

In this example, we will choose gemini-pro :

model = genai.GenerativeModel('gemini-pro')

4. Create a chat session

Now let’s start a chat and see the response:

chat = model.start_chat(history=[])
response = chat.send_message(prompt, stream=True)
for chunk in response:
if chunk.text:
print(chunk.text)

With stream=True , we can process the response in chunks and see the streaming results.

Now let’s ask users to input anything to make it interactive:

chat = model.start_chat(history=[])

while True:
prompt = input("Ask me anything: ")
if (prompt == "exit"):
break
response = chat.send_message(prompt, stream=True)
for chunk in response:
if chunk.text:
print(chunk.text)

The chat stops when the input is exit , otherwise it can go forever.

The complete code looks like this:

import os
import google.generativeai as genai


GOOGLE_API_KEY=os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)

for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
print(m.name)

model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat(history=[])

while True:
prompt = input("Ask me anything: ")
if (prompt == "exit"):
break
response = chat.send_message(prompt, stream=True)
for chunk in response:
if chunk.text:
print(chunk.text)

Example of output:

$ GOOGLE_API_KEY=<MY_API_KEY> python gemini_bot.py
models/gemini-1.0-pro
models/gemini-1.0-pro-001
models/gemini-1.0-pro-latest
models/gemini-1.0-pro-vision-latest
models/gemini-pro
models/gemini-pro-vision
Ask me anything: hi
Hello. How are you doing today?
Ask me anything: How is the weather today?
The weather today is mostly sunny, with a high near 55 degrees Fahrenheit
and a low near 38 degrees Fahrenheit. There is a 10% chance of rain.
Ask me anything:

See how easy to build a chatbot!? With only a few lines of code, you build a powerful interactive chatbot! By leveraging the Google Gemini API, you can construct a powerful and engaging chatbot, adding a valuable tool to your development arsenal. So, dive in, explore the possibilities, and craft a chatbot that stands out from the crowd!
Congratulations on your first chatbot!

--

--