CoolFace
Apppublic

Faniyi/Apextech-knowledge-base

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
App README

๐Ÿข ApexTech Solutions โ€” Company Knowledge Base

An internal AI-powered assistant that allows ApexTech employees to query company documentation using natural language. Built with RAG (Retrieval-Augmented Generation), it retrieves relevant information from internal documents and generates accurate, sourced answers.


๐Ÿ—๏ธ Architecture

Query
  โ”‚
  โ–ผ
Query Rewriter (gpt-4o-mini)        โ† Rewrites vague/conversational queries
  โ”‚
  โ–ผ
Hybrid Search                        โ† Dense (Chroma) + Sparse (BM25) fused via RRF
  โ”‚
  โ–ผ
Cross-Encoder Reranker               โ† ms-marco-MiniLM-L-6-v2 reranks top-k chunks
  โ”‚
  โ–ผ
LLM (gpt-4.1-mini) + System Prompt  โ† Grounded answer from retrieved context
  โ”‚
  โ–ผ
Answer + Sources

๐Ÿ“ Project Structure

company-knowledge-base/
โ”œโ”€โ”€ UI/
โ”‚   โ””โ”€โ”€ ui.py                   # Chainlit UI (User mode)
โ”œโ”€โ”€ rag/
โ”‚   โ”œโ”€โ”€ answer.py               # Full RAG pipeline
โ”‚   โ”œโ”€โ”€ hybrid_search.py        # Dense + BM25 + RRF + cross-encoder reranker
โ”‚   โ”œโ”€โ”€ query_rewriter.py       # LLM query rewriter
โ”‚   โ”œโ”€โ”€ ingest.py               # Document ingestion
โ”‚   โ”œโ”€โ”€ chunking.py             # Text splitting
โ”‚   โ”œโ”€โ”€ embedding.py            # HuggingFace embeddings
โ”‚   โ””โ”€โ”€ vectorstore.py          # Chroma vector store
โ”œโ”€โ”€ llm/
โ”‚   โ”œโ”€โ”€ llm.py                  # LLM initialisation
โ”‚   โ””โ”€โ”€ prompt.py               # System prompt template
โ”œโ”€โ”€ evals/
โ”‚   โ”œโ”€โ”€ datasets/
โ”‚   โ”‚   โ”œโ”€โ”€ qa_dataset.json     # 60 QA pairs (easy + hard)
โ”‚   โ”‚   โ””โ”€โ”€ edge_cases.json     # 20 adversarial / edge cases
โ”‚   โ”œโ”€โ”€ metrics/
โ”‚   โ”‚   โ”œโ”€โ”€ retrieval_metrics.py    # recall@k, precision@k, MRR
โ”‚   โ”‚   โ”œโ”€โ”€ generation_metrics.py   # faithfulness, relevance, correctness
โ”‚   โ”‚   โ”œโ”€โ”€ context_metrics.py      # context coverage, context relevance
โ”‚   โ”‚   โ””โ”€โ”€ e2e_metrics.py          # task success, unanswerable awareness, CI gates
โ”‚   โ”œโ”€โ”€ runners/
โ”‚   โ”‚   โ””โ”€โ”€ run_evals.py        # local + ci modes with LangSmith integration
โ”‚   โ””โ”€โ”€ results/                # Timestamped JSON eval reports
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ raw/                    # Source .md documents (canonical)
โ”‚   โ””โ”€โ”€ vector_db/              # Chroma persistent store
โ”œโ”€โ”€ main.py                     # Entry point โ€” ingest check + UI launch
โ””โ”€โ”€ chainlit.md                 # Chainlit welcome screen

๐Ÿš€ Getting Started

1. Clone the repository

bash
git clone https://github.com/faniyi-akinbobola/company-knowledge-base.git
cd company-knowledge-base

2. Install dependencies

bash
uv sync

3. Set up environment variables

bash
cp .env.example .env

Edit .env:

env
OPENAI_API_KEY=sk-...
LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=lsv2_...
LANGCHAIN_PROJECT=company-knowledge-base

4. Run the app

bash
uv run python main.py

This will:

  • โ€”โœ… Check if the vector store exists
  • โ€”โœ… Auto-ingest documents if not found
  • โ€”โœ… Launch the Chainlit UI at http://localhost:8000

