working storing relationships

This commit is contained in:
Per Stark
2024-11-06 21:24:30 +01:00
parent 7d587f53cf
commit d2323a9262
3 changed files with 120 additions and 115 deletions

View File

@@ -51,6 +51,7 @@ impl From<String> for KnowledgeEntityType {
/// Represents a relationship between two knowledge entities.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct KnowledgeRelationship {
#[serde(deserialize_with = "thing_to_string")]
pub id: String,
#[serde(rename = "in")]
pub in_: String, // Target KnowledgeEntity ID

View File

@@ -84,13 +84,13 @@ impl TextContent {
for relationship in relationships {
// info!("{:?}", relationship);
// let _created: Option<Record> = db_client
// .client
// .insert(("knowledge_relationship", &relationship.id.to_string()))
// .content(relationship)
// .await?;
let _created: Option<KnowledgeRelationship> = db_client
.client
.insert(("knowledge_relationship", &relationship.id.to_string()))
.content(relationship)
.await?;
// debug!("{:?}",_created);
debug!("{:?}",_created);
}

View File

@@ -1,20 +1,14 @@
use async_openai::types::ChatCompletionRequestSystemMessage;
use async_openai::types::ChatCompletionRequestUserMessage;
use async_openai::types::CreateChatCompletionRequestArgs;
use serde::Deserialize;
use serde::Serialize;
use surrealdb::sql::Thing;
use surrealdb::RecordId;
use tracing::debug;
use tracing::info;
use uuid::Uuid;
use crate::models::graph_entities::GraphMapper;
use crate::models::graph_entities::KnowledgeEntity;
use crate::models::graph_entities::KnowledgeEntityType;
use crate::models::graph_entities::KnowledgeRelationship;
use crate::models::text_content::ProcessingError;
use crate::surrealdb::SurrealDbClient;
use async_openai::types::{CreateChatCompletionRequestArgs, ChatCompletionRequestUserMessage, ChatCompletionRequestSystemMessage };
use serde::{Deserialize, Serialize};
use serde_json::json;
use tracing::{info,debug};
use uuid::Uuid;
/// Represents a single knowledge entity from the LLM.
#[derive(Debug, Serialize, Deserialize, Clone)]
@@ -42,11 +36,15 @@ pub struct LLMGraphAnalysisResult {
}
impl LLMGraphAnalysisResult {
pub fn to_database_entities(&self, source_id: &Uuid) -> (Vec<KnowledgeEntity>, Vec<KnowledgeRelationship>) {
pub fn to_database_entities(
&self,
source_id: &Uuid,
) -> (Vec<KnowledgeEntity>, Vec<KnowledgeRelationship>) {
let mut mapper = GraphMapper::new();
// First pass: Create all entities and map their keys to UUIDs
let entities: Vec<KnowledgeEntity> = self.knowledge_entities
let entities: Vec<KnowledgeEntity> = self
.knowledge_entities
.iter()
.map(|llm_entity| {
let id = mapper.assign_id(&llm_entity.key);
@@ -62,7 +60,8 @@ impl LLMGraphAnalysisResult {
.collect();
// Second pass: Create relationships using mapped UUIDs
let relationships: Vec<KnowledgeRelationship> = self.relationships
let relationships: Vec<KnowledgeRelationship> = self
.relationships
.iter()
.filter_map(|llm_rel| {
let source_id = mapper.get_id(&llm_rel.source)?;
@@ -83,7 +82,12 @@ impl LLMGraphAnalysisResult {
}
/// 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
let entities: Vec<KnowledgeEntity> = db_client.client.select("knowledge_entity").await?;
info!("{:?}", entities);
@@ -91,8 +95,6 @@ pub async fn create_json_ld(category: &str, instructions: &str, text: &str, db_c
let deleted: Vec<KnowledgeEntity> = db_client.client.delete("knowledge_entity").await?;
info! {"{:?}", deleted};
let client = async_openai::Client::new();
let schema = json!({
"type": "object",
@@ -179,7 +181,6 @@ pub async fn create_json_ld(category: &str, instructions: &str, text: &str, db_c
6. Only create relationships between existing KnowledgeEntities.
"#;
let user_message = format!(
"Category: {}\nInstructions: {}\nContent:\n{}",
category, instructions, text
@@ -194,12 +195,15 @@ pub async fn create_json_ld(category: &str, instructions: &str, text: &str, db_c
ChatCompletionRequestUserMessage::from(user_message).into(),
])
.response_format(response_format)
.build().map_err(|e| ProcessingError::LLMError(e.to_string()))?;
.build()
.map_err(|e| ProcessingError::LLMError(e.to_string()))?;
// Send the request to OpenAI
let response = client.chat().create(request).await.map_err(|e| {
ProcessingError::LLMError(format!("OpenAI API request failed: {}", e))
})?;
let response = client
.chat()
.create(request)
.await
.map_err(|e| ProcessingError::LLMError(format!("OpenAI API request failed: {}", e)))?;
debug!("{:?}", response);