infernum-server 0.2.0-rc.2

HTTP API server for local LLM inference
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! RAG HTTP endpoints for Stolas integration.
//!
//! Provides HTTP API for document indexing and semantic search via the Stolas RAG pipeline.

use std::collections::HashMap;
use std::sync::Arc;

use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::Json;
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;

use stolas::{
    ContextItem, Document, Embedder, InMemoryStore, MockEmbedder, RagPipeline, RetrievalConfig,
    VectorStore,
};

use crate::error_response::{api_error, ErrorCode};

/// RAG state for the server.
pub struct RagState {
    /// The RAG pipeline (lazy-initialized).
    pub pipeline: Option<RagPipeline>,
    /// Document metadata storage (id -> metadata).
    pub documents: HashMap<String, DocumentMeta>,
    /// Whether RAG is initialized.
    pub initialized: bool,
}

impl Default for RagState {
    fn default() -> Self {
        Self::new()
    }
}

impl RagState {
    /// Creates a new RAG state.
    pub fn new() -> Self {
        Self {
            pipeline: None,
            documents: HashMap::new(),
            initialized: false,
        }
    }

    /// Initializes the RAG pipeline with default configuration.
    pub fn initialize(&mut self) {
        if self.initialized {
            return;
        }

        // Use MockEmbedder for now - in production, use EngineEmbedder with the loaded model
        let embedder: Arc<dyn Embedder> = Arc::new(MockEmbedder::new(384));
        let store: Arc<dyn VectorStore> = Arc::new(InMemoryStore::new());
        let config = RetrievalConfig::default();

        self.pipeline = Some(RagPipeline::new(embedder, store, config));
        self.initialized = true;
    }

    /// Returns the pipeline if initialized.
    pub fn pipeline(&self) -> Option<&RagPipeline> {
        self.pipeline.as_ref()
    }
}

/// Document metadata stored in server.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentMeta {
    /// Document ID.
    pub id: String,
    /// Document name/filename.
    pub name: String,
    /// Number of chunks indexed.
    pub chunk_count: usize,
    /// Timestamp when indexed (Unix ms).
    pub indexed_at: u64,
    /// Additional metadata.
    #[serde(default)]
    pub metadata: HashMap<String, serde_json::Value>,
}

// =============================================================================
// Request/Response Types
// =============================================================================

/// RAG health/status response.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RagHealthResponse {
    /// Number of indexed documents.
    pub document_count: usize,
    /// Total chunks across all documents.
    pub chunk_count: usize,
    /// Embedding model in use.
    pub embedding_model: Option<String>,
    /// Last update timestamp (ISO 8601).
    pub last_updated: Option<String>,
    /// Whether RAG is ready.
    pub initialized: bool,
}

/// Document index request.
#[derive(Debug, Deserialize)]
pub struct IndexDocumentRequest {
    /// Document name/filename.
    pub name: String,
    /// Document content to index.
    pub content: String,
    /// Optional metadata.
    #[serde(default)]
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Document list response.
#[derive(Debug, Serialize)]
pub struct DocumentListResponse {
    /// List of documents.
    pub documents: Vec<DocumentMeta>,
}

/// Document count response.
#[derive(Debug, Serialize)]
pub struct DocumentCountResponse {
    /// Number of documents.
    pub count: usize,
}

/// Search request.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchRequest {
    /// Search query.
    pub query: String,
    /// Number of results to return.
    #[serde(default = "default_top_k")]
    pub top_k: usize,
    /// Minimum similarity score.
    #[serde(default)]
    pub min_score: Option<f32>,
    /// Enable cross-encoder reranking.
    #[serde(default)]
    pub rerank: bool,
}

fn default_top_k() -> usize {
    5
}

/// Search result item.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchResultItem {
    /// Content text.
    pub content: String,
    /// Source document ID.
    pub source_id: String,
    /// Chunk index in source document.
    pub chunk_index: usize,
    /// Similarity score (0-1).
    pub score: f32,
    /// Document metadata.
    #[serde(default)]
    pub metadata: HashMap<String, serde_json::Value>,
}

impl From<ContextItem> for SearchResultItem {
    fn from(item: ContextItem) -> Self {
        Self {
            content: item.content,
            source_id: item.source_id,
            chunk_index: item.chunk_index,
            score: item.score,
            metadata: item.metadata,
        }
    }
}

