2024-08-13 14:35:43 -04:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from datetime import datetime
|
|
|
|
from uuid import uuid1
|
2024-08-14 10:17:12 -04:00
|
|
|
|
|
|
|
from openai import OpenAI
|
|
|
|
from pydantic import BaseModel, Field
|
2024-08-13 14:35:43 -04:00
|
|
|
from neo4j import AsyncDriver
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Node(BaseModel, ABC):
|
2024-08-14 10:17:12 -04:00
|
|
|
uuid: Field(default_factory=lambda: uuid1().hex)
|
2024-08-13 14:35:43 -04:00
|
|
|
name: str
|
|
|
|
labels: list[str]
|
|
|
|
transaction_from: datetime
|
|
|
|
|
|
|
|
@abstractmethod
|
2024-08-14 10:17:12 -04:00
|
|
|
async def save(self, driver: AsyncDriver): ...
|
2024-08-13 14:35:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
class EpisodicNode(Node):
|
|
|
|
source: str # source type
|
|
|
|
source_description: str # description of the data source
|
|
|
|
content: str # raw episode data
|
|
|
|
semantic_edges: list[str] # list of semantic edges referenced in this episode
|
|
|
|
valid_from: datetime = None # datetime of when the original document was created
|
|
|
|
|
|
|
|
async def save(self, driver: AsyncDriver):
|
2024-08-14 10:17:12 -04:00
|
|
|
result = await driver.execute_query(
|
|
|
|
"""
|
2024-08-13 14:35:43 -04:00
|
|
|
MERGE (n:Episodic {uuid: $uuid})
|
|
|
|
SET n = {uuid: $uuid, name: $name, source_description: $source_description, content: $content,
|
|
|
|
semantic_edges: $semantic_edges, transaction_from: $transaction_from, valid_from: $valid_from}
|
|
|
|
RETURN n.uuid AS uuid""",
|
2024-08-14 10:17:12 -04:00
|
|
|
uuid=self.uuid,
|
|
|
|
name=self.name,
|
|
|
|
source_description=self.source_description,
|
|
|
|
content=self.content,
|
|
|
|
semantic_edges=self.semantic_edges,
|
|
|
|
transaction_from=self.transaction_from,
|
|
|
|
valid_from=self.valid_from,
|
|
|
|
_database="neo4j",
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.info(f"Saved Node to neo4j: {self.uuid}")
|
2024-08-13 14:35:43 -04:00
|
|
|
print(self.uuid)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class SemanticNode(Node):
|
|
|
|
summary: str # regional summary of surrounding edges
|
|
|
|
|
2024-08-14 10:17:12 -04:00
|
|
|
async def refresh_summary(self, driver: AsyncDriver, llm_client: OpenAI): ...
|
2024-08-13 14:35:43 -04:00
|
|
|
|
2024-08-14 10:17:12 -04:00
|
|
|
async def save(self, driver: AsyncDriver):
|
|
|
|
result = await driver.execute_query(
|
|
|
|
"""
|
2024-08-13 14:35:43 -04:00
|
|
|
MERGE (n:Semantic {uuid: $uuid})
|
|
|
|
SET n = {uuid: $uuid, name: $name, summary: $summary, transaction_from: $transaction_from}
|
|
|
|
RETURN n.uuid AS uuid""",
|
2024-08-14 10:17:12 -04:00
|
|
|
uuid=self.uuid,
|
|
|
|
name=self.name,
|
|
|
|
summary=self.summary,
|
|
|
|
transaction_from=self.transaction_from,
|
|
|
|
)
|
2024-08-13 14:35:43 -04:00
|
|
|
|
2024-08-14 10:17:12 -04:00
|
|
|
logger.info(f"Saved Node to neo4j: {self.uuid}")
|
2024-08-13 14:35:43 -04:00
|
|
|
|
|
|
|
return result
|