Incremental engine for long horizon agents π Star if you like it!
Rust agentic-data-frameworkaiai-agentschange-data-capturecodebase-intelligencecontext-engineeringdata-engineeringdata-indexingdata-processingetlhelp-wantedindexingknowledge-graphllmlong-horizon-agentpythonragreal-timerustsemantic-search
"""Data models for Conversation-to-Knowledge pipeline."""
from __future__ import annotations
import re as _re
from dataclasses import dataclass, field
import cocoindex as coco
import pydantic
# ---------------------------------------------------------------------------
# Context keys (shared across modules)
# ---------------------------------------------------------------------------
LLM_MODEL = coco.ContextKey[str]("llm_model", detect_change=True)
RESOLUTION_LLM_MODEL = coco.ContextKey[str]("resolution_llm_model", detect_change=True)
# ---------------------------------------------------------------------------
# Entity type configuration
# ---------------------------------------------------------------------------
PERSON_ENTITY_NAME = "person"
@dataclass
class EntityTypeConfig:
"""Configuration for a named entity type (person, tech, org, ...)."""
name: str
llm_description: str # Injected into the extraction prompt
llm_examples: list[str] = field(default_factory=list) # Canonical name examples
ENTITY_TYPES: list[EntityTypeConfig] = [
EntityTypeConfig(
name=PERSON_ENTITY_NAME,
llm_description=(
"Real people, using Wikipedia-style full names. "
"Only include people you can confidently identify with their full name β "
"omit anyone you cannot identify. Do not guess."
),
llm_examples=["Lex Fridman", "Sam Altman", "Franklin D. Roosevelt"],
),
EntityTypeConfig(
name="tech",
llm_description="Technologies, tools, frameworks, and concepts.",
llm_examples=[
"Python (programming language)",
"Large language model",
"ChatGPT",
],
),
EntityTypeConfig(
name="org",
llm_description="Organizations and companies, using their canonical names.",
llm_examples=["OpenAI", "Google DeepMind", "US Department of Education"],
),
]
# ---------------------------------------------------------------------------
# SurrealDB entity models (dataclasses for declare_record)
# ---------------------------------------------------------------------------
@dataclass
class Session:
id: int # Generated by IdGenerator
youtube_id: str
name: str
transcript: str
description: str | None = None
date: str | None = None
@dataclass
class Entity:
"""Generic named entity node (person, tech, org). Name is used as ID."""
id: str
name: str
@dataclass
class Statement:
id: int # Generated by IdGenerator
statement: str
# ---------------------------------------------------------------------------
# LLM extraction models (Pydantic, for instructor)
# ---------------------------------------------------------------------------
class SpeakerIdentification(pydantic.BaseModel):
"""Maps a diarization label to a real person name."""
label: str = pydantic.Field(
description='The diarization label from the transcript, e.g. "A", "B".'
)
name: str = pydantic.Field(
description=(
"Full canonical, Wikipedia-style name of the speaker "
'(e.g. "Lex Fridman", "Sam Altman"). '
"Only include speakers you can confidently identify β do not guess."
)
)
class SessionMetadata(pydantic.BaseModel):
"""LLM output from Step 1: session metadata + speaker mapping."""
name: str = pydantic.Field(
description="A clear, descriptive name for this session or episode."
)
description: str | None = pydantic.Field(
default=None,
description="A brief 1-2 sentence summary of the session's main topics.",
)
date: str | None = pydantic.Field(
default=None,
description=(
"Date of the session in ISO format (YYYY-MM-DD). "
"Use the date mentioned in the conversation if available; "
"otherwise fall back to the upload date."
),
)
speakers: list[SpeakerIdentification] = pydantic.Field(
description=(
"Identified speakers: maps each diarization label to the speaker's "
"real full name. Omit any speaker you cannot confidently identify."
)
)
class RawStatement(pydantic.BaseModel):
"""A thematic claim or statement made during the session."""
statement: str = pydantic.Field(
description=(
"The claim or assertion, written as a clear, standalone sentence. "
"Do NOT include the speaker's name or attribution "
'(avoid "X says...", "X believes...", "According to X...") β '
"the speaker is captured separately."
)
)
speakers: list[str] = pydantic.Field(
description=(
"Full canonical names of the speakers who made this statement, "
"exactly as they appear in the transcript."
)
)
mentioned_person: list[str] = pydantic.Field(
default=[],
description=(
"Full canonical names of people this statement is ABOUT. "
"Do NOT include the speakers themselves unless the statement is "
"specifically about them (e.g. their background or credentials)."
),
)
mentioned_tech: list[str] = pydantic.Field(
default=[],
description="Technologies, tools, frameworks, or technical concepts this statement is about.",
)
mentioned_org: list[str] = pydantic.Field(
default=[],
description="Organizations or companies this statement is about, using their canonical names.",
)
class StatementExtraction(pydantic.BaseModel):
"""LLM output from Step 2: statements with involved entities."""
statements: list[RawStatement]
# ---------------------------------------------------------------------------
# Internal transfer types
# ---------------------------------------------------------------------------
@dataclass
class Utterance:
"""A single speaker-labeled utterance from diarized transcription."""
speaker: str # Diarization label, e.g. "A", "B"
text: str
@dataclass
class SessionTranscript:
"""Carries structured utterances and yt-dlp metadata."""
utterances: list[Utterance]
yt_channel: str
yt_title: str
yt_description: str | None
yt_upload_date: str | None
@dataclass
class IdentifiedStatement:
"""A statement with its generated ID, for carrying through the pipeline."""
id: int
raw: RawStatement
@dataclass
class SessionRawEntities:
"""Raw entities from a single session, for entity resolution."""
session_id: int
# Maps entity type name β session-level participants (e.g. "person" β identified speakers).
# Statement-level entities are accessed directly via IdentifiedStatement.raw.
raw_entities: dict[str, list[str]]
statements: list[IdentifiedStatement]
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def resolve_canonical(name: str, dedup: dict[str, str | None]) -> str:
"""Chase dedup chains to find the canonical name."""
while dedup.get(name) is not None:
name = dedup[name] # type: ignore[assignment]
return name
_SPEAKER_LABEL_RE = _re.compile(r"^\(?speaker\s+[a-z]\)?$", _re.IGNORECASE)
def is_speaker_label(name: str) -> bool:
"""Check if a name is a speaker diarization label (e.g. 'Speaker A', '(Speaker B)')."""
return _SPEAKER_LABEL_RE.match(name.strip()) is not None