ppsingh commited on
Commit
3f62146
·
1 Parent(s): b9ffe28

adding components

Browse files
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Chabo Dev
3
- emoji: 🌍
4
- colorFrom: yellow
5
- colorTo: green
6
  sdk: gradio
7
  sdk_version: 5.47.2
8
  app_file: app.py
 
1
  ---
2
  title: Chabo Dev
3
+ emoji: 🤖 [⚙️]
4
+ colorFrom: indigo
5
+ colorTo: purple
6
  sdk: gradio
7
  sdk_version: 5.47.2
8
  app_file: app.py
__pycache__/fileingestor.cpython-310.pyc ADDED
Binary file (1.51 kB). View file
 
__pycache__/generator.cpython-310.pyc ADDED
Binary file (3.53 kB). View file
 
__pycache__/orchestrator.cpython-310.pyc ADDED
Binary file (23.7 kB). View file
 
__pycache__/retriever.cpython-310.pyc ADDED
Binary file (3.77 kB). View file
 
__pycache__/vectorDB.cpython-310.pyc ADDED
Binary file (2.19 kB). View file
 
app.py CHANGED
@@ -1,5 +1,9 @@
1
  import gradio as gr
2
-
 
 
 
 
3
  # Define the HTML template for embedding an external Hugging Face Space
4
  # Replace <space-url> with your actual Space URLs (e.g., "https://hf.co/spaces/user/app-name")
5
  def embed_space(space_url: str, height: int = 800) -> gr.HTML:
@@ -16,41 +20,130 @@ def embed_space(space_url: str, height: int = 800) -> gr.HTML:
16
  return gr.HTML(iframe_html)
17
 
18
  # --- Define the Layout ---
19
- with gr.Blocks(theme=gr.themes.Monochrome()) as dashboard_app:
20
  gr.Markdown(
21
  """
22
- # 🚀 Professional ML Dashboard Hub
23
- Welcome to the central hub for all our machine learning applications.
24
  """
25
  )
26
 
27
  with gr.Tabs():
28
 
29
- # 1. Local App Tab (Integrated Directly)
30
- # This is for apps whose code is running within this main Gradio instance
31
- with gr.Tab("Data Analyzer"):
32
- gr.Markdown("## Local Data Analysis Tool")
33
-
34
- with gr.Row():
35
- name_input = gr.Textbox(label="Enter Name")
36
- greeting_output = gr.Textbox(label="Greeting")
37
-
38
- def greet(name):
39
- return f"Hello, {name}! Ready to analyze your data."
40
-
41
- name_input.change(fn=greet, inputs=name_input, outputs=greeting_output)
42
-
43
- # 2. External Space 1 Tab (Embedded via iframe)
44
- # Replace the placeholder URL with your actual Space URL
45
- with gr.Tab("Image Classifier"):
46
- gr.Markdown("## External Image Classification Space")
47
- embed_space("https://hf.co/spaces/gradio/image-classification", height=700)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- # 3. External Space 2 Tab (Embedded via iframe)
50
- # Replace the placeholder URL with your actual Space URL
51
- with gr.Tab("LLM Chatbot"):
52
- gr.Markdown("## External LLM Chatbot Space")
53
- embed_space("https://hf.co/spaces/HuggingFaceH4/open-llm-leaderboard", height=700)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  # Launch the app
56
  dashboard_app.launch()
 
1
  import gradio as gr
2
+ from vectorDB import vectordbText
3
+ from retriever import retriverText
4
+ from fileingestor import fileingestorText
5
+ from generator import generatorText
6
+ from orchestrator import orchestratorText
7
  # Define the HTML template for embedding an external Hugging Face Space
8
  # Replace <space-url> with your actual Space URLs (e.g., "https://hf.co/spaces/user/app-name")
9
  def embed_space(space_url: str, height: int = 800) -> gr.HTML:
 
20
  return gr.HTML(iframe_html)
21
 
22
  # --- Define the Layout ---
23
+ with gr.Blocks(theme=gr.themes.Monochrome(), fill_width = True) as dashboard_app:
24
  gr.Markdown(
25
  """
26
+ # 🚀 ChaBo: Modular chatbot framework
27
+ Discover the essential microservices hub designed for the modular development and efficient deployment of Retrieval-Augmented Generation (RAG) chatbots
28
  """
29
  )
30
 
31
  with gr.Tabs():
32
 
