refactoring: completed storage, now using new fn to construct

This commit is contained in:
Per Stark
2024-11-21 09:23:42 +01:00
parent fc20526789
commit 200707af90
8 changed files with 121 additions and 148 deletions

View File

@@ -0,0 +1,55 @@
use crate::stored_object;
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum KnowledgeEntityType {
Idea,
Project,
Document,
Page,
TextSnippet,
// Add more types as needed
}
impl From<String> for KnowledgeEntityType {
fn from(s: String) -> Self {
match s.to_lowercase().as_str() {
"idea" => KnowledgeEntityType::Idea,
"project" => KnowledgeEntityType::Project,
"document" => KnowledgeEntityType::Document,
"page" => KnowledgeEntityType::Page,
"textsnippet" => KnowledgeEntityType::TextSnippet,
_ => KnowledgeEntityType::Document, // Default case
}
}
}
stored_object!(KnowledgeEntity, "knowledge_entity", {
source_id: String,
name: String,
description: String,
entity_type: KnowledgeEntityType,
metadata: Option<serde_json::Value>,
embedding: Vec<f32>
});
impl KnowledgeEntity {
pub fn new(
source_id: String,
name: String,
description: String,
entity_type: KnowledgeEntityType,
metadata: Option<serde_json::Value>,
embedding: Vec<f32>,
) -> Self {
Self {
id: Uuid::new_v4().to_string(),
source_id,
name,
description,
entity_type,
metadata,
embedding,
}
}
}

View File

@@ -0,0 +1,26 @@
use crate::stored_object;
use uuid::Uuid;
stored_object!(KnowledgeRelationship, "knowledge_relationship", {
in_: String,
out: String,
relationship_type: String,
metadata: Option<serde_json::Value>
});
impl KnowledgeRelationship {
pub fn new(
in_: String,
out: String,
relationship_type: String,
metadata: Option<serde_json::Value>,
) -> Self {
Self {
id: Uuid::new_v4().to_string(),
in_,
out,
relationship_type,
metadata,
}
}
}

View File

@@ -1,5 +1,7 @@
use axum::async_trait;
use serde::{Deserialize, Serialize};
pub mod knowledge_entity;
pub mod knowledge_relationship;
pub mod text_chunk;
pub mod text_content;
@@ -11,7 +13,7 @@ pub trait StoredObject: Serialize + for<'de> Deserialize<'de> {
#[macro_export]
macro_rules! stored_object {
($name:ident, $table:expr, {$($field:ident: $ty:ty),*}) => {
($name:ident, $table:expr, {$($(#[$attr:meta])* $field:ident: $ty:ty),*}) => {
use axum::async_trait;
use serde::{Deserialize, Deserializer, Serialize};
use surrealdb::sql::Thing;

View File

@@ -11,15 +11,18 @@ stored_object!(TextContent, "text_content", {
});
impl TextContent {
pub fn new(text: String, instructions: String, category: String) -> Self {
pub fn new(
text: String,
instructions: String,
category: String,
file_info: Option<FileInfo>,
) -> Self {
Self {
id: Uuid::new_v4().to_string(),
text,
file_info: None,
file_info,
instructions,
category,
}
}
// Other methods...
}