Add helper functions for converting documents to records and records to text
This commit is contained in:
parent
05e6036be2
commit
c9a282bdea
4 changed files with 43 additions and 11 deletions
3
src/backend/langflow/helpers/__init__.py
Normal file
3
src/backend/langflow/helpers/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from .record import docs_to_records, records_to_text
|
||||
|
||||
__all__ = ["docs_to_records", "records_to_text"]
|
||||
37
src/backend/langflow/helpers/record.py
Normal file
37
src/backend/langflow/helpers/record.py
Normal 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)
|
||||
3
src/backend/langflow/schema/__init__.py
Normal file
3
src/backend/langflow/schema/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from .schema import Record
|
||||
|
||||
__all__ = ["Record"]
|
||||
|
|
@ -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]
|
||||
Loading…
Add table
Add a link
Reference in a new issue