From a9cb30c37805c42ab838282fe5bca9c924dcdfe4 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 8 Feb 2024 19:26:04 -0300 Subject: [PATCH] Add JSON formatting for dict objects in ChatVertex --- src/backend/langflow/graph/vertex/types.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/backend/langflow/graph/vertex/types.py b/src/backend/langflow/graph/vertex/types.py index 3be211746..65da2bff9 100644 --- a/src/backend/langflow/graph/vertex/types.py +++ b/src/backend/langflow/graph/vertex/types.py @@ -1,8 +1,8 @@ import ast +import json from typing import Callable, Dict, List, Optional, Union from langchain_core.messages import AIMessage - from langflow.graph.utils import UnbuiltObject, flatten_list from langflow.graph.vertex.base import StatefulVertex, StatelessVertex from langflow.interface.utils import extract_input_variables_from_prompt @@ -333,8 +333,13 @@ class ChatVertex(StatelessVertex): sender_name=sender_name, ) elif not isinstance(self._built_object, UnbuiltObject): - if not isinstance(self._built_object, str): + if isinstance(self._built_object, dict): + # Turn the dict into a pleasing to + # read JSON inside a code block + self._built_object = dict_to_codeblock(self._built_object) + elif not isinstance(self._built_object, str): self._built_object = str(self._built_object) + artifacts = ChatOutputResponse( message=self._built_object, sender=sender, @@ -346,3 +351,8 @@ class ChatVertex(StatelessVertex): else: await super()._run(*args, **kwargs) + + +def dict_to_codeblock(d: dict) -> str: + json_str = json.dumps(d, indent=4) + return f"```json\n{json_str}\n```"