Why Gemini API?
Google's Gemini API is one of the most accessible ways to build AI-powered applications today. It supports text generation, multi-turn conversations, image understanding, and function calling all through a clean Python SDK. For building chatbots, it's my go-to choice because of its generous free tier, fast response times, and excellent multi-turn conversation handling.
What We're Building
By the end of this guide, you'll have a fully functional chatbot that:
- Maintains conversation history (multi-turn chat)
- Has a custom system prompt defining its personality
- Runs in a clean Streamlit web interface
- Handles errors gracefully
Step 1: Setup and Installation
First, install the required packages and get your API key from Google AI Studio:
# Install dependencies
pip install google-generativeai streamlit
Get your free API key from Google AI Studio (aistudio.google.com). Store it securely never hard-code API keys in your source code.
Step 2: Basic Gemini Chat
Let's start with a simple script that sends a message and gets a response:
import google.generativeai as genai
import os
# Configure with your API key
genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))
# Initialise the model
model = genai.GenerativeModel("gemini-pro")
# Start a chat session
chat = model.start_chat(history=[])
# Send a message
response = chat.send_message("Hello! What can you help me with?")
print(response.text)
# The chat object automatically maintains conversation history
response = chat.send_message("Tell me more about the first thing you mentioned.")
print(response.text)
The key insight: model.start_chat() creates a stateful conversation. Each subsequent send_message() call includes the full conversation history, so the AI remembers context.
Step 3: Adding a System Prompt
System prompts define your chatbot's personality, expertise, and boundaries:
# Define the chatbot's personality
system_prompt = """You are TechBot, a friendly AI assistant created by Aditya Sahani.
You specialise in:
- Python programming and debugging
- Web development (HTML, CSS, JavaScript)
- Data science with Pandas and Streamlit
- Generative AI concepts
Rules:
- Keep responses concise and practical
- Include code examples when relevant
- If you don't know something, say so honestly
- Always be encouraging to beginners"""
model = genai.GenerativeModel(
"gemini-pro",
system_instruction=system_prompt
)
chat = model.start_chat(history=[])
Step 4: Building the Streamlit Interface
Now let's wrap everything in a proper web app:
import streamlit as st
import google.generativeai as genai
import os
st.set_page_config(page_title="TechBot", page_icon="")
st.title(" TechBot")
st.caption("AI assistant by Aditya Sahani powered by Gemini")
# Configure Gemini
genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))
# Initialise chat in session state
if "chat" not in st.session_state:
model = genai.GenerativeModel(
"gemini-pro",
system_instruction="You are TechBot, a friendly Python and AI assistant."
)
st.session_state.chat = model.start_chat(history=[])
st.session_state.messages = []
# Display chat history
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# Chat input
if prompt := st.chat_input("Ask me anything about Python, AI, or web dev..."):
# Show user message
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Get AI response
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
response = st.session_state.chat.send_message(prompt)
st.markdown(response.text)
st.session_state.messages.append({"role": "assistant", "content": response.text})
Step 5: Error Handling
Production chatbots need robust error handling API calls can fail for many reasons:
import google.api_core.exceptions as api_exceptions
try:
response = chat.send_message(user_input)
return response.text
except api_exceptions.ResourceExhausted:
return " Rate limit reached. Please wait a moment and try again."
except api_exceptions.InvalidArgument:
return " Your message couldn't be processed. Please try rephrasing."
except Exception as e:
return f" Something went wrong. Please try again."
Advanced: Adding Conversation Memory Limits
Long conversations consume more tokens and can hit context limits. I always add a sliding window:
# Keep only the last N exchanges to manage context
MAX_HISTORY = 20
if len(st.session_state.messages) > MAX_HISTORY * 2:
# Keep system context + recent messages
st.session_state.messages = st.session_state.messages[-(MAX_HISTORY * 2):]
# Restart chat with trimmed history
model = genai.GenerativeModel("gemini-pro", system_instruction=system_prompt)
history = []
for msg in st.session_state.messages:
role = "user" if msg["role"] == "user" else "model"
history.append({"role": role, "parts": [msg["content"]]})
st.session_state.chat = model.start_chat(history=history)
What I Learned Building Chatbots
- System prompts matter enormously A few well-crafted sentences completely change the chatbot's personality and usefulness
- Test edge cases What happens when users send empty messages, extremely long messages, or try to break the bot?
- Streaming makes it feel alive Use
stream=Trueinsend_message()for a typing effect instead of waiting for the full response - Don't expose your API key Use environment variables or Streamlit secrets, never commit keys to Git
- Add a clear chat button Users need a way to start fresh without refreshing the page
The Bottom Line
Building a chatbot with Gemini API is surprisingly straightforward. The hard part isn't the code it's designing the right system prompt and handling edge cases gracefully. Start simple, test thoroughly, and iterate based on real user behaviour.
I build intelligent chatbots for businesses using Gemini API and Dialogflow.
Get in Touch '