mirror of
https://github.com/perstarkse/minne.git
synced 2026-03-24 18:31:45 +01:00
feat: creation of nodes and edges complete
This commit is contained in:
@@ -79,6 +79,14 @@ impl GraphMapper {
|
||||
key_to_id: HashMap::new(),
|
||||
}
|
||||
}
|
||||
/// Get ID, tries to parse UUID
|
||||
pub fn get_or_parse_id(&mut self, key: &str) -> Uuid {
|
||||
if let Ok(parsed_uuid) = Uuid::parse_str(key) {
|
||||
parsed_uuid
|
||||
} else {
|
||||
self.key_to_id.get(key).unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// Assigns a new UUID for a given key.
|
||||
pub fn assign_id(&mut self, key: &str) -> Uuid {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use async_openai::{error::OpenAIError, types::{CreateEmbeddingRequest, CreateEmbeddingRequestArgs}};
|
||||
use async_openai::error::OpenAIError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use surrealdb::{engine::remote::ws::Client, Surreal};
|
||||
use tracing::{debug, info};
|
||||
@@ -50,6 +50,15 @@ impl TextContent {
|
||||
// Store TextContent
|
||||
let db_client = SurrealDbClient::new().await?;
|
||||
|
||||
// let deleted: Vec<KnowledgeEntity> = db_client.delete("knowledge_entity").await?;
|
||||
// info! {"{:?} KnowledgeEntities deleted", deleted.len()};
|
||||
|
||||
// let relationships_deleted: Vec<KnowledgeRelationship> =
|
||||
// db_client.delete("knowledge_relationship").await?;
|
||||
// info!("{:?} Relationships deleted", relationships_deleted.len());
|
||||
|
||||
// panic!("STOP");
|
||||
|
||||
// db_client.query("REMOVE INDEX embeddings ON knowledge_entity").await?;
|
||||
// db_client.query("DEFINE INDEX embeddings ON knowledge_entity FIELDS embedding HNSW DIMENSION 1536").await?;
|
||||
db_client.query("REBUILD INDEX IF EXISTS embeddings ON knowledge_entity").await?;
|
||||
@@ -78,7 +87,7 @@ impl TextContent {
|
||||
db_client: &Surreal<Client>,
|
||||
) -> Result<(), ProcessingError> {
|
||||
for entity in &entities {
|
||||
// info!("{:?}", &entity);
|
||||
info!("{:?}, {:?}, {:?}", &entity.id, &entity.name, &entity.description);
|
||||
|
||||
let _created: Option<KnowledgeEntity> = db_client
|
||||
.create(("knowledge_entity", &entity.id.to_string()))
|
||||
@@ -99,6 +108,18 @@ impl TextContent {
|
||||
debug!("{:?}",_created);
|
||||
}
|
||||
|
||||
for relationship in &relationships {
|
||||
let in_entity: Option<KnowledgeEntity> = db_client.select(("knowledge_entity",relationship.in_.to_string())).await?;
|
||||
let out_entity: Option<KnowledgeEntity> = db_client.select(("knowledge_entity", relationship.out.to_string())).await?;
|
||||
|
||||
if let (Some(in_), Some(out)) = (in_entity, out_entity) {
|
||||
info!("{} - {} is {} to {} - {}", in_.id, in_.name, relationship.relationship_type, out.id, out.name);
|
||||
}
|
||||
else {
|
||||
info!("No in or out entities found");
|
||||
}
|
||||
}
|
||||
|
||||
info!("Inserted to database: {:?} entities, {:?} relationships", entities.len(), relationships.len());
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,21 +1,16 @@
|
||||
use core::panic;
|
||||
|
||||
use crate::models::graph_entities::{
|
||||
GraphMapper, KnowledgeEntity, KnowledgeEntityType, KnowledgeRelationship,
|
||||
};
|
||||
use crate::models::text_content::ProcessingError;
|
||||
use async_openai::types::{
|
||||
ChatCompletionRequestSystemMessage, ChatCompletionRequestUserMessage,
|
||||
CreateChatCompletionRequestArgs, CreateEmbeddingRequestArgs, Embedding,
|
||||
CreateChatCompletionRequestArgs, CreateEmbeddingRequestArgs
|
||||
};
|
||||
use futures::future::try_join_all;
|
||||
use futures::SinkExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use surrealdb::engine::remote::ws::Client;
|
||||
use surrealdb::Surreal;
|
||||
use tokio::try_join;
|
||||
use tracing::{debug, info};
|
||||
use tracing::debug;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Represents a single knowledge entity from the LLM.
|
||||
@@ -135,13 +130,14 @@ impl LLMGraphAnalysisResult {
|
||||
.relationships
|
||||
.iter()
|
||||
.filter_map(|llm_rel| {
|
||||
let source_db_id = mapper.get_id(&llm_rel.source)?;
|
||||
let target_db_id = mapper.get_id(&llm_rel.target)?;
|
||||
let source_db_id = mapper.get_or_parse_id(&llm_rel.source);
|
||||
let target_db_id = mapper.get_or_parse_id(&llm_rel.target);
|
||||
debug!("IN: {}, OUT: {}", &source_db_id, &target_db_id);
|
||||
|
||||
Some(KnowledgeRelationship {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
out: source_db_id.to_string(),
|
||||
in_: target_db_id.to_string(),
|
||||
in_: source_db_id.to_string(),
|
||||
out: target_db_id.to_string(),
|
||||
relationship_type: llm_rel.type_.clone(),
|
||||
metadata: None,
|
||||
})
|
||||
@@ -173,6 +169,7 @@ pub async fn create_json_ld(
|
||||
|
||||
// Perform query and deserialize to struct
|
||||
let closest_entities: Vec<KnowledgeEntity> = db_client.query(closest_query).await?.take(0)?;
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
struct KnowledgeEntityToLLM {
|
||||
id: String,
|
||||
@@ -187,24 +184,8 @@ pub async fn create_json_ld(
|
||||
description: entity.description
|
||||
}).collect();
|
||||
|
||||
info!("{:?}", closest_entities_to_llm);
|
||||
debug!("{:?}", closest_entities_to_llm);
|
||||
|
||||
for entity in closest_entities_to_llm {
|
||||
info!("{:?}, {:?}", entity.name, entity.description);
|
||||
}
|
||||
// info!("Closest entities: {:?}", closest_entities);
|
||||
panic!("Quitting");
|
||||
|
||||
let deleted: Vec<KnowledgeEntity> = db_client.delete("knowledge_entity").await?;
|
||||
info! {"{:?} KnowledgeEntities deleted", deleted.len()};
|
||||
|
||||
// let relationships: Vec<KnowledgeRelationship> =
|
||||
// db_client.select("knowledge_relationship").await?;
|
||||
// info!("{:?} Relationships defined", relationships.len());
|
||||
|
||||
let relationships_deleted: Vec<KnowledgeRelationship> =
|
||||
db_client.delete("knowledge_relationship").await?;
|
||||
info!("{:?} Relationships deleted", relationships_deleted.len());
|
||||
|
||||
let schema = json!({
|
||||
"type": "object",
|
||||
@@ -258,7 +239,7 @@ pub async fn create_json_ld(
|
||||
|
||||
// Construct the system and user messages
|
||||
let system_message = r#"
|
||||
You are an expert document analyzer. You will receive a document's text content, along with user instructions and a category. Your task is to provide a structured JSON object representing the content in a graph format suitable for a graph database.
|
||||
You are an expert document analyzer. You will receive a document's text content, along with user instructions and a category. Your task is to provide a structured JSON object representing the content in a graph format suitable for a graph database. You will also be presented with some existing knowledge_entities, do not replicate these!
|
||||
|
||||
The JSON should have the following structure:
|
||||
|
||||
@@ -275,8 +256,8 @@ pub async fn create_json_ld(
|
||||
"relationships": [
|
||||
{
|
||||
"type": "RelationshipType",
|
||||
"source": "unique-key-1",
|
||||
"target": "unique-key-2"
|
||||
"source": "unique-key-1 or UUID from existing database",
|
||||
"target": "unique-key-1 or UUID from existing database"
|
||||
},
|
||||
// More relationships...
|
||||
]
|
||||
@@ -288,12 +269,13 @@ pub async fn create_json_ld(
|
||||
3. Define the type of each KnowledgeEntity using the following categories: Idea, Project, Document, Page, TextSnippet.
|
||||
4. Establish relationships between entities using types like RelatedTo, RelevantTo, SimilarTo.
|
||||
5. Use the `source` key to indicate the originating entity and the `target` key to indicate the related entity"
|
||||
6. You will be presented with a few existing KnowledgeEntities that are similar to the current ones. They will have an existing UUID. When creating relationships to these entities, use their UUID.
|
||||
7. Only create relationships between existing KnowledgeEntities.
|
||||
"#;
|
||||
|
||||
let user_message = format!(
|
||||
"Category: {}\nInstructions: {}\nContent:\n{}",
|
||||
category, instructions, text
|
||||
"Category: {}\nInstructions: {}\nContent:\n{}\nExisting KnowledgeEntities:{:?}",
|
||||
category, instructions, text, closest_entities_to_llm
|
||||
);
|
||||
|
||||
// Build the chat completion request
|
||||
|
||||
Reference in New Issue
Block a user