working entity storage

This commit is contained in:
Per Stark
2024-11-05 22:00:04 +01:00
parent 40cf2e3b5b
commit 6d8cd05c1a
3 changed files with 30 additions and 22 deletions
+14 -3
View File
@@ -1,17 +1,28 @@
use serde::Deserialize; use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize; use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
use surrealdb::sql::Thing;
use uuid::Uuid; use uuid::Uuid;
/// Represents a generic knowledge entity in the graph. /// Represents a generic knowledge entity in the graph.
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct KnowledgeEntity { pub struct KnowledgeEntity {
pub id: String, // Generated in Rust #[serde(deserialize_with = "thing_to_string")]
pub id: String,
pub name: String, pub name: String,
pub description: String, pub description: String,
pub entity_type: KnowledgeEntityType, pub entity_type: KnowledgeEntityType,
pub source_id: String, // Links to FileInfo or TextContent pub source_id: String,
pub metadata: Option<serde_json::Value>, // Additional metadata pub metadata: Option<serde_json::Value>,
}
fn thing_to_string<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let thing = Thing::deserialize(deserializer)?;
Ok(thing.id.to_raw())
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
+8 -15
View File
@@ -1,5 +1,4 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use surrealdb::RecordId;
use tracing::{debug, info}; use tracing::{debug, info};
use uuid::Uuid; use uuid::Uuid;
use crate::{models::file_info::FileInfo, surrealdb::{SurrealDbClient, SurrealError}, utils::llm::create_json_ld}; use crate::{models::file_info::FileInfo, surrealdb::{SurrealDbClient, SurrealError}, utils::llm::create_json_ld};
@@ -17,12 +16,6 @@ pub struct TextContent {
pub category: String, pub category: String,
} }
#[derive(Debug,Deserialize)]
struct Record {
#[allow(dead_code)]
id: RecordId,
}
/// Error types for processing `TextContent`. /// Error types for processing `TextContent`.
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum ProcessingError { pub enum ProcessingError {
@@ -79,7 +72,7 @@ impl TextContent {
for entity in entities { for entity in entities {
info!("{:?}", entity); info!("{:?}", entity);
let _created: Option<Record> = db_client let _created: Option<KnowledgeEntity> = db_client
.client .client
.create(("knowledge_entity", &entity.id.to_string())) .create(("knowledge_entity", &entity.id.to_string()))
.content(entity) .content(entity)
@@ -89,15 +82,15 @@ impl TextContent {
} }
for relationship in relationships { for relationship in relationships {
info!("{:?}", relationship); // info!("{:?}", relationship);
let _created: Option<Record> = db_client // let _created: Option<Record> = db_client
.client // .client
.insert(("knowledge_relationship", &relationship.id.to_string())) // .insert(("knowledge_relationship", &relationship.id.to_string()))
.content(relationship) // .content(relationship)
.await?; // .await?;
debug!("{:?}",_created); // debug!("{:?}",_created);
} }
+8 -4
View File
@@ -3,6 +3,8 @@ use async_openai::types::ChatCompletionRequestUserMessage;
use async_openai::types::CreateChatCompletionRequestArgs; use async_openai::types::CreateChatCompletionRequestArgs;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use surrealdb::sql::Thing;
use surrealdb::RecordId;
use tracing::debug; use tracing::debug;
use tracing::info; use tracing::info;
use uuid::Uuid; use uuid::Uuid;
@@ -83,11 +85,13 @@ 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, db_client: &SurrealDbClient) -> 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 // Get the nodes from the database
let mut result = db_client.client.query("SELECT * FROM knowledge_entity").await?; let entities: Vec<KnowledgeEntity> = db_client.client.select("knowledge_entity").await?;
info!("{:?}", result.num_statements()); info!("{:?}", entities);
let deleted: Vec<KnowledgeEntity> = db_client.client.delete("knowledge_entity").await?;
info!{"{:?}", deleted};
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!({