| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Services;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- class PromptService
- {
- protected string $baseUrl;
- public function __construct()
- {
- // 从配置文件读取base_url
- $this->baseUrl = config('services.question_bank.base_url', env('QUESTION_BANK_API_BASE', 'http://localhost:5015'));
- $this->baseUrl = rtrim($this->baseUrl, '/');
- }
- /**
- * 获取提示词列表
- */
- public function listPrompts(?string $type = null, ?string $active = null): array
- {
- try {
- $query = array_filter([
- 'type' => $type,
- 'active' => $active,
- ], fn($value) => filled($value));
- $response = Http::timeout(10)
- ->get($this->baseUrl . '/prompts', $query);
- if ($response->successful()) {
- return $response->json();
- }
- Log::warning('获取提示词列表失败', [
- 'status' => $response->status()
- ]);
- } catch (\Exception $e) {
- Log::error('获取提示词列表异常', [
- 'error' => $e->getMessage()
- ]);
- }
- return [];
- }
- /**
- * 保存提示词
- */
- public function savePrompt(array $data): array
- {
- try {
- // 先尝试更新
- $response = Http::timeout(10)
- ->put($this->baseUrl . '/prompts/default', $data);
- // 如果更新失败(可能不存在),则尝试创建
- if (!isset($response->json()['success']) || !$response->json()['success']) {
- $response = Http::timeout(10)
- ->post($this->baseUrl . '/prompts', $data);
- }
- if ($response->successful()) {
- return $response->json();
- }
- Log::warning('保存提示词失败', [
- 'status' => $response->status()
- ]);
- } catch (\Exception $e) {
- Log::error('保存提示词异常', [
- 'error' => $e->getMessage()
- ]);
- }
- return ['success' => false, 'message' => '保存失败'];
- }
- /**
- * 获取默认提示词模板
- */
- public function getDefaultPromptTemplate(): string
- {
- return '你是资深的中学数学命题专家,请为{knowledge_point}知识点生成高质量题目。
- 【核心要求】
- 1. 题目必须符合{grade_level}年级水平
- 2. 难度分布:基础({basic_ratio}%) + 中等({intermediate_ratio}%) + 拔高({advanced_ratio}%)
- 3. 题型分配:选择题({choice}道) + 填空题({fill}道) + 解答题({solution}道)
- 【技能覆盖】
- {skill_coverage}
- 【质量标准】
- - 准确性:100%正确
- - 多样性:避免重复
- - 梯度性:难度递进合理
- - 实用性:贴近实际应用
- 【输出格式】
- {
- "total": {count},
- "questions": [
- {
- "id": "唯一标识",
- "stem": "题干",
- "answer": "标准答案",
- "solution": "详细解答",
- "difficulty": 难度值(0.3/0.6/0.85),
- "skill": "关联技能"
- }
- ]
- }';
- }
- /**
- * 检查服务健康状态
- */
- public function checkHealth(): bool
- {
- try {
- $response = Http::timeout(5)
- ->get($this->baseUrl . '/health');
- return $response->successful();
- } catch (\Exception $e) {
- Log::error('提示词服务健康检查失败', [
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- }
|