...

/

Challenge: Creating a Chatbot with Gemini

Challenge: Creating a Chatbot with Gemini

Learn how to create a Gemini-powered chatbot using Streamlit.

The scenario

You are a novice, new to the field of generative AI, burdened by the industry to perform miracles in this newly flourishing field. You’re unsure how to proceed. There is too much to do and too little time. You panic. Breathe. We have you covered.

OpenAI’s chatbot, ChatGPT, reached 1 million users in just 5 days and soared to 100 million users in just about 2 months.

Given how chatbots are the hype all around, let’s create our very own chatbot powered by Gemini.

The requirements

As the old saying goes, measure twice, cut once. Before we get started, let’s set our expectations and requirements.

  1. The chatbot must use Gemini as its service.

  2. The chatbot should have an easy-to-use frontend that users can interact with.

  3. Since we know basic Python, it should be built using Python.

  4. Bonus: The chatbot will always give you witty and funny replies!

This seems simple enough, but how do we get started? Let’s choose the basic building block of our application first. Streamlit has been gaining traction in the AI community as an easy-to-use framework to build data applications quickly and easily using Python. Since front-end development might not be our forte, let’s proceed with Streamlit as our base.

Setting up a chat

The Gemini API offers a chat feature that allows us to build applications involving conversations with the model. These include a series of questions and answers with the Gemini model. This is well-suited for chatbots or support assistants. Let’s see it in action:

Press + to interact
Please provide values for the following:
API_KEY
Not Specified...
import google.generativeai as genai
API_KEY = "{{API_KEY}}"
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel("gemini-1.5-flash")
chat = model.start_chat(history=[])
message = "In 50 words, tell me about streamlit"
response = chat.send_message(message)
print(response.text)

The code might appear to be very similar to what we have been using so far. Let’s review the changes:

  • Line 7: We create a ChatSession ...