🐛 (message.py): fix issue where message content was not being properly formatted and displayed in the HumanMessage object

📝 (message.py): improve the logic for handling message content and formatting to ensure correct display in the HumanMessage object
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-06-21 15:10:05 -03:00
commit ba343be992

View file

@ -80,7 +80,7 @@ class Message(Data):
human_message = HumanMessage(content=contents) # type: ignore human_message = HumanMessage(content=contents) # type: ignore
else: else:
human_message = HumanMessage( human_message = HumanMessage(
content=[{"type": "text", "text": self.text}], content=self.text,
) )
return human_message return human_message
@ -167,13 +167,18 @@ class Message(Data):
@classmethod @classmethod
async def from_template_and_variables(cls, template: str, **variables): async def from_template_and_variables(cls, template: str, **variables):
instance = cls(template=template, variables=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 # Get all Message instances from the kwargs
message = HumanMessage(content=text)
contents = []
for value in variables.values(): for value in variables.values():
if isinstance(value, cls): if isinstance(value, cls) and value.files:
content_dicts = await value.get_file_content_dicts() content_dicts = await value.get_file_content_dicts()
contents.extend(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.prompt = jsonable_encoder(prompt_template.to_json())
instance.messages = instance.prompt.get("kwargs", {}).get("messages", []) instance.messages = instance.prompt.get("kwargs", {}).get("messages", [])
return instance return instance