PromptService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Http;
  4. use Illuminate\Support\Facades\Log;
  5. class PromptService
  6. {
  7. protected string $baseUrl;
  8. public function __construct()
  9. {
  10. // 从配置文件读取base_url
  11. $this->baseUrl = config('services.question_bank.base_url', env('QUESTION_BANK_API_BASE', 'http://localhost:5015'));
  12. $this->baseUrl = rtrim($this->baseUrl, '/');
  13. }
  14. /**
  15. * 获取提示词列表
  16. */
  17. public function listPrompts(?string $type = null, ?string $active = null): array
  18. {
  19. try {
  20. $query = array_filter([
  21. 'type' => $type,
  22. 'active' => $active,
  23. ], fn($value) => filled($value));
  24. $response = Http::timeout(10)
  25. ->get($this->baseUrl . '/prompts', $query);
  26. if ($response->successful()) {
  27. return $response->json();
  28. }
  29. Log::warning('获取提示词列表失败', [
  30. 'status' => $response->status()
  31. ]);
  32. } catch (\Exception $e) {
  33. Log::error('获取提示词列表异常', [
  34. 'error' => $e->getMessage()
  35. ]);
  36. }
  37. return [];
  38. }
  39. /**
  40. * 保存提示词
  41. */
  42. public function savePrompt(array $data): array
  43. {
  44. try {
  45. // 先尝试更新
  46. $response = Http::timeout(10)
  47. ->put($this->baseUrl . '/prompts/default', $data);
  48. // 如果更新失败(可能不存在),则尝试创建
  49. if (!isset($response->json()['success']) || !$response->json()['success']) {
  50. $response = Http::timeout(10)
  51. ->post($this->baseUrl . '/prompts', $data);
  52. }
  53. if ($response->successful()) {
  54. return $response->json();
  55. }
  56. Log::warning('保存提示词失败', [
  57. 'status' => $response->status()
  58. ]);
  59. } catch (\Exception $e) {
  60. Log::error('保存提示词异常', [
  61. 'error' => $e->getMessage()
  62. ]);
  63. }
  64. return ['success' => false, 'message' => '保存失败'];
  65. }
  66. /**
  67. * 获取默认提示词模板
  68. */
  69. public function getDefaultPromptTemplate(): string
  70. {
  71. return '你是资深的中学数学命题专家,请为{knowledge_point}知识点生成高质量题目。
  72. 【核心要求】
  73. 1. 题目必须符合{grade_level}年级水平
  74. 2. 难度分布:基础({basic_ratio}%) + 中等({intermediate_ratio}%) + 拔高({advanced_ratio}%)
  75. 3. 题型分配:选择题({choice}道) + 填空题({fill}道) + 解答题({solution}道)
  76. 【技能覆盖】
  77. {skill_coverage}
  78. 【质量标准】
  79. - 准确性:100%正确
  80. - 多样性:避免重复
  81. - 梯度性:难度递进合理
  82. - 实用性:贴近实际应用
  83. 【输出格式】
  84. {
  85. "total": {count},
  86. "questions": [
  87. {
  88. "id": "唯一标识",
  89. "stem": "题干",
  90. "answer": "标准答案",
  91. "solution": "详细解答",
  92. "difficulty": 难度值(0.3/0.6/0.85),
  93. "skill": "关联技能"
  94. }
  95. ]
  96. }';
  97. }
  98. /**
  99. * 检查服务健康状态
  100. */
  101. public function checkHealth(): bool
  102. {
  103. try {
  104. $response = Http::timeout(5)
  105. ->get($this->baseUrl . '/health');
  106. return $response->successful();
  107. } catch (\Exception $e) {
  108. Log::error('提示词服务健康检查失败', [
  109. 'error' => $e->getMessage()
  110. ]);
  111. return false;
  112. }
  113. }
  114. }