This Retrieval Augmented Generation (RAG) system uses OpenAI embeddings and ChatGPT to create a document-based question answering system with vector storage.
This project consists of several Python scripts that work together:
openAILoad.py- Loads text files into ChromaDB vector databaseopenAILoadMarkdown.py- Loads markdown files into a separate ChromaDB vector databaseopenAIRetrieve.py- Chat interface using the vector database for context-aware responsessimple_chat.py- Standalone chat interface with OpenAI (without RAG)scrapWebPagesIntoMarkdown.js- Scraps Websites to fetch content from websites and convert it to markdown format
First, run either or both of these scripts depending on your document types:
-
openAILoad.py- Processes .txt and .pdf files into a vector database- Loads files from the
./txtand./pdfdirectories - Splits documents into chunks
- Creates embeddings using OpenAI
- Stores them in the
./chromaDBdirectory
- Loads files from the
-
openAILoadMarkdown.py- Processes .md files into a separate vector database- Loads files from the
./mddirectory - Splits markdown documents into appropriate chunks
- Creates embeddings using OpenAI
- Stores them in the
./chromaDB_MDdirectory
- Loads files from the
After loading your documents, run:
openAIRetrieve.py- Interactive chat interface that:- Connects to the previously created ChromaDB vector store
- Creates a conversation chain with memory
- Allows users to ask questions about the loaded documents
- Uses OpenAI's GPT-4o model to generate contextually relevant answers
If you simply want to chat with OpenAI without document retrieval:
- simple_chat.py - Direct chat interface with OpenAI GPT-4o
- Creates a conversation session with OpenAI
- Maintains conversation history
- No connection to vector database or document context
Before using the RAG system, you'll need documents to work with. The project includes a JavaScript-based web scraping tool to fetch content from websites and convert it to markdown format if you need it :
scrapWebPagesIntoMarkdown.js
This script extracts text from web pages and converts it to clean markdown format using Mozilla's Readability (the same technology used in Firefox's reader view). It can be used to scrape any website, and in this example, it's configured to extract content from engineering school websites.
Setup requirements:
npm install node-fetch jsdom @mozilla/readability turndown
Usage:
node scrapWebPagesIntoMarkdown.js
The script:
- Fetches HTML content from specified URLs
- Uses Mozilla's Readability to extract the main content
- Converts the HTML to clean markdown
- Removes links and images
- Saves the content to markdown files in the ./md directory
You can modify the URLs in the script to scrape content from any website relevant to your needs.
Before running these scripts:
- Install required packages:
pip install langchain openai "langchain[docarray]" langchain-community langchain-openai "unstructured[md]"
-
Set your OpenAI API key by replacing "YOUR_KEY" in each script.
-
Optional: Set your HuggingFace token if needed.
- Run the loading script(s) first to create your vector database.
- Run the retrieve script to start asking questions about your documents.
- Type "exit" to end the chat session.
You can also use a simple OpenAI chat interface without RAG by running simple_chat.py (the third script shown in your example).
Note that “schools” are mentioned in several places, but this is just one case where the RAG is based on elements taken from the websites of several engineering schools.