graphrag-search 0.2.11

GraphRAG — Hybrid search engine: vector embeddings + knowledge graph over Markdown notes. 100% local, single Rust binary.
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
use anyhow::{anyhow, Context, Result};
use log::debug;
use serde::{Deserialize, Serialize};

use crate::chunking::slugify;

/// Una entidad extraída del texto
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Entity {
    pub id: String,
    pub label: String,
    pub type_: String,
    pub score: f64,
}

/// Etiquetas por defecto para extracción de entidades
pub const DEFAULT_LABELS: &[&str] = &[
    "technology",
    "framework",
    "programming language",
    "database",
    "tool",
    "person",
    "organization",
    "concept",
    "protocol",
    "operating system",
    "library",
    "format",
];

/// Envía un prompt al modelo Ollama y devuelve la respuesta cruda (response o thinking).
fn call_ollama(ollama_url: &str, model: &str, prompt: &str) -> Result<String> {
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(120))
        .build()
        .context("Failed to create HTTP client")?;

    let body = serde_json::json!({
        "model": model,
        "prompt": prompt,
        "stream": false,
        "options": {
            "temperature": 0.1,
            "num_predict": 4096
        }
    });

    let resp = client
        .post(format!("{}/api/generate", ollama_url.trim_end_matches('/')))
        .json(&body)
        .send()
        .context("Error connecting to Ollama for entity extraction")?;

    let status = resp.status();
    if !status.is_success() {
        let body = resp.text().unwrap_or_default();
        return Err(anyhow!(
            "Ollama returned HTTP {}: {}",
            status.as_u16(),
            body.trim()
        ));
    }

    let resp_text = resp.text().context("Failed to read response body")?;
    let preview: String = resp_text.chars().take(200).collect();
    debug!(
        "NER respuesta cruda ({} chars): {}",
        resp_text.len(),
        preview
    );

    let data: OllamaResponse =
        serde_json::from_str(&resp_text).context("Failed to parse Ollama response JSON")?;

    // Algunos modelos (como qwen3.5) ponen la respuesta en 'thinking' en lugar de 'response'
    let raw = if !data.response.trim().is_empty() {
        data.response
    } else if !data.thinking.trim().is_empty() {
        debug!(
            "NER: response vacío, usando campo 'thinking' ({} chars)",
            data.thinking.len()
        );
        data.thinking
    } else {
        debug!("NER respuesta vacía para modelo '{}'.", model);
        return Ok(String::new());
    };

    Ok(raw)
}

#[derive(Deserialize)]
struct OllamaResponse {
    response: String,
    #[serde(default)]
    thinking: String,
}

/// Parsea una respuesta JSON cruda de Ollama en un Vec de valores JSON.
/// Intenta 3 estrategias: array directo, objeto con campo "entities"/"results", o regex.
fn parse_entities_json(raw: &str) -> Vec<serde_json::Value> {
    let raw = raw.trim();
    if raw.is_empty() {
        return Vec::new();
    }

    // Limpiar posibles wrappers: ```json ... ```, ``` ... ```, o texto alrededor
    let cleaned = raw
        .strip_prefix("```json")
        .or_else(|| raw.strip_prefix("```"))
        .map(|s| s.strip_suffix("```").unwrap_or(s))
        .unwrap_or(raw)
        .trim();

    // Intento 1: el LLM devolvió un array JSON directamente
    if let Ok(entities) = serde_json::from_str::<Vec<serde_json::Value>>(cleaned) {
        return entities;
    }

    // Intento 2: el LLM devolvió un objeto con campo "entities" o "results"
    if let Ok(wrapper) = serde_json::from_str::<serde_json::Value>(cleaned) {
        return wrapper
            .get("entities")
            .or_else(|| wrapper.get("results"))
            .and_then(|arr| serde_json::from_value::<Vec<serde_json::Value>>(arr.clone()).ok())
            .unwrap_or_default();
    }

    // Intento 3: buscar un array JSON dentro del texto (modelos thinking o con markdown)
    // Usamos (?s) para que . cruce saltos de línea
    let re = regex::Regex::new(r"(?s)\[.*?\]").ok();
    if let Some(re) = re {
        if let Some(mat) = re.find(cleaned) {
            let json_str = mat.as_str();
            if let Ok(entities) = serde_json::from_str::<Vec<serde_json::Value>>(json_str) {
                return entities;
            }
        }
    }

    Vec::new()
}

/// Convierte un Vec de valores JSON crudos en `Vec<Entity>`, deduplicando y
/// filtrando por score >= 0.5. Si `section` está presente en el objeto,
/// se ignora (el caller lo usa para agrupar).
fn raw_entities_to_entities(raw_entities: Vec<serde_json::Value>) -> Vec<Entity> {
    let mut seen = std::collections::HashSet::new();
    let mut entities = Vec::new();

    for raw in raw_entities {
        let label = raw
            .get("label")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .trim()
            .to_string();

        if label.is_empty() {
            continue;
        }

        let key = label.to_lowercase();
        if seen.contains(&key) {
            continue;
        }

        let type_ = raw
            .get("type")
            .and_then(|v| v.as_str())
            .unwrap_or("concept")
            .to_string();

        let score = raw.get("score").and_then(|v| v.as_f64()).unwrap_or(0.5);

        if score < 0.5 {
            continue;
        }

        seen.insert(key);
        entities.push(Entity {
            id: slugify(&label),
            label,
            type_,
            score,
        });
    }

    entities
}

/// Extrae entidades de un texto usando Ollama LLM.
///
/// Envía un prompt estructurado al modelo solicitando extracción de entidades
/// en formato JSON. Esto reemplaza a GLiNER del original Python.
///
/// El prompt pide explícitamente que devuelva solo JSON válido para poder
/// parsear la respuesta de forma determinista.
#[allow(dead_code)]
pub fn extract_entities(
    ollama_url: &str,
    model: &str,
    text: &str,
    labels: &[&str],
) -> Result<Vec<Entity>> {
    if text.trim().is_empty() {
        return Ok(Vec::new());
    }

    let labels_str = labels.join(", ");

    let prompt = format!(
        r#"Extract named entities from the following text. Only extract entities that match these categories: {labels_str}.

Return a JSON array of objects with fields:
- "label": the entity text exactly as written
- "type": one of the categories listed above
- "score": confidence score between 0.0 and 1.0 (be honest, use 0.5 if unsure)

Rules:
- Deduplicate: if the same entity appears multiple times, list it once with the highest score
- Return ONLY the raw JSON array. NO markdown, NO code fences, NO ```json, NO explanations.
- Start with [ and end with ]. Nothing else.
- If no entities found, return an empty array []

Text:
---
{text}
---"#,
        labels_str = labels_str,
        text = text,
    );

    let raw = call_ollama(ollama_url, model, &prompt)?;

    if raw.is_empty() {
        return Ok(Vec::new());
    }

    let raw_entities = parse_entities_json(&raw);

    let entities = raw_entities_to_entities(raw_entities);

    let preview: String = text.chars().take(40).collect();
    debug!(
        "NER extraídas: {} entidades de '{}'...",
        entities.len(),
        preview
    );
    Ok(entities)
}

/// Extrae entidades de MÚLTIPLES textos (chunks) en una SOLA llamada a Ollama.
///
/// Envía todos los textos separados por marcadores `---SECTION N---` y pide
/// al modelo que incluya un campo `"section"` en cada entidad. Luego agrupa
/// las entidades por sección en un `Vec<Vec<Entity>>`.
///
/// # Argumentos
///
/// * `ollama_url` — URL del servidor Ollama.
/// * `model` — Nombre del modelo Ollama.
/// * `texts` — Slice de textos (chunks) a procesar.
/// * `labels` — Categorías de entidades a extraer.
///
/// # Devuelve
///
/// Un `Vec<Vec<Entity>>` donde el índice 0 corresponde al primer texto,
/// el índice 1 al segundo, etc. Si un texto no tiene entidades, su vector
/// estará vacío.
pub fn extract_entities_batch(
    ollama_url: &str,
    model: &str,
    texts: &[&str],
    labels: &[&str],
) -> Result<Vec<Vec<Entity>>> {
    if texts.is_empty() {
        return Ok(Vec::new());
    }

    let labels_str = labels.join(", ");

    // Construir el prompt con todas las secciones
    let mut prompt_parts = Vec::new();
    prompt_parts.push(
        r#"Extract named entities from each section of the following text. Sections are separated by "---SECTION N---" markers.

Only extract entities that match these categories: {labels_str}

Return a JSON array of objects with fields:
- "label": the entity text exactly as written
- "type": one of the categories listed above
- "score": confidence score between 0.0 and 1.0 (be honest, use 0.5 if unsure)
- "section": the section number (1-based integer) this entity belongs to

Rules:
- Deduplicate within each section: if the same entity appears multiple times in a section, list it once with the highest score
- Return ONLY the raw JSON array. NO markdown, NO code fences, NO ```json, NO explanations, NO extra text.
- Start with [ and end with ]. Nothing else.
- If no entities found in any section, return an empty array []

Text:
---"#.replace("{labels_str}", &labels_str),
    );

    for (i, text) in texts.iter().enumerate() {
        prompt_parts.push(format!("---SECTION {}---\n{}", i + 1, text));
    }

    prompt_parts.push("---".to_string());

    let prompt = prompt_parts.join("\n");

    let raw = call_ollama(ollama_url, model, &prompt)?;

    if raw.is_empty() {
        return Ok(texts.iter().map(|_| Vec::new()).collect());
    }

    let raw_entities = parse_entities_json(&raw);

    // Agrupar entidades por sección
    let mut result: Vec<Vec<Entity>> = texts.iter().map(|_| Vec::new()).collect();

    // Primero, agrupar los raw JSON values por section para deduplicar
    // dentro de cada sección
    struct RawEntity {
        value: serde_json::Value,
        section: usize,
    }

    let raw_with_sections: Vec<RawEntity> = raw_entities
        .into_iter()
        .map(|v| {
            let section = v
                .get("section")
                .and_then(|s| s.as_i64())
                .map(|s| (s as usize).saturating_sub(1)) // 1-based → 0-based
                .unwrap_or(0);
            RawEntity { value: v, section }
        })
        .collect();

    // Agrupar por sección
    use std::collections::HashMap;
    let mut by_section: HashMap<usize, Vec<serde_json::Value>> = HashMap::new();
    for re in raw_with_sections {
        by_section.entry(re.section).or_default().push(re.value);
    }

    let num_sections = by_section.len();

    // Convertir cada grupo a Entities y colocarlo en el índice correspondiente
    for (section_idx, raw_group) in by_section {
        if section_idx < result.len() {
            let entities = raw_entities_to_entities(raw_group);
            result[section_idx] = entities;
        }
    }

    debug!(
        "NER batch: {} textos, {} secciones con entidades",
        texts.len(),
        num_sections,
    );

    Ok(result)
}

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

    #[test]
    fn test_empty_text_returns_empty() {
        let result = extract_entities("http://localhost:11434", "llama3", "", DEFAULT_LABELS);
        assert!(result.is_ok());
        assert!(result.unwrap().is_empty());
    }

    #[test]
    fn test_whitespace_text_returns_empty() {
        let result = extract_entities(
            "http://localhost:11434",
            "llama3",
            "   \n  ",
            DEFAULT_LABELS,
        );
        assert!(result.is_ok());
        assert!(result.unwrap().is_empty());
    }

    #[test]
    fn test_slugify_is_accessible() {
        let id = slugify("FastAPI Framework");
        assert_eq!(id, "fastapi-framework");
    }

    #[test]
    fn test_entity_serde_roundtrip() {
        let entity = Entity {
            id: "python".to_string(),
            label: "Python".to_string(),
            type_: "programming language".to_string(),
            score: 0.95,
        };
        let json = serde_json::to_string(&entity).unwrap();
        let deserialized: Entity = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.id, "python");
        assert_eq!(deserialized.label, "Python");
        assert_eq!(deserialized.type_, "programming language");
        assert!((deserialized.score - 0.95).abs() < f64::EPSILON);
    }

    #[test]
    fn test_default_labels_are_not_empty() {
        assert!(!DEFAULT_LABELS.is_empty());
        assert!(DEFAULT_LABELS.contains(&"technology"));
        assert!(DEFAULT_LABELS.contains(&"person"));
    }
}