From ba343be9923132ed76773df23a29ad09388ca0fe Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 21 Jun 2024 15:10:05 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20(message.py):=20fix=20issue=20wh?= =?UTF-8?q?ere=20message=20content=20was=20not=20being=20properly=20format?= =?UTF-8?q?ted=20and=20displayed=20in=20the=20HumanMessage=20object?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 📝 (message.py): improve the logic for handling message content and formatting to ensure correct display in the HumanMessage object --- src/backend/base/langflow/schema/message.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/backend/base/langflow/schema/message.py b/src/backend/base/langflow/schema/message.py index 9114965b2..86d2ac205 100644 --- a/src/backend/base/langflow/schema/message.py +++ b/src/backend/base/langflow/schema/message.py @@ -80,7 +80,7 @@ class Message(Data): human_message = HumanMessage(content=contents) # type: ignore else: human_message = HumanMessage( - content=[{"type": "text", "text": self.text}], + content=self.text, ) return human_message @@ -167,13 +167,18 @@ class Message(Data): @classmethod async def from_template_and_variables(cls, template: str, **variables): instance = cls(template=template, variables=variables) - contents = [{"type": "text", "text": instance.format_text()}] + text = instance.format_text() # Get all Message instances from the kwargs + message = HumanMessage(content=text) + contents = [] for value in variables.values(): - if isinstance(value, cls): + if isinstance(value, cls) and value.files: content_dicts = await value.get_file_content_dicts() contents.extend(content_dicts) - prompt_template = ChatPromptTemplate.from_messages([HumanMessage(content=contents)]) # type: ignore + if contents: + message = HumanMessage(content=[{"type": "text", "text": text}] + contents) + + prompt_template = ChatPromptTemplate.from_messages([message]) # type: ignore instance.prompt = jsonable_encoder(prompt_template.to_json()) instance.messages = instance.prompt.get("kwargs", {}).get("messages", []) return instance