CoolFace
Apppublic

b05un/school_rag_api

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
0likes
App README

πŸ“š 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 documents
  • β€”CHROMA_PATH β€” directory for the Chroma vector database
  • β€”OPENAI_API_KEY β€” your OpenAI API key for embeddings and LLM

Defaults for Hugging Face Spaces:

  • β€”DATA_PATH=/code/data
  • β€”CHROMA_PATH=/tmp/chroma_db

πŸ“Œ Document Loading & Splitting

  1. 1.DirectoryLoader loads all .txt files from the data/ directory.
  2. 2.RecursiveCharacterTextSplitter splits documents into chunks of 1000 characters with 150 characters overlap β€” improving retrieval granularity.
python
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: /

MethodRouteDescription
GET/Health check route
POST/rag-queryReceives a query and responds via RAG

πŸ“‘ /rag-query Endpoint Workflow

Request Body:

json
{
  "message": "Your question here",
  "history": "Previous conversation if any"
}

Response:

json
{
  "response": "RAG-based answer"
}

Process:

  1. 1.Retrieve Top-5 Relevant Documents using Chroma retriever.
  2. 2.Construct RAG Prompt β€” combines:
  3. 3.System instructions for a helpful FUOYE assistant
  4. 4.User’s question
  5. 5.Conversation history
  6. 6.Retrieved knowledge content
  7. 7.LLM Streaming Response using OpenAI gpt-4o-mini
  8. 8.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 7860 for Hugging Face compatibility.
  • β€”Runs the app using uvicorn.

To Deploy:

  1. 1.Push code to Hugging Face repository.
  2. 2.Set HF API Token and OPENAI_API_KEY in repository secrets.
  3. 3.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 .txt files containing FUOYE knowledge.
  • β€”On app start, the Chroma vectorstore rebuilds automatically.
  • β€”Adjust chunk size and retrieval k as needed for performance tuning.

✨ Technologies Used


πŸ“– License

MIT License β€” free for educational, research, and personal projects.