Add RecordsAsTextComponent to convert Records to text
This commit is contained in:
parent
603ffd1f1b
commit
3a1fa30984
1 changed files with 27 additions and 0 deletions
27
src/backend/langflow/components/utilities/RecordsAsText.py
Normal file
27
src/backend/langflow/components/utilities/RecordsAsText.py
Normal 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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue