sqlite_graphrag/extract/
mod.rs1use crate::errors::AppError;
10use async_trait::async_trait;
11use serde::{Deserialize, Serialize};
12use std::sync::Arc;
13
14#[derive(Debug, Clone, Default, Serialize, Deserialize)]
16pub struct ExtractionHints {
17 pub memory_name: Option<String>,
19 pub memory_type: Option<String>,
21 pub existing_entities: Vec<String>,
23 pub skip_relations: bool,
25 pub seed: Option<u64>,
27}
28
29#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31pub struct ExtractedEntity {
32 pub name: String,
34 pub entity_type: String,
36 pub description: Option<String>,
38 pub confidence: Option<f32>,
40}
41
42#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
44pub struct ExtractedRelationship {
45 pub source: String,
47 pub target: String,
49 pub relation: String,
51 pub strength: f32,
53}
54
55#[derive(Debug, Clone, Default, Serialize, Deserialize)]
57pub struct ExtractionOutput {
58 pub entities: Vec<ExtractedEntity>,
60 pub relationships: Vec<ExtractedRelationship>,
62 pub embedding: Option<Vec<f32>>,
64 pub backend: String,
66 pub elapsed_ms: u64,
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
72#[serde(rename_all = "kebab-case")]
73pub enum BackendKind {
74 Llm,
76 Embedding,
78 None,
80 Composite,
82}
83
84impl BackendKind {
85 pub fn as_str(self) -> &'static str {
87 match self {
88 BackendKind::Llm => "llm",
89 BackendKind::Embedding => "embedding",
90 BackendKind::None => "none",
91 BackendKind::Composite => "composite",
92 }
93 }
94
95 pub fn parse(s: &str) -> Option<Self> {
97 match s.to_ascii_lowercase().as_str() {
98 "llm" => Some(BackendKind::Llm),
99 "embedding" => Some(BackendKind::Embedding),
100 "none" => Some(BackendKind::None),
101 "both" | "composite" => Some(BackendKind::Composite),
102 _ => None,
103 }
104 }
105}
106
107#[async_trait]
113pub trait ExtractionBackend: Send + Sync {
114 fn kind(&self) -> BackendKind;
116
117 fn model_name(&self) -> String;
119
120 async fn extract(
125 &self,
126 content: &str,
127 hints: &ExtractionHints,
128 ) -> Result<ExtractionOutput, AppError>;
129
130 async fn health(&self) -> Result<BackendHealth, AppError>;
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct BackendHealth {
137 pub kind: BackendKind,
139 pub healthy: bool,
141 pub model_name: String,
143 pub message: String,
145}
146
147pub type SharedBackend = Arc<dyn ExtractionBackend>;
149
150pub mod composite_backend;
151pub mod embedding_backend;
152pub mod llm_backend;
153pub mod none_backend;
154
155pub use composite_backend::{backend_from_kind, default_backend, CompositeBackend};
156pub use embedding_backend::EmbeddingBackend;
157pub use llm_backend::{LlmBackend, LlmExtractorConfig};
158pub use none_backend::NoneBackend;