File size: 7,019 Bytes
df62db1
 
 
 
 
 
 
 
 
 
 
 
4d7825a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
---
title: GenAI Hackathon
emoji: ๐Ÿ”ฅ
colorFrom: indigo
colorTo: green
sdk: gradio
sdk_version: 6.19.0
python_version: '3.13'
app_file: app.py
pinned: false
---

# ๐Ÿ‘๏ธ HybridSight

> A multimodal AI assistant that combines Retrieval-Augmented Generation (RAG), Web Search, and Vision into a single application through a custom reasoning agent.

HybridSight is a lightweight GenAI application capable of answering questions from uploaded documents, uploaded images, and the web. Instead of relying on a fixed pipeline, it uses a custom-built reasoning agent that dynamically decides which tool(s) should be used for every query while exposing its reasoning process to the user.

---

## โœจ Features

### ๐Ÿ’ฌ Hybrid Chat
- Free-form conversational interface
- Dynamic tool selection using a custom reasoning agent
- Supports document retrieval, web search, and image understanding
- Multi-tool reasoning for complex queries
- Collapsible reasoning trace for every response

### ๐Ÿ“„ Document QA
- PDF upload and indexing into ChromaDB
- Semantic retrieval using sentence embeddings
- Multi-turn document conversations
- Streaming responses
- Source-aware answers

### ๐Ÿ–ผ๏ธ Image Studio
- Image upload with adjustable resolution
- Vision-based question answering
- Image metadata preview
- Token-aware image preprocessing
- Automatic validation before sending images to the Vision model

---

# ๐Ÿ—๏ธ Architecture

```
                     User
                       โ”‚
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚              โ”‚              โ”‚
        โ–ผ              โ–ผ              โ–ผ
   Hybrid Chat    Document QA    Image Studio
        โ”‚              โ”‚              โ”‚
        โ–ผ              โ–ผ              โ–ผ
  Custom Agent      RAG Engine    Vision Pipeline
        โ”‚              โ”‚              โ”‚
        โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ search_documents โ”€โ”€โ”€โ”ค
        โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ web_search โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ describe_image โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚
                       โ–ผ
                    Groq API
```

---

# โš™๏ธ Technology Stack

| Component | Technology |
|------------|------------|
| LLM | Llama 3.3 70B (Groq) |
| Vision Model | Llama 4 Scout Vision |
| Framework | Gradio |
| Retrieval | LangChain |
| Vector Database | ChromaDB |
| Embeddings | all-MiniLM-L6-v2 |
| Search | DuckDuckGo Search |
| Image Processing | Pillow |

---

# ๐Ÿ“‚ Project Structure

```
HybridSight
โ”‚
โ”œโ”€โ”€ app.py
โ”œโ”€โ”€ agent.py
โ”œโ”€โ”€ tools.py
โ”œโ”€โ”€ rag.py
โ”œโ”€โ”€ retrival.py
โ”œโ”€โ”€ indexing.py
โ”œโ”€โ”€ vision.py
โ”œโ”€โ”€ safety.py
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ .gitignore
```

---

# ๐Ÿš€ Running Locally

Clone the repository

```bash
git clone <repository-url>
```

Install dependencies

```bash
pip install -r requirements.txt
```

Create a `.env` file

```
GROQ_API_KEY=YOUR_API_KEY
```

Run the application

```bash
python app.py
```

---


# ๐Ÿงช Tested Scenarios

โœ… Pure document questions

โœ… Pure web-search questions

โœ… Pure image questions

โœ… Image + document reasoning

โœ… Graceful handling of API failures

โœ… Missing document handling

โœ… Missing image handling

โœ… Session-specific document retrieval

---

# ๐Ÿ’ก Engineering Decisions

## Custom Reasoning Agent

Instead of relying on a fixed workflow, HybridSight implements its own reasoning loop.

The agent repeatedly:

```
Question
    โ”‚
Planner
    โ”‚
Tool
    โ”‚
Observation
    โ”‚
Planner
    โ”‚
Final Answer
```

This provides full control over:

- tool routing
- stopping conditions
- reasoning traces
- memory
- runtime behaviour
- tool-specific execution

It also makes it straightforward to add new tools without changing the overall architecture.

---

## Runtime Image Placeholder

A common challenge with multimodal agents is handling image data efficiently.

Instead of sending the complete Base64 image to the planner model, the planner reasons only with a lightweight placeholder:

```
CURRENT_IMAGE
```

During execution, the runtime replaces this placeholder with the actual image before invoking the vision tool.

```
Planner
    โ”‚
CURRENT_IMAGE
    โ”‚
Runtime
    โ”‚
Vision Tool
```

This approach:

- reduces prompt size
- lowers token usage
- avoids unnecessary serialization
- keeps reasoning independent of image size
- prevents duplicate image processing

---

## Token-Aware Vision Pipeline

The resolution slider is not merely a UI feature.

Large images generate significantly larger Base64 payloads, increasing latency, token consumption, and the likelihood of request failures.

Before an image reaches the Vision model, HybridSight performs:

```
Upload
    โ”‚
Resize
    โ”‚
JPEG Compression
    โ”‚
Token Estimation
    โ”‚
Metadata Generation
    โ”‚
Validation
    โ”‚
Vision Model
```

Only images that satisfy the configured token threshold are analyzed.

This protects the application from oversized requests while improving responsiveness.

---

## Dedicated Document QA

Hybrid Chat and Document QA serve different purposes.

Hybrid Chat focuses on open-ended multimodal reasoning.

```
Question
      โ”‚
 Agent
      โ”‚
 Multiple Tools
      โ”‚
 Answer
```

Document QA is optimized specifically for retrieval.

```
PDF
   โ”‚
Embedding
   โ”‚
ChromaDB
   โ”‚
Retriever
   โ”‚
LLM
```

Separating these workflows keeps document conversations faster, source-grounded, and free from unnecessary planner overhead.

---

## Session Isolation

Each user receives an independent ChromaDB collection.

```
collection_name = session_id
```

This ensures uploaded documents remain isolated between users and prevents retrieval across sessions.

---

## Reasoning Trace

Every tool invocation is recorded and displayed.

For each reasoning step, the application exposes:

- selected tool
- tool input
- tool output

This makes the decision-making process transparent and simplifies debugging.

---

## Unified Safety Layer

External dependencies are wrapped with a unified safety decorator.

Instead of terminating the application, common failures are handled gracefully, including:

- rate limits
- invalid API keys
- network errors
- JSON parsing failures
- timeout errors
- missing files
- invalid tool requests

This keeps the UI responsive even when external services fail.

---

## Progressive Document Indexing

Document indexing provides real-time progress updates.

```
Loading PDF
      โ†“
Splitting
      โ†“
Preparing Metadata
      โ†“
Generating Embeddings
      โ†“
Writing ChromaDB
      โ†“
Completed
```

This improves user feedback, especially for larger documents.

---

# ๐Ÿ”ฎ Future Improvements

- Multi-document collections
- OCR support for scanned PDFs
- Citation highlighting
- Streaming reasoning traces
- Persistent cloud memory
- Additional knowledge tools (Wikipedia, ArXiv, etc.)
- Parallel tool execution

---