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,
36 pub entity_type: String,
38 pub description: Option<String>,
40 pub confidence: Option<f32>,
42}
43
44#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
46pub struct ExtractedRelationship {
47 pub source: String,
49 pub target: String,
51 pub relation: String,
53 pub strength: f32,
55}
56
57#[derive(Debug, Clone, Default, Serialize, Deserialize)]
59pub struct ExtractionOutput {
60 pub entities: Vec<ExtractedEntity>,
62 pub relationships: Vec<ExtractedRelationship>,
64 pub embedding: Option<Vec<f32>>,
66 pub backend: String,
68 pub elapsed_ms: u64,
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
74#[serde(rename_all = "kebab-case")]
75pub enum BackendKind {
76 Llm,
78 Embedding,
80 None,
82 Composite,
84}
85
86impl BackendKind {
87 pub fn as_str(self) -> &'static str {
89 match self {
90 BackendKind::Llm => "llm",
91 BackendKind::Embedding => "embedding",
92 BackendKind::None => "none",
93 BackendKind::Composite => "composite",
94 }
95 }
96
97 pub fn parse(s: &str) -> Option<Self> {
99 match s.to_ascii_lowercase().as_str() {
100 "llm" => Some(BackendKind::Llm),
101 "embedding" => Some(BackendKind::Embedding),
102 "none" => Some(BackendKind::None),
103 "both" | "composite" => Some(BackendKind::Composite),
104 _ => None,
105 }
106 }
107}
108
109#[async_trait]
115pub trait ExtractionBackend: Send + Sync {
116 fn kind(&self) -> BackendKind;
118
119 fn model_name(&self) -> String;
121
122 async fn extract(
127 &self,
128 content: &str,
129 hints: &ExtractionHints,
130 ) -> Result<ExtractionOutput, AppError>;
131
132 async fn health(&self) -> Result<BackendHealth, AppError>;
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize)]
138pub struct BackendHealth {
139 pub kind: BackendKind,
141 pub healthy: bool,
143 pub model_name: String,
145 pub message: String,
147}
148
149pub type SharedBackend = Arc<dyn ExtractionBackend>;
151
152pub mod composite_backend;
153pub mod embedding_backend;
154pub mod llm_backend;
155pub mod llm_embedding;
156pub mod none_backend;
157
158pub use composite_backend::{backend_from_kind, default_backend, CompositeBackend};
159pub use embedding_backend::EmbeddingBackend;
160pub use llm_backend::{LlmBackend, LlmExtractorConfig};
161pub use llm_embedding::{EmbeddingFlavour, LlmEmbedding};
162pub use none_backend::NoneBackend;