Browse Source

fix tool_inputs parse error in message that in CoT(ReAct) agent mode (#2949)

listeng 1 year ago
parent
commit
d70bd4aaa4

+ 6 - 2
api/core/features/assistant_base_runner.py

@@ -566,7 +566,11 @@ class BaseAssistantApplicationRunner(AppRunner):
                         tools = tools.split(';')
                         tool_calls: list[AssistantPromptMessage.ToolCall] = []
                         tool_call_response: list[ToolPromptMessage] = []
-                        tool_inputs = json.loads(agent_thought.tool_input)
+                        try:
+                            tool_inputs = json.loads(agent_thought.tool_input)
+                        except Exception as e:
+                            logging.warning("tool execution error: {}, tool_input: {}.".format(str(e), agent_thought.tool_input))
+                            tool_inputs = { agent_thought.tool: agent_thought.tool_input }
                         for tool in tools:
                             # generate a uuid for tool call
                             tool_call_id = str(uuid.uuid4())
@@ -599,4 +603,4 @@ class BaseAssistantApplicationRunner(AppRunner):
 
         db.session.close()
 
-        return result
+        return result

+ 2 - 2
api/core/features/assistant_cot_runner.py

@@ -182,7 +182,7 @@ class AssistantCotApplicationRunner(BaseAssistantApplicationRunner):
                             delta=LLMResultChunkDelta(
                                 index=0,
                                 message=AssistantPromptMessage(
-                                    content=json.dumps(chunk)
+                                    content=json.dumps(chunk, ensure_ascii=False) # if ensure_ascii=True, the text in webui maybe garbled text
                                 ),
                                 usage=None
                             )
@@ -667,4 +667,4 @@ class AssistantCotApplicationRunner(BaseAssistantApplicationRunner):
         try:
             return json.dumps(tools, ensure_ascii=False)
         except json.JSONDecodeError:
-            return json.dumps(tools)
+            return json.dumps(tools)