Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
AI ✦ Beginner ⏱ 30 minutes

✨ Build an AI Chatbot with Google Gemini API in Python

Build a free AI chatbot using Google's Gemini API — no credit card needed, generous free tier, and surprisingly powerful out of the box.

🎯 What You'll Build

A Python chatbot powered by Google Gemini that holds real conversations, remembers context, and runs on Google's free API tier — no payment required to get started.

📋 What You'll Need

Gemini is Google's flagship AI model — and unlike OpenAI, the API has a generous free tier that's perfect for learning, side projects, and small apps. No credit card, no billing setup. Let's build a chatbot.

1

Set up your project

Create a folder called gemini-chatbot. Open it in VS Code. Create a new file chatbot.py. Open a terminal (Ctrl + ` ) and verify Python:

python --version

You should see Python 3.8 or higher.

2

Install the Gemini SDK

Google provides an official Python SDK called google-generativeai. Install it:

pip install google-generativeai

This is the only library you need — it handles all the API communication.

3

Get your free Gemini API key

This is where Gemini beats OpenAI — completely free, no credit card required.

  1. Go to aistudio.google.com
  2. Sign in with your Google account
  3. Click "Get API key" on the left sidebar
  4. Click "Create API key""Create in new project"
  5. Copy the key (starts with AIza...)
💡 Free tier: Gemini's free tier gives you 15 requests per minute and millions of tokens per day — more than enough for learning and small projects.
4

Make your first Gemini request

Open chatbot.py and add this code. Replace YOUR_API_KEY with your real key:

chatbot.py
import google.generativeai as genai

genai.configure(api_key='YOUR_API_KEY')

model = genai.GenerativeModel('gemini-2.0-flash')

response = model.generate_content('Hello! What can you do?')
print(response.text)

Run it: python chatbot.py. You should see a Gemini response. You just used a state-of-the-art AI model for free!

5

Create a chat session with memory

For a real chatbot we need memory. Gemini's start_chat() method makes this easy — it automatically tracks all messages in the conversation:

chatbot.py
import google.generativeai as genai

genai.configure(api_key='YOUR_API_KEY')
model = genai.GenerativeModel('gemini-2.0-flash')
chat = model.start_chat(history=[])

print('✨ Gemini Chatbot — type "quit" to exit\n')

while True:
    user_input = input('You: ')
    if user_input.lower() == 'quit':
        break

    response = chat.send_message(user_input)
    print(f'Gemini: {response.text}\n')

Try asking "What's 25 × 8?" then "Now multiply that by 3" — Gemini will remember the first answer.

6

Add a custom personality

Give your bot a persona using system_instruction:

model = genai.GenerativeModel(
    'gemini-2.0-flash',
    system_instruction='You are a witty Python tutor. Explain concepts simply with humor and code examples.'
)

Now your chatbot has personality. Change the instruction to make it act as a chef, philosopher, or anything you can imagine.

7

Stream responses (like ChatGPT)

For a more engaging feel, stream responses word-by-word using stream=True:

response = chat.send_message(user_input, stream=True)
print('Gemini: ', end='', flush=True)
for chunk in response:
    print(chunk.text, end='', flush=True)
print('\n')

Now your chatbot responds in real-time, just like ChatGPT. Much more polished feel.

🎉

You built a Gemini chatbot!

You now know how to use Google's flagship AI for free, with conversation memory, custom personalities, and streaming responses. The same Gemini that powers Google's products is now in your code.

🚀 Take It Further

← All Tutorials

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.