Spaces:
Runtime error
Runtime error
from typing import Literal | |
from llama_index.schema import NodeWithScore | |
from pydantic import BaseModel, Field | |
from app.server.ingest.schemas import IngestedDoc | |
class Chunk(BaseModel): | |
object: Literal["context.chunk"] | |
score: float = Field(examples=[0.023]) | |
document: IngestedDoc | |
text: str = Field(examples=["Outbound sales increased 20%, driven by new leads."]) | |
previous_texts: list[str] | None = Field( | |
default=None, | |
examples=[["SALES REPORT 2023", "Inbound didn't show major changes."]], | |
) | |
next_texts: list[str] | None = Field( | |
default=None, | |
examples=[ | |
[ | |
"New leads came from Google Ads campaign.", | |
"The campaign was run by the Marketing Department", | |
] | |
], | |
) | |
def from_node(cls: type["Chunk"], node: NodeWithScore) -> "Chunk": | |
doc_id = node.node.ref_doc_id if node.node.ref_doc_id is not None else "-" | |
return cls( | |
object="context.chunk", | |
score=node.score or 0.0, | |
document=IngestedDoc( | |
object="ingest.document", | |
doc_id=doc_id, | |
doc_metadata=node.metadata, | |
), | |
text=node.get_content(), | |
) | |
class Completion(BaseModel): | |
response: str | |
sources: list[Chunk] | None = None | |