/// Search response.
#[derive(Debug, Serialize)]
pub struct SearchResponse {
    /// Search results.
    pub results: Vec<SearchResultItem>,
    /// Total results found.
    pub total: usize,
}

/// Delete response.
#[derive(Debug, Serialize)]
pub struct DeleteResponse {
    /// Number of chunks deleted.
    pub deleted: usize,
}

// =============================================================================
// Handlers
// =============================================================================

/// GET /api/rag/health - Get RAG status.
pub async fn rag_health(State(rag): State<Arc<RwLock<RagState>>>) -> impl IntoResponse {
    let state = rag.read().await;

    let (document_count, chunk_count) = if state.initialized {
        (
            state.documents.len(),
            state.documents.values().map(|d| d.chunk_count).sum(),
        )
    } else {
        (0, 0)
    };

    let last_updated = state
        .documents
        .values()
        .map(|d| d.indexed_at)
        .max()
        .map(|ts| {
            chrono::DateTime::from_timestamp_millis(ts as i64)
                .map(|dt| dt.to_rfc3339())
                .unwrap_or_default()
        });

    Json(RagHealthResponse {
        document_count,
        chunk_count,
        embedding_model: if state.initialized {
            Some("mock-embedder-384".to_string())
        } else {
            None
        },
        last_updated,
        initialized: state.initialized,
    })
}

/// GET /api/rag/documents - List indexed documents.
pub async fn list_documents(State(rag): State<Arc<RwLock<RagState>>>) -> impl IntoResponse {
    let state = rag.read().await;

    if !state.initialized {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(api_error(
                ErrorCode::ServiceUnavailable,
                "RAG not initialized",
            )),
        )
            .into_response();
    }

    let documents: Vec<DocumentMeta> = state.documents.values().cloned().collect();
    Json(DocumentListResponse { documents }).into_response()
}

/// GET /api/rag/documents/count - Get document count.
pub async fn document_count(State(rag): State<Arc<RwLock<RagState>>>) -> impl IntoResponse {
    let state = rag.read().await;
    Json(DocumentCountResponse {
        count: state.documents.len(),
    })
}

/// POST /api/rag/documents - Index a new document.
pub async fn index_document(
    State(rag): State<Arc<RwLock<RagState>>>,
    Json(req): Json<IndexDocumentRequest>,
) -> impl IntoResponse {
    // Validate request
    if req.name.is_empty() {
        return (
            StatusCode::BAD_REQUEST,
            Json(api_error(
                ErrorCode::InvalidRequest,
                "Document name is required",
            )),
        )
            .into_response();
    }

    if req.content.is_empty() {
        return (
            StatusCode::BAD_REQUEST,
            Json(api_error(
                ErrorCode::InvalidRequest,
                "Document content is required",
            )),
        )
            .into_response();
    }

    let mut state = rag.write().await;

    // Auto-initialize if not already
    if !state.initialized {
        state.initialize();
    }

    let pipeline = match state.pipeline.as_ref() {
        Some(p) => p,
        None => {
            return (
                StatusCode::SERVICE_UNAVAILABLE,
                Json(api_error(
                    ErrorCode::ServiceUnavailable,
                    "RAG pipeline not available",
                )),
            )
                .into_response();
        },
    };

    // Generate document ID
    let doc_id = format!("doc_{}", uuid::Uuid::new_v4().simple());

    // Create document
    let mut doc = Document::new(&doc_id, &req.content);
    for (key, value) in &req.metadata {
        doc = doc.with_metadata(key, value.clone());
    }
    doc = doc.with_metadata("name", serde_json::json!(req.name));

    // Ingest document
    let chunk_count = match pipeline.ingest(doc).await {
        Ok(count) => count,
        Err(e) => {
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(api_error(
                    ErrorCode::InternalError,
                    &format!("Failed to index document: {}", e),
                )),
            )
                .into_response();
        },
    };

    // Store document metadata
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0);

    let meta = DocumentMeta {
        id: doc_id.clone(),
        name: req.name,
        chunk_count,
        indexed_at: now,
        metadata: req.metadata,
    };

    state.documents.insert(doc_id, meta.clone());

    (StatusCode::CREATED, Json(meta)).into_response()
}

/// DELETE /api/rag/documents/:id - Delete a document.
pub async fn delete_document(
    State(rag): State<Arc<RwLock<RagState>>>,
    Path(doc_id): Path<String>,
) -> impl IntoResponse {
    let mut state = rag.write().await;

    if !state.initialized {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(api_error(
                ErrorCode::ServiceUnavailable,
                "RAG not initialized",
            )),
        )
            .into_response();
    }

    // Check if document exists
    let meta = match state.documents.remove(&doc_id) {
        Some(m) => m,
        None => {
            return (
                StatusCode::NOT_FOUND,
                Json(api_error(ErrorCode::NotFound, "Document not found")),
            )
                .into_response();
        },
    };

    // Note: InMemoryStore doesn't support deletion by ID yet, so we just remove metadata
    // In production with LanceDB, we would delete the vectors too

    Json(DeleteResponse {
        deleted: meta.chunk_count,
    })
    .into_response()
}

/// POST /api/rag/search - Search the knowledge base.
pub async fn search(
    State(rag): State<Arc<RwLock<RagState>>>,
    Json(req): Json<SearchRequest>,
) -> impl IntoResponse {
    if req.query.is_empty() {
        return (
            StatusCode::BAD_REQUEST,
            Json(api_error(ErrorCode::InvalidRequest, "Query is required")),
        )
            .into_response();
    }

    let state = rag.read().await;

    if !state.initialized {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            Json(api_error(
                ErrorCode::ServiceUnavailable,
                "RAG not initialized",
            )),
        )
            .into_response();
    }

    let pipeline = match state.pipeline.as_ref() {
        Some(p) => p,
        None => {
            return (
                StatusCode::SERVICE_UNAVAILABLE,
                Json(api_error(
                    ErrorCode::ServiceUnavailable,
                    "RAG pipeline not available",
                )),
            )
                .into_response();
        },
    };

    // Retrieve results
    let results = match pipeline.retrieve(&req.query).await {
        Ok(items) => items,
        Err(e) => {
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(api_error(
                    ErrorCode::InternalError,
                    &format!("Search failed: {}", e),
                )),
            )
                .into_response();
        },
    };

    // Convert to response format
    let results: Vec<SearchResultItem> = results
        .into_iter()
        .take(req.top_k)
        .filter(|r| req.min_score.map(|m| r.score >= m).unwrap_or(true))
        .map(SearchResultItem::from)
        .collect();

    let total = results.len();

    Json(SearchResponse { results, total }).into_response()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rag_state_new() {
        let state = RagState::new();
        assert!(!state.initialized);
        assert!(state.pipeline.is_none());
        assert!(state.documents.is_empty());
    }

    #[test]
    fn test_rag_state_initialize() {
        let mut state = RagState::new();
        state.initialize();
        assert!(state.initialized);
        assert!(state.pipeline.is_some());
    }

    #[test]
    fn test_rag_state_double_initialize() {
        let mut state = RagState::new();
        state.initialize();
        state.initialize(); // Should not panic
        assert!(state.initialized);
    }

    #[test]
    fn test_document_meta_serialization() {
        let meta = DocumentMeta {
            id: "doc_123".to_string(),
            name: "test.txt".to_string(),
            chunk_count: 5,
            indexed_at: 1234567890000,
            metadata: HashMap::new(),
        };
        let json = serde_json::to_string(&meta).unwrap();
        assert!(json.contains("doc_123"));
        assert!(json.contains("test.txt"));
    }

    #[test]
    fn test_search_request_defaults() {
        let json = r#"{"query": "test"}"#;
        let req: SearchRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.query, "test");
        assert_eq!(req.top_k, 5);
        assert!(!req.rerank);
    }

    #[test]
    fn test_context_item_to_search_result() {
        let item = ContextItem {
            content: "test content".to_string(),
            source_id: "doc_1".to_string(),
            chunk_index: 2,
            score: 0.85,
            metadata: HashMap::new(),
        };
        let result: SearchResultItem = item.into();
        assert_eq!(result.content, "test content");
        assert_eq!(result.source_id, "doc_1");
        assert_eq!(result.chunk_index, 2);
        assert!((result.score - 0.85).abs() < 0.001);
    }
}