Add RecordsAsTextComponent to convert Records to text

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-02-07 22:33:14 -03:00
commit 3a1fa30984

View file

@ -0,0 +1,27 @@
from langflow import CustomComponent
from langflow.field_typing import Text
from langflow.schema import Record
class RecordsAsTextComponent(CustomComponent):
display_name = "Records to Text"
description = "Converts Records a list of Records to text using a template."
def build_config(self):
return {
"records": {"display_name": "Records", "info": "The records to convert to text."},
"template": {
"display_name": "Template",
"info": "The template to use for formatting the records. It must contain the keys {text} and {data}.",
},
}
def build(
self,
records: list[Record],
template: str = "Text: {text}\nData: {data}",
) -> Text:
if isinstance(records, Record):
records = [records]
formated_records = [template.format(text=record.text, data=record.data) for record in records]
return "\n".join(formated_records)