Skip to content

Commit

Permalink
fix: add prompt_message transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Nov 29, 2024
1 parent 9f6754d commit 4afefe2
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions python/dify_plugin/entities/model/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ def is_empty(self) -> bool:
"""
return not self.content

@field_validator("content", mode="before")
def transform_content(
self, value: list[dict] | str | None
) -> Optional[str | list[PromptMessageContent]]:
"""
Transform content to list of prompt message content.
"""
if isinstance(value, str):
return value
else:
result = []
for content in value or []:
if content.get("type") == PromptMessageContentType.TEXT.value:
result.append(TextPromptMessageContent(**content))
elif content.get("type") == PromptMessageContentType.IMAGE.value:
result.append(ImagePromptMessageContent(**content))
elif content.get("type") == PromptMessageContentType.DOCUMENT.value:
result.append(DocumentPromptMessageContent(**content))
return result

class UserPromptMessage(PromptMessage):
"""
Expand Down

0 comments on commit 4afefe2

Please sign in to comment.