๐Ÿงช Evals

bash
# Fast local run โ€” no LLM judge
uv run python evals/runners/run_evals.py --mode local

# With LLM-as-judge (faithfulness, correctness, relevance, hallucination)
uv run python evals/runners/run_evals.py --mode local --llm-judge

# CI mode โ€” requires LANGCHAIN_API_KEY, exits with code 1 on threshold breach
uv run python evals/runners/run_evals.py --mode ci

Latest eval results

MetricScoreCI Threshold
recall@k87.5%โ€”
precision@k98.0%โ€”
MRR0.77โ€”
answerfoundrate85.0%โ€”
faithfulness0.81โ€”
answer_relevance0.85โ€”
correctness0.62โ€”
tasksuccessrate80.4%โ‰ฅ 80% โœ…
unanswerable_awareness100%โ‰ฅ 70% โœ…
notfoundfalsepositiverate8.9%โ‰ค 10% โœ…
llmjudgescore0.83โ‰ฅ 0.70 โœ…

๐Ÿ”ญ LangSmith Tracing

All LLM calls (UI + evals) are automatically traced to LangSmith when LANGCHAIN_TRACING_V2=true is set. No extra code required. View traces at smith.langchain.com.


๐Ÿš€ Deploying to HuggingFace Spaces

Step 1 โ€” Create a new Space

  1. 1.Go to huggingface.co/new-space
  2. 2.Fill in:
  3. 3.Owner: your HuggingFace username or org
  4. 4.Space name: e.g. apextech-knowledge-base
  5. 5.License: choose one (e.g. MIT)
  6. 6.SDK: select Docker
  7. 7.Visibility: Private (this is an internal tool)
  8. 8.Hardware: CPU Basic โ€” free tier (app peaks at ~470MB RAM โœ…)
  9. 9.Click Create Space

Step 2 โ€” Add your OpenAI API key as a Secret

โš ๏ธ Do this before pushing code โ€” the build needs it to run the LLM.
  1. 1.In your Space, go to Settings (top right)
  2. 2.Scroll to Repository secrets
  3. 3.Click New secret and add:
NameValue
OPENAI_API_KEYsk-... your OpenAI key
LANGCHAIN_API_KEYlsv2_... (optional โ€” enables LangSmith tracing)
LANGCHAIN_TRACING_V2true (optional)
LANGCHAIN_PROJECTcompany-knowledge-base (optional)

Step 3 โ€” Push your code to the Space

Run these commands from your project root:

bash
# One-time setup: add the Space as a git remote
# Replace YOUR_USERNAME and SPACE_NAME with your actual values
git remote add space https://huggingface.co/spaces/YOUR_USERNAME/SPACE_NAME

# Push your master branch to the Space
git push space master
If you get an authentication error, use a HuggingFace token: git remote set-url space https://YOUR_HF_TOKEN@huggingface.co/spaces/YOUR_USERNAME/SPACE_NAME Generate a token at huggingface.co/settings/tokens with write access.

Step 4 โ€” Monitor the build

  1. 1.Go to your Space page on HuggingFace
  2. 2.Click the Build logs tab
  3. 3.The build will:
  4. 4.Install all Python dependencies (~3โ€“5 min)
  5. 5.Download the HuggingFace embedding + reranker models (~2 min)
  6. 6.Run rag/ingest.py to build the vector database (~1 min)
  7. 7.Start the Chainlit server on port 7860
  8. 8.When the build is complete the Space shows Running (green)
  9. 9.Click the app URL to open the assistant

Total first-build time: ~10โ€“15 minutes. Subsequent pushes are faster due to Docker layer caching.


Step 5 โ€” Updating the app

Every time you push to the space remote, HuggingFace rebuilds and redeploys automatically:

bash
# Make your changes, commit, then:
git push space master

What the Dockerfile does at build time

StepWhat happensSecrets needed?
uv syncInstalls all Python dependenciesNo
Download modelsPulls all-MiniLM-L6-v2 + ms-marco-MiniLM-L-6-v2 from HuggingFaceNo
rag/ingest.pyBuilds ChromaDB vector store from raw .md + .csv docsNo
Runtime startLaunches Chainlit on port 7860OPENAI_API_KEY โœ…

Models and vector DB are baked into the image โ€” zero cold-start delay.