Add record_template parameter to ChatComponent

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-03-29 16:16:32 -03:00
commit d71ebc7ea1

View file

@ -2,6 +2,7 @@ import warnings
from typing import Optional, Union
from langflow.field_typing import Text
from langflow.helpers.record import records_to_text
from langflow.interface.custom.custom_component import CustomComponent
from langflow.memory import add_messages
from langflow.schema import Record
@ -34,6 +35,12 @@ class ChatComponent(CustomComponent):
"info": "Return the message as a record containing the sender, sender_name, and session_id.",
"advanced": True,
},
"record_template": {
"display_name": "Record Template",
"multiline": True,
"info": "In case of Message being a Record, this template will be used to convert it to text.",
"advanced": True,
},
}
def store_message(
@ -76,9 +83,10 @@ class ChatComponent(CustomComponent):
self,
sender: Optional[str] = "User",
sender_name: Optional[str] = "User",
input_value: Optional[str] = None,
input_value: Optional[Union[str, Record]] = None,
session_id: Optional[str] = None,
return_record: Optional[bool] = False,
record_template: Optional[str] = "Text: {text}\nData: {data}",
) -> Union[Text, Record]:
input_value_record: Optional[Record] = None
if return_record:
@ -96,6 +104,8 @@ class ChatComponent(CustomComponent):
"session_id": session_id,
},
)
elif isinstance(input_value, Record):
input_value = records_to_text(template=record_template, records=input_value)
if not input_value:
input_value = ""
if return_record and input_value_record: