This repository powers Retrieval-Augmented Generation (RAG) features for mkhuda.com. It contains builders that sync WordPress posts into vector stores (FAISS and Chroma) plus several chat front ends (CLI, Gradio, FastAPI) that query those vectors with OpenAI models.
builder/– scripts that pull content from WordPress and build FAISS or Chroma indexes.app/– chat experiences (CLI, Gradio UI, FastAPI) that read from the prepared indexes.utils/– helpers, includingpydantic_langchain_fix.pythat patches LangChain models for Pydantic v2.mkhuda_faiss_index/,mkhuda_chroma/– persisted vector stores and accompanying JSON backups/metadata.docs.json,docs_chroma.json– cached exports of the latest WordPress corpus used for incremental builds.
- Python 3.12+
- uv package manager (
pip install uvor follow Astral’s instructions) - Had access to your WordPress MySQL database
- OpenAI API key with access to
text-embedding-3-smallandgpt-4o-mini - pyenv for easy Python versioning
- Instll
pyenv(Recommended)
brew install pyenv
pyenv install 3.12.7
pyenv local 3.12.7-
Clone the repository and install dependencies with
uv:git clone https://github.com/mkhuda/mkhuda-blog-llm-rag.git cd mkhuda-blog-llm-rag uv sync -
Create a
.envfile in the project root (copy.env.exampleif you have one) and fill in your credentials:OPENAI_API_KEY=sk-... MYSQL_HOST=your-db-host MYSQL_PORT=port MYSQL_DATABASE=wordpress_db MYSQL_USER=db_user MYSQL_PASSWORD=db_password
The scripts load these variables via
python-dotenv. -
(Optional) if you need to install extra tools into the virtual environment later, use
uv pip install <package>.
- To use this project inside docker, first you need to build:
DOCKER_BUILDKIT=1 docker build --secret id=dotenv,src=.env -t mkhuda-rag-api-image:latest . docker-compose up -d
All builder scripts assume you are in the project root. They will persist files to the top-level mkhuda_* directories.
-
Chroma index (primary for LangChain apps):
uv run python builder/rag_chroma_builder.py
- Pulls the latest WordPress posts, cleans HTML, writes
docs_chroma.json, updates themkhuda_chromadirectory, and logs metadata tomkhuda_chroma_meta.json. - Handles incremental updates by skipping URLs already present in the existing collection. Falls back to cached JSON if the DB is unreachable.
- Pulls the latest WordPress posts, cleans HTML, writes
-
FAISS index (LangChain):
uv run python builder/rag_faiss_builder.py
- Loads the previous FAISS store if available, otherwise rebuilds from scratch.
- Uses
docs.json(latest full corpus) andmkhuda_faiss_backup.jsonas fallbacks to keep the vector store in sync.
-
FAISS index (LlamaIndex):
uv run python builder/rag_build_llama.py
- Maintains a FAISS index compatible with LlamaIndex using the same WordPress source.
-
Legacy FAISS builder (
builder/rag_build.py) is kept for backwards compatibility; prefer the scripts above.
-
CLI with FAISS (LangChain):
uv run python app/rag_faiss_chat.py
- Interactive terminal prompt that retrieves with FAISS and answers with
gpt-4o-mini.
- Interactive terminal prompt that retrieves with FAISS and answers with
-
CLI with Chroma (LangChain):
uv run python app/rag_chroma_chat.py
-
Gradio web app:
uv run python app/rag_gradio.py # FAISS-backed uv run python app/rag_gradio_chroma.py # Chroma-backed
Visit the local URL printed in the terminal.
-
FastAPI service:
uv run python app/rag_fastapi.py
Exposes an HTTP endpoint for programmatic access.
All chat apps expect the corresponding index directories to exist before launch. Run the builder scripts first if you see missing index errors.
- Re-run the builder scripts whenever new posts are published on mkhuda.com.
docs.jsonandmkhuda_faiss_backup.jsonare safe to commit to backups but contain full article text; handle according to your data policies.- If LangChain breaks due to Pydantic updates, ensure
utils/pydantic_langchain_fix.pyis imported before other LangChain modules (already handled in the app scripts). - The repository uses
pyproject.toml+uv.lockto pin dependencies. Useuv lockto refresh the lockfile when upgrading packages.
This project is licensed under the MIT License. Pull requests and suggestions are welcome. Reach out before introducing large structural changes to align on direction.***