33
+ # 1. About us
34
+ # This is info page
35
+ with gr.Tab("About ChaBo"):
36
+ with gr.Row(elem_classes = "centered-content-row"):
37
+ gr.Markdown("""## ChaBo: A modular chatbot framework
38
+ This framework is designed around a microservices architecture \
39
+ allowing different conversational components (like Vector database, Retrieval, Generator and other components)\
40
+ to be developed, deployed, and scaled independently. \
41
+ This design promotes flexibility and robust, complex chatbot development \
42
+ by enabling developers to easily swap out or upgrade individual services.
43
+
44
+ **Note**: As of now this is more adapted towards delpoyment of these services \
45
+ individually as individual spaces on HF infra, soon we will be releasing \
46
+ the docker-compose method for dedicated deployment
47
+ """)
48
+
49
+ # 2. Vector DB
50
+ with gr.Tab("Vector Database: Qdrant"):
51
+ with gr.Row(elem_classes = "centered-content-row"):
52
+ with gr.Column(scale=1):
53
+ gr.Markdown("### What is a Vector Database?")
54
+ gr.Markdown("""
55
+ A Vector Database is a specialized database designed to efficiently store, manage, \
56
+ and retrieve **vector embeddings**—high-dimensional numerical representations of \
57
+ unstructured data like text, images, or audio.It is the cornerstone of modern AI \
58
+ applications like semantic search and Retrieval-Augmented Generation (RAG). \
59
+ Unlike traditional databases, a vector database excels at **Nearest Neighbor Search (NNS)**, \
60
+ allowing it to quickly find semantically similar data points, which is essential for \
61
+ grounding large language models with external knowledge.
62
+ """)
63
+ gr.Markdown(vectordbText)
64
+ gr.Image(
65
+ value="qdrant.png", # <- Change this file path
66
+ label="Qdrant Dashboard",
67
+ show_label=True,
68
+ container=False,)
69
+
70
 
71
+ # 3. Retriever and Reranker (Embedded via iframe)
72
+ with gr.Tab("Retriever and Reranker"):
73
+ with gr.Row(elem_classes = "centered-content-row"):
74
+ with gr.Column(scale=1):
75
+ gr.Markdown("## What is Retriever ?")
76
+ gr.Markdown("""It is the crucial process of efficiently finding and extracting relevant \
77
+ information from a vast knowledge base to ground and inform the chatbot's final answer.""")
78
+ gr.Markdown(retriverText)
79
+ embed_space("https://giz-chatfed-retriever0-3.hf.space", height=700)
80
+
81
+ # 4. File Ingestor (Embedding via iframe pending due to compliance and Readme documentation missing)
82
+ with gr.Tab("File Ingestor"):
83
+ with gr.Row(elem_classes = "centered-content-row"):
84
+ with gr.Column(scale=1):
85
+ gr.Markdown("## What is File Ingestor")
86
+ gr.Markdown("""In certain chatbot use-cases it might be that user input can be a file upload,\
87
+ on top of existing Vector Database. In this case it's important that we ingest this \
88
+ file and use it for next for relevant use """)
89
+ gr.Markdown(fileingestorText)
90
+ ### Enables this once space is public: after Proper EU aI act compliance release embed_space("https://giz-chatfed-whisp.hf.space", height=700)
91
+
92
+ # 5. Generator
93
+ with gr.Tab("Generator"):
94
+ with gr.Row(elem_classes = "centered-content-row"):
95
+ with gr.Column(scale=1):
96
+ gr.Markdown("## What is Generator?")
97
+ gr.Markdown("""It is the microserviceis the crucial process of efficiently finding and extracting relevant \
98
+ information from a vast knowledge base to ground and inform the chatbot's final answer.""")
99
+ gr.Markdown(generatorText)
100
+
101
+ # 6. Orchest
102
+ with gr.Tab("Orchestrator"):
103
+ with gr.Row(elem_classes = "centered-content-row"):
104
+ with gr.Column(scale=1):
105
+ gr.Markdown("## What is Orchestrator ?")
106
+ gr.Markdown(""" The Orchestrator is the central command module, defining the exact \
107
+ steps and flow of data: it sequences the initial user prompt, directs the query \
108
+ to the correct vector retrieval module, manages the document reranking (if applicable),\
109
+ and finally routes the retrieved context and original prompt to the Large Language Model \
110
+ (LLM) for final answer generation..""")
111
+ gr.Markdown(orchestratorText)
112
+
113
+ with gr.Tab("HuggingFace Chat UI"):
114
+ with gr.Row(elem_classes = "centered-content-row"):
115
+ with gr.Column(scale=1):
116
+ gr.Markdown("## What is Retriever ?")
117
+ gr.Markdown("""It is the crucial process of efficiently finding and extracting relevant \
118
+ information from a vast knowledge base to ground and inform the chatbot's final answer.""")
119
+ gr.Markdown(""" This mciroservice integrates with the vector database to retrieve semantically relevant documents,\
120
+ with optional reranking for precision, ready for seamless use in ChaBo RAG workflows. \
121
+
122
+ For more info on Retriever and code base visit the following links:
123
+ - ChaBo_Retriever : [**ReadMe**](https://huggingface.co/spaces/GIZ/chatfed_retriever0.3/blob/main/README.md)
124
+ - ChaBo_Retriever: [**Codebase**](https://huggingface.co/spaces/GIZ/chatfed_retriever0.3/tree/main)""")
125
+ embed_space("https://giz-chatfed-retriever0-3.hf.space", height=700)
126
 
