File size: 11,148 Bytes
7c012de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# KnowledgeBridge - System Architecture & Flow

## 🎯 Overview

This document provides a comprehensive technical overview of the KnowledgeBridge system architecture, data flow, and AI processing pipeline.

## πŸ“Š Main Data Flow

```
User Query β†’ AI Enhancement β†’ Multi-Source Search β†’ URL Validation β†’ Results Display
```

## πŸ”„ Detailed Process Flow

### Stage 1: Input Processing & Enhancement
**Components:**
- Enhanced Search Interface (React/TypeScript)
- Input validation and sanitization
- Rate limiting middleware

**Technical Details:**
- React captures user input with real-time validation
- Optional AI query enhancement using Nebius DeepSeek models
- Express.js endpoint with comprehensive security middleware
- Request body size limits and input sanitization

### Stage 2: AI-Powered Query Enhancement
**Components:**
- Nebius AI client with DeepSeek-R1-0528 model
- Smart query analysis and improvement
- Intent recognition and keyword extraction

**Technical Details:**
- Nebius API call: `deepseek-ai/DeepSeek-R1-0528`
- Analyzes user intent and suggests improvements
- Provides enhanced query, keywords, and alternative suggestions
- Fallback to original query if enhancement fails

### Stage 3: Embedding Generation
**Components:**
- Nebius embedding service
- BAAI/bge-en-icl model for vector generation
- Mock embedding fallback for reliability

**Technical Details:**
- Primary model: `BAAI/bge-en-icl`
- Generates high-dimensional vector representations
- Fallback to deterministic mock embeddings for demos
- Semantic meaning captured in numerical vectors

### Stage 4: Multi-Source Search
**Components:**
- Local storage search (in-memory with sample data)
- GitHub repository search with advanced filtering
- Wikipedia API integration
- ArXiv academic paper search

**Technical Details:**
- **Local Search**: Keyword matching with relevance scoring
- **GitHub API**: Enhanced with author filtering and fallback strategies
- **Wikipedia API**: 3-second timeout with content validation
- **ArXiv API**: Format validation and paper existence verification
- **Parallel Processing**: Concurrent search across all sources

### Stage 5: URL Validation & Content Verification
**Components:**
- Smart URL validation system
- ArXiv paper ID format checking
- Content-based error detection
- Concurrent processing with rate limits

**Technical Details:**
- **ArXiv Validation**: Checks paper ID format (2024.12345, cs.AI/1234567)
- **Content Verification**: Detects error pages that return 200 status
- **Rate Limiting**: Configurable concurrency to prevent API abuse
- **Trusted Domains**: Fast-path for reliable sources (GitHub, Wikipedia)

### Stage 6: Document Analysis (Optional)
**Components:**
- Nebius AI with configurable output formatting
- DeepSeek-R1 model for comprehensive analysis
- Content cleanup and markdown processing

**Technical Details:**
- Analysis types: summary, classification, key_points, quality_score
- Configurable markdown vs plain text output
- DeepSeek R1 thinking tag cleanup for clean results
- Custom prompts optimized for each analysis type

### Stage 7: Results Processing & Display
**Components:**
- Result ranking and relevance scoring
- Citation management system
- Interactive UI with error boundaries

**Technical Details:**
- Relevance-based sorting with multiple factors
- Rich metadata display with type-safe rendering
- Error boundaries prevent UI crashes
- Real-time result updates and filtering

## πŸ—οΈ System Architecture

### Frontend Stack
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    React 18 + TypeScript                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Enhanced Search Interface  β”‚  Knowledge Graph  β”‚  AI   β”‚
β”‚  - Unified search & AI      β”‚  - D3.js visualization  β”‚ Toolsβ”‚
β”‚  - Query enhancement        β”‚  - Interactive nodes     β”‚Panel β”‚
β”‚  - Configurable analysis    β”‚  - Relationship mapping  β”‚     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚           TanStack Query (Data Fetching & Caching)      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚              Radix UI + Tailwind CSS                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Backend Stack
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                Express.js + Security Middleware         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Helmet.js  β”‚  Rate Limiting  β”‚  Input Validation  β”‚CORSβ”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                    API Routes Layer                     β”‚
β”‚  /api/search  β”‚  /api/analyze-document  β”‚  /api/health β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚            Service Layer                                β”‚
β”‚  Nebius Client  β”‚  Modal Client  β”‚  Storage Service    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### AI & Processing Layer
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Nebius AI Platform                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  DeepSeek-R1-0528        β”‚  BAAI/bge-en-icl            β”‚
β”‚  - Chat completions      β”‚  - Embedding generation      β”‚
β”‚  - Document analysis     β”‚  - Semantic similarity       β”‚
β”‚  - Query enhancement     β”‚  - Vector search             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                      Modal Platform                     β”‚
β”‚  - Distributed processing   β”‚  - Scalable compute       β”‚
β”‚  - Batch operations         β”‚  - Resource management    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## πŸ”’ Security Architecture

