Activity 1.2: Basic Chat App using Streamlit with Gemini API
Work in progress
This section is under construction. This information hasn’t been reviewed or edited yet!
Practical Activity Overview
This application creates a simple chat interface in a web browser where users can interact with Google’s Gemini AI model. We’ll be building and running this application locally on your machine.
Prerequisites
- Python 3.8 or higher installed on your system
- Basic familiarity with command line/terminal
- A text editor or IDE of your choice
- Google API key (obtain from Google AI Studio)
Activities
Step 1: Set Up Your Development Environment
1.1 Create a new directory for your project:
mkdir chapter-1-lab
cd chapter-1-lab1.2 Create and activate a virtual environment:
- On Windows:
python -m venv venv
.\venv\Scripts\activate- On macOS/Linux:
python3 -m venv venv
source venv/bin/activateStep 2: Install Required Packages
2.1 Create a new file called requirements.txt and add the following lines to it:
streamlit
langchain
langchain_google_genai
python-dotenvThese are the libraries we’ll need to import into our code to be able to use the application we are going to build. As we progress through the activities, we’ll come back and add more dependencies to this file.
2.2 Now, with your virtual environment activated, install the necessary packages:
pip install -r requirements.txtStep 3: Set Up Your Gemini API Key
3.1 Create a .env file in your project directory:
# .env
GOOGLE_API_KEY=your_api_key_here3.2 Create a new file called app.py with the following code:
import os
import streamlit as st
from langchain_google_genai import ChatGoogleGenerativeAI
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize Gemini model with API key from environment variable
if not os.getenv('GOOGLE_API_KEY'):
st.error("Please set your Google API Key in the .env file!")
st.stop()
# Create the chat model
chat_model = ChatGoogleGenerativeAI(model="gemini-2.0-flash")
st.title("Gemini Chat App")
# Here we create our chat input
user_input = st.chat_input("Type your message here...")
# Generate and display response
if user_input:
st.chat_message("user").write(user_input)
try:
response = chat_model.invoke(user_input)
st.chat_message("assistant").write(response.content)
except Exception as e:
st.error(str(e))Project Structure
Your final project structure should look like this:
chapter-1-lab/
├── venv/
├── .env
├── requirements.txt
└── app.pyStep 4: Run Your Application
4.1 Make sure your virtual environment is activated
4.2 Run the Streamlit application:
streamlit run app.py4.3 Your default web browser should automatically open to http://localhost:8501
Step 5: Testing Your Application
5.1 Ensure your API key is correctly set in the .env file
5.2 Type a message in the chat input
5.3 Verify you receive a response from Gemini
Troubleshooting
- If you get API errors, verify your API key is correctly set in the
.envfile - Make sure your virtual environment is activated before running the application
- Check that all required packages are installed using
pip list - If you see import errors, verify you’re running the correct Python version
- Make sure you’re in the project directory when running the application
Key Learning Points
Deploying your own LLM locally gives you a few advantages:
- Complete control over your environment
- No timeout limitations
- Better development experience
- Easier debugging
- Can work offline (except for API calls)
- No resource sharing
- Better performance
Security Note
Never commit your .env file to version control. Add it to your .gitignore file if you’re using Git.