b05un/school_rag_api
0
π FUOYE RAG API β Hugging Face Space Deployment
This project is a Retrieval-Augmented Generation (RAG) API service for Federal University Oye-Ekiti (FUOYE), built with FastAPI, LangChain, OpenAI, Chroma, and containerized with Docker. Itβs deployed on Hugging Face Spaces.
The app answers questions using pre-loaded university documents, providing helpful, friendly, and relevant responses β or redirecting users politely when information is unavailable.
π¦ Project Structure
.
βββ app.py # Main FastAPI app and RAG logic
βββ Dockerfile # Docker configuration for deployment on HF Spaces
βββ data/ # Directory containing .txt documents to index
βββ .env # Environment variables (OPENAI_API_KEY etc.)
βββ chroma_db/ # Local Chroma vectorstore directory (auto-generated)βοΈ How It Works
π Environment Setup
- The app loads environment variables using
python-dotenv. - Important variables:
DATA_PATHβ path to directory containing text documentsCHROMA_PATHβ directory for the Chroma vector databaseOPENAI_API_KEYβ your OpenAI API key for embeddings and LLM
Defaults for Hugging Face Spaces:
DATA_PATH=/code/dataCHROMA_PATH=/tmp/chroma_db
π Document Loading & Splitting
- DirectoryLoader loads all
.txtfiles from thedata/directory. - RecursiveCharacterTextSplitter splits documents into chunks of
1000characters with150characters overlap β improving retrieval granularity.
loader = DirectoryLoader(DATA_PATH, glob="**/*.txt", loader_cls=TextLoader)
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)π Embedding & Vector Store Creation
- Uses OpenAI Embedding model `text-embedding-3-large` to generate embeddings.
- Chunks are stored in a Chroma vectorstore locally at
CHROMA_PATH. - Vectorstore is created on app start via
get_vector_store().
π API Endpoints
Base URL: /
π‘ /rag-query Endpoint Workflow
Request Body:
{
"message": "Your question here",
"history": "Previous conversation if any"
}Response:
{
"response": "RAG-based answer"
}Process:
- Retrieve Top-5 Relevant Documents using Chroma retriever.
- Construct RAG Prompt β combines:
- System instructions for a helpful FUOYE assistant
- Userβs question
- Conversation history
- Retrieved knowledge content
- LLM Streaming Response using OpenAI
gpt-4o-mini - Return Final Answer as JSON.
π¦ Docker Deployment (on Hugging Face Spaces)
Dockerfile Highlights:
- Python 3.11 base image.
- Installs dependencies from
requirements.txt - Exposes port
7860for Hugging Face compatibility. - Runs the app using
uvicorn.
To Deploy:
- Push code to Hugging Face repository.
- Set
HF API TokenandOPENAI_API_KEYin repository secrets. - Hugging Face automatically builds Docker image and runs container.
π Example .env
OPENAI_API_KEY=sk-yourkeyhere
DATA_PATH=/code/data
CHROMA_PATH=/tmp/chroma_dbβ Usage Notes
- Update
data/with.txtfiles containing FUOYE knowledge. - On app start, the Chroma vectorstore rebuilds automatically.
- Adjust chunk size and retrieval
kas needed for performance tuning.
β¨ Technologies Used
π License
MIT License β free for educational, research, and personal projects.