127
+ with gr.Tab("Integrated UI"):
128
+ with gr.Row(elem_classes = "centered-content-row"):
129
+ with gr.Column(scale=1):
130
+ gr.Markdown("## What is Retriever ?")
131
+ gr.Markdown("""It is the crucial process of efficiently finding and extracting relevant \
132
+ information from a vast knowledge base to ground and inform the chatbot's final answer.""")
133
+ gr.Markdown(""" This mciroservice integrates with the vector database to retrieve semantically relevant documents,\
134
+ with optional reranking for precision, ready for seamless use in ChaBo RAG workflows. \
135
+
136
+ For more info on Retriever and code base visit the following links:
137
+ - ChaBo_Retriever : [**ReadMe**](https://huggingface.co/spaces/GIZ/chatfed_retriever0.3/blob/main/README.md)
138
+ - ChaBo_Retriever: [**Codebase**](https://huggingface.co/spaces/GIZ/chatfed_retriever0.3/tree/main)""")
139
+ embed_space("https://giz-chatfed-retriever0-3.hf.space", height=700)
140
+
141
+
142
+ dashboard_app.css = """
143
+ .centered-content-row {
144
+ max-width: 1000px; /* Adjust this value for your desired max width */
145
+ margin: 0 auto; /* Centers the container horizontally */
146
+ }
147
+ """
148
  # Launch the app
149
  dashboard_app.launch()
