Skip to content

Utkarsha88/memory_chatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

MemoryChatBOT

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.

Overview

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.

Features

  • 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

Project Structure

GenAI-Local/
├── MemoryChatBOT.ipynb       # Main notebook with chatbot implementation
├── helper.py                  # Helper functions (generated from notebook)
└── README.md                  # This file

Requirements

  • Python 3.8+
  • Google Generative AI SDK
  • Google Colab environment (or alternative API key management)

Installation

Prerequisites

  1. Google API Key: Obtain a free API key from Google AI Studio

  2. Install Dependencies:

pip install google-genai

Setup in Google Colab

  1. Open the notebook in Google Colab
  2. Set your API key in Colab Secrets:
    • Click on the 🔑 icon in the left sidebar
    • Add a new secret named GEMINI_API_KEY with your API key value
  3. Run all cells in sequence

Local Setup

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

Usage

Running in Jupyter/Colab

  1. Run all cells in MemoryChatBOT.ipynb
  2. The chatbot will start with: Chatbot with memory started. Type 'exit' to stop
  3. Enter your messages when prompted
  4. Type exit to stop the chatbot

Basic Example

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

How It Works

Core Components

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

Architecture

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

Configuration

Model Selection

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)

System Prompt Customization

Modify the system_prompt variable to change the chatbot's behavior:

system_prompt = "Your custom system prompt here"

API Usage

Free Tier Limits

  • 15 requests per minute
  • 1,500 requests per day
  • Check Google's pricing for details

Development

To extend the chatbot:

  1. Add new memory sections:
agent_memory = {
    "human": "",
    "agent": "",
    "context": "",    # New section
    "insights": ""    # New section
}
  1. Add tool-calling capability: Modify agent_step() to parse responses and call external tools

  2. Implement persistent storage: Save memory to JSON/database for persistence between sessions

Troubleshooting

Issue: GEMINI_API_KEY not found

  • Solution: Ensure your API key is properly set in Colab Secrets or environment variables

Issue: ImportError: No module named 'google'

  • Solution: Run pip install google-genai in your environment

Issue: Rate limit exceeded

  • Solution: Wait before making new requests or upgrade to a paid plan

Future Enhancements

  • 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

License

This project is open source and available under the MIT License.

Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests

References

Author

Created as part of the GenAI-Local project for exploring local GenAI implementations.

Support

For issues, questions, or suggestions, please open an issue in the repository.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors