From 1b228a422c997c84ec93cf2946e73fa4c3103f6c Mon Sep 17 00:00:00 2001 From: Silvano Cerza <3314350+silvanocerza@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:23:30 +0100 Subject: [PATCH] Disable ensure_ascii when dumping to json tool responses (#134) --- haystack_experimental/components/generators/chat/openai.py | 3 ++- .../components/tools/openai/function_caller.py | 3 ++- haystack_experimental/components/tools/openapi/openapi_tool.py | 3 ++- haystack_experimental/components/tools/tool_invoker.py | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/haystack_experimental/components/generators/chat/openai.py b/haystack_experimental/components/generators/chat/openai.py index f7a028f3..7517d508 100644 --- a/haystack_experimental/components/generators/chat/openai.py +++ b/haystack_experimental/components/generators/chat/openai.py @@ -55,7 +55,8 @@ def _convert_message_to_openai_format(message: ChatMessage) -> Dict[str, Any]: { "id": tc.id, "type": "function", - "function": {"name": tc.tool_name, "arguments": json.dumps(tc.arguments)}, + # We disable ensure_ascii so special chars like emojis are not converted + "function": {"name": tc.tool_name, "arguments": json.dumps(tc.arguments, ensure_ascii=False)}, } ) openai_msg["tool_calls"] = openai_tool_calls diff --git a/haystack_experimental/components/tools/openai/function_caller.py b/haystack_experimental/components/tools/openai/function_caller.py index 0c12b963..c4e1a573 100644 --- a/haystack_experimental/components/tools/openai/function_caller.py +++ b/haystack_experimental/components/tools/openai/function_caller.py @@ -87,8 +87,9 @@ def run(self, messages: List[ChatMessage]): try: function_response = function_to_call(**function_args) messages.append( + # We disable ensure_ascii so special chars like emojis are not converted ChatMessage.from_function( - content=json.dumps(function_response), + content=json.dumps(function_response, ensure_ascii=False), name=function_name, ) ) diff --git a/haystack_experimental/components/tools/openapi/openapi_tool.py b/haystack_experimental/components/tools/openapi/openapi_tool.py index 7bcb80d5..9c9b33a2 100644 --- a/haystack_experimental/components/tools/openapi/openapi_tool.py +++ b/haystack_experimental/components/tools/openapi/openapi_tool.py @@ -172,7 +172,8 @@ def run( except Exception as e: # pylint: disable=broad-exception-caught logger.error("Error invoking OpenAPI endpoint. Error: {e}", e=str(e)) service_response = {"error": str(e)} - response_messages = [ChatMessage.from_user(json.dumps(service_response))] + # We disable ensure_ascii so special chars like emojis are not converted + response_messages = [ChatMessage.from_user(json.dumps(service_response, ensure_ascii=False))] return {"service_response": response_messages} diff --git a/haystack_experimental/components/tools/tool_invoker.py b/haystack_experimental/components/tools/tool_invoker.py index 08156605..14e7ffe3 100644 --- a/haystack_experimental/components/tools/tool_invoker.py +++ b/haystack_experimental/components/tools/tool_invoker.py @@ -145,7 +145,8 @@ def _prepare_tool_result_message(self, result: Any, tool_call: ToolCall) -> Chat if self.convert_result_to_json_string: try: - tool_result_str = json.dumps(result) + # We disable ensure_ascii so special chars like emojis are not converted + tool_result_str = json.dumps(result, ensure_ascii=False) except Exception as e: if self.raise_on_failure: raise StringConversionError("Failed to convert tool result to string using `json.dumps`") from e