fileingestor.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ fileingestorText = """ This mciroservice integrates with the Orchestrator and HuggingFace Chat UI and \
2
+ uses the deterministic tool for further processing/actions. \
3
+
4
+ [ChaBo_FileIngestor](https://huggingface.co/spaces/GIZ/eudr_chabo_ingestor) hosts a microservice which takes the \
5
+ Geojson file input and calls [WHISP API]("https://whisp.openforis.org/api/submit/geojson").
6
+
7
+ **API documentation**: 1 API Endpoint
8
+
9
+ ### api_name: /ingest
10
+
11
+ Params:
12
+ - filepath(filepath): Required
13
+
14
+ Returns(str): Relevant response base don internal code of the microservice.
15
+
16
+ **How to Connect**
17
+
18
+ ```python
19
+
20
+ from gradio_client import Client, handle_file
21
+ client = Client("https://giz-eudr-chabo-ingestor.hf.space/")
22
+ result = client.predict(
23
+ file=handle_file('https://github.com/gradio-app/gradio/raw/main/test/test_files/sample_file.pdf'),
24
+ api_name="/ingest"
25
+ )
26
+ ```
27
+ """
generator.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ generatorText = """ This microservice integrates with the Retriever to answer the user query in ChaBo RAG workflows. \
2
+
3
+ # ChaBo Generator on Hugging Face Spaces
4
+
5
+ [ChaBo_Generator](https://huggingface.co/spaces/GIZ/eudr_chabo_generator/blob/main/README.md) Space hosts \
6
+ a Generator microservice for answering user query. This is just a Infrastructural component and doesnt\
7
+ not serve any user application through its User Interfaceas the its consumed in ChaBo workflow thorugh Orchestrator.
8
+
9
+ ChaBo Generator - MCP Server
10
+
11
+ A language model-based generation service designed for ChatFed RAG\
12
+ (Retrieval-Augmented Generation) pipelines. This module serves as an \
13
+ **MCP (Model Context Protocol) server** that generates contextual responses \
14
+ using configurable LLM providers with support for retrieval result processing.
15
+
16
+ **API Endpoint**: 1 API which provides context-aware text generation using \
17
+ configurable LLM providers when properly configured with API credentials.
18
+
19
+ ### api_name: /generate
20
+
21
+ Parameters:
22
+ - `query` (str, required): The question or query to be answered
23
+ - `context` (str|list, required): Context for answering - can be plain text or list of retrieval result dictionaries
24
+
25
+ Returns: String containing the generated answer based on the provided context and query.
26
+
27
+ **Hot to connect**:
28
+ ```python
29
+ from gradio_client import Client
30
+
31
+ client = Client("ENTER CONTAINER URL / SPACE ID")
32
+ result = client.predict(
33
+ query="What are the key findings?",
34
+ context="Your relevant documents or context here...",
35
+ api_name="/generate"
36
+ )
37
+ print(result)
38
+ ```
39
+
40
+ #### Configuration
41
+
42
+ LLM Provider Configuration:
43
+ 1. Set your preferred inference provider in `params.cfg`
44
+ 2. Configure the model and generation parameters
45
+ 3. Set the required API key environment variable
46
+ 4. [Optional] Adjust temperature and max_tokens settings
47
+ 5. Run the app:
48
+
49
+ ```bash
50
+ docker build -t chatfed-generator .
51
+ docker run -p 7860:7860 chatfed-generator
52
+ ```
53
+
54
+ #### Environment Variables Required
55
+
56
+ Make sure to set the appropriate environment variables:
57
+ - OpenAI: `OPENAI_API_KEY`
58
+ - Anthropic: `ANTHROPIC_API_KEY`
59
+ - Cohere: `COHERE_API_KEY`
60
+ - HuggingFace: `HF_TOKEN`
61
+
62
+ For more info on Retriever and code base visit the following links:
63
+ - ChaBo_Generator : [**ReadMe**](https://huggingface.co/spaces/GIZ/eudr_chabo_generator/blob/main/README.md)
64
+ - ChaBo_Generator: [**Codebase**](https://huggingface.co/spaces/GIZ/eudr_chabo_generator/tree/main)"""
orchestrator.py ADDED
@@ -0,0 +1,532 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ orchestratorText = """ # Chabo Orchestrator Documentation
2
+ ## Table of Contents
3
+ 1. Overview
4
+ 2. System Architecture
5
+ 3. Components
6
+ 4. Configuration
7
+ 5. Deployment Guide
8
+ 6. API Reference
9
+ 7. Usage Examples
10
+ 8. Troubleshooting
11
+
12
+ ## Overview
13
+
14
+ The Chabo Orchestrator is the central coordination module of the Chabo RAG system. It orchestrates the flow between multiple microservices to provide intelligent document processing and question-answering capabilities. The system is designed for deployment on Huggingface Spaces.
15
+
16
+ ### Key Features
17
+ - **Workflow Orchestration**: Uses LangGraph to manage complex processing pipelines
18
+ - **Multi-Modal Support**: Handles files dependent on ChatUI and Ingestor config (e.g. PDF, DOCX, GeoJSON, and JSON )
19
+ - **Streaming Responses**: Real-time response generation with Server-Sent Events (SSE)
20
+ - **Dual Processing Modes**:
21
+ - **Direct Output Mode**: Returns ingestor results immediately (e.g. EUDR use case)
22
+ - **Standard RAG Mode**: Full retrieval-augmented generation pipeline
23
+ - **Intelligent Caching**: Prevents redundant file processing (e.g. EUDR use case)
24
+ - **Multiple Interfaces**: FastAPI endpoints for modules; LangServe endpoints for ChatUI; Gradio UI for testing
25
+
26
+
27
+ ## System Architecture
28
+
29
+ ### High-Level Architecture
30
+
31
+ ```
32
+ ┌─────────────────┐
33
+ │ ChatUI │
34
+ │ Frontend │
35
+ └────────┬────────┘
36
+ │ HTTP/SSE
37
+
38
+ ┌─────────────────────────────────┐
39
+ │ Chabo Orchestrator │
40
+ │ ┌─────────────────────────┐ │
41
+ │ │ LangGraph Workflow │ │
42
+ │ │ ┌─────────────────┐ │ │
43
+ │ │ │ Detect File │ │ │
44
+ │ │ │ Type │ │ │
45
+ │ │ └────────┬────────┘ │ │
46
+ │ │ │ │ │
47
+ │ │ ┌────────▼────────┐ │ │
48
+ │ │ │ Ingest File │ │ │
49
+ │ │ └────────┬────────┘ │ │
50
+ │ │ │ │ │
51
+ │ │ ┌─────┴──────┐ │ │
52
+ │ │ │ │ │ │
53
+ │ │ ┌──▼───┐ ┌────▼───┐ │ │
54
+ │ │ │Direct│ │Retrieve│ │ │
55
+ │ │ │Output│ │Context │ │ │
56
+ │ │ └──┬───┘ └────┬───┘ │ │
57
+ │ │ │ │ │ │
58
+ │ │ │ ┌────▼───┐ │ │
59
+ │ │ │ │Generate│ │ │
60
+ │ │ │ │Response│ │ │
61
+ │ │ │ └────────┘ │ │
62
+ │ └──────┴──────────────────┘ │
63
+ └──────┬───────────┬──────────┬───┘
64
+ │ │ │
65
+ ┌───▼──┐ ┌───▼───┐ ┌──▼────┐
66
+ │Ingest│ │Retrie-│ │Genera-│
67
+ │or │ │ver │ │tor │
68
+ └──────┘ └───────┘ └───────┘
69
+ ```
70
+
71
+ ### Component Communication
72
+
73
+ All communication between modules happens over HTTP:
74
+ - **Orchestrator ↔ Ingestor**: Gradio Client (file upload, processing)
75
+ - **Orchestrator ↔ Retriever**: Gradio Client (semantic search)
76
+ - **Orchestrator ↔ Generator**: HTTP streaming (SSE for real-time responses)
77
+ - **ChatUI ↔ Orchestrator**: LangServe streaming endpoints
78
+
79
+ ### Workflow Logic
80
+
81
+ The orchestrator implements two distinct workflows:
82
+
83
+ **Direct Output Workflow** (when `DIRECT_OUTPUT=True` and file is new):
84
+ ```
85
+ File Upload → Detect Type → Ingest → Direct Output → Return Results
86
+ ```
87
+
88
+ **Standard RAG Workflow** (default or cached files):
89
+ ```
90
+ Query → Retrieve Context → Generate Response → Stream to User
91
+ ```
92
+
93
+ ## Components
94
+
95
+ ### 1. Main Application (`main.py`)
96
+
97
+ - LangServe endpoints for ChatUI integration
98
+ - Gradio web interface for testing
99
+ - FastAPI endpoints for diagnostics and future use (e.g. /health)
100
+ - Cache management endpoint (for direct output use cases)
101
+
102
+ **Key Functions:**
103
+ - `chatui_adapter()`: Handles text-only queries
104
+ - `chatui_file_adapter()`: Handles file uploads with queries
105
+ - `create_gradio_interface()`: Test UI
106
+
107
+ ### 2. Workflow Nodes (`nodes.py`)
108
+
109
+ LangGraph nodes that implement the processing pipeline:
110
+
111
+ **Node Functions:**
112
+
113
+ - `detect_file_type_node()`: Identifies file type and determines routing
114
+ - `ingest_node()`: Processes files through appropriate ingestor
115
+ - `direct_output_node()`: Returns raw ingestor results
116
+ - `retrieve_node()`: Fetches relevant context from vector store
117
+ - `generate_node_streaming()`: Streams LLM responses
118
+ - `route_workflow()`: Conditional routing logic
119
+
120
+ **Helper Functions:**
121
+
122
+ - `process_query_streaming()`: Unified streaming interface
123
+ - `compute_file_hash()`: SHA256 hashing for deduplication
124
+ - `clear_direct_output_cache()`: Cache management
125
+
126
+ ### 3. Data Models (`models.py`)
127
+
128
+ Pydantic models for type validation
129
+
130
+ ### 4. Retriever Adapter (`retriever_adapter.py`)
131
+
132
+ Abstraction layer for managing different retriever configurations:
133
+ - Handles authentication
134
+ - Formats queries and filters
135
+
136
+ ### 5. Utilities (`utils.py`)
137
+
138
+ Helper functions
139
+
140
+ #### Conversation Context Management
141
+
142
+ The `build_conversation_context()` function manages conversation history to provide relevant context to the generator while respecting token limits and conversation flow.
143
+
144
+ **Key Features:**
145
+
146
+ - **Context Selection**: Always includes the first user and assistant messages to maintain conversation context
147
+ - **Recent Turn Limiting**: Includes only the last N complete turns (user + assistant pairs) to focus on recent conversation (default: 3)
148
+ - **Character Limit Management**: Truncates to maximum character limits to prevent context overflow
149
+
150
+
151
+ **Function Parameters:**
152
+
153
+ ```python
154
+ def build_conversation_context(
155
+ messages, # List of Message objects from conversation
156
+ max_turns: int = 3, # Maximum number of recent turns to include
157
+ max_chars: int = 8000 # Maximum total characters in context
158
+ ) -> str
159
+ ```
160
+
161
+ ## Configuration
162
+
163
+ ### Configuration File (`params.cfg`)
164
+
165
+ ```ini
166
+ [file_processing]
167
+ # Enable direct output mode: when True, ingestor results are returned directly
168
+ # without going through the generator. When False, all files go through full RAG pipeline.
169
+ # This also prevents ChatUI from resending the file in the conversation history with each turn
170
+ # Note: File type validation is handled by the ChatUI frontend
171
+ DIRECT_OUTPUT = True
172
+
173
+ [conversation_history]
174
+ # Limit the context window for the conversation history
175
+ MAX_TURNS = 3
176
+ MAX_CHARS = 12000
177
+
178
+ [retriever]
179
+ RETRIEVER = https://giz-chatfed-retriever0-3.hf.space/
180
+ # Optional
181
+ COLLECTION_NAME = EUDR
182
+
183
+ [generator]
184
+ GENERATOR = https://giz-eudr-chabo-generator.hf.space
185
+
186
+ [ingestor]
187
+ INGESTOR = https://giz-eudr-chabo-ingestor.hf.space
188
+
189
+ [general]
190
+ # need to include this for HF inference endpoint limits
191
+ MAX_CONTEXT_CHARS = 15000
192
+ ```
193
+
194
+ ### Environment Variables
195
+
196
+ Create a `.env` file with:
197
+
198
+ ```bash
199
+ # Required for private HuggingFace Spaces
200
+ HF_TOKEN=hf_xxxxxxxxxxxxxxxxxxxxx
201
+
202
+ ```
203
+
204
+ ### ChatUI Configuration
205
+
206
+ ChatUI `DOTENV_LOCAL` example deployment configuration:
207
+
208
+ ```javascript
209
+ MODELS=`[
210
+ {
211
+ "name": "asistente_eudr",
212
+ "displayName": "Asistente EUDR",
213
+ "description": "Retrieval-augmented generation on EUDR Whisp API powered by ChatFed modules.",
214
+ "instructions": {
215
+ "title": "EUDR Asistente: Instructiones",
216
+ "content": "Hola, soy Asistente EUDR, un asistente conversacional basado en inteligencia artificial diseñado para ayudarle a comprender el cumplimiento y el análisis del Reglamento de la UE sobre la deforestación. Responderé a sus preguntas utilizando los informes EUDR y los archivos GeoJSON cargados.\n\n💡 **Cómo utilizarlo (panel a la derecha)**\n\n**Modo de uso:** elija entre subir un archivo GeoJSON para su análisis o consultar los informes EUDR filtrados por país.\n\n**Ejemplos:** seleccione entre preguntas de ejemplo seleccionadas de diferentes categorías.\n\n**Referencias:** consulte las fuentes de contenido utilizadas para la verificación de datos.\n\n⚠️ Para conocer las limitaciones y la información sobre la recopilación de datos, consulte la pestaña «Exención de responsibilidad».\n\n⚠️ Al utilizar esta aplicación, usted acepta que recopilemos estadísticas de uso (como preguntas formuladas, comentarios realizados, duración de la sesión, tipo de dispositivo e información geográfica anónima) para comprender el rendimiento y mejorar continuamente la herramienta, basándonos en nuestro interés legítimo por mejorar nuestros servicios."
217
+ },
218
+ "multimodal": true,
219
+ "multimodalAcceptedMimetypes": [
220
+ "application/geojson"
221
+ ],
222
+ "chatPromptTemplate": "{{#each messages}}{{#ifUser}}{{content}}{{/ifUser}}{{#ifAssistant}}{{content}}{{/ifAssistant}}{{/each}}",
223
+ "parameters": {
224
+ "temperature": 0.0,
225
+ "max_new_tokens": 2048
226
+ },
227
+ "endpoints": [{
228
+ "type": "langserve-streaming",
229
+ "url": "https://giz-eudr-chabo-orchestrator.hf.space/chatfed-ui-stream",
230
+ "streamingFileUploadUrl": "https://giz-eudr-chabo-orchestrator.hf.space/chatfed-with-file-stream",
231
+ "inputKey": "text",
232
+ "fileInputKey": "files"
233
+ }]
234
+ }
235
+ ]`
236
+
237
+ PUBLIC_ANNOUNCEMENT_BANNERS=`[
238
+ {
239
+ "title": "This is Chat Prototype for DSC users",
240
+ "linkTitle": "Keep it Clean"
241
+ }
242
+ ]`
243
+
244
+ PUBLIC_APP_DISCLAIMER_MESSAGE="Disclaimer: AI is an area of active research with known problems such as biased generation and misinformation. Do not use this application for high-stakes decisions or advice. Do not insert your personal data, especially sensitive, like health data."
245
+ PUBLIC_APP_DESCRIPTION="Internal Chat-tool for DSC users for testing"
246
+
247
+ PUBLIC_APP_NAME="EUDR ChatUI"
248
+ ENABLE_ASSISTANTS=false
249
+ ENABLE_ASSISTANTS_RAG=false
250
+ COMMUNITY_TOOLS=false
251
+ MONGODB_URL=mongodb://localhost:27017
252
+
253
+ # Disable LLM-based title generation to prevent template queries
254
+ LLM_SUMMARIZATION=false
255
+ ```
256
+
257
+ Key things to ensure here:
258
+ - multimodalAcceptedMimetypes: file types to accept for upload via ChatUI
259
+ - endpoints: orchestrator url + endpoints
260
+
261
+ ## Deployment Guide
262
+
263
+ ### Local Development
264
+
265
+ **Prerequisites:**
266
+ - Python 3.10+
267
+ - pip
268
+
269
+ **Steps:**
270
+
271
+ 1. Clone the repository:
272
+ ```bash
273
+ git clone <your-repo-url>
274
+ cd chabo-orchestrator
275
+ ```
276
+
277
+ 2. Install dependencies:
278
+ ```bash
279
+ pip install -r requirements.txt
280
+ ```
281
+
282
+ 3. Configure the system:
283
+ ```bash
284
+ # Create .env file
285
+ echo "HF_TOKEN=your_token_here" > .env
286
+
287
+ # Edit params.cfg with your service URLs
288
+ nano params.cfg
289
+ ```
290
+
291
+ 4. Run the application:
292
+ ```bash
293
+ python app/main.py
294
+ ```
295
+
296
+ 5. Access interfaces:
297
+ - Gradio UI: http://localhost:7860/gradio
298
+ - API Docs: http://localhost:7860/docs
299
+ - Health Check: http://localhost:7860/health
300
+
301
+ ### Docker Deployment
302
+
303
+ **Build the image:**
304
+
305
+ ```bash
306
+ docker build -t chabo-orchestrator .
307
+ ```
308
+
309
+ **Run the container:**
310
+
311
+ ```bash
312
+ docker run -d \
313
+ --name chabo-orchestrator \
314
+ -p 7860:7860 \
315
+ chabo-orchestrator
316
+ ```
317
+
318
+ ### HuggingFace Spaces Deployment
319
+
320
+ **Repository Structure:**
321
+ ```
322
+ your-space/
323
+ ├── app/
324
+ │ ├── main.py
325
+ │ ├── nodes.py
326
+ │ ├── models.py
327
+ │ ├── retriever_adapter.py
328
+ │ └── utils.py
329
+ ├── Dockerfile
330
+ ├── requirements.txt
331
+ ├── params.cfg
332
+ └── README.md
333
+ ```
334
+
335
+ **Steps:**
336
+
337
+ 1. Create a new Space on HuggingFace
338
+ 2. Select "Docker" as the SDK
339
+ 3. Push your code to the Space repository
340
+ 4. Add secrets in Space settings:
341
+ - `HF_TOKEN`: Your HuggingFace token
342
+ 5. The Space will automatically build and deploy
343
+
344
+ **Important:** Ensure all service URLs in `params.cfg` are publicly accessible.
345
+
346
+ ### Docker Compose (Multi-Service)
347
+
348
+ Example orchestrated deployment for the entire Chabo stack (*NOTE - docker-compose will not run on Huggingface spaces*)
349
+
350
+ ```yaml
351
+ version: '3.8'
352
+
353
+ services:
354
+ orchestrator:
355
+ build: ./orchestrator
356
+ ports:
357
+ - "7860:7860"
358
+ environment:
359
+ - HF_TOKEN=${HF_TOKEN}
360
+ - RETRIEVER=http://retriever:7861
361
+ - GENERATOR=http://generator:7862
362
+ - INGESTOR=http://ingestor:7863
363
+ depends_on:
364
+ - retriever
365
+ - generator
366
+ - ingestor
367
+
368
+ retriever:
369
+ build: ./retriever
370
+ ports:
371
+ - "7861:7861"
372
+ environment:
373
+ - QDRANT_API_KEY=${QDRANT_API_KEY}
374
+
375
+ generator:
376
+ build: ./generator
377
+ ports:
378
+ - "7862:7862"
379
+ environment:
380
+ - HF_TOKEN=${HF_TOKEN}
381
+
382
+ ingestor:
383
+ build: ./ingestor
384
+ ports:
385
+ - "7863:7863"
386
+ ```
387
+
388
+ ## API Reference
389
+
390
+ ### Endpoints
391
+
392
+ #### Health Check
393
+ ```
394
+ GET /health
395
+ ```
396
+ Returns service health status.
397
+
398
+ **Response:**
399
+ ```json
400
+ {
401
+ "status": "healthy"
402
+ }
403
+ ```
404
+
405
+ #### Root Information
406
+ ```
407
+ GET /
408
+ ```
409
+ Returns API metadata and available endpoints.
410
+
411
+ #### Text Query (Streaming)
412
+ ```
413
+ POST /chatfed-ui-stream/stream
414
+ Content-Type: application/json
415
+ ```
416
+
417
+ **Request Body:**
418
+ ```json
419
+ {
420
+ "input": {
421
+ "text": "What are EUDR requirements?"
422
+ }
423
+ }
424
+ ```
425
+
426
+ **Response:** Server-Sent Events stream
427
+ ```
428
+ event: data
429
+ data: "The EUDR requires..."
430
+
431
+ event: sources
432
+ data: {"sources": [...]}
433
+
434
+ event: end
435
+ data: ""
436
+ ```
437
+
438
+ #### File Upload Query (Streaming)
439
+ ```
440
+ POST /chatfed-with-file-stream/stream
441
+ Content-Type: application/json
442
+ ```
443
+
444
+ **Request Body:**
445
+ ```json
446
+ {
447
+ "input": {
448
+ "text": "Analyze this GeoJSON",
449
+ "files": [
450
+ {
451
+ "name": "boundaries.geojson",
452
+ "type": "base64",
453
+ "content": "base64_encoded_content"
454
+ }
455
+ ]
456
+ }
457
+ }
458
+ ```
459
+
460
+ #### Clear Cache
461
+ ```
462
+ POST /clear-cache
463
+ ```
464
+ Clears the direct output file cache.
465
+
466
+ **Response:**
467
+ ```json
468
+ {
469
+ "status": "cache cleared"
470
+ }
471
+ ```
472
+
473
+ ### Gradio Interface
474
+
475
+ #### Interactive Query
476
+
477
+ Gradio's default API endpoint for UI interactions. If running on huggingface spaces, access via: https://[ORG_NAME]-[SPACE_NAME].hf.space/gradio/
478
+
479
+
480
+ ## Troubleshooting
481
+
482
+ ### Common Issues
483
+
484
+ #### 1. File Upload Fails
485
+
486
+ **Symptoms:** "Error reading file" or "Failed to decode uploaded file"
487
+
488
+ **Solutions:**
489
+ - Verify file is properly base64 encoded
490
+ - Check file size limits (default: varies by deployment)
491
+ - Ensure MIME type is in `multimodalAcceptedMimetypes`
492
+
493
+ #### 2. Slow Responses
494
+
495
+ **Symptoms:** Long wait times for responses
496
+
497
+ **Solutions:**
498
+ - Check network latency to external services
499
+ - Verify `MAX_CONTEXT_CHARS` isn't too high
500
+ - Consider enabling `DIRECT_OUTPUT` for suitable file types
501
+ - Check logs for retrieval/generation bottlenecks
502
+
503
+ #### 3. Cache Not Clearing
504
+
505
+ **Symptoms:** Same file shows cached results when it shouldn't
506
+
507
+ **Solutions:**
508
+ - Call `/clear-cache` endpoint
509
+ - Restart the service (clears in-memory cache)
510
+ - Check if `DIRECT_OUTPUT=True` in config
511
+
512
+ #### 4. Service Connection Errors
513
+
514
+ **Symptoms:** "Connection refused" or timeout errors
515
+
516
+ **Solutions:**
517
+ - Verify all service URLs in `params.cfg` are accessible
518
+ - Check HF_TOKEN is valid and has access to private spaces (*NOTE - THE ORCHESTRATOR CURRENTLY MUST BE PUBLIC*)
519
+ - Test each service independently with health checks
520
+ - Review firewall/network policies
521
+
522
+
523
+ ### Version History
524
+
525
+ - **v1.0.0**: Initial release with LangGraph orchestration
526
+ - Current implementation supports streaming, caching, and dual-mode processing
527
+
528
+ ---
529
+
530
+ **Documentation Last Updated:** 2025-10-01
531
+ **Compatible With:** Python 3.10+, LangGraph 0.2+, FastAPI 0.100+
532
+ """
qdrant.png ADDED
retriever.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ retriverText = """ This mciroservice integrates with the vector database to retrieve semantically relevant documents,\
2
+ with optional reranking for precision, ready for seamless use in ChaBo RAG workflows.
3
+
4
+ # Retriever and Reranker Microservice on Hugging Face Spaces
5
+
6
+ [ChaBo_Retrieval](https://huggingface.co/spaces/GIZ/chatfed_retriever0.3) hosts a Retrieval and Reranker mciroservice.\
7
+ Some of key feature of Retrieval service are:
8
+ - The embedding of the user query is done by retriever itself using Sentence-Transformer.
9
+ - ReRanker is available as optional component.
10
+ - This is rate determining step as the emedding of user query can be compute intensive if using dedicated model.
11
+ - Model config, Qdrant server url and other params can be set through \
12
+ [params.cfg](https://huggingface.co/spaces/GIZ/chatfed_retriever0.3/blob/main/params.cfg)
13
+
14
+ ```
15
+ [vectorstore]
16
+ # Qdrant-Server usage:
17
+ PROVIDER = qdrant
18
+ URL = giz-chatfed-qdrantserver.hf.space
19
+ COLLECTION_NAME = EUDR
20
+
21
+ [embeddings]
22
+ MODEL_NAME = BAAI/bge-m3
23
+
24
+ [retriever]
25
+ TOP_K = 10
26
+ SCORE_THRESHOLD = 0.6
27
+
28
+ [reranker]
29
+ MODEL_NAME = BAAI/bge-reranker-v2-m3
30
+ TOP_K = 10
31
+ ENABLED = true
32
+ # use this to scale out the total docs retrieved prior to reranking (i.e. retriever top_k * TOP_K_SCALE_FACTOR)
33
+ TOP_K_SCALE_FACTOR = 2
34
+ ```
35
+
36
+ **API documentation**: 1 API Endpoint
37
+
38
+ ### api_name: /retrieve
39
+
40
+ Params:
41
+ - query(str): Required
42
+ - collection_name(str): collection_name in the Qdrant server which need to be queried. Defualts to None.
43
+ - filter_metadata(dict): metadata filtering for Qdrant vector store which will be
44
+ applied to the collection mentioned above. Defuals to None
45
+
46
+ Returns: List of retrieved context along with metadata as string,
47
+ where each context is dict with two key 'answer' and 'answer_metadata'
48
+
49
+ **How to Connect**
50
+
51
+ ```python
52
+ from gradio_client import Client
53
+
54
+ client = Client("https://giz-chatfed-retriever0-3.hf.space/")
55
+ result = client.predict(
56
+ query="What is Circular Economy",
57
+ collection_name="Humboldt",
58
+ filter_metadata=None,
59
+ api_name="/retrieve"
60
+ )
61
+ ```
62
+ For more info on Retriever and code base visit the following links:
63
+ - ChaBo_Retriever : [**ReadMe**](https://huggingface.co/spaces/GIZ/chatfed_retriever0.3/blob/main/README.md)
64
+ - ChaBo_Retriever: [**Codebase**](https://huggingface.co/spaces/GIZ/chatfed_retriever0.3/tree/main)"""
vectorDB.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ vectordbText = """
2
+ We will use the [Qdrant](https://qdrant.tech/documentation/) server deployment as microservice. \
3
+ You can either deploy it as individually or you can use it as one server to serve multiple \
4
+ chatbots (like in image below) by having multiple collections (multiple collection can also serve one chatbot)
5
+
6
+
7
+ # Qdrant Vector Database Server on Hugging Face Spaces
8
+
9
+ [ChaBo_QdrantServer](https://huggingface.co/spaces/GIZ/chatfed_QdrantServer/blob/main/README.md) Space hosts \
10
+ a Qdrant vector database instance. This is just a Infrastructural component and doesnt\
11
+ not serve any user application through its User Interface. However the admin task can be performed by\
12
+ accessing "<embedded space url>/dashboard" Ex:https://giz-chatfed-qdrantserver.hf.space/dashboard \
13
+ which is passsword protected.
14
+
15
+ **Persistence:** Data is stored persistently in the `/data/qdrant_data` directory due to enabled persistent storage.
16
+
17
+ **How to connect:**
18
+ From your client application (e.g., your retrieval microservice), use the `qdrant-client` \
19
+ with the host set to your Space's direct URL and the appropriate port:
20
+
21
+ ```python
22
+ from qdrant_client import QdrantClient
23
+
24
+ # Replace with your actual Space URL (e.g., [https://your-username-qdrant-server.hf.space](https://your-username-qdrant-server.hf.space))
25
+ QDRANT_HOST = "giz-chatfed-qdrantserver.hf.space"
26
+ client = QdrantClient(
27
+ host = QDRANT_HOST,
28
+ port=443, # very important that port to be used for python client
29
+ https=True,
30
+ api_key = <QDRANT_API_KEY>,)
31
+ ```
32
+
33
+ API Documentation: https://api.qdrant.tech/api-reference
34
+
35
+ """