diff --git a/deeptutor/agents/question/agents/generator.py b/deeptutor/agents/question/agents/generator.py index 8bcd42437..1de03e830 100644 --- a/deeptutor/agents/question/agents/generator.py +++ b/deeptutor/agents/question/agents/generator.py @@ -120,14 +120,28 @@ class Generator(BaseAgent): "Template: {template}\n" "User topic: {user_topic}\n" "Preference: {preference}\n" - "Conversation context: {history_context}\n" + "Previously generated questions (do not repeat):\n{history_context}\n" "Knowledge context: {knowledge_context}\n" "Enabled tools: {available_tools}\n\n" 'Return JSON {{"question_type":"","question":"","options":{{}},"correct_answer":"","explanation":""}}' ) + # Serialize the template without the bulky knowledge_context in metadata — + # it is already included via the dedicated {knowledge_context} placeholder + # and its presence inside the template JSON would cause it to appear twice, + # dominating the prompt and making the LLM generate identical questions for + # every template regardless of their individual concentrations. + template_dict = template.__dict__.copy() + if isinstance(template_dict.get("metadata"), dict): + stripped_metadata = { + k: v + for k, v in template_dict["metadata"].items() + if k != "knowledge_context" + } + template_dict["metadata"] = stripped_metadata + user_prompt = user_prompt_template.format( - template=json.dumps(template.__dict__, ensure_ascii=False, indent=2), + template=json.dumps(template_dict, ensure_ascii=False, indent=2), user_topic=user_topic, preference=preference or "(none)", history_context=history_context or "(none)", diff --git a/deeptutor/agents/question/coordinator.py b/deeptutor/agents/question/coordinator.py index a9180077c..62ba40e62 100644 --- a/deeptutor/agents/question/coordinator.py +++ b/deeptutor/agents/question/coordinator.py @@ -273,6 +273,7 @@ class AgentCoordinator: generator = self._create_generator() results: list[dict[str, Any]] = [] total = len(templates) + generated_questions: list[str] = [] for idx, template in enumerate(templates, 1): await self._send_ws_update( @@ -285,13 +286,25 @@ class AgentCoordinator: }, ) + # Build cumulative context from questions already generated in this + # session so the Generator can avoid producing duplicates. + cumulative_history = history_context + if generated_questions: + previous = "\n".join( + f"Q{i}: {q}" for i, q in enumerate(generated_questions, start=1) + ) + if cumulative_history: + cumulative_history = f"{cumulative_history}\n\nQuestions generated in this session:\n{previous}" + else: + cumulative_history = f"Questions generated in this session:\n{previous}" + success = True try: qa_pair = await generator.process( template=template, user_topic=user_topic, preference=preference, - history_context=history_context, + history_context=cumulative_history, ) except Exception as exc: success = False @@ -314,6 +327,10 @@ class AgentCoordinator: } results.append(result) + # Track successfully generated question text for diversity enforcement + if success and qa_pair.question: + generated_questions.append(qa_pair.question) + await self._send_ws_update( "result", { diff --git a/deeptutor/agents/question/prompts/en/generator.yaml b/deeptutor/agents/question/prompts/en/generator.yaml index 6106d3e67..6b0e49e17 100644 --- a/deeptutor/agents/question/prompts/en/generator.yaml +++ b/deeptutor/agents/question/prompts/en/generator.yaml @@ -19,6 +19,9 @@ generate: | Tools enabled by the user: {available_tools} + Previously generated questions in this session (your new question MUST be different from all of these): + {history_context} + Requirements: - Keep strict alignment with template.concentration and template.difficulty. - Respect template.question_type exactly. Do not silently change it. @@ -33,6 +36,7 @@ generate: | - Provide a clear explanation. - Use the knowledge context when it helps ground the question. - Treat the enabled tools list as capability guidance only; do not mention tool names in the final output. + - Do NOT generate a question that is identical or nearly identical to any of the previously generated questions listed above. Return JSON only: {{ diff --git a/deeptutor/agents/question/prompts/zh/generator.yaml b/deeptutor/agents/question/prompts/zh/generator.yaml index 096044580..abfbb620c 100644 --- a/deeptutor/agents/question/prompts/zh/generator.yaml +++ b/deeptutor/agents/question/prompts/zh/generator.yaml @@ -19,6 +19,9 @@ generate: | 用户当前启用的工具: {available_tools} + 本次会话中已生成的题目(你的新题目必须与以下所有题目不同): + {history_context} + 要求: - 严格对齐 template.concentration 与 template.difficulty。 - 必须严格遵循 template.question_type,不要私自改题型。 @@ -32,6 +35,7 @@ generate: | - 给出清晰解释。 - 在有帮助时使用知识上下文增强题目质量。 - 可将启用工具列表视为能力提示,但不要在最终题目中直接提及工具名称。 + - 不要生成与上面已生成题目相同或高度相似的题目。 仅返回 JSON: {{