|
2 | 2 | from typing import Any, Dict, List |
3 | 3 |
|
4 | 4 | from neo4j import AsyncDriver, RoutingControl |
5 | | -from pydantic import BaseModel |
| 5 | +from pydantic import BaseModel, Field |
6 | 6 |
|
7 | 7 |
|
8 | 8 | # Set up logging |
|
11 | 11 |
|
12 | 12 | # Models for our knowledge graph |
13 | 13 | class Entity(BaseModel): |
14 | | - name: str |
15 | | - type: str |
16 | | - observations: List[str] |
| 14 | + """Represents a memory entity in the knowledge graph. |
| 15 | + |
| 16 | + Example: |
| 17 | + { |
| 18 | + "name": "John Smith", |
| 19 | + "type": "person", |
| 20 | + "observations": ["Works at Neo4j", "Lives in San Francisco", "Expert in graph databases"] |
| 21 | + } |
| 22 | + """ |
| 23 | + name: str = Field( |
| 24 | + description="Unique identifier/name for the entity. Should be descriptive and specific.", |
| 25 | + min_length=1, |
| 26 | + examples=["John Smith", "Neo4j Inc", "San Francisco"] |
| 27 | + ) |
| 28 | + type: str = Field( |
| 29 | + description="Category or classification of the entity. Common types: 'person', 'company', 'location', 'concept', 'event'", |
| 30 | + min_length=1, |
| 31 | + examples=["person", "company", "location", "concept", "event"] |
| 32 | + ) |
| 33 | + observations: List[str] = Field( |
| 34 | + description="List of facts, observations, or notes about this entity. Each observation should be a complete, standalone fact.", |
| 35 | + examples=[["Works at Neo4j", "Lives in San Francisco"], ["Headquartered in Sweden", "Graph database company"]] |
| 36 | + ) |
17 | 37 |
|
18 | 38 | class Relation(BaseModel): |
19 | | - source: str |
20 | | - target: str |
21 | | - relationType: str |
| 39 | + """Represents a relationship between two entities in the knowledge graph. |
| 40 | + |
| 41 | + Example: |
| 42 | + { |
| 43 | + "source": "John Smith", |
| 44 | + "target": "Neo4j Inc", |
| 45 | + "relationType": "WORKS_AT" |
| 46 | + } |
| 47 | + """ |
| 48 | + source: str = Field( |
| 49 | + description="Name of the source entity (must match an existing entity name exactly)", |
| 50 | + min_length=1, |
| 51 | + examples=["John Smith", "Neo4j Inc"] |
| 52 | + ) |
| 53 | + target: str = Field( |
| 54 | + description="Name of the target entity (must match an existing entity name exactly)", |
| 55 | + min_length=1, |
| 56 | + examples=["Neo4j Inc", "San Francisco"] |
| 57 | + ) |
| 58 | + relationType: str = Field( |
| 59 | + description="Type of relationship between source and target. Use descriptive, uppercase names with underscores.", |
| 60 | + min_length=1, |
| 61 | + examples=["WORKS_AT", "LIVES_IN", "MANAGES", "COLLABORATES_WITH", "LOCATED_IN"] |
| 62 | + ) |
22 | 63 |
|
23 | 64 | class KnowledgeGraph(BaseModel): |
24 | | - entities: List[Entity] |
25 | | - relations: List[Relation] |
| 65 | + """Complete knowledge graph containing entities and their relationships.""" |
| 66 | + entities: List[Entity] = Field( |
| 67 | + description="List of all entities in the knowledge graph", |
| 68 | + default=[] |
| 69 | + ) |
| 70 | + relations: List[Relation] = Field( |
| 71 | + description="List of all relationships between entities", |
| 72 | + default=[] |
| 73 | + ) |
26 | 74 |
|
27 | 75 | class ObservationAddition(BaseModel): |
28 | | - entityName: str |
29 | | - observations: List[str] |
| 76 | + """Request to add new observations to an existing entity. |
| 77 | + |
| 78 | + Example: |
| 79 | + { |
| 80 | + "entityName": "John Smith", |
| 81 | + "observations": ["Recently promoted to Senior Engineer", "Speaks fluent German"] |
| 82 | + } |
| 83 | + """ |
| 84 | + entityName: str = Field( |
| 85 | + description="Exact name of the existing entity to add observations to", |
| 86 | + min_length=1, |
| 87 | + examples=["John Smith", "Neo4j Inc"] |
| 88 | + ) |
| 89 | + observations: List[str] = Field( |
| 90 | + description="New observations/facts to add to the entity. Each should be unique and informative.", |
| 91 | + min_length=1 |
| 92 | + ) |
30 | 93 |
|
31 | 94 | class ObservationDeletion(BaseModel): |
32 | | - entityName: str |
33 | | - observations: List[str] |
| 95 | + """Request to delete specific observations from an existing entity. |
| 96 | + |
| 97 | + Example: |
| 98 | + { |
| 99 | + "entityName": "John Smith", |
| 100 | + "observations": ["Old job title", "Outdated contact info"] |
| 101 | + } |
| 102 | + """ |
| 103 | + entityName: str = Field( |
| 104 | + description="Exact name of the existing entity to remove observations from", |
| 105 | + min_length=1, |
| 106 | + examples=["John Smith", "Neo4j Inc"] |
| 107 | + ) |
| 108 | + observations: List[str] = Field( |
| 109 | + description="Exact observation texts to delete from the entity (must match existing observations exactly)", |
| 110 | + min_length=1 |
| 111 | + ) |
34 | 112 |
|
35 | 113 | class Neo4jMemory: |
36 | 114 | def __init__(self, neo4j_driver: AsyncDriver): |
|
0 commit comments