A Python application that provides an interactive chat interface using Google's Gemini AI model (gemini-2.0-flash) with optional real-time Google Search capabilities.
- 🤖 AI Chat Interface: Interactive conversations with Google's Gemini 2.0 Flash model
- 🔍 Real-Time Search: Optional Google Search integration for up-to-date information
- 💬 Conversation History: Track and view full conversation history
- 🎯 Two Modes:
- Hardcoded examples for testing
- Interactive command-line interface for live conversations
- 🔐 Secure Configuration: Environment variable-based API key management
- Python 3.7 or higher
- Google Gemini API key (Get one here)
-
Clone or navigate to the project directory:
cd /home/aayushmaan/Internship/GenAIModel -
Create and activate a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
Create a
.envfile in the project root:touch .env
Add your Gemini API key to the
.envfile:GEMINI_API_KEY=your_api_key_here
Run the interactive chat interface:
python InteractiveChat.pyAvailable Commands:
- Type your message normally to chat
search- Enable real-time search for your next queryhistory- View full conversation historyclear- Clear conversation historyexit- Exit the chat
Example Session:
You: What is Python used for?
Bot: [Response from Gemini]
You: search
🔍 Real-time search enabled for next query. Ask your question:
You: What is the current price of Bitcoin?
Bot: [Response with real-time data]
Run the script with predefined examples:
python HardcodedChat.pyThis will execute several example conversations demonstrating:
- Regular chat interactions
- Real-time search queries (Bitcoin price, weather, etc.)
- Conversation history display
GenAIModel/
├── HardcodedChat.py # Script with hardcoded example conversations
├── InteractiveChat.py # Interactive command-line chat interface
├── requirements.txt # Python dependencies
├── .env # Environment variables (create this file)
└── README.md # This file
Both scripts use the same GeminiChatWithRealTimeSearch class with the following methods:
__init__(api_key): Initialize the Gemini clientinitialize_chat(system_instruction=""): Initialize a chat sessionenable_real_time_search(): Enable Google Search capabilitysend_message(user_message, use_search=False): Send a message and get responseget_conversation_history(): Retrieve full conversation historyclear_history(): Clear conversation historydisplay_history(): Display formatted conversation history
Key dependencies include:
google-genai: Google's Gemini AI SDKpython-dotenv: Environment variable managementgoogle-auth: Authentication for Google APIs
See requirements.txt for the complete list of dependencies.
The application uses the following configuration:
- Model:
gemini-2.0-flash - System Instruction: "You are a helpful AI assistant with access to real-time information."
- API Key: Loaded from
GEMINI_API_KEYenvironment variable (via.envfile)
The application includes error handling for:
- Missing API keys
- API response errors
- Invalid user inputs
- Keyboard interrupts (Ctrl+C)
- The real-time search feature requires enabling the
google_searchtool for each query - Conversation history is maintained in memory and cleared when the script exits
- The
.envfile should be added to.gitignoreto keep your API key secure
This project is under standard MIT license.