Add helper functions for converting documents to records and records to text

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-03-01 09:17:18 -03:00
commit c9a282bdea
4 changed files with 43 additions and 11 deletions

View file

@ -0,0 +1,3 @@
from .record import docs_to_records, records_to_text
__all__ = ["docs_to_records", "records_to_text"]

View file

@ -0,0 +1,37 @@
from langchain_core.documents import Document
from langflow.schema import Record
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]
def records_to_text(template: str, records: list[Record]) -> list[str]:
"""
Converts a list of Records to a list of texts.
Args:
records (list[Record]): The list of Records to convert.
Returns:
list[str]: The converted list of texts.
"""
if isinstance(records, Record):
records = [records]
# Check if there are any format strings in the template
formated_records = [
template.format(text=record.text, data=record.data, **record.data)
for record in records
]
return "\n".join(formated_records)

View file

@ -0,0 +1,3 @@
from .schema import Record
__all__ = ["Record"]

View file

@ -57,14 +57,3 @@ class Record(BaseModel):
return self.text
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]