QuestionPromptService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Services;
  3. class QuestionPromptService
  4. {
  5. public function buildKnowledgeMatchPrompt(string $questionText, array $candidates, ?string $context = null): string
  6. {
  7. $candidateLines = array_map(
  8. fn ($item) => ($item['kp_code'] ?? '') . ' - ' . ($item['name'] ?? ''),
  9. $candidates
  10. );
  11. $candidateText = implode("\n", array_filter($candidateLines));
  12. $template = config('ai.knowledge_match_prompt');
  13. if (!$template) {
  14. $template = <<<PROMPT
  15. 你是一名数学知识点匹配器。给定题目内容与知识点候选列表,输出最相关的知识点。
  16. 要求:
  17. - 只输出 JSON,不要输出其它内容
  18. - 输出字段为 knowledge_points 数组,每个元素包含 kp_code 与 weight (0~1)
  19. - 若无法匹配,返回空数组
  20. 辅助信息(可能包含教材目录):
  21. {context}
  22. 题目内容:
  23. {question}
  24. 候选列表:
  25. {candidates}
  26. 输出 JSON 示例:
  27. {
  28. "knowledge_points": [
  29. {"kp_code": "A01", "weight": 0.82},
  30. {"kp_code": "B03", "weight": 0.45}
  31. ]
  32. }
  33. PROMPT;
  34. }
  35. return str_replace(
  36. ['{question}', '{candidates}', '{context}'],
  37. [
  38. $context ? ("{$questionText}\n\n上下文提示:\n{$context}") : $questionText,
  39. $candidateText,
  40. $context ?? '',
  41. ],
  42. $template
  43. );
  44. }
  45. public function buildSolutionStepsPrompt(string $questionText): string
  46. {
  47. $template = config('ai.solution_steps_prompt');
  48. if (!$template) {
  49. $template = <<<PROMPT
  50. 你是一名数学解题专家,请为题目生成“分步骤评分解题过程”。
  51. 要求:
  52. - 只输出 JSON
  53. - steps 为数组,每一步包含:
  54. - step_index: 从 1 开始
  55. - title: 本步标题
  56. - content: 本步解释
  57. - score: 本步分值(数字)
  58. - kp_codes: 与本步相关的知识点数组
  59. 题目内容:
  60. {question}
  61. 输出 JSON 示例:
  62. {
  63. "solution": "整体解题思路概述",
  64. "steps": [
  65. {"step_index": 1, "title": "...", "content": "...", "score": 4, "kp_codes": ["A01"]},
  66. {"step_index": 2, "title": "...", "content": "...", "score": 6, "kp_codes": ["B02", "C03"]}
  67. ]
  68. }
  69. PROMPT;
  70. }
  71. return str_replace('{question}', $questionText, $template);
  72. }
  73. public function buildQuestionFromSourcePrompt(string $sourceText): string
  74. {
  75. $promptService = app(PromptService::class);
  76. $promptService->listPrompts(type: 'question_enrich', active: 'yes');
  77. $template = $promptService->getPromptContent('question_enrich_default')
  78. ?: config('ai.question_generation_prompt');
  79. if (!$template) {
  80. $template = <<<PROMPT
  81. 你是一名“数学题目生成器”。请根据给定材料生成可入库的题目 JSON。
  82. 要求:
  83. - 只输出 JSON
  84. - 必须包含字段:stem, options, answer, solution, question_type, difficulty, knowledge_points, solution_steps
  85. - answer 类题目必须提供 solution_steps(分步评分),每步包含 score 与 kp_codes
  86. - knowledge_points 为题目级知识点列表
  87. 材料内容:
  88. {content}
  89. 输出 JSON 示例:
  90. {
  91. "stem": "...",
  92. "options": {"A": "...", "B": "..."},
  93. "answer": "...",
  94. "solution": "...",
  95. "question_type": "answer",
  96. "difficulty": 0.6,
  97. "knowledge_points": ["A01"],
  98. "solution_steps": [
  99. {"step_index": 1, "title": "...", "content": "...", "score": 4, "kp_codes": ["A01"]}
  100. ]
  101. }
  102. PROMPT;
  103. }
  104. return str_replace('{content}', $sourceText, $template);
  105. }
  106. public function buildSolutionRegenPrompt(string $stem, string $answer, array $images = []): string
  107. {
  108. $promptService = app(PromptService::class);
  109. $promptService->listPrompts(type: 'question_solution_regen', active: 'yes');
  110. $template = $promptService->getPromptContent('question_solution_regen_default');
  111. if (!$template) {
  112. $template = "请根据题干与答案生成解题思路,输出 JSON,包含 solution 与 steps。";
  113. }
  114. $imageText = '';
  115. if (!empty($images)) {
  116. $imageText = implode("\n", array_map('strval', $images));
  117. }
  118. return str_replace(
  119. ['{stem}', '{provided_answer}', '{image_urls}'],
  120. [$stem, $answer, $imageText],
  121. $template
  122. );
  123. }
  124. }