sqlite_graphrag/extract/
mod.rs1pub mod codex_compat;
10
11use crate::errors::AppError;
12use async_trait::async_trait;
13use serde::{Deserialize, Serialize};
14use std::sync::Arc;
15
16#[derive(Debug, Clone, Default, Serialize, Deserialize)]
18pub struct ExtractionHints {
19 pub memory_name: Option<String>,
21 pub memory_type: Option<String>,
23 pub existing_entities: Vec<String>,
25 pub skip_relations: bool,
27 pub seed: Option<u64>,
29}
30
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
33pub struct ExtractedEntity {
34 pub name: String,
35 pub entity_type: String,
36 pub description: Option<String>,
37 pub confidence: Option<f32>,
38}
39
40#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42pub struct ExtractedRelationship {
43 pub source: String,
44 pub target: String,
45 pub relation: String,
46 pub strength: f32,
47}
48
49#[derive(Debug, Clone, Default, Serialize, Deserialize)]
51pub struct ExtractionOutput {
52 pub entities: Vec<ExtractedEntity>,
53 pub relationships: Vec<ExtractedRelationship>,
54 pub embedding: Option<Vec<f32>>,
56 pub backend: String,
58 pub elapsed_ms: u64,
60}
61
62#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
64#[serde(rename_all = "kebab-case")]
65pub enum BackendKind {
66 Llm,
67 Embedding,
68 None,
69 Composite,
70}
71
72impl BackendKind {
73 pub fn as_str(self) -> &'static str {
74 match self {
75 BackendKind::Llm => "llm",
76 BackendKind::Embedding => "embedding",
77 BackendKind::None => "none",
78 BackendKind::Composite => "composite",
79 }
80 }
81
82 pub fn parse(s: &str) -> Option<Self> {
83 match s.to_ascii_lowercase().as_str() {
84 "llm" => Some(BackendKind::Llm),
85 "embedding" => Some(BackendKind::Embedding),
86 "none" => Some(BackendKind::None),
87 "both" | "composite" => Some(BackendKind::Composite),
88 _ => None,
89 }
90 }
91}
92
93#[async_trait]
99pub trait ExtractionBackend: Send + Sync {
100 fn kind(&self) -> BackendKind;
102
103 fn model_name(&self) -> String;
105
106 async fn extract(
111 &self,
112 content: &str,
113 hints: &ExtractionHints,
114 ) -> Result<ExtractionOutput, AppError>;
115
116 async fn health(&self) -> Result<BackendHealth, AppError>;
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
122pub struct BackendHealth {
123 pub kind: BackendKind,
124 pub healthy: bool,
125 pub model_name: String,
126 pub message: String,
127}
128
129pub type SharedBackend = Arc<dyn ExtractionBackend>;
131
132pub mod composite_backend;
133pub mod embedding_backend;
134pub mod llm_backend;
135pub mod llm_embedding;
136pub mod none_backend;
137
138pub use composite_backend::{backend_from_kind, default_backend, CompositeBackend};
139pub use embedding_backend::EmbeddingBackend;
140pub use llm_backend::{LlmBackend, LlmExtractorConfig};
141pub use llm_embedding::{EmbeddingFlavour, LlmEmbedding, EMBEDDING_DIM as LLM_EMBEDDING_DIM};
142pub use none_backend::NoneBackend;