liumaolin commited on
Commit
20d87ca
·
1 Parent(s): 6556ced

Refactor `test_llm_dialogue.py` for improved pipeline creation and streamlined testing

Browse files

- Replace manual chain setup with `create_langchain_pipeline` for cleaner initialization.
- Simplify warmup logic and add optional pipeline parameter.
- Enhance test logging with structured outputs and detailed round information.

Files changed (1) hide show
  1. tests/test_llm_dialogue.py +35 -29
tests/test_llm_dialogue.py CHANGED
@@ -44,18 +44,12 @@ class TestLLMDialogue(unittest.TestCase):
44
  model_path = paths.LLM_MODELS_PATH / 'qwen' / 'Qwen3-8B-Q6_K.gguf'
45
  self.langchain_instance = ChatLlamaCpp(model_path=model_path.as_posix(), **model_params)
46
 
47
- system_message = SystemMessage(content=CHINESE_SYSTEM_PROMPT)
48
- human_message = HumanMessagePromptTemplate.from_template("{input}")
49
- prompt = ChatPromptTemplate(messages=[
50
- system_message,
51
- MessagesPlaceholder(variable_name="history"),
52
- human_message
53
- ])
54
-
55
- chain = prompt | self.langchain_instance
56
- self.chain_with_history = RunnableWithMessageHistory(chain, self.get_session_history,
57
- history_messages_key='history')
58
- self.warmup()
59
 
60
  self.test_datasets = [
61
  {
@@ -64,16 +58,12 @@ class TestLLMDialogue(unittest.TestCase):
64
  'questions': [
65
  # 第1轮:开放性话题引入
66
  "最近人工智能技术发展很快,你觉得AI对我们日常生活带来了哪些改变?",
67
-
68
  # 第2轮:基于前一个回答的深入探讨
69
  "你刚才提到的这些改变中,哪一个你认为是最重要的?为什么?",
70
-
71
  # 第3轮:转向具体场景和个人观点
72
  "如果让你选择一个AI应用来帮助解决教育领域的问题,你会选择什么?具体怎么实现?",
73
-
74
  # 第4轮:挑战性问题,测试逻辑思维
75
  "但是也有人担心AI在教育中会让学生过度依赖技术,失去独立思考能力。你怎么看待这个担忧?",
76
-
77
  # 第5轮:总结性问题,测试整合能力
78
  "综合我们刚才讨论的内容,你认为在AI快速发展的时代,普通人应该如何适应和准备?"
79
  ]
@@ -161,10 +151,10 @@ class TestLLMDialogue(unittest.TestCase):
161
  self.history_store[session_id] = InMemoryChatMessageHistory(messages=messages)
162
  return self.history_store[session_id]
163
 
164
- def warmup(self):
165
  session_id = 'warmup'
166
  config = {"configurable": {"session_id": session_id}}
167
- for chunk in self.chain_with_history.stream(input={'input': 'This is a warmup step.'}, config=config):
168
  pass
169
 
170
  def _get_prompt_by_language(self, language: str) -> str:
@@ -177,15 +167,31 @@ class TestLLMDialogue(unittest.TestCase):
177
  def test_dialogue(self):
178
  for test_dataset in self.test_datasets:
179
  session_id = test_dataset.get('session_id')
180
- print(f'Test dataset: {session_id}')
181
- print('=' * 80)
182
- for question in test_dataset.get('questions'):
183
- print('Test question:', question)
184
- config = {"configurable": {"session_id": session_id}}
185
- prompt = self._get_prompt_by_language(test_dataset.get('language'))
186
- pipeline = create_langchain_pipeline(self.langchain_instance, prompt, self.get_session_history)
187
- print(f'LLM answer: ', end='')
 
 
 
 
 
 
 
 
 
 
 
 
188
  for chunk in pipeline.stream(input={'input': question}, config=config):
189
- print(chunk.content, end='')
190
- print()
191
- print('-' * 80)
 
 
 
 
 
44
  model_path = paths.LLM_MODELS_PATH / 'qwen' / 'Qwen3-8B-Q6_K.gguf'
45
  self.langchain_instance = ChatLlamaCpp(model_path=model_path.as_posix(), **model_params)
46
 
47
+ pipeline = create_langchain_pipeline(
48
+ self.langchain_instance,
49
+ self._get_prompt_by_language('zh'),
50
+ self.get_session_history
51
+ )
52
+ self.warmup(pipeline)
 
 
 
 
 
 
53
 
54
  self.test_datasets = [
55
  {
 
58
  'questions': [
59
  # 第1轮:开放性话题引入
60
  "最近人工智能技术发展很快,你觉得AI对我们日常生活带来了哪些改变?",
 
61
  # 第2轮:基于前一个回答的深入探讨
62
  "你刚才提到的这些改变中,哪一个你认为是最重要的?为什么?",
 
63
  # 第3轮:转向具体场景和个人观点
64
  "如果让你选择一个AI应用来帮助解决教育领域的问题,你会选择什么?具体怎么实现?",
 
65
  # 第4轮:挑战性问题,测试逻辑思维
66
  "但是也有人担心AI在教育中会让学生过度依赖技术,失去独立思考能力。你怎么看待这个担忧?",
 
67
  # 第5轮:总结性问题,测试整合能力
68
  "综合我们刚才讨论的内容,你认为在AI快速发展的时代,普通人应该如何适应和准备?"
69
  ]
 
151
  self.history_store[session_id] = InMemoryChatMessageHistory(messages=messages)
152
  return self.history_store[session_id]
153
 
154
+ def warmup(self, pipeline: RunnableWithMessageHistory = None):
155
  session_id = 'warmup'
156
  config = {"configurable": {"session_id": session_id}}
157
+ for chunk in pipeline.stream(input={'input': 'This is a warmup step.'}, config=config):
158
  pass
159
 
160
  def _get_prompt_by_language(self, language: str) -> str:
 
167
  def test_dialogue(self):
168
  for test_dataset in self.test_datasets:
169
  session_id = test_dataset.get('session_id')
170
+ language = test_dataset.get('language')
171
+ questions = test_dataset.get('questions')
172
+
173
+ # --- Test Suite Header ---
174
+ print("\n" + "=" * 80)
175
+ print(f" 🧪 EXECUTING TEST SUITE: {session_id} ({language.upper()})")
176
+ print("=" * 80)
177
+
178
+ # --- Langchain Pipeline Setup ---
179
+ prompt = self._get_prompt_by_language(language)
180
+ pipeline = create_langchain_pipeline(self.langchain_instance, prompt, self.get_session_history)
181
+ config = {"configurable": {"session_id": session_id}}
182
+
183
+ for i, question in enumerate(questions):
184
+ round_num = i + 1
185
+ print(f"\n🗣️ Round {round_num}")
186
+ print(f" 👤 User: {question}")
187
+ print(f" 🤖 AI: ", end='')
188
+
189
+ # --- Stream and print LLM response ---
190
  for chunk in pipeline.stream(input={'input': question}, config=config):
191
+ print(chunk.content, end='', flush=True)
192
+
193
+ print("\n" + "-" * 80)
194
+
195
+ # --- Test Suite Footer ---
196
+ print(f"\n✅ TEST SUITE COMPLETED: {session_id}")
197
+ print("=" * 80 + "\n")