2024-08-13 14:35:43 -04:00
|
|
|
from abc import ABC, abstractmethod
|
2024-08-15 12:03:41 -04:00
|
|
|
from pydantic import Field
|
2024-08-13 14:35:43 -04:00
|
|
|
from datetime import datetime
|
2024-08-15 12:03:41 -04:00
|
|
|
from uuid import uuid4
|
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-15 12:03:41 -04:00
|
|
|
uuid: str = Field(default_factory=lambda: str(uuid4()))
|
2024-08-13 14:35:43 -04:00
|
|
|
name: str
|
2024-08-15 12:03:41 -04:00
|
|
|
labels: list[str] = Field(default_factory=list)
|
2024-08-15 11:04:57 -04:00
|
|
|
created_at: datetime
|
2024-08-13 14:35:43 -04:00
|
|
|
|
|
|
|
@abstractmethod
|
2024-08-14 10:17:12 -04:00
|
|
|
async def save(self, driver: AsyncDriver): ...
|
2024-08-13 14:35:43 -04:00
|
|
|
|
2024-08-16 09:29:57 -04:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.uuid)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, Node):
|
|
|
|
return self.uuid == other.uuid
|
|
|
|
return False
|
|
|
|
|
2024-08-13 14:35:43 -04:00
|
|
|
|
|
|
|
class EpisodicNode(Node):
|
2024-08-15 12:03:41 -04:00
|
|
|
source: str = Field(description="source type")
|
|
|
|
source_description: str = Field(description="description of the data source")
|
|
|
|
content: str = Field(description="raw episode data")
|
|
|
|
entity_edges: list[str] = Field(
|
|
|
|
description="list of entity edges referenced in this episode",
|
|
|
|
default_factory=list,
|
|
|
|
)
|
|
|
|
valid_at: datetime | None = Field(
|
|
|
|
description="datetime of when the original document was created",
|
|
|
|
default=None,
|
|
|
|
)
|
2024-08-13 14:35:43 -04:00
|
|
|
|
|
|
|
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})
|
2024-08-16 09:29:57 -04:00
|
|
|
SET n = {uuid: $uuid, name: $name, source_description: $source_description, source: $source, content: $content,
|
2024-08-15 11:04:57 -04:00
|
|
|
entity_edges: $entity_edges, created_at: $created_at, valid_at: $valid_at}
|
2024-08-13 14:35:43 -04:00
|
|
|
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,
|
2024-08-15 11:04:57 -04:00
|
|
|
entity_edges=self.entity_edges,
|
|
|
|
created_at=self.created_at,
|
|
|
|
valid_at=self.valid_at,
|
2024-08-16 09:29:57 -04:00
|
|
|
source=self.source,
|
2024-08-14 10:17:12 -04:00
|
|
|
_database="neo4j",
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.info(f"Saved Node to neo4j: {self.uuid}")
|
2024-08-13 14:35:43 -04:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2024-08-15 11:04:57 -04:00
|
|
|
class EntityNode(Node):
|
2024-08-15 12:03:41 -04:00
|
|
|
summary: str = Field(description="regional summary of surrounding edges")
|
|
|
|
|
|
|
|
async def update_summary(self, driver: AsyncDriver): ...
|
2024-08-13 14:35:43 -04:00
|
|
|
|
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-15 11:04:57 -04:00
|
|
|
MERGE (n:Entity {uuid: $uuid})
|
|
|
|
SET n = {uuid: $uuid, name: $name, summary: $summary, created_at: $created_at}
|
2024-08-13 14:35:43 -04:00
|
|
|
RETURN n.uuid AS uuid""",
|
2024-08-14 10:17:12 -04:00
|
|
|
uuid=self.uuid,
|
|
|
|
name=self.name,
|
|
|
|
summary=self.summary,
|
2024-08-15 11:04:57 -04:00
|
|
|
created_at=self.created_at,
|
2024-08-14 10:17:12 -04:00
|
|
|
)
|
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
|