update entity/repostitory/service

This commit is contained in:
phernandez
2025-01-06 18:57:04 -06:00
parent fb330fe745
commit ea359d2484
10 changed files with 115 additions and 253 deletions
@@ -100,19 +100,6 @@ async def get_entity(path_id: PathId, entity_service: EntityServiceDep) -> Entit
raise HTTPException(status_code=404, detail=f"Entity with {path_id} not found")
@router.post("/search", response_model=SearchNodesResponse)
async def search_nodes(
data: SearchNodesRequest, entity_service: EntityServiceDep
) -> SearchNodesResponse:
"""Search for entities in the knowledge graph."""
logger.debug(f"Searching nodes with query: {data.query}")
matches = await entity_service.search(data.query)
logger.debug(f"Found {len(matches)} matches for '{data.query}'")
return SearchNodesResponse(
matches=[EntityResponse.model_validate(entity) for entity in matches], query=data.query
)
@router.post("/nodes", response_model=EntityListResponse)
async def open_nodes(data: OpenNodesRequest, entity_service: EntityServiceDep) -> EntityListResponse:
-5
View File
@@ -40,10 +40,5 @@ class Document(Base):
DateTime, server_default=text("CURRENT_TIMESTAMP"), onupdate=text("CURRENT_TIMESTAMP")
)
# Relationships
entities: Mapped[List["Entity"]] = relationship( # pyright: ignore [reportUndefinedVariable] # noqa: F821
"Entity", back_populates="document", cascade="all, delete-orphan"
)
def __repr__(self) -> str:
return f"Document(id={self.id}, path_id='{self.path_id}', checksum='{self.checksum}', created_at='{self.created_at}', updated_at='{self.updated_at}')"
+41 -23
View File
@@ -3,47 +3,69 @@
from datetime import datetime
from typing import Optional
from sqlalchemy import Integer, String, Text, ForeignKey, UniqueConstraint, text, DateTime, Index
from sqlalchemy import (
Integer,
String,
Text,
ForeignKey,
UniqueConstraint,
text,
DateTime,
Index,
JSON,
CheckConstraint,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from basic_memory.models.base import Base
from basic_memory.models.documents import Document
from enum import Enum
class EntityType(str, Enum):
"""Types of knowledge nodes."""
KNOWLEDGE = "knowledge"
NOTE = "note"
class Entity(Base):
"""
Core entity in the knowledge graph.
Entities represent semantic nodes maintained by the AI layer. Each entity:
- Has a unique numeric ID (database-generated)
- Maps to a document file on disk (optional)
- Maps to a file on disk
- Maintains a checksum for change detection
- Tracks both source document and semantic properties
"""
__tablename__ = "entity"
__table_args__ = (
UniqueConstraint("entity_type", "name", name="uix_entity_type_name"),
UniqueConstraint("path_id", name="uix_entity_path_id"), # Make path_id unique
Index("ix_entity_type", "entity_type"),
Index("ix_entity_doc_id", "doc_id"),
Index("ix_entity_created_at", "created_at"), # For timeline queries
Index("ix_entity_updated_at", "updated_at") # For timeline queries
Index("ix_entity_updated_at", "updated_at"), # For timeline queries
CheckConstraint(
f"entity_type IN {tuple(t.value for t in EntityType)}", name="check_entity_type"
),
)
# Core identity
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String)
entity_type: Mapped[str] = mapped_column(String)
# Normalized path for URIs - must be unique
path_id: Mapped[str] = mapped_column(String, index=True)
entity_type: Mapped[EntityType] = mapped_column(String, default=EntityType.KNOWLEDGE)
entity_metadata: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
# Normalized path for URIs
path_id: Mapped[str] = mapped_column(String, unique=True, index=True)
# Actual filesystem relative path
file_path: Mapped[str] = mapped_column(String, unique=True, index=True)
# Content and validation
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
# checksum of file
checksum: Mapped[Optional[str]] = mapped_column(String, nullable=True)
# Content for knowledge entity_type
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
# Metadata and tracking
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
@@ -51,11 +73,6 @@ class Entity(Base):
DateTime, server_default=text("CURRENT_TIMESTAMP"), onupdate=text("CURRENT_TIMESTAMP")
)
# Relations
doc_id: Mapped[Optional[int]] = mapped_column(
Integer, ForeignKey("document.id", ondelete="SET NULL"), nullable=True
)
# Relationships
observations = relationship(
"Observation", back_populates="entity", cascade="all, delete-orphan"
@@ -72,7 +89,6 @@ class Entity(Base):
foreign_keys="[Relation.to_id]",
cascade="all, delete-orphan",
)
document: Mapped[Optional[Document]] = relationship(Document, back_populates="entities")
@property
def relations(self):
@@ -99,8 +115,8 @@ class Observation(Base):
__tablename__ = "observation"
__table_args__ = (
Index("ix_observation_entity_id", "entity_id"), # Add FK index
Index("ix_observation_category", "category"), # Add category index
Index("ix_observation_entity_id", "entity_id"), # Add FK index
Index("ix_observation_category", "category"), # Add category index
Index("ix_observation_created_at", "created_at"), # For timeline queries
Index("ix_observation_updated_at", "updated_at"), # For timeline queries
)
@@ -112,7 +128,7 @@ class Observation(Base):
String,
nullable=False,
default=ObservationCategory.NOTE.value,
server_default=ObservationCategory.NOTE.value
server_default=ObservationCategory.NOTE.value,
)
context: Mapped[str] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
@@ -153,8 +169,10 @@ class Relation(Base):
)
# Relationships
from_entity = relationship("Entity", foreign_keys=[from_id], back_populates="outgoing_relations")
from_entity = relationship(
"Entity", foreign_keys=[from_id], back_populates="outgoing_relations"
)
to_entity = relationship("Entity", foreign_keys=[to_id], back_populates="incoming_relations")
def __repr__(self) -> str:
return f"Relation(id={self.id}, from_id={self.from_id}, to_id={self.to_id}, type='{self.relation_type}')"
return f"Relation(id={self.id}, from_id={self.from_id}, to_id={self.to_id}, type='{self.relation_type}')"
@@ -26,7 +26,6 @@ class EntityRepository(Repository[Entity]):
async def list_entities(
self,
entity_type: Optional[str] = None,
doc_id: Optional[int] = None,
sort_by: Optional[str] = "updated_at",
include_related: bool = False,
) -> Sequence[Entity]:
@@ -52,9 +51,6 @@ class EntityRepository(Repository[Entity]):
else:
query = query.where(Entity.entity_type == entity_type)
if doc_id:
query = query.where(Entity.doc_id == doc_id)
# Apply sorting
if sort_by:
sort_field = getattr(Entity, sort_by, Entity.updated_at)
@@ -63,42 +59,6 @@ class EntityRepository(Repository[Entity]):
result = await self.execute_query(query)
return list(result.scalars().all())
async def get_entity_types(self) -> List[str]:
"""Get list of distinct entity types."""
query = select(Entity.entity_type).distinct()
result = await self.execute_query(query, use_query_options=False)
return list(result.scalars().all())
async def search(self, query_str: str) -> List[Entity]:
"""
Search for entities.
Searches across:
- Entity names
- Entity types
- Entity descriptions
- Associated Observations content
"""
search_term = f"%{query_str}%"
query = (
self.select()
.where(
or_(
Entity.name.ilike(search_term),
Entity.entity_type.ilike(search_term),
Entity.description.ilike(search_term),
Entity.observations.any(Observation.content.ilike(search_term)),
)
)
.options(*self.get_load_options())
)
result = await self.execute_query(query)
return list(result.scalars().all())
async def delete_entities_by_doc_id(self, doc_id: int) -> bool:
"""Delete all entities associated with a document."""
return await self.delete_by_fields(doc_id=doc_id)
async def delete_by_file_path(self, file_path: str) -> bool:
"""Delete entity with the provided file_path."""
+10 -11
View File
@@ -34,10 +34,10 @@ Common Relation Types:
import re
from enum import Enum
from typing import List, Optional, Annotated
from typing import List, Optional, Annotated, Dict
from annotated_types import MinLen, MaxLen
from pydantic import BaseModel, BeforeValidator
from pydantic import BaseModel, BeforeValidator, Field
def to_snake_case(name: str) -> str:
@@ -122,17 +122,15 @@ Examples:
- "Depends on SQLAlchemy for database operations"
"""
EntityType = Annotated[str, BeforeValidator(to_snake_case), MinLen(1), MaxLen(200)]
"""Classification of entity (e.g., 'person', 'project', 'concept').
The type serves multiple purposes:
1. Organizes entities in the filesystem
2. Enables filtering and querying
3. Provides context for relations
4. Helps generate meaningful IDs
class EntityType(str, Enum):
"""Type of entity.
Common types are listed in the module docstring.
"""
- knowledge: Contain information used in the semantic graph
- note: Free form information
"""
KNOWLEDGE = "knowledge"
NOTE = "note"
RelationType = Annotated[str, BeforeValidator(to_snake_case), MinLen(1), MaxLen(200)]
"""Type of relationship between entities. Always use active voice present tense.
@@ -252,6 +250,7 @@ class Entity(BaseModel):
name: str
entity_type: EntityType
entity_metadata: Optional[Dict] = Field(default=None, description="Optional metadata")
description: Optional[str] = None
observations: List[Observation] = []
+1
View File
@@ -121,6 +121,7 @@ class EntityResponse(SQLAlchemyModel):
path_id: PathId
name: str
entity_type: EntityType
entity_metadata: Optional[Dict] = None
description: Optional[str] = None
observations: List[ObservationResponse] = []
relations: List[RelationResponse] = []
+1 -5
View File
@@ -15,6 +15,7 @@ def entity_model(entity: EntitySchema):
model = EntityModel(
name=entity.name,
entity_type=entity.entity_type,
entity_metadata=entity.entity_metadata,
path_id=entity.path_id,
file_path=entity.file_path,
description=entity.description,
@@ -29,11 +30,6 @@ class EntityService(BaseService[EntityModel]):
def __init__(self, entity_repository: EntityRepository):
super().__init__(entity_repository)
async def search(self, query: str) -> Sequence[EntityModel]:
"""Search entities using LIKE pattern matching."""
logger.debug(f"Searching entities with query: {query}")
return await self.repository.search(query)
async def create_entity(self, entity: EntitySchema) -> EntityModel:
"""Create a new entity in the database."""
logger.debug(f"Creating entity in DB: {entity}")