Fennec Logo Fennec
Fennec Community community/rag/rag_ui.md

πŸ–₯️ RagChatUI β€” Professional Chat Interface

Overview

RagChatUI turns any RAGSystem into a professional web-based chat interface that runs in the browser. Built on Flask, it supports: file uploads, multiple sessions, live-streaming answers, and programmatic usage without a UI.


Installation & Import

from fennec_community.rag.rag_ui import RagChatUI

Additional Requirements

pip install flask

Core Methods

__init__

RagChatUI(
    rag,
    title="RAG Chat",
    description="you can ask about anything in your documents",
    theme="dark",
    max_sessions=30,
    include_sources=True,
    language=None,  # Best left as None so it responds in the same language as the question
    allowed_extensions=None,
    on_upload=None
)
Parameter Type Default Description
rag Any β€” RAGSystem (required)
title str "RAG Chat" Interface title shown in the browser
description str "you can ask..." Description shown on the welcome screen
theme str "dark" "dark" or "light"
max_sessions int 30 Maximum number of sessions held in memory
include_sources bool True Show answer sources
language str None Force a specific response language
allowed_extensions set Auto File extensions allowed for upload
on_upload Callable None Custom upload handler fn(path, doc_id) -> str

launch

launch(
    host: str = "0.0.0.0",
    port: int = 7860,
    open_browser: bool = True,
    debug: bool = False
) -> None

Description: Starts the interface server. Opens the browser automatically.

Parameter Type Description
host str Listening address
port int Port number
open_browser bool Automatically open the browser
debug bool Flask debug mode

ask

ask(query: str, session_id: Optional[str] = None) -> Dict[str, Any]

Description: Send a question programmatically (without the UI).

Returns:

{
    "session_id": "abc-123",
    "session_title": "Session 1",
    "answer": "The answer",
    "sources": [...],
    "latency_ms": 350.2,
    "msg_id": "msg-456"
}

create_session / get_session / delete_session

session = ui.create_session()       # Create a new session
session = ui.get_session(sid)       # Retrieve a session
ok = ui.delete_session(sid)         # Delete a session

get_stats

get_stats() -> Dict[str, Any]

Returns:

{
    "sessions": 3,
    "total_messages": 42,
    "rag": {...}
}

REST API

The interface automatically exposes the following endpoints:

Path Method Description
GET / GET Main page
GET /api/health GET Health check
POST /api/session/new POST Create a new session
GET /api/sessions GET List all sessions
GET /api/session/<sid> GET Session details
DELETE /api/session/<sid> DELETE Delete a session
POST /api/session/<sid>/clear POST Clear session messages
POST /api/chat POST Send a question
POST /api/chat/stream POST Send a question with SSE streaming
POST /api/upload POST Upload a file
GET /api/stats GET Statistics

Full Practical Example

from fennec_community.llm import MistralInterface
from fennec_community.document_loaders import TextLoader 
from fennec_community.vector_database import FAISSVectorDatabase
from fennec_community.chunks import ArabicTextChunker
from fennec_community.context import ContextManager
from fennec_community.embeddings import OllamaEmbedder
from fennec_community.rag.core import RAGSystem 
from fennec_community.rag.rag_ui import RagChatUI       

# --- 1. Set up the RAG system ---
loader_1 = TextLoader("./data_kn/faq.txt").load()
chunker = ArabicTextChunker(chunk_size=100, overlap=20)
embedder = OllamaEmbedder()
vector_db = FAISSVectorDatabase(embedder=embedder)
llm = MistralInterface(api_key=llm_api)
context_manager = ContextManager()
rag_system = RAGSystem(llm=llm, vector_db=vector_db,chunker=chunker, context_manager=context_manager)


rag_system.add_documents(loader_1)
def custom_upload_handler(file_path: str, doc_id: str) -> str:
    import PyPDF2
    with open(file_path, "rb") as f:
        reader = PyPDF2.PdfReader(f)
        text = "\n".join(p.extract_text() for p in reader.pages)
    rag_system.add_documents({doc_id: text})
    return f"βœ… ΨͺΩ… رفع PDF وΨ₯آافΨͺΩ‡ ({len(text)} حرف)"

# --- 4. Ψ₯Ω†Ψ΄Ψ§Ψ‘ Ψ§Ω„ΩˆΨ§Ψ¬Ω‡Ψ© ---
ui = RagChatUI(
    rag=rag_system,
    title="Ω…Ψ³Ψ§ΨΉΨ― Ψ§Ω„Ψ΄Ψ±ΩƒΨ© Ψ§Ω„Ψ°ΩƒΩŠ",
    description="Ψ§Ψ³Ψ£Ω„Ω†ΩŠ ΨΉΩ† أي شيؑ Ω…ΨͺΨΉΩ„Ω‚ Ψ¨Ψ§Ω„Ψ΄Ψ±ΩƒΨ©ΨŒ Ψ§Ω„Ω…Ω†ΨͺΨ¬Ψ§Ψͺ، أو Ψ§Ω„Ψ―ΨΉΩ… Ψ§Ω„ΨͺΩ‚Ω†ΩŠ",
    theme="dark",
    max_sessions=50,
    include_sources=True,
    allowed_extensions={".pdf", ".txt", ".docx"},
    on_upload=custom_upload_handler,
)
session = ui.create_session()
sid = session.session_id
ui.launch()
Source: community/rag/rag_ui.md