| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace App\Services;
- class QuestionPromptService
- {
- public function buildKnowledgeMatchPrompt(string $questionText, array $candidates): string
- {
- $candidateLines = array_map(
- fn ($item) => ($item['kp_code'] ?? '') . ' - ' . ($item['name'] ?? ''),
- $candidates
- );
- $candidateText = implode("\n", array_filter($candidateLines));
- $template = config('ai.knowledge_match_prompt');
- if (!$template) {
- $template = <<<PROMPT
- 你是一名数学知识点匹配器。给定题目内容与知识点候选列表,输出最相关的知识点。
- 要求:
- - 只输出 JSON,不要输出其它内容
- - 输出字段为 knowledge_points 数组,每个元素包含 kp_code 与 weight (0~1)
- - 若无法匹配,返回空数组
- 题目内容:
- {question}
- 候选列表:
- {candidates}
- 输出 JSON 示例:
- {
- "knowledge_points": [
- {"kp_code": "A01", "weight": 0.82},
- {"kp_code": "B03", "weight": 0.45}
- ]
- }
- PROMPT;
- }
- return str_replace(
- ['{question}', '{candidates}'],
- [$questionText, $candidateText],
- $template
- );
- }
- public function buildSolutionStepsPrompt(string $questionText): string
- {
- $template = config('ai.solution_steps_prompt');
- if (!$template) {
- $template = <<<PROMPT
- 你是一名数学解题专家,请为题目生成“分步骤评分解题过程”。
- 要求:
- - 只输出 JSON
- - steps 为数组,每一步包含:
- - step_index: 从 1 开始
- - title: 本步标题
- - content: 本步解释
- - score: 本步分值(数字)
- - kp_codes: 与本步相关的知识点数组
- 题目内容:
- {question}
- 输出 JSON 示例:
- {
- "solution": "整体解题思路概述",
- "steps": [
- {"step_index": 1, "title": "...", "content": "...", "score": 4, "kp_codes": ["A01"]},
- {"step_index": 2, "title": "...", "content": "...", "score": 6, "kp_codes": ["B02", "C03"]}
- ]
- }
- PROMPT;
- }
- return str_replace('{question}', $questionText, $template);
- }
- public function buildQuestionFromSourcePrompt(string $sourceText): string
- {
- $template = config('ai.question_generation_prompt');
- if (!$template) {
- $template = <<<PROMPT
- 你是一名“数学题目生成器”。请根据给定材料生成可入库的题目 JSON。
- 要求:
- - 只输出 JSON
- - 必须包含字段:stem, options, answer, solution, question_type, difficulty, knowledge_points, solution_steps
- - short/answer 类题目必须提供 solution_steps(分步评分),每步包含 score 与 kp_codes
- - knowledge_points 为题目级知识点列表
- 材料内容:
- {content}
- 输出 JSON 示例:
- {
- "stem": "...",
- "options": {"A": "...", "B": "..."},
- "answer": "...",
- "solution": "...",
- "question_type": "short",
- "difficulty": 0.6,
- "knowledge_points": ["A01"],
- "solution_steps": [
- {"step_index": 1, "title": "...", "content": "...", "score": 4, "kp_codes": ["A01"]}
- ]
- }
- PROMPT;
- }
- return str_replace('{content}', $sourceText, $template);
- }
- }
|