From 3a1fa3098448102ed65eaec80ab2104d11046402 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 7 Feb 2024 22:33:14 -0300 Subject: [PATCH] Add RecordsAsTextComponent to convert Records to text --- .../components/utilities/RecordsAsText.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/backend/langflow/components/utilities/RecordsAsText.py diff --git a/src/backend/langflow/components/utilities/RecordsAsText.py b/src/backend/langflow/components/utilities/RecordsAsText.py new file mode 100644 index 000000000..785a67684 --- /dev/null +++ b/src/backend/langflow/components/utilities/RecordsAsText.py @@ -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)