### Input Protection
```
Request β†’ Rate Limiter β†’ Helmet.js β†’ Input Validator β†’ API Route
  ↓           ↓            ↓             ↓              ↓
100/15min   CSP Headers   Body Size    Zod Schema   Business Logic
10/min*     XSS Protection  10MB Limit   Type Safety    Error Handling

* Sensitive endpoints
```

### Error Handling Chain
```
React Error Boundary β†’ API Error Handler β†’ Service Error Handler
        ↓                      ↓                     ↓
   UI Graceful Fallback   HTTP Status Codes   Logging & Recovery
```

## πŸš€ Performance Characteristics

### Response Times
| Operation | Average Time | Details |
|-----------|-------------|---------|
| Local Search | <100ms | In-memory keyword matching |
| URL Validation | 1-3s per URL | Concurrent processing |
| Document Analysis | 3-5s | AI model processing time |
| Embedding Generation | 500ms-1s | Nebius API call |
| Query Enhancement | 1-2s | DeepSeek model inference |

### Scalability Features
- **Horizontal Scaling**: Modal platform for distributed processing
- **Rate Limiting**: Prevents API abuse and ensures fair usage
- **Caching**: TanStack Query for client-side data caching
- **Error Recovery**: Graceful degradation when services are unavailable
- **Load Distribution**: Concurrent processing of multiple requests

## πŸ”§ Data Flow Patterns

### Search Request Flow
```mermaid
graph TD
    A[User Query] --> B[Rate Limiter]
    B --> C[Input Validation]
    C --> D[AI Enhancement?]
    D -->|Yes| E[Nebius Query Enhancement]
    D -->|No| F[Direct Search]
    E --> F[Multi-Source Search]
    F --> G[Local Storage]
    F --> H[GitHub API]
    F --> I[Wikipedia API]
    F --> J[ArXiv API]
    G --> K[URL Validation]
    H --> K
    I --> K
    J --> K
    K --> L[Results Ranking]
    L --> M[Response Formatting]
    M --> N[Client Display]
```

### Document Analysis Flow
```mermaid
graph TD
    A[Document Content] --> B[Content Validation]
    B --> C[Analysis Type Selection]
    C --> D[Nebius DeepSeek API]
    D --> E[Response Processing]
    E --> F[Format Selection]
    F -->|Markdown| G[Rich Formatting]
    F -->|Plain Text| H[Clean Text Output]
    G --> I[Client Display]
    H --> I
```

## πŸ› οΈ Technology Integration Points

### External API Integration
- **Nebius AI**: Primary AI service for all language model tasks
- **Modal**: Distributed processing and compute scaling
- **GitHub API**: Repository search with authentication
- **Wikipedia API**: Authoritative content with caching
- **ArXiv API**: Academic paper search with validation

### Internal Service Communication
- **REST APIs**: Standard HTTP/JSON for client-server communication
- **Event-Driven**: React state management for UI updates
- **Error Propagation**: Structured error handling across all layers
- **Type Safety**: TypeScript contracts for all service boundaries

## πŸ“Š Quality Assurance

### Code Quality
- **TypeScript**: 100% type coverage across frontend and backend
- **Input Validation**: Zod schemas for all API endpoints
- **Error Boundaries**: React error boundaries prevent UI crashes
- **Security Middleware**: Comprehensive protection against common attacks

### Testing Strategy
- **Type Checking**: Continuous TypeScript compilation validation
- **API Testing**: Health checks and endpoint validation
- **Error Testing**: Graceful handling of service failures
- **Performance Testing**: Response time monitoring and optimization

This architecture provides a robust, scalable, and secure foundation for AI-powered knowledge discovery with comprehensive error handling and performance optimization.