Khaled0fouad000's picture
add README
b344f06
|
Raw History Blame Contribute Delete
8.55 kB
metadata
title: Knowledge Assistant API
emoji: πŸ€™πŸ½
colorFrom: blue
colorTo: green
sdk: docker
pinned: false

πŸ€™πŸ½ AI Knowledge Assistant

Turn any document into a smart chatbot β€” with citations, memory, and hybrid search.

Live Demo Telegram Bot API Python FastAPI License


Preview

AI Knowledge Assistant


What Is This?

AI Knowledge Assistant is a production-ready RAG (Retrieval-Augmented Generation) system that ingests documents from multiple sources, builds a searchable knowledge base, and answers questions with accurate, cited responses.

Upload a PDF, paste a URL, or send a CSV β€” then ask anything. The assistant retrieves the most relevant chunks using hybrid search (semantic + keyword), and generates answers grounded in your document β€” not hallucinated.

Available as a web app, a Telegram bot, and a REST API.


Features

  • 🧠 Hybrid RAG Search β€” Combines FAISS semantic search with BM25 keyword retrieval for higher accuracy
  • πŸ“„ Multi-source Ingestion β€” PDF, TXT, CSV, DOCX, and URLs
  • πŸ’¬ Conversation Memory β€” Maintains context across multiple turns per session
  • πŸ“Ž Page-level Citations β€” Every answer references its source page or chunk
  • πŸ€– Telegram Bot β€” Chat with your documents directly from Telegram
  • 🌐 Next.js Frontend β€” Clean, responsive web interface
  • ⚑ Fast Inference β€” Powered by Groq (LLaMA 3.3 70B) for near-instant responses
  • πŸ”„ Session Management β€” Independent conversation history per user/session

How It Works

User (Web / Telegram)
        ↓
  Upload Document
        ↓
  Text Extraction  ──→  Chunking  ──→  Embeddings  ──→  FAISS Index
                                                              ↓
  User Question  ──→  Hybrid Retrieval (FAISS + BM25)
                                ↓
                      Top K Relevant Chunks
                                ↓
                  Groq LLM (LLaMA 3.3 70B) + Chat History
                                ↓
                    Answer with Page Citations

Why Hybrid Search?

Pure semantic search misses exact keyword matches. Pure BM25 misses conceptual similarity. Combining both covers more ground β€” the system deduplicates results and returns the best chunks from either approach.


πŸ—‚οΈ Project Structure

knowledge-assistant/
β”‚
β”œβ”€β”€ main.py             # FastAPI backend β€” all endpoints, RAG logic, session management
β”œβ”€β”€ bot.py              # Telegram bot β€” file handling, URL ingestion, message routing
β”œβ”€β”€ app.py              # Streamlit UI β€” lightweight web interface
β”‚
β”œβ”€β”€ requirements.txt    # Python dependencies
β”œβ”€β”€ Dockerfile          # Container configuration
β”œβ”€β”€ Procfile            # Process definitions
β”‚
└── README.md

Frontend (separate repo):

knowledge-frontend/
β”‚
└── app/
    └── page.tsx        # Next.js chat interface β€” file upload, URL input, chat UI

βš™οΈ Setup & Run

1. Clone the repository

git clone https://github.com/Khaled-fouad0/knowledge-assistant.git
cd knowledge-assistant

2. Install dependencies

pip install -r requirements.txt

3. Configure environment

Create a .env file:

GROQ_API_KEY=your_groq_key_here
TELEGRAM_TOKEN=your_telegram_token_here

Get a free Groq key at console.groq.com β€” no credit card required.

4. Run the backend

uvicorn main:app --reload

API available at http://localhost:8000
Interactive docs at http://localhost:8000/docs

5. Run the Telegram bot (optional)

python bot.py

6. Run the Streamlit UI (optional)

streamlit run app.py

πŸ”Œ API Reference

Upload Endpoints

Method Endpoint Description
POST /upload Upload a PDF file
POST /upload_text Upload a TXT file
POST /upload_csv Upload a CSV file
POST /upload_docx Upload a DOCX file
POST /upload_url Ingest content from a URL

Example β€” Upload PDF:

curl -X POST http://localhost:8000/upload \
  -F "file=@document.pdf"

Response:

{
  "status": "success",
  "pages": 12,
  "chunks": 47,
  "filename": "document.pdf"
}

Conversation Endpoints

POST /ask

Ask a question about the uploaded document.

Request:

{
  "message": "What is the main conclusion of the report?",
  "session_id": "user_123"
}

Response:

{
  "answer": "According to page 4, the main conclusion is...",
  "session_id": "user_123"
}

POST /chat

General chat without document context (no RAG).

Request:

{
  "message": "Explain what a vector database is.",
  "session_id": "user_123"
}

DELETE /reset/{session_id}

Clear conversation history for a session.

curl -X DELETE http://localhost:8000/reset/user_123

POST /upload_url

Ingest a webpage into the knowledge base.

Request:

{
  "url": "https://example.com/article"
}

Response:

{
  "status": "success",
  "chunks": 31,
  "url": "https://example.com/article"
}

πŸ€– Telegram Bot Usage

Find the bot at @7asaneen_bot

Action How
Upload a file Send any PDF, TXT, CSV, or DOCX directly
Ingest a URL /url https://example.com
Ask a question Just type your message
Start / Help /start

🧱 Tech Stack

Layer Technology
Backend FastAPI + Uvicorn
LLM Groq β€” LLaMA 3.3 70B
Embeddings HuggingFace β€” all-MiniLM-L6-v2
Vector Store FAISS
Keyword Search BM25 (rank-bm25)
PDF Parsing PyMuPDF (fitz)
Web Scraping httpx + BeautifulSoup4
Memory LangChain ChatMessageHistory
Frontend Next.js + Tailwind CSS
Alt UI Streamlit
Bot python-telegram-bot
Deployment Hugging Face Spaces + Vercel
Container Docker

πŸš€ Deployment

Backend β€” Hugging Face Spaces (Docker)

The backend is deployed as a Docker Space on Hugging Face.
The Dockerfile and Procfile are included in the repo.

Live API: khaled0fouad000-knowledge-assistant-api.hf.space

Frontend β€” Vercel

The Next.js frontend is deployed on Vercel.

Live app: knowledge-frontend-ten.vercel.app


πŸ”’ Notes

  • allow_origins=["*"] is set for development β€” restrict in production
  • Each session maintains independent conversation history
  • The document index is shared globally β€” designed for single-user or demo use; extend with per-session vectorstores for multi-tenant production use
  • LLM responses are grounded in retrieved context only β€” hallucination is minimized by design

🚧 Possible Extensions

  • Per-session document isolation (multi-tenant support)
  • Persistent vectorstore (save/load FAISS index to disk)
  • Streaming responses (SSE / token-by-token)
  • Multi-document support (upload and query multiple files at once)
  • Confidence scoring per answer
  • Admin dashboard for session monitoring
  • Authentication layer (API keys or OAuth)
  • Support for more file types (PPTX, XLSX, audio transcripts)

πŸ‘€ Author

Built by Khaled πŸ€™πŸ½

GitHub


πŸ“„ License

MIT β€” free to use, modify, and distribute.