QuestionPromptService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Services;
  3. class QuestionPromptService
  4. {
  5. public function buildKnowledgeMatchPrompt(string $questionText, array $candidates): 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. {question}
  22. 候选列表:
  23. {candidates}
  24. 输出 JSON 示例:
  25. {
  26. "knowledge_points": [
  27. {"kp_code": "A01", "weight": 0.82},
  28. {"kp_code": "B03", "weight": 0.45}
  29. ]
  30. }
  31. PROMPT;
  32. }
  33. return str_replace(
  34. ['{question}', '{candidates}'],
  35. [$questionText, $candidateText],
  36. $template
  37. );
  38. }
  39. public function buildSolutionStepsPrompt(string $questionText): string
  40. {
  41. $template = config('ai.solution_steps_prompt');
  42. if (!$template) {
  43. $template = <<<PROMPT
  44. 你是一名数学解题专家,请为题目生成“分步骤评分解题过程”。
  45. 要求:
  46. - 只输出 JSON
  47. - steps 为数组,每一步包含:
  48. - step_index: 从 1 开始
  49. - title: 本步标题
  50. - content: 本步解释
  51. - score: 本步分值(数字)
  52. - kp_codes: 与本步相关的知识点数组
  53. 题目内容:
  54. {question}
  55. 输出 JSON 示例:
  56. {
  57. "solution": "整体解题思路概述",
  58. "steps": [
  59. {"step_index": 1, "title": "...", "content": "...", "score": 4, "kp_codes": ["A01"]},
  60. {"step_index": 2, "title": "...", "content": "...", "score": 6, "kp_codes": ["B02", "C03"]}
  61. ]
  62. }
  63. PROMPT;
  64. }
  65. return str_replace('{question}', $questionText, $template);
  66. }
  67. public function buildQuestionFromSourcePrompt(string $sourceText): string
  68. {
  69. $template = config('ai.question_generation_prompt');
  70. if (!$template) {
  71. $template = <<<PROMPT
  72. 你是一名“数学题目生成器”。请根据给定材料生成可入库的题目 JSON。
  73. 要求:
  74. - 只输出 JSON
  75. - 必须包含字段:stem, options, answer, solution, question_type, difficulty, knowledge_points, solution_steps
  76. - short/answer 类题目必须提供 solution_steps(分步评分),每步包含 score 与 kp_codes
  77. - knowledge_points 为题目级知识点列表
  78. 材料内容:
  79. {content}
  80. 输出 JSON 示例:
  81. {
  82. "stem": "...",
  83. "options": {"A": "...", "B": "..."},
  84. "answer": "...",
  85. "solution": "...",
  86. "question_type": "short",
  87. "difficulty": 0.6,
  88. "knowledge_points": ["A01"],
  89. "solution_steps": [
  90. {"step_index": 1, "title": "...", "content": "...", "score": 4, "kp_codes": ["A01"]}
  91. ]
  92. }
  93. PROMPT;
  94. }
  95. return str_replace('{content}', $sourceText, $template);
  96. }
  97. }