reasonkit-mem 0.1.7

High-performance vector database & RAG memory layer - hybrid search, embeddings, RAPTOR trees, BM25 fusion, and semantic retrieval for AI systems
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! ReasonKit-mem HTTP API Server
//!
//! Provides an HTTP interface to reasonkit-mem's HybridRetriever for
//! integration with external systems like OpenWebUI.
//!
//! # Endpoints
//!
//! - `POST /v1/search` - Hybrid search (dense + sparse)
//! - `POST /v1/embed` - Generate embeddings for texts
//! - `POST /v1/documents` - Add documents to the knowledge base
//! - `GET /v1/stats` - Get retrieval statistics
//! - `GET /health` - Health check
//!
//! # Usage
//!
//! ```bash
//! cargo run --bin rkmem_server --features http-server
//! ```
//!
//! # Configuration
//!
//! Environment variables:
//! - `RKMEM_HOST`: Bind address (default: 0.0.0.0)
//! - `RKMEM_PORT`: Port number (default: 8765)
//! - `RKMEM_DATA_DIR`: Data directory (default: ~/.reasonkit/mem)

use axum::{
    extract::State,
    http::StatusCode,
    routing::{get, post},
    Json, Router,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::sync::RwLock;
use tower_http::cors::{Any, CorsLayer};
use tower_http::trace::TraceLayer;
use tracing::{info, Level};
use uuid::Uuid;

use reasonkit_mem::{
    retrieval::HybridRetriever, Chunk, Document, DocumentType, EmbeddingIds, RetrievalConfig,
    Source, SourceType,
};

/// Application state shared across handlers
struct AppState {
    retriever: RwLock<HybridRetriever>,
}

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

#[derive(Debug, Deserialize)]
struct SearchRequest {
    /// Search query
    query: String,
    /// Number of results to return (default: 10)
    #[serde(default = "default_top_k")]
    top_k: usize,
    /// Balance between dense and sparse (0.0 = pure BM25, 1.0 = pure vector)
    #[serde(default = "default_alpha")]
    alpha: f32,
    /// Search mode: "hybrid", "sparse", "dense"
    #[serde(default = "default_mode")]
    mode: String,
}

fn default_top_k() -> usize {
    10
}
fn default_alpha() -> f32 {
    0.7
}
fn default_mode() -> String {
    "hybrid".to_string()
}

#[derive(Debug, Serialize)]
struct SearchResponse {
    results: Vec<SearchResultItem>,
    query: String,
    mode: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    stats: Option<SearchStats>,
}

#[derive(Debug, Serialize)]
struct SearchResultItem {
    doc_id: String,
    chunk_id: String,
    text: String,
    score: f32,
    #[serde(skip_serializing_if = "Option::is_none")]
    dense_score: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    sparse_score: Option<f32>,
    match_source: String,
}

#[derive(Debug, Serialize)]
struct SearchStats {
    total_results: usize,
    search_time_ms: u64,
}

#[derive(Debug, Deserialize)]
struct EmbedRequest {
    /// Texts to embed
    texts: Vec<String>,
}

#[derive(Debug, Serialize)]
struct EmbedResponse {
    embeddings: Vec<Vec<f32>>,
    model: String,
    dimension: usize,
}

#[derive(Debug, Deserialize)]
struct AddDocumentRequest {
    /// Document content
    content: String,
    /// Document title (optional)
    #[serde(default)]
    title: Option<String>,
    /// Document metadata (reserved for future use)
    #[serde(default)]
    #[allow(dead_code)]
    metadata: HashMap<String, String>,
    /// Source path or URL
    #[serde(default)]
    source: Option<String>,
}

#[derive(Debug, Serialize)]
struct AddDocumentResponse {
    id: String,
    chunks: usize,
    message: String,
}

#[derive(Debug, Serialize)]
struct StatsResponse {
    document_count: usize,
    chunk_count: usize,
    indexed_chunks: usize,
    embedding_count: usize,
    storage_bytes: u64,
    index_bytes: u64,
}

#[derive(Debug, Serialize)]
struct HealthResponse {
    status: String,
    version: String,
    uptime_secs: u64,
}

#[derive(Debug, Serialize)]
struct ErrorResponse {
    error: String,
    code: String,
}

// ============================================================================
// OpenWebUI-Compatible Types (External RAG Interface)
// ============================================================================

/// OpenWebUI external search request format
#[derive(Debug, Deserialize)]
struct OpenWebUISearchRequest {
    query: String,
    #[serde(default = "default_count")]
    count: usize,
}

fn default_count() -> usize {
    5
}

/// OpenWebUI external search result format
#[derive(Debug, Serialize)]
struct OpenWebUISearchResult {
    link: String,
    title: String,
    snippet: String,
}

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

/// Health check endpoint
async fn health() -> Json<HealthResponse> {
    Json(HealthResponse {
        status: "healthy".to_string(),
        version: env!("CARGO_PKG_VERSION").to_string(),
        uptime_secs: 0, // TODO: Track actual uptime
    })
}

/// Hybrid search endpoint
async fn search(
    State(state): State<Arc<AppState>>,
    Json(req): Json<SearchRequest>,
) -> Result<Json<SearchResponse>, (StatusCode, Json<ErrorResponse>)> {
    let start = std::time::Instant::now();

    let retriever = state.retriever.read().await;

    let results = match req.mode.as_str() {
        "sparse" | "bm25" => retriever
            .search_sparse(&req.query, req.top_k)
            .await
            .map_err(|e| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    Json(ErrorResponse {
                        error: e.to_string(),
                        code: "SEARCH_ERROR".to_string(),
                    }),
                )
            })?,
        "dense" | "vector" => retriever
            .search_dense(&req.query, req.top_k)
            .await
            .map_err(|e| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    Json(ErrorResponse {
                        error: e.to_string(),
                        code: "SEARCH_ERROR".to_string(),
                    }),
                )
            })?,
        _ => {
            // Default: hybrid search
            let config = RetrievalConfig {
                top_k: req.top_k,
                alpha: req.alpha,
                ..Default::default()
            };
            retriever
                .search_hybrid(&req.query, None, &config)
                .await
                .map_err(|e| {
                    (
                        StatusCode::INTERNAL_SERVER_ERROR,
                        Json(ErrorResponse {
                            error: e.to_string(),
                            code: "SEARCH_ERROR".to_string(),
                        }),
                    )
                })?
        }
    };

    let elapsed = start.elapsed();

    let response_items: Vec<SearchResultItem> = results
        .into_iter()
        .map(|r| SearchResultItem {
            doc_id: r.doc_id.to_string(),
            chunk_id: r.chunk_id.to_string(),
            text: r.text,
            score: r.score,
            dense_score: r.dense_score,
            sparse_score: r.sparse_score,
            match_source: format!("{:?}", r.match_source),
        })
        .collect();

    let total = response_items.len();

    Ok(Json(SearchResponse {
        results: response_items,
        query: req.query,
        mode: req.mode,
        stats: Some(SearchStats {
            total_results: total,
            search_time_ms: elapsed.as_millis() as u64,
        }),
    }))
}

/// Embedding endpoint (placeholder - requires embedding pipeline)
async fn embed(
    State(state): State<Arc<AppState>>,
    Json(req): Json<EmbedRequest>,
) -> Result<Json<EmbedResponse>, (StatusCode, Json<ErrorResponse>)> {
    let retriever = state.retriever.read().await;

    // Check if embedding pipeline is configured
    if retriever.embedding_pipeline().is_none() {
        return Err((
            StatusCode::SERVICE_UNAVAILABLE,
            Json(ErrorResponse {
                error: "Embedding pipeline not configured. Start server with embeddings enabled."
                    .to_string(),
                code: "EMBEDDING_NOT_CONFIGURED".to_string(),
            }),
        ));
    }

    let pipeline = retriever.embedding_pipeline().unwrap();

    let mut embeddings = Vec::with_capacity(req.texts.len());
    for text in &req.texts {
        let embedding = pipeline.embed_text(text).await.map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: e.to_string(),
                    code: "EMBEDDING_ERROR".to_string(),
                }),
            )
        })?;
        embeddings.push(embedding);
    }

    let dimension = embeddings.first().map(|e| e.len()).unwrap_or(0);

    Ok(Json(EmbedResponse {
        embeddings,
        model: "bge-m3".to_string(), // TODO: Get actual model name
        dimension,
    }))
}

/// Add document endpoint
async fn add_document(
    State(state): State<Arc<AppState>>,
    Json(req): Json<AddDocumentRequest>,
) -> Result<Json<AddDocumentResponse>, (StatusCode, Json<ErrorResponse>)> {
    use chrono::Utc;

    // Create source
    let source = Source {
        source_type: if req
            .source
            .as_ref()
            .map(|s| s.starts_with("http"))
            .unwrap_or(false)
        {
            SourceType::Website
        } else {
            SourceType::Local
        },
        url: req.source.clone().filter(|s| s.starts_with("http")),
        path: req.source.clone().filter(|s| !s.starts_with("http")),
        arxiv_id: None,
        github_repo: None,
        retrieved_at: Utc::now(),
        version: None,
    };

    // Create document
    let mut doc = Document::new(DocumentType::Note, source).with_content(req.content.clone());

    if let Some(title) = req.title {
        doc.metadata.title = Some(title);
    }

    // Simple chunking: split by double newlines or every 1000 chars
    let chunks: Vec<Chunk> = req
        .content
        .split("\n\n")
        .enumerate()
        .filter(|(_, text)| !text.trim().is_empty())
        .map(|(i, text)| Chunk {
            id: Uuid::new_v4(),
            text: text.to_string(),
            index: i,
            start_char: 0, // Simplified
            end_char: text.len(),
            token_count: None,
            section: None,
            page: None,
            embedding_ids: EmbeddingIds::default(),
        })
        .collect();

    let chunk_count = chunks.len();
    doc.chunks = chunks;

    // Add to retriever
    let retriever = state.retriever.read().await;
    retriever.add_document(&doc).await.map_err(|e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: e.to_string(),
                code: "ADD_DOCUMENT_ERROR".to_string(),
            }),
        )
    })?;

    Ok(Json(AddDocumentResponse {
        id: doc.id.to_string(),
        chunks: chunk_count,
        message: format!("Document added with {} chunks", chunk_count),
    }))
}

/// OpenWebUI-compatible external RAG search endpoint
/// Returns results in the format expected by OpenWebUI's search_external function
async fn openwebui_search(
    State(state): State<Arc<AppState>>,
    Json(req): Json<OpenWebUISearchRequest>,
) -> Result<Json<Vec<OpenWebUISearchResult>>, (StatusCode, Json<ErrorResponse>)> {
    let retriever = state.retriever.read().await;

    // Use BM25 search for best keyword matching
    let results = retriever
        .search_sparse(&req.query, req.count)
        .await
        .map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: e.to_string(),
                    code: "SEARCH_ERROR".to_string(),
                }),
            )
        })?;

    // Convert to OpenWebUI format
    let response: Vec<OpenWebUISearchResult> = results
        .into_iter()
        .map(|r| {
            // Truncate text to ~500 chars for snippet
            let snippet = if r.text.len() > 500 {
                format!("{}...", &r.text[..497])
            } else {
                r.text.clone()
            };

            OpenWebUISearchResult {
                // Use doc_id as link (could be enhanced to include actual source URL)
                link: format!("rkmem://doc/{}/chunk/{}", r.doc_id, r.chunk_id),
                // Extract first line as title, or use doc_id
                title: r
                    .text
                    .lines()
                    .next()
                    .map(|l| l.chars().take(100).collect::<String>())
                    .unwrap_or_else(|| format!("Chunk {}", r.chunk_id)),
                snippet,
            }
        })
        .collect();

    Ok(Json(response))
}

/// Stats endpoint
async fn stats(
    State(state): State<Arc<AppState>>,
) -> Result<Json<StatsResponse>, (StatusCode, Json<ErrorResponse>)> {
    let retriever = state.retriever.read().await;

    let stats = retriever.stats().await.map_err(|e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: e.to_string(),
                code: "STATS_ERROR".to_string(),
            }),
        )
    })?;

    Ok(Json(StatsResponse {
        document_count: stats.document_count,
        chunk_count: stats.chunk_count,
        indexed_chunks: stats.indexed_chunks,
        embedding_count: stats.embedding_count,
        storage_bytes: stats.storage_bytes,
        index_bytes: stats.index_bytes,
    }))
}

// ============================================================================
// Main
// ============================================================================

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Initialize tracing
    tracing_subscriber::fmt().with_max_level(Level::INFO).init();

    // Get config from environment
    let host = std::env::var("RKMEM_HOST").unwrap_or_else(|_| "0.0.0.0".to_string());
    let port: u16 = std::env::var("RKMEM_PORT")
        .unwrap_or_else(|_| "8765".to_string())
        .parse()?;

    info!(
        "Starting ReasonKit-mem HTTP Server v{}",
        env!("CARGO_PKG_VERSION")
    );

    // Create retriever (in-memory for now)
    let retriever = HybridRetriever::in_memory()?;

    // TODO: Optionally configure embedding pipeline
    // let pipeline = EmbeddingPipeline::new(...);
    // let retriever = retriever.with_embedding_pipeline(Arc::new(pipeline));

    let state = Arc::new(AppState {
        retriever: RwLock::new(retriever),
    });

    // CORS configuration
    let cors = CorsLayer::new()
        .allow_origin(Any)
        .allow_methods(Any)
        .allow_headers(Any);

    // Build router
    let app = Router::new()
        .route("/health", get(health))
        .route("/v1/search", post(search))
        .route("/v1/embed", post(embed))
        .route("/v1/documents", post(add_document))
        .route("/v1/stats", get(stats))
        // OpenWebUI-compatible external RAG endpoint
        .route("/v1/openwebui/search", post(openwebui_search))
        .layer(cors)
        .layer(TraceLayer::new_for_http())
        .with_state(state);

    let addr: SocketAddr = format!("{}:{}", host, port).parse()?;
    info!("Listening on http://{}", addr);
    info!("Endpoints:");
    info!("  POST /v1/search           - Hybrid search");
    info!("  POST /v1/embed            - Generate embeddings");
    info!("  POST /v1/documents        - Add documents");
    info!("  GET  /v1/stats            - Get statistics");
    info!("  POST /v1/openwebui/search - OpenWebUI external RAG");
    info!("  GET  /health              - Health check");

    let listener = tokio::net::TcpListener::bind(addr).await?;
    axum::serve(listener, app).await?;

    Ok(())
}