Explorar o código

Optimize response detector prompt and update unit test

StrayWarrior hai 1 día
pai
achega
8c807262f9
Modificáronse 2 ficheiros con 31 adicións e 1 borrados
  1. 1 0
      pqai_agent/prompt_templates.py
  2. 30 1
      tests/unit_test.py

+ 1 - 0
pqai_agent/prompt_templates.py

@@ -279,6 +279,7 @@ RESPONSE_TYPE_DETECT_PROMPT = """
 * 默认使用文本
 * 如果用户明确提到使用语音形式,尽量选择语音
 * 用户自身偏向于使用语音形式沟通时,可选择语音
+* 如果用户不认字或有阅读障碍,且内容适合语音朗读,可选择语音
 * 注意分析即将发送的消息内容,如果有不适合使用语音朗读的内容,不要选择使用语音
 * 注意对话中包含的时间!注意时间流逝和情境切换!判断合适的回复方式!
 

+ 30 - 1
tests/unit_test.py

@@ -6,6 +6,7 @@ import pytest
 from unittest.mock import Mock, MagicMock
 
 import pqai_agent.abtest.client
+import pqai_agent.configs
 from pqai_agent.agent_service import AgentService
 from pqai_agent.dialogue_manager import DialogueState, TimeContext
 from pqai_agent.message_queue_backend import MemoryQueueBackend
@@ -56,7 +57,7 @@ def test_env():
     yield service, queues
 
     service.shutdown(sync=True)
-    pqai_agent.abtest.client.get_client().shutdown(blocking=True)
+    pqai_agent.abtest.client.get_client().shutdown()
 
 def test_agent_state_change(test_env):
     service, _ = test_env
@@ -235,3 +236,31 @@ def test_response_type_detector(test_env):
     assert ResponseTypeDetector.is_chinese_only(case3) == False
     case4 = '大哥,那可得提前了解下天气'
     assert ResponseTypeDetector.if_message_suitable_for_voice(case4) == True
+
+    global_config = pqai_agent.configs.get()
+    global_config.get('debug_flags', {}).update({'disable_llm_api_call': False})
+
+    response_detector = ResponseTypeDetector()
+    dialogue1 = [
+        {'role': 'user', 'content': '你好', 'timestamp': 1744979571000, 'type': MessageType.TEXT},
+        {'role': 'assistant', 'content': '你好呀', 'timestamp': 1744979581000},
+    ]
+    assert response_detector.detect_type(dialogue1[:-1], dialogue1[-1]) == MessageType.TEXT
+
+    dialogue2 = [
+        {'role': 'user', 'content': '你可以读一个故事给我听吗', 'timestamp': 1744979591000},
+        {'role': 'assistant', 'content': '当然可以啦!想听什么?', 'timestamp': 1744979601000},
+        {'role': 'user', 'content': '我想听小王子', 'timestamp': 1744979611000},
+        {'role': 'assistant', 'content': '《小王子》讲述了一位年轻王子离开自己的小世界去探索宇宙的冒险经历。 在旅途中,他遇到了各种各样的人,包括被困的飞行员、狐狸和聪明的蛇。 王子通过这些遭遇学到了关于爱情、友谊和超越表面的必要性的重要教训。', 'timestamp': 1744979611000},
+    ]
+    assert response_detector.detect_type(dialogue2[:-1], dialogue2[-1]) == MessageType.VOICE
+
+    dialogue3 = [
+        {'role': 'user', 'content': '他说的是西洋参呢,晓不得到底是不是西洋参。那个样,那个茶是抽的真空的紧包包。我泡他两包,两包泡到十几盒,13盒,我还拿回来的。', 'timestamp': 1744979591000},
+        {'role': 'assistant', 'content': '咋啦?是突然想到啥啦,还是有其他事想和我分享分享?', 'timestamp': 1744979601000},
+        {'role': 'user', 'content': '不要打字,还不要打。听不到。不要打字,不要打字,打字我认不到。打字我认不到,不要打字不要打字,打字我认不到。', 'timestamp': 1744979611000},
+        {'role': 'assistant', 'content': '真是不好意思', 'timestamp': 1744979611000},
+    ]
+    assert response_detector.detect_type(dialogue3[:-1], dialogue3[-1]) == MessageType.VOICE
+
+    global_config.get('debug_flags', {}).update({'disable_llm_api_call': True})