Lunch & Learn: Deploying a Chat LLM System Using Python
Overview This learning guide will walk your colleagues—who have no prior experience in Python, AI, LLMs, or prompt engineering—through the step-by-step process of deploying a Chat LLM system. This is a hands-on experience designed to be playful and accessible, with every detail included for both macOS and Windows environments. The goal is for everyone to have a fully functional LLM system running on their computer by the end of the session.
Training Outline
- Setting up the Environment
- Python Installation (macOS & Windows)
- Virtual Environment Setup
- Installing Dependencies
- Getting the OpenAI API Key
- Step-by-Step Guide to Building the Chat LLM App
- Application Overview
- Code Walkthrough (app.py, templates, etc.)
- Running the Application
- Prompts for Building Each Script
- Hands-on Practice
1. Install Python
-
macOS:
- Open Terminal (
Cmd + Space
, type "Terminal"). - Run the command to install Python:
Prompt: "How do I install Python on macOS using Terminal?"
brew install python3
- Verify installation:
Prompt: "How do I check if Python is installed on my Mac?"
python3 --version
- Open Terminal (
-
Windows:
- Download Python from python.org.
- Run the installer, and make sure to check "Add Python to PATH". Prompt: "What are the steps to install Python on Windows and add it to PATH?"
- Verify installation by opening Command Prompt (
Win + R
, type "cmd"):Prompt: "How can I verify that Python is installed on Windows?"python --version
2. Create and Activate a Virtual Environment
-
macOS:
- Navigate to your working directory:
cd ~/Desktop
- Create a virtual environment:
Prompt: "How do I create a virtual environment in Python on macOS?"
python3 -m venv venv
- Activate it:
Prompt: "How do I activate a Python virtual environment on macOS?"
source venv/bin/activate
- Navigate to your working directory:
-
Windows:
- Navigate to your working directory:
cd Desktop
- Create a virtual environment:
Prompt: "How do I create a virtual environment in Python on Windows?"
python -m venv venv
- Activate it:
Prompt: "How do I activate a Python virtual environment on Windows?"
venv\Scripts\activate
- Navigate to your working directory:
3. Install Dependencies
- Both macOS and Windows:
- After activating the virtual environment, install the dependencies using the
requirements.txt
file:Prompt: "How do I install all dependencies from a requirements.txt file?"pip install -r requirements.txt
- If you don’t have
requirements.txt
, install the required libraries manually:Prompt: "What is the command to install Flask, Pandas, Matplotlib, PyPDF2, and python-dotenv using pip?"pip install openai flask pandas matplotlib PyPDF2 python-dotenv
- After activating the virtual environment, install the dependencies using the
4. Obtain OpenAI API Key
- Go to OpenAI and generate your API key.
- Create a
.env
file in your project directory with the following format to store the API key securely:Prompt: "How do I create a .env file to securely store my OpenAI API key in a Python project?"OPENAI_API_KEY=your_openai_api_key_here
1. Understand the Application Structure
app.py
: The main Python script that runs the Flask application.templates/
: HTML files that control how the application pages are rendered.static/
: Stores static assets like images, JavaScript, and CSS.documents/
: Stores uploaded documents for processing.
2. Setting Up the Application
- Copy all extracted files (
app.py
,templates
,static
, etc.) into your project folder on your desktop. Ensure the.env
file is also in this directory. - Prompt for Setup: "How do I organize a Python Flask project with templates and static files?"
3. Running the Application
- macOS & Windows:
- Make sure your virtual environment is activated.
- Run the application:
Prompt: "How do I run a Flask application using Python?"
python app.py
- Open a browser and navigate to
http://127.0.0.1:5001
to interact with the chat LLM. Prompt: "Where can I access my Flask application once it is running?"
4. Example Prompts for Building the Application
- To understand each component of the app, ask:
What does the `app.py` script do in my Flask application? Explain each function and route.
- To understand how document uploads are handled:
Explain the purpose of the `/upload` route in Flask.
- To know how the OpenAI API is integrated:
How does the `/chat` route interact with the OpenAI API?
5. Code Walkthrough
- Imports: Explain why each import is necessary (e.g., Flask for the web framework, OpenAI for interacting with the API).
- Routes: Walk through each route in
app.py
./upload
: Handles document uploads and stores them./chat
: Sends the context to the OpenAI API and displays responses.- Prompt for Context: "How does Flask handle file uploads securely?"
- Functions: Discuss helper functions like
extract_text()
to extract content from uploaded documents.- Prompt for Extraction: "How can I extract text from different file types in Python?"
Exercise 1: Upload and Chat
- Upload a sample document (e.g., a PDF with some text) and ask a question.
- Prompt for Upload: "How do I upload a file and interact with it using a chat feature in Flask?"
Exercise 2: Modify the HTML
- Add a personalized message or title to
index.html
.- Prompt: "How do I edit an HTML file to add a custom title in a Flask app?"
Exercise 3: Add a New Route
- Add a new route called
/about
that returns information about the application.- Prompt: "How do I add a new route in Flask to display an 'About' page?"
Deployment Practice
1. Running on a Different Port
- Modify
app.py
to run on a different port, like 8080:Prompt: "How do I change the port of my Flask application?"if __name__ == '__main__': app.run(debug=True, host='127.0.0.1', port=8080)
2. Creating a Batch File (Windows) and Shell Script (macOS) to Run the App
-
Windows:
- Open Notepad and paste the following script:
@echo off call venv\Scripts\activate python app.py pause
- Save it as
run_app.bat
on your desktop. - Double-click the
.bat
file to run the application.
- Prompt: "How do I create a batch file to automate running my Python app on Windows?"
- Open Notepad and paste the following script:
-
macOS:
- Open Terminal and create a new shell script:
touch run_app.sh
- Edit the script:
nano run_app.sh
- Add the following lines:
#!/bin/bash source venv/bin/activate python app.py
- Save (
Ctrl + O
) and exit (Ctrl + X
). - Make the script executable:
chmod +x run_app.sh
- Run it:
./run_app.sh
- Prompt: "How do I create and run a shell script to automate my Python app on macOS?"
- Open Terminal and create a new shell script:
Summary and Q&A
- Recap: From installing Python and dependencies to running the app and interacting with an LLM.
- Emphasize that mistakes are okay—every error is a learning opportunity.
- Open the floor for questions, providing playful analogies if needed (e.g., "The virtual environment is like putting on your lab coat before experimenting—keeps things clean!").
Follow-up Resources
- Python Basics: Learn Python
- Flask Documentation: Flask Docs
- OpenAI API Guide: OpenAI API Documentation
Next Steps
- Encourage colleagues to extend the app—maybe add a feature to analyze uploaded text sentiment.
- Plan a follow-up session in a month to review progress and celebrate everyone's learning achievements!