Skip to content

lpgcast.ground

The model copies a quote; LPGCast finds it in the episode and records the character offsets. Offsets are never taken from the model. A quote that cannot be found is a hallucination flag, handled by validation.

Classes

Grounding

Bases: BaseModel

One assertion and the span of text it was read from.

Carried in the cast result only; nothing here is written to the graph.

Source code in lpgcast/ground.py
class Grounding(BaseModel):
    """One assertion and the span of text it was read from.

    Carried in the cast result only; nothing here is written to the graph.
    """

    source_id: str = Field(description="Episode the assertion was read from.")
    kind: Literal["vertex", "edge", "property"]
    vertex: str | None = Field(
        default=None, description="Vertex type, for a vertex or its property."
    )
    identity: dict[str, Any] | None = Field(
        default=None, description="Identity of the vertex, as written or looked up by."
    )
    edge: EdgeId | None = Field(
        default=None, description="Edge, for an edge or its property."
    )
    source_identity: dict[str, Any] | None = None
    target_identity: dict[str, Any] | None = None
    property: str | None = Field(
        default=None, description="Property name, for a property."
    )
    char_start: int
    char_end: int
    in_context: str | None = Field(
        default=None,
        description="Source id of the context item the span points into; None "
        "for the episode's own text. Only identity values ground in context.",
    )
    quote: str = Field(description="The text at the span, as it stands in the source.")
    extractor: str = Field(description="``lpgcast/<version>``.")
    model: str | None = Field(
        default=None, description="Model that produced the reply."
    )
    schema_ref: str = Field(description="``<schema name>@<version>``.")
    reference_time: datetime | None = None

Attributes

char_end instance-attribute
char_start instance-attribute
edge = Field(default=None, description='Edge, for an edge or its property.') class-attribute instance-attribute
extractor = Field(description='``lpgcast/<version>``.') class-attribute instance-attribute
identity = Field(default=None, description='Identity of the vertex, as written or looked up by.') class-attribute instance-attribute
in_context = Field(default=None, description="Source id of the context item the span points into; None for the episode's own text. Only identity values ground in context.") class-attribute instance-attribute
kind instance-attribute
model = Field(default=None, description='Model that produced the reply.') class-attribute instance-attribute
property = Field(default=None, description='Property name, for a property.') class-attribute instance-attribute
quote = Field(description='The text at the span, as it stands in the source.') class-attribute instance-attribute
reference_time = None class-attribute instance-attribute
schema_ref = Field(description='``<schema name>@<version>``.') class-attribute instance-attribute
source_id = Field(description='Episode the assertion was read from.') class-attribute instance-attribute
source_identity = None class-attribute instance-attribute
target_identity = None class-attribute instance-attribute
vertex = Field(default=None, description='Vertex type, for a vertex or its property.') class-attribute instance-attribute

Span

Bases: BaseModel

Character offsets, end exclusive, into the episode's text or, when context is set, into the text of that context item.

Source code in lpgcast/ground.py
class Span(BaseModel):
    """Character offsets, end exclusive, into the episode's text or, when
    ``context`` is set, into the text of that context item."""

    start: int
    end: int
    context: str | None = Field(
        default=None, description="Source id of the context item, if not the text."
    )

Attributes

context = Field(default=None, description='Source id of the context item, if not the text.') class-attribute instance-attribute
end instance-attribute
start instance-attribute

Functions:

locate(quote, text, *, near=None)

Find quote in text: exactly, then ignoring case, accents, whitespace runs and typographic quotes and dashes, then also without surrounding punctuation. Of several occurrences, the one nearest near wins, else the first.

Source code in lpgcast/ground.py
def locate(quote: str, text: str, *, near: Span | None = None) -> Span | None:
    """Find *quote* in *text*: exactly, then ignoring case, accents, whitespace
    runs and typographic quotes and dashes, then also without surrounding
    punctuation. Of several occurrences, the one nearest *near* wins, else the
    first."""
    if not quote.strip():
        return None
    spans = _occurrences(quote, text)
    if not spans:
        return None
    if near is None:
        return spans[0]
    return min(spans, key=lambda span: (_gap(span, near), span.start))