1. What Is the Gemini API?
Gemini is Google's family of large language models — the same technology that powers Google's AI features. The Gemini API lets you call these models from your own Python code and build applications on top of them.
You can use it to build chatbots, summarise documents, generate code, analyse images, and much more. The free tier gives you enough API calls to build and test real projects without spending anything.
A terminal-based chatbot that remembers conversation history, responds intelligently, and can be extended into a full web app. All in under 50 lines of Python.
2. Setup — API Key and Library
Step 1: Get a Free API Key
- Go to aistudio.google.com and sign in with your Google account
- Click Get API Key → Create API Key
- Copy the key — it looks like
AIzaSy...
Step 2: Install the Library
pip install google-generativeai
Step 3: Store Your Key Safely
Never hardcode API keys into your code. Use an environment variable or a .env file.
set GEMINI_API_KEY=your_key_here
export GEMINI_API_KEY=your_key_here
3. Your First Response
Let's start with the simplest possible call — send a message, get a reply.
import google.generativeai as genai
import os
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content("What is an ESP32 and what can it do?")
print(response.text)
Run it with python simple_test.py and you'll get a detailed answer. gemini-1.5-flash is the free, fast model — perfect for building and testing.
4. Multi-Turn Chatbot with Memory
A single message is useful, but a real chatbot needs to remember what was said earlier in the conversation. Gemini handles this through a chat session — a running history of messages that gets sent with every new request.
import google.generativeai as genai
import os
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
system_instruction=(
"You are a helpful assistant for engineering students in India. "
"Explain things clearly and use simple examples. "
"When giving code, prefer Python or Arduino."
)
)
chat = model.start_chat(history=[])
print("Chatbot ready. Type 'quit' to exit.\n")
while True:
user_input = input("You: ").strip()
if not user_input:
continue
if user_input.lower() in ("quit", "exit", "bye"):
print("Goodbye!")
break
response = chat.send_message(user_input)
print(f"\nBot: {response.text}\n")
The system_instruction tells the model its personality and context. The chat object stores message history automatically — every message you send includes all previous messages so the model has full context.
Run the chatbot. Ask "What is PWM?" then follow up with "Can you show me Arduino code for that?" — the bot should give code specific to PWM, not a generic example, because it remembers the previous message.
5. Customise with System Instructions
The system instruction is where you define the chatbot's purpose. This is the most important lever you have. Here are three examples:
Engineering Study Assistant
You are a study assistant for B.Tech engineering students in Kerala. Explain every concept with a simple analogy first, then the technical explanation. Always ask if they want a quick quiz at the end.
Arduino Troubleshooter
You are an embedded systems expert who helps students debug Arduino and ESP32 projects. When a student shares code or an error, first identify the most likely cause, then ask one clarifying question before giving a solution.
Resume and Career Coach
You are a career coach helping engineering students in India get internships and jobs. Give specific, actionable advice. When reviewing resumes or emails, be direct about what needs to change.
6. Streaming Responses
For long responses, streaming prints each word as it arrives — just like ChatGPT. This feels much more responsive than waiting for the full answer.
import google.generativeai as genai
import os
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
model = genai.GenerativeModel("gemini-1.5-flash")
chat = model.start_chat()
while True:
msg = input("You: ").strip()
if msg.lower() == "quit":
break
print("Bot: ", end="", flush=True)
response = chat.send_message(msg, stream=True)
for chunk in response:
print(chunk.text, end="", flush=True)
print("\n")
7. What to Build Next
Once you have the core chatbot working, here are natural extensions that make great portfolio projects:
- Flask web app — wrap the chatbot in a simple Flask backend with an HTML frontend
- PDF Q&A — let users upload a document and ask questions about its contents
- Voice chatbot — add Python's speech_recognition library to take spoken input
- Subject-specific tutor — customise the system prompt for a specific course like Electronics or Python
- Telegram bot — deploy using the python-telegram-bot library so anyone can chat via Telegram
Gemini 1.5 Flash free tier gives 15 requests per minute and 1 million tokens per day — more than enough for development and demos. For production apps with real users, you'll need to add billing, but student projects rarely hit these limits.
