A conversational chatbot powered by Google's Gemini API with built-in memory management. This project demonstrates how to create an intelligent chatbot that maintains context across multiple conversations through persistent memory tracking.
MemoryChatBOT is an AI-powered chatbot that leverages Google's Gemini 3.1 Flash Lite model to provide intelligent, context-aware responses. The chatbot maintains a memory of previous interactions, allowing it to provide more personalized and contextually relevant responses over time.
- Gemini API Integration: Uses Google's state-of-the-art Gemini 3.1 Flash Lite language model
- Conversation Memory: Maintains separate memory sections for user and agent interactions
- Context-Aware Responses: System prompt includes memory context for more relevant answers
- Interactive CLI: Simple command-line interface for real-time conversation
- Memory Management: Built-in functions to save and retrieve conversation history
- Easy to Extend: Modular design makes it simple to add new features
GenAI-Local/
├── MemoryChatBOT.ipynb # Main notebook with chatbot implementation
├── helper.py # Helper functions (generated from notebook)
└── README.md # This file
- Python 3.8+
- Google Generative AI SDK
- Google Colab environment (or alternative API key management)
-
Google API Key: Obtain a free API key from Google AI Studio
-
Install Dependencies:
pip install google-genai- Open the notebook in Google Colab
- Set your API key in Colab Secrets:
- Click on the 🔑 icon in the left sidebar
- Add a new secret named
GEMINI_API_KEYwith your API key value
- Run all cells in sequence
If running locally, modify the helper.py file to load the API key from environment variables:
import os
from google import genai
def get_gemini_api_key():
return os.getenv("GEMINI_API_KEY")Then set the environment variable:
export GEMINI_API_KEY="your-api-key-here" # Linux/Mac
set GEMINI_API_KEY=your-api-key-here # Windows- Run all cells in
MemoryChatBOT.ipynb - The chatbot will start with:
Chatbot with memory started. Type 'exit' to stop - Enter your messages when prompted
- Type
exitto stop the chatbot
Chatbot with memory started. Type 'exit' to stop
You: What is machine learning?
Bot: Machine learning is a subset of artificial intelligence...
You: Tell me more about it.
Bot: Building on what I mentioned earlier...
You: exit
1. Memory Structure
agent_memory = {
"human": "", # Stores all user messages
"agent": "" # Stores all bot responses
}2. System Prompt The chatbot uses a system prompt that includes the current memory state:
You are a chatbot. You have a section of your context called [MEMORY] that contains
information relevant to your conversation
3. Agent Step Function
The agent_step() function:
- Takes user input as a parameter
- Combines system prompt with current memory
- Sends to Gemini API for processing
- Updates memory with user message and bot response
- Returns the bot's reply
4. Memory Management Each conversation turn:
- User input is appended to
agent_memory["human"] - Bot response is appended to
agent_memory["agent"] - Memory is included in each API request for context
User Input
↓
agent_step() function
↓
Build prompt with memory context
↓
Send to Gemini API
↓
Receive response
↓
Update memory (human + agent sections)
↓
Return response to user
Current model: gemini-3.1-flash-lite (optimized for speed and low latency)
To change the model, modify this line in the notebook:
model = "gemini-3.1-flash-lite"Available models:
gemini-3.1-flash-lite(recommended for general use)gemini-3.1-flash(more capable)gemini-2.0-pro(most powerful)
Modify the system_prompt variable to change the chatbot's behavior:
system_prompt = "Your custom system prompt here"- 15 requests per minute
- 1,500 requests per day
- Check Google's pricing for details
- Add new memory sections:
agent_memory = {
"human": "",
"agent": "",
"context": "", # New section
"insights": "" # New section
}-
Add tool-calling capability: Modify
agent_step()to parse responses and call external tools -
Implement persistent storage: Save memory to JSON/database for persistence between sessions
- Solution: Ensure your API key is properly set in Colab Secrets or environment variables
- Solution: Run
pip install google-genaiin your environment
- Solution: Wait before making new requests or upgrade to a paid plan
- Add persistent memory storage (JSON/Database)
- Implement tool-calling for external integrations
- Add memory summarization to handle long conversations
- Web interface for easier interaction
- Multi-user support with separate memory stores
- Memory export functionality
- Conversation analytics
This project is open source and available under the MIT License.
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
Created as part of the GenAI-Local project for exploring local GenAI implementations.
For issues, questions, or suggestions, please open an issue in the repository.