Browse Source

增加解析

xueyiming 1 week ago
parent
commit
290b1d4955
1 changed files with 29 additions and 1 deletions
  1. 29 1
      pqai_agent/agent_service.py

+ 29 - 1
pqai_agent/agent_service.py

@@ -472,11 +472,39 @@ class AgentService:
         chat_response = self.sanitize_response(chat_response)
 
         if response := agent.generate_response(chat_response):
-            return response
+            return self.extract_content(response)
         else:
             logger.warning(f"staff[{agent.staff_id}] user[{agent.user_id}]: no response generated")
             return None
 
+    def extract_content(self, response_str : str):
+        # 使用正则表达式提取函数调用部分
+        match = re.search(r'<\|FunctionCallBegin\|>(.*?)<\|FunctionCallEnd\|>', response_str)
+        if not match:
+            return response_str
+
+        # 尝试解析 JSON
+        try:
+            function_call = json.loads(match.group(1))
+
+            # 检查是否为列表格式 (示例中是列表)
+            if isinstance(function_call, list) and function_call:
+                first_call = function_call[0]
+                if first_call.get('name') == 'output_multimodal_message':
+                    parameters = first_call.get('parameters', {})
+                    return parameters.get('content')
+
+            # 或者如果是字典格式
+            elif isinstance(function_call, dict):
+                if function_call.get('name') == 'output_multimodal_message':
+                    parameters = function_call.get('parameters', {})
+                    return parameters.get('content')
+
+        except json.JSONDecodeError:
+            pass
+
+        return response_str
+
     def _get_chat_response_v2(self, main_agent: DialogueManager) -> List[Dict]:
         agent_config = get_agent_abtest_config('chat', main_agent.user_id,
                                                self.service_module_manager, self.agent_config_manager)