Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

7.7 KB
Raw Blame
"""LLM entity/metadata extraction via instructor + litellm."""

from __future__ import annotations

from collections.abc import Mapping
from typing import cast

import cocoindex as coco
import instructor
import litellm

from .models import (
    ENTITY_TYPES,
    LLM_MODEL,
    PERSON_ENTITY_NAME,
    SessionMetadata,
    SessionTranscript,
    StatementExtraction,
    Utterance,
    is_speaker_label,
)

litellm.drop_params = True

# ---------------------------------------------------------------------------
# Shared transcript formatter
# ---------------------------------------------------------------------------


def format_transcript(
    utterances: list[Utterance], speaker_map: Mapping[str, str | None]
) -> str:
    """Format structured utterances into text with speaker names.

    Used by both extraction steps:
    - Step 1: pass empty dict β†’ all speakers shown as "(Speaker A)", ...
    - Step 2: pass mapping from Step 1 β†’ recognized speakers get real names,
              unrecognized stay as "(Speaker X)"
    """
    lines: list[str] = []
    for u in utterances:
        name = speaker_map.get(u.speaker)
        if name is not None:
            lines.append(f"{name}: {u.text}")
        else:
            lines.append(f"(Speaker {u.speaker}): {u.text}")
    return "\n".join(lines)


# ---------------------------------------------------------------------------
# Step 1: Extract metadata + identify speakers
# ---------------------------------------------------------------------------

METADATA_PROMPT = """\
You are an expert knowledge extractor analyzing a podcast/interview transcript.

Given the transcript (with speaker labels like "(Speaker A)") and YouTube metadata \
(channel name, video title, description, upload date), extract:

1. **name**: A clear, descriptive name for this session/episode. Use the video title \
and channel name as hints but make it informative.
2. **description**: A brief 1-2 sentence description of the session's main topics.
3. **date**: The date of the session in ISO format (YYYY-MM-DD) if mentioned in the \
conversation. Otherwise use the upload date provided.
4. **speakers**: For each speaker label (A, B, ...) in the transcript, identify who \
they are. Use the channel name, video title, description, and conversation content as \
clues. Return their full, canonical, Wikipedia-style name (e.g. "Lex Fridman", \
"Sam Altman", not just "Lex" or "Sam"). Only include speakers you can confidently \
identify with their full name β€” omit any speaker you cannot identify. Do not guess.
"""


# `deps` puts the prompt and the output schema (whose field descriptions the LLM
# also reads) into the function's logic: editing either re-runs Step 1.
# CocoIndex fingerprints the function's source, not module-level values.
@coco.fn(
    memo=True,
    deps={
        "prompt": METADATA_PROMPT,
        "schema": SessionMetadata.model_json_schema(),
    },
)
async def extract_metadata(
    reformatted_transcript: str, transcript: SessionTranscript
) -> SessionMetadata:
    """Give LLM the reformatted transcript + all YouTube metadata to identify speakers."""
    client = cast(
        instructor.AsyncInstructor,
        instructor.from_litellm(litellm.acompletion, mode=instructor.Mode.JSON),
    )
    result = await client.chat.completions.create(
        model=coco.use_context(LLM_MODEL),
        response_model=SessionMetadata,
        messages=[
            {"role": "system", "content": METADATA_PROMPT},
            {
                "role": "user",
                "content": (
                    f"YouTube channel: {transcript.yt_channel}\n"
                    f"Video title: {transcript.yt_title}\n"
                    f"Description: {transcript.yt_description or 'N/A'}\n"
                    f"Upload date: {transcript.yt_upload_date or 'unknown'}\n\n"
                    f"Transcript:\n{reformatted_transcript}"
                ),
            },
        ],
    )
    # Re-validate to restore class identity for pickling.
    metadata = SessionMetadata.model_validate(result.model_dump())
    # Post-filter: drop speakers whose name is not a plausible person name
    # (e.g. LLM might output "null", "all", "unknown", single words, etc.)
    metadata.speakers = [
        s for s in metadata.speakers if _is_plausible_person_name(s.name)
    ]
    return metadata


def _is_plausible_person_name(name: str) -> bool:
    """Check if a name looks like a real person name (at least two words)."""
    return len(name.split()) >= 2


# ---------------------------------------------------------------------------
# Step 2: Extract statements + involved entities
# ---------------------------------------------------------------------------


def _build_statements_prompt() -> str:
    entity_lines = "\n".join(
        f"  - {cfg.name.capitalize()}: {cfg.llm_description} "
        f"Examples: {', '.join(f'{chr(34)}{e}{chr(34)}' for e in cfg.llm_examples)}."
        for cfg in ENTITY_TYPES
    )
    return f"""\
You are an expert knowledge extractor. Given a podcast/interview transcript where \
speakers are identified by name, extract thematic claims and statements.

For each statement:
- Write the statement as a clear, standalone claim. Do NOT include the speaker's name \
or attribution in the statement text (e.g. avoid "X says...", "X believes...") \
β€” the speaker is already captured separately.
- List the speaker(s) who made it (by their full name as shown in the transcript).
- List entities the statement is ABOUT. Do NOT include the speaker(s) in \
mentioned_{PERSON_ENTITY_NAME} unless the statement is specifically about them \
(e.g. their background, credentials, or personal experience). The speaker relationship \
is already captured separately.

IMPORTANT RULES:
- All entity names must be SELF-CONTAINED. Never use pronouns ("he", "she", "they"), \
speaker labels ("Speaker A"), or contextual references ("the host", "the guest", \
"the interviewer"). Every name must be a clear, unambiguous identifier that stands \
on its own.
- Use canonical, Wikipedia-style names for all entities:
{entity_lines}
- Statements from unrecognized speakers (shown as "(Speaker X)") should still be \
extracted with their involved entities, but leave the speakers list empty for them.
- Be thorough but avoid trivial statements. Focus on substantive claims, opinions, \
and factual assertions.
"""


STATEMENTS_PROMPT = _build_statements_prompt()


# STATEMENTS_PROMPT is built from ENTITY_TYPES, so adding or editing an entity
# type changes it and re-runs Step 2, as does an edit to the output schema.
@coco.fn(
    memo=True,
    deps={
        "prompt": STATEMENTS_PROMPT,
        "schema": StatementExtraction.model_json_schema(),
    },
)
async def extract_statements(
    reformatted_transcript: str,
) -> StatementExtraction:
    """Extract statements and involved entities from the reformatted transcript."""
    client = cast(
        instructor.AsyncInstructor,
        instructor.from_litellm(litellm.acompletion, mode=instructor.Mode.JSON),
    )
    result = await client.chat.completions.create(
        model=coco.use_context(LLM_MODEL),
        response_model=StatementExtraction,
        messages=[
            {"role": "system", "content": STATEMENTS_PROMPT},
            {
                "role": "user",
                "content": f"Transcript:\n{reformatted_transcript}",
            },
        ],
    )
    # Re-validate to restore class identity for pickling.
    extraction = StatementExtraction.model_validate(result.model_dump())
    # Post-filter: strip speaker labels that leaked through from the LLM
    for stmt in extraction.statements:
        stmt.speakers = [s for s in stmt.speakers if not is_speaker_label(s)]
        stmt.mentioned_person = [
            p for p in stmt.mentioned_person if not is_speaker_label(p)
        ]
    return extraction