types.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from __future__ import annotations
  2. from pydantic import BaseModel, Field
  3. from typing import Any, Optional
  4. class ExtractedEntity(BaseModel):
  5. """A single entity extracted from text by the LLM."""
  6. entity_name: str = Field(
  7. description="Name of the entity. Use title case for case-insensitive names."
  8. )
  9. entity_type: str = Field(description="Type/category of the entity.")
  10. entity_description: str = Field(
  11. description="Concise yet comprehensive description of the entity based on the input text."
  12. )
  13. class ExtractedRelationship(BaseModel):
  14. """A single relationship between two entities extracted from text."""
  15. source_entity: str = Field(
  16. description="Name of the source entity in the relationship."
  17. )
  18. target_entity: str = Field(
  19. description="Name of the target entity in the relationship."
  20. )
  21. relationship_keywords: str = Field(
  22. description="Comma-separated high-level keywords summarizing the relationship."
  23. )
  24. relationship_description: str = Field(
  25. description="Concise explanation of the relationship between source and target entities."
  26. )
  27. class EntityExtractionResult(BaseModel):
  28. """Structured output format for entity and relationship extraction from text."""
  29. entities: list[ExtractedEntity] = Field(
  30. default_factory=list,
  31. description="List of entities extracted from the input text.",
  32. )
  33. relationships: list[ExtractedRelationship] = Field(
  34. default_factory=list,
  35. description="List of relationships between entities extracted from the input text.",
  36. )
  37. class KnowledgeGraphNode(BaseModel):
  38. id: str
  39. labels: list[str]
  40. properties: dict[str, Any] # anything else goes here
  41. class KnowledgeGraphEdge(BaseModel):
  42. id: str
  43. type: Optional[str]
  44. source: str # id of source node
  45. target: str # id of target node
  46. properties: dict[str, Any] # anything else goes here
  47. class KnowledgeGraph(BaseModel):
  48. nodes: list[KnowledgeGraphNode] = []
  49. edges: list[KnowledgeGraphEdge] = []
  50. is_truncated: bool = False