CoolFace
Apppublic

Faniyi/Apextech-knowledge-base

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
README.md250 linesDownload Raw Back to root
1---2title: ApexTech Knowledge Base3emoji: ๐Ÿข4colorFrom: blue5colorTo: indigo6sdk: docker7app_port: 78608pinned: false9---10 11# ๐Ÿข ApexTech Solutions โ€” Company Knowledge Base12 13An 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.14 15---16 17## ๐Ÿ—๏ธ Architecture18 19```20Query21  โ”‚22  โ–ผ23Query Rewriter (gpt-4o-mini)        โ† Rewrites vague/conversational queries24  โ”‚25  โ–ผ26Hybrid Search                        โ† Dense (Chroma) + Sparse (BM25) fused via RRF27  โ”‚28  โ–ผ29Cross-Encoder Reranker               โ† ms-marco-MiniLM-L-6-v2 reranks top-k chunks30  โ”‚31  โ–ผ32LLM (gpt-4.1-mini) + System Prompt  โ† Grounded answer from retrieved context33  โ”‚34  โ–ผ35Answer + Sources36```37 38---39 40## ๐Ÿ“ Project Structure41 42```43company-knowledge-base/44โ”œโ”€โ”€ UI/45โ”‚   โ””โ”€โ”€ ui.py                   # Chainlit UI (User mode)46โ”œโ”€โ”€ rag/47โ”‚   โ”œโ”€โ”€ answer.py               # Full RAG pipeline48โ”‚   โ”œโ”€โ”€ hybrid_search.py        # Dense + BM25 + RRF + cross-encoder reranker49โ”‚   โ”œโ”€โ”€ query_rewriter.py       # LLM query rewriter50โ”‚   โ”œโ”€โ”€ ingest.py               # Document ingestion51โ”‚   โ”œโ”€โ”€ chunking.py             # Text splitting52โ”‚   โ”œโ”€โ”€ embedding.py            # HuggingFace embeddings53โ”‚   โ””โ”€โ”€ vectorstore.py          # Chroma vector store54โ”œโ”€โ”€ llm/55โ”‚   โ”œโ”€โ”€ llm.py                  # LLM initialisation56โ”‚   โ””โ”€โ”€ prompt.py               # System prompt template57โ”œโ”€โ”€ evals/58โ”‚   โ”œโ”€โ”€ datasets/59โ”‚   โ”‚   โ”œโ”€โ”€ qa_dataset.json     # 60 QA pairs (easy + hard)60โ”‚   โ”‚   โ””โ”€โ”€ edge_cases.json     # 20 adversarial / edge cases61โ”‚   โ”œโ”€โ”€ metrics/62โ”‚   โ”‚   โ”œโ”€โ”€ retrieval_metrics.py    # recall@k, precision@k, MRR63โ”‚   โ”‚   โ”œโ”€โ”€ generation_metrics.py   # faithfulness, relevance, correctness64โ”‚   โ”‚   โ”œโ”€โ”€ context_metrics.py      # context coverage, context relevance65โ”‚   โ”‚   โ””โ”€โ”€ e2e_metrics.py          # task success, unanswerable awareness, CI gates66โ”‚   โ”œโ”€โ”€ runners/67โ”‚   โ”‚   โ””โ”€โ”€ run_evals.py        # local + ci modes with LangSmith integration68โ”‚   โ””โ”€โ”€ results/                # Timestamped JSON eval reports69โ”œโ”€โ”€ data/70โ”‚   โ”œโ”€โ”€ raw/                    # Source .md documents (canonical)71โ”‚   โ””โ”€โ”€ vector_db/              # Chroma persistent store72โ”œโ”€โ”€ main.py                     # Entry point โ€” ingest check + UI launch73โ””โ”€โ”€ chainlit.md                 # Chainlit welcome screen74```75 76---77 78## ๐Ÿš€ Getting Started79 80### 1. Clone the repository81 82```bash83git clone https://github.com/faniyi-akinbobola/company-knowledge-base.git84cd company-knowledge-base85```86 87### 2. Install dependencies88 89```bash90uv sync91```92 93### 3. Set up environment variables94 95```bash96cp .env.example .env97```98 99Edit `.env`:100 101```env102OPENAI_API_KEY=sk-...103LANGCHAIN_TRACING_V2=true104LANGCHAIN_API_KEY=lsv2_...105LANGCHAIN_PROJECT=company-knowledge-base106```107 108### 4. Run the app109 110```bash111uv run python main.py112```113 114This will:115 116- โœ… Check if the vector store exists117- โœ… Auto-ingest documents if not found118- โœ… Launch the Chainlit UI at `http://localhost:8000`119 120---121 122## ๐Ÿงช Evals123 124```bash125# Fast local run โ€” no LLM judge126uv run python evals/runners/run_evals.py --mode local127 128# With LLM-as-judge (faithfulness, correctness, relevance, hallucination)129uv run python evals/runners/run_evals.py --mode local --llm-judge130 131# CI mode โ€” requires LANGCHAIN_API_KEY, exits with code 1 on threshold breach132uv run python evals/runners/run_evals.py --mode ci133```134 135### Latest eval results136 137| Metric                        | Score | CI Threshold |138| ----------------------------- | ----- | ------------ |139| recall@k                      | 87.5% | โ€”            |140| precision@k                   | 98.0% | โ€”            |141| MRR                           | 0.77  | โ€”            |142| answer_found_rate             | 85.0% | โ€”            |143| faithfulness                  | 0.81  | โ€”            |144| answer_relevance              | 0.85  | โ€”            |145| correctness                   | 0.62  | โ€”            |146| task_success_rate             | 80.4% | โ‰ฅ 80% โœ…     |147| unanswerable_awareness        | 100%  | โ‰ฅ 70% โœ…     |148| not_found_false_positive_rate | 8.9%  | โ‰ค 10% โœ…     |149| llm_judge_score               | 0.83  | โ‰ฅ 0.70 โœ…    |150 151---152 153## ๐Ÿ”ญ LangSmith Tracing154 155All 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](https://smith.langchain.com).156 157---158 159## ๐Ÿš€ Deploying to HuggingFace Spaces160 161### Step 1 โ€” Create a new Space162 1631. Go to [huggingface.co/new-space](https://huggingface.co/new-space)1642. Fill in:165   - **Owner**: your HuggingFace username or org166   - **Space name**: e.g. `apextech-knowledge-base`167   - **License**: choose one (e.g. MIT)168   - **SDK**: select **Docker**169   - **Visibility**: **Private** (this is an internal tool)170   - **Hardware**: CPU Basic โ€” free tier (app peaks at ~470MB RAM โœ…)1713. Click **Create Space**172 173---174 175### Step 2 โ€” Add your OpenAI API key as a Secret176 177> โš ๏ธ Do this **before** pushing code โ€” the build needs it to run the LLM.178 1791. In your Space, go to **Settings** (top right)1802. Scroll to **Repository secrets**1813. Click **New secret** and add:182 183| Name                   | Value                                               |184| ---------------------- | --------------------------------------------------- |185| `OPENAI_API_KEY`       | `sk-...` your OpenAI key                            |186| `LANGCHAIN_API_KEY`    | `lsv2_...` _(optional โ€” enables LangSmith tracing)_ |187| `LANGCHAIN_TRACING_V2` | `true` _(optional)_                                 |188| `LANGCHAIN_PROJECT`    | `company-knowledge-base` _(optional)_               |189 190---191 192### Step 3 โ€” Push your code to the Space193 194Run these commands from your project root:195 196```bash197# One-time setup: add the Space as a git remote198# Replace YOUR_USERNAME and SPACE_NAME with your actual values199git remote add space https://huggingface.co/spaces/YOUR_USERNAME/SPACE_NAME200 201# Push your master branch to the Space202git push space master203```204 205> If you get an authentication error, use a HuggingFace token:206> `git remote set-url space https://YOUR_HF_TOKEN@huggingface.co/spaces/YOUR_USERNAME/SPACE_NAME`207> Generate a token at [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens) with **write** access.208 209---210 211### Step 4 โ€” Monitor the build212 2131. Go to your Space page on HuggingFace2142. Click the **Build logs** tab2153. The build will:216   - Install all Python dependencies (~3โ€“5 min)217   - Download the HuggingFace embedding + reranker models (~2 min)218   - Run `rag/ingest.py` to build the vector database (~1 min)219   - Start the Chainlit server on port 78602204. When the build is complete the Space shows **Running** (green)2215. Click the app URL to open the assistant222 223Total first-build time: **~10โ€“15 minutes**. Subsequent pushes are faster due to Docker layer caching.224 225---226 227### Step 5 โ€” Updating the app228 229Every time you push to the `space` remote, HuggingFace rebuilds and redeploys automatically:230 231```bash232# Make your changes, commit, then:233git push space master234```235 236---237 238### What the Dockerfile does at build time239 240| Step            | What happens                                                         | Secrets needed?     |241| --------------- | -------------------------------------------------------------------- | ------------------- |242| `uv sync`       | Installs all Python dependencies                                     | No                  |243| Download models | Pulls `all-MiniLM-L6-v2` + `ms-marco-MiniLM-L-6-v2` from HuggingFace | No                  |244| `rag/ingest.py` | Builds ChromaDB vector store from raw `.md` + `.csv` docs            | No                  |245| Runtime start   | Launches Chainlit on port 7860                                       | `OPENAI_API_KEY` โœ… |246 247Models and vector DB are **baked into the image** โ€” zero cold-start delay.248 249---250