Download README.md from Khaled0fouad000/knowledge-assistant-api: direct link, hf CLI and curl.
- Browser
- Download file 8.55 kB
-
https://huggingface.co/spaces/Khaled0fouad000/knowledge-assistant-api/resolve/main/README.md
- Command line
-
hf download hf://spaces/Khaled0fouad000/knowledge-assistant-api/README.md
-
curl -L -o README.md https://huggingface.co/spaces/Khaled0fouad000/knowledge-assistant-api/resolve/main/README.md
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.
Preview
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 π€π½
π License
MIT β free to use, modify, and distribute.
