where('template_type', $type); } if ($active !== null) { $query->where('is_active', $active === 'yes' || $active === true); } return $query->orderByDesc('updated_at') ->get() ->map(fn (PromptTemplate $prompt) => $this->mapPrompt($prompt)) ->values() ->all(); } /** * 保存提示词 */ public function savePrompt(array $data): array { try { $templateName = (string) ($data['template_name'] ?? ''); if ($templateName === '') { return ['success' => false, 'message' => '模板名称不能为空']; } $payload = [ 'template_type' => $data['template_type'] ?? 'question_generation', 'template_content' => $data['template_content'] ?? '', 'variables' => $this->parseJsonField($data['variables'] ?? []), 'description' => $data['description'] ?? null, 'tags' => $this->parseJsonField($data['tags'] ?? []), 'is_active' => ($data['is_active'] ?? 'yes') === 'yes' || ($data['is_active'] === true), ]; PromptTemplate::updateOrCreate( ['template_name' => $templateName], $payload ); return ['success' => true, 'message' => '提示词已保存']; } catch (\Exception $e) { Log::error('保存提示词异常', [ 'error' => $e->getMessage() ]); return ['success' => false, 'message' => '保存失败']; } } public function deletePrompt(string $templateName): bool { return PromptTemplate::where('template_name', $templateName)->delete() > 0; } public function importFromArray(array $prompts): array { $imported = 0; $updated = 0; $errors = []; foreach ($prompts as $prompt) { $templateName = (string) ($prompt['template_name'] ?? $prompt['name'] ?? ''); if ($templateName === '') { continue; } $payload = [ 'template_name' => $templateName, 'template_type' => $prompt['template_type'] ?? $prompt['type'] ?? 'question_generation', 'template_content' => $prompt['template_content'] ?? '', 'variables' => $this->parseJsonField($prompt['variables'] ?? []), 'description' => $prompt['description'] ?? null, 'tags' => $this->parseJsonField($prompt['tags'] ?? []), 'is_active' => ($prompt['is_active'] ?? 'yes') === 'yes' || ($prompt['is_active'] === true), ]; try { $existing = PromptTemplate::query()->where('template_name', $templateName)->first(); PromptTemplate::updateOrCreate(['template_name' => $templateName], $payload); $existing ? $updated++ : $imported++; } catch (\Throwable $e) { $errors[] = $templateName . ': ' . $e->getMessage(); } } return [ 'success' => true, 'imported' => $imported, 'updated' => $updated, 'errors' => $errors, ]; } public function getPrompt(string $templateName): ?array { $prompt = PromptTemplate::where('template_name', $templateName)->first(); return $prompt ? $this->mapPrompt($prompt) : null; } public function getPromptContent(string $templateName): ?string { $prompt = PromptTemplate::where('template_name', $templateName)->first(); return $prompt?->template_content; } /** * 获取默认提示词模板 */ public function getDefaultPromptTemplate(): string { return '你是资深的中学数学命题专家,请为{knowledge_point}知识点生成高质量题目。 【核心要求】 1. 题目必须符合{grade_level}年级水平 2. 难度分布:基础({basic_ratio}%) + 中等({intermediate_ratio}%) + 拔高({advanced_ratio}%) 3. 题型分配:选择题({choice}道) + 填空题({fill}道) + 解答题({solution}道) 【技能覆盖】 {skill_coverage} 【图示处理】 - 如果原题涉及图形/示意图/坐标系/几何草图,必须在题干内内嵌一段完整的 标签来还原图形;不要使用外链图片、base64 或占位符。 - SVG 要包含明确的宽高(建议 260~360 像素),只使用基础图元(line、rect、circle、polygon、path、text),并给出必要的坐标、角点和标注文本。 - 确保题干文本描述与 SVG 一致,例如“如图所示”后紧跟 SVG,且 SVG 放在题干末尾即可被前端直接渲染。 【质量标准】 - 准确性:100%正确 - 多样性:避免重复 - 梯度性:难度递进合理 - 实用性:贴近实际应用 【输出格式】 { "total": {count}, "questions": [ { "id": "唯一标识", "stem": "题干", "answer": "标准答案", "solution": "详细解答", "difficulty": 难度值(0.3/0.6/0.85), "skill": "关联技能" } ] }'; } /** * 检查服务健康状态 */ public function checkHealth(): bool { return true; } private function parseJsonField($value): array { if (is_array($value)) { return $value; } if (is_string($value) && $value !== '') { try { $decoded = json_decode($value, true, 512, JSON_THROW_ON_ERROR); return is_array($decoded) ? $decoded : []; } catch (\Throwable) { return []; } } return []; } private function mapPrompt(PromptTemplate $prompt): array { $variables = $prompt->variables ?? []; $tags = $prompt->tags ?? []; return [ 'template_name' => $prompt->template_name, 'template_type' => $prompt->template_type, 'template_content' => $prompt->template_content, 'variables' => json_encode($variables, JSON_UNESCAPED_UNICODE), 'description' => $prompt->description, 'tags' => json_encode($tags, JSON_UNESCAPED_UNICODE), 'is_active' => $prompt->is_active ? 'yes' : 'no', 'updated_at' => $prompt->updated_at, ]; } }