mirror of
https://github.com/perstarkse/minne.git
synced 2026-04-24 09:48:32 +02:00
storing relationships
This commit is contained in:
@@ -40,11 +40,11 @@ impl From<String> for KnowledgeEntityType {
|
|||||||
/// Represents a relationship between two knowledge entities.
|
/// Represents a relationship between two knowledge entities.
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct KnowledgeRelationship {
|
pub struct KnowledgeRelationship {
|
||||||
pub id: Uuid, // Generated in Rust
|
pub id: String,
|
||||||
#[serde(rename = "in")]
|
#[serde(rename = "in")]
|
||||||
pub in_: Uuid, // Target KnowledgeEntity ID
|
pub in_: String, // Target KnowledgeEntity ID
|
||||||
pub out: Uuid, // Source KnowledgeEntity ID
|
pub out: String, // Source KnowledgeEntity ID
|
||||||
pub relationship_type: String, // e.g., RelatedTo, RelevantTo
|
pub relationship_type: String, // e.g., RelatedTo, RelevantTo
|
||||||
pub metadata: Option<serde_json::Value>, // Additional metadata
|
pub metadata: Option<serde_json::Value>, // Additional metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,9 +50,10 @@ impl TextContent {
|
|||||||
/// Processes the `TextContent` by sending it to an LLM, storing in a graph DB, and vector DB.
|
/// Processes the `TextContent` by sending it to an LLM, storing in a graph DB, and vector DB.
|
||||||
pub async fn process(&self) -> Result<(), ProcessingError> {
|
pub async fn process(&self) -> Result<(), ProcessingError> {
|
||||||
// Store TextContent
|
// Store TextContent
|
||||||
|
let db_client = SurrealDbClient::new().await?;
|
||||||
|
|
||||||
// Step 1: Send to LLM for analysis
|
// Step 1: Send to LLM for analysis
|
||||||
let analysis = create_json_ld(&self.category, &self.instructions, &self.text).await?;
|
let analysis = create_json_ld(&self.category, &self.instructions, &self.text, &db_client).await?;
|
||||||
// info!("{:#?}", &analysis);
|
// info!("{:#?}", &analysis);
|
||||||
|
|
||||||
|
|
||||||
@@ -60,7 +61,7 @@ impl TextContent {
|
|||||||
let (entities, relationships) = analysis.to_database_entities(&self.id);
|
let (entities, relationships) = analysis.to_database_entities(&self.id);
|
||||||
|
|
||||||
// Step 3: Store in database
|
// Step 3: Store in database
|
||||||
self.store_in_graph_db(entities, relationships).await?;
|
self.store_in_graph_db(entities, relationships, &db_client).await?;
|
||||||
|
|
||||||
|
|
||||||
// Step 4: Split text and store in Vector DB
|
// Step 4: Split text and store in Vector DB
|
||||||
@@ -72,10 +73,9 @@ impl TextContent {
|
|||||||
async fn store_in_graph_db(
|
async fn store_in_graph_db(
|
||||||
&self,
|
&self,
|
||||||
entities: Vec<KnowledgeEntity>,
|
entities: Vec<KnowledgeEntity>,
|
||||||
relationships: Vec<KnowledgeRelationship>
|
relationships: Vec<KnowledgeRelationship>,
|
||||||
|
db_client: &SurrealDbClient,
|
||||||
) -> Result<(), ProcessingError> {
|
) -> Result<(), ProcessingError> {
|
||||||
let db_client = SurrealDbClient::new().await?;
|
|
||||||
|
|
||||||
for entity in entities {
|
for entity in entities {
|
||||||
info!("{:?}", entity);
|
info!("{:?}", entity);
|
||||||
|
|
||||||
@@ -85,11 +85,20 @@ impl TextContent {
|
|||||||
.content(entity)
|
.content(entity)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
info!("{:?}",_created);
|
debug!("{:?}",_created);
|
||||||
}
|
}
|
||||||
|
|
||||||
for relationship in relationships {
|
for relationship in relationships {
|
||||||
info!("{:?}", relationship);
|
info!("{:?}", relationship);
|
||||||
|
|
||||||
|
let _created: Option<Record> = db_client
|
||||||
|
.client
|
||||||
|
.insert(("knowledge_relationship", &relationship.id.to_string()))
|
||||||
|
.content(relationship)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
debug!("{:?}",_created);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ use async_openai::types::CreateChatCompletionRequestArgs;
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
use tracing::info;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use crate::models::graph_entities::GraphMapper;
|
use crate::models::graph_entities::GraphMapper;
|
||||||
use crate::models::graph_entities::KnowledgeEntity;
|
use crate::models::graph_entities::KnowledgeEntity;
|
||||||
use crate::models::graph_entities::KnowledgeEntityType;
|
use crate::models::graph_entities::KnowledgeEntityType;
|
||||||
use crate::models::graph_entities::KnowledgeRelationship;
|
use crate::models::graph_entities::KnowledgeRelationship;
|
||||||
use crate::models::text_content::ProcessingError;
|
use crate::models::text_content::ProcessingError;
|
||||||
|
use crate::surrealdb::SurrealDbClient;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
/// Represents a single knowledge entity from the LLM.
|
/// Represents a single knowledge entity from the LLM.
|
||||||
@@ -65,9 +67,9 @@ impl LLMGraphAnalysisResult {
|
|||||||
let target_id = mapper.get_id(&llm_rel.target)?;
|
let target_id = mapper.get_id(&llm_rel.target)?;
|
||||||
|
|
||||||
Some(KnowledgeRelationship {
|
Some(KnowledgeRelationship {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4().to_string(),
|
||||||
out: *source_id,
|
out: source_id.to_string(),
|
||||||
in_: *target_id,
|
in_: target_id.to_string(),
|
||||||
relationship_type: llm_rel.type_.clone(),
|
relationship_type: llm_rel.type_.clone(),
|
||||||
metadata: None,
|
metadata: None,
|
||||||
})
|
})
|
||||||
@@ -79,7 +81,14 @@ impl LLMGraphAnalysisResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Sends text to an LLM for analysis.
|
/// Sends text to an LLM for analysis.
|
||||||
pub async fn create_json_ld(category: &str, instructions: &str, text: &str) -> Result<LLMGraphAnalysisResult, ProcessingError> {
|
pub async fn create_json_ld(category: &str, instructions: &str, text: &str, db_client: &SurrealDbClient) -> Result<LLMGraphAnalysisResult, ProcessingError> {
|
||||||
|
// Get the nodes from the database
|
||||||
|
let mut result = db_client.client.query("SELECT * FROM knowledge_entity").await?;
|
||||||
|
info!("{:?}", result.num_statements());
|
||||||
|
|
||||||
|
let db_representation: Vec<KnowledgeEntity> = result.take(1)?;
|
||||||
|
info!("{:?}", db_representation);
|
||||||
|
|
||||||
let client = async_openai::Client::new();
|
let client = async_openai::Client::new();
|
||||||
let schema = json!({
|
let schema = json!({
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
|||||||
Reference in New Issue
Block a user