Add Record class and conversion functions
This commit is contained in:
parent
7a4b2dfc7e
commit
603ffd1f1b
1 changed files with 52 additions and 0 deletions
52
src/backend/langflow/schema.py
Normal file
52
src/backend/langflow/schema.py
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
from typing import Optional
|
||||
|
||||
from langchain_core.documents import Document
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Record(BaseModel):
|
||||
"""
|
||||
Represents a record with text and optional data.
|
||||
|
||||
Attributes:
|
||||
text (str): The text of the record.
|
||||
data (dict, optional): Additional data associated with the record.
|
||||
"""
|
||||
|
||||
text: str
|
||||
data: Optional[dict] = None
|
||||
|
||||
@classmethod
|
||||
def from_document(cls, document: Document) -> "Record":
|
||||
"""
|
||||
Converts a Document to a Record.
|
||||
|
||||
Args:
|
||||
document (Document): The Document to convert.
|
||||
|
||||
Returns:
|
||||
Record: The converted Record.
|
||||
"""
|
||||
return cls(text=document.page_content, data=document.metadata)
|
||||
|
||||
def to_lc_document(self) -> Document:
|
||||
"""
|
||||
Converts the Record to a Document.
|
||||
|
||||
Returns:
|
||||
Document: The converted Document.
|
||||
"""
|
||||
return Document(page_content=self.text, metadata=self.data)
|
||||
|
||||
|
||||
def docs_to_records(documents: list[Document]) -> list[Record]:
|
||||
"""
|
||||
Converts a list of Documents to a list of Records.
|
||||
|
||||
Args:
|
||||
documents (list[Document]): The list of Documents to convert.
|
||||
|
||||
Returns:
|
||||
list[Record]: The converted list of Records.
|
||||
"""
|
||||
return [Record.from_document(document) for document in documents]
|
||||
Loading…
Add table
Add a link
Reference in a new issue