LLM-powered websites offer chatbots, document querying, and text-to-SQL capabilities. However, direct access to the underlying code provides greater control and customization. This article presents six Python projects to explore generative AI.
1. Build a Chatbot with Llama 2, Streamlit, and Replicate
This project demonstrates building a chatbot using Meta’s Llama 2 model within the Streamlit web framework. A tutorial by Chanin Nantasenamat is available on GitHub, YouTube, and as a blog post.
Setup
- Create a Replicate account (GitHub login works). A free account is sufficient for light use; larger models or frequent queries may require payment.
- Obtain your Replicate API token (default token or generate a new one).
- Clone the repository:
git clone https://github.com/dataprofessor/llama2.git
(or download the ZIP). - Create a virtual environment:
python -m venv env
(or use your preferred method). - Activate the environment:
source env/bin/activate
(Mac/Linux) orenv\Scripts\activate
(Windows). - Install packages:
pip install -r requirements.txt
- Create a
.streamlit/secrets.toml
file (add.streamlit/secrets.toml
to your.gitignore
):
REPLICATE_API_TOKEN = "your_replicate_token"
or, to test locally without a token:
FAKE_TOKEN = "some fake token"
- Run the Streamlit app:
streamlit run streamlit_app_v2.py
. Adjust model parameters, especially output length, for better results.
Deployment
Deploy to Streamlit Community Cloud (from your GitHub account) or Hugging Face Spaces. For public deployments, require users to provide their own API key to avoid excessive costs.
2. Visualize Data with Matplotlib, Streamlit, and OpenAI
This app lets you upload a CSV file, ask a question, and receive a Matplotlib graph in response. It uses the OpenAI API.
Setup
- Clone the repository:
git clone https://github.com/michaelweiss/chat-with-your-data.git
- Create and activate a virtual environment.
- Set your OpenAI key:
export OPENAI_API_KEY="your_open_ai_key"
- Install packages:
pip install openai pandas streamlit matplotlib
- Run the app:
streamlit run chat_data.py
Tweak your questions to get optimal results. The permissive MIT license allows for modification and reuse.
3. Query a Text Document with OpenAI, LangChain, and Chainlit
This project creates an application for querying a single uploaded .txt document. It uses Chainlit for the chat interface.
Setup
- Create and activate a Python virtual environment.
- Set your OpenAI API key (or use a different LLM).
- Install packages:
pip install python-dotenv langchain chromadb tiktoken chainlit openai
- Copy Chainlit’s example code (https://docs.chainlit.io/examples/qa).
- Update the code with your OpenAI key (or a more secure key loading method).
- Run the app:
chainlit run -w qa.py
Chainlit provides built-in functionality for displaying LLM steps and links to source document chunks. Consider using additional LangChain loaders for different file types.
4. Query Saved Documents with LangChain, OpenAI, and Gradio
This application processes and stores multiple documents for LLM-based querying (Retrieval-Augmented Generation or RAG). It includes a chatbot interface.
Setup
- Clone or download the code from https://github.com/hwchase17/chat-your-data.
- Create and activate a virtual environment.
- Follow setup steps 0, 1, and 2 in the project’s README.
- Launch the Gradio app:
python app.py
To customize with your documents:
- Delete
vectorstore.pkl
andstate_of_the_union.txt
. - Create a
docs
folder and add your documents (PDFs, DOCX, TXT). - Modify
ingest_data.py
to handle multiple file types (see provided code snippet). - Update
query_data.py
andapp.py
with relevant text and titles. - Install
pypdf
if using PDFs:pip install pypdf
- Re-run
python ingest_data.py
and then launch the app.
Consider using Gradio’s new chat interface for streamlined responses.
5. LLM-Powered Web Research with LangChain, OpenAI, and FastAPI
The GPT Researcher project provides step-by-step installation instructions. It uses the Tavily search engine (currently free, but queries are used for model training).
Setup
- Ensure Python 3.11 or later is installed.
- Create and activate a virtual environment (using the correct Python version).
- Install packages:
pip install -r requirements.txt
- Create a
.env
file with your OpenAI API key:OPENAI_API_KEY="your-key-here"
- Run the application:
uvicorn main:app -reload
Access the application at http://localhost:8000
. The application provides research reports with source links. Consider deployment using Docker.
6. Natural Language to SQL with LlamaIndex, SQLAlchemy, and OpenAI
This project converts natural language to SQL queries using LlamaIndex and SQLAlchemy. It runs from the command line.
Setup
- Create a project directory and activate a virtual environment.
- Install packages:
pip install openai sqlalchemy llama-index
- Install
python-dotenv
:pip install python-dotenv
- Create a
.env
file with your OpenAI API key:OPENAI_API_KEY="my_api_key"
- Create an
app.py
file (using the provided code snippet). - Run the app:
python app.py
Modify the code to adjust the database schema and sample data. Alternative LLMs can be used with LlamaIndex.
Further Exploration
Explore additional resources for more Python generative AI projects:
- Shiny for Python chatstream
- Streamlit projects (website and blog)
- WASM Chatbot (LangChain blog post)
- LangChain projects (use cases and examples)
- Chainlit projects (website and cookbook repository)
Happy coding!