QuestionGeneration.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\KnowledgeGraphService;
  4. use App\Services\QuestionServiceApi;
  5. use BackedEnum;
  6. use Filament\Notifications\Notification;
  7. use Filament\Pages\Page;
  8. use UnitEnum;
  9. use Livewire\Attributes\Computed;
  10. class QuestionGeneration extends Page
  11. {
  12. protected static ?string $title = '题目生成';
  13. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-sparkles';
  14. protected static ?string $navigationLabel = '题目生成';
  15. protected static string|UnitEnum|null $navigationGroup = '卷子生成管理';
  16. protected static ?int $navigationSort = 9;
  17. protected string $view = 'filament.pages.question-generation';
  18. public ?string $generateKpCode = null;
  19. public array $selectedSkills = [];
  20. public int $questionCount = 100;
  21. public ?string $generateDifficulty = null;
  22. public ?string $generateType = null;
  23. public ?string $promptTemplate = null;
  24. public bool $isGenerating = false;
  25. public ?string $currentTaskId = null;
  26. public int $currentTaskProgress = 0;
  27. public ?string $currentTaskMessage = null;
  28. #[Computed(cache: false)]
  29. public function knowledgePointOptions(): array
  30. {
  31. return app(\App\Services\QuestionServiceApi::class)->getKnowledgePointOptions();
  32. }
  33. #[Computed(cache: false)]
  34. public function skillsOptions(): array
  35. {
  36. if (!$this->generateKpCode) {
  37. return [];
  38. }
  39. $service = app(KnowledgeGraphService::class);
  40. return $service->getSkillsByKnowledgePoint($this->generateKpCode);
  41. }
  42. #[Computed(cache: false)]
  43. public function questionTypeOptions(): array
  44. {
  45. return [
  46. 'CHOICE' => '单选题',
  47. 'MULTIPLE_CHOICE' => '多选题',
  48. 'FILL_IN_THE_BLANK' => '填空题',
  49. 'CALCULATION' => '计算题',
  50. 'WORD_PROBLEM' => '应用题',
  51. 'PROOF' => '证明题',
  52. ];
  53. }
  54. #[Computed(cache: false)]
  55. public function promptOptions(): array
  56. {
  57. $service = app(\App\Services\QuestionServiceApi::class);
  58. $prompts = $service->listPrompts(type: 'question_generation', active: 'yes');
  59. // 只展示激活的题目生成模板
  60. $options = [];
  61. foreach ($prompts as $prompt) {
  62. $label = $prompt['template_name'];
  63. if (!empty($prompt['description'])) {
  64. $label .= ' - ' . (is_string($prompt['description']) ? $prompt['description'] : json_encode($prompt['description']));
  65. }
  66. $options[$prompt['template_name']] = $label;
  67. }
  68. // 自动选择第一个激活模板
  69. if (!$this->promptTemplate && !empty($options)) {
  70. $this->promptTemplate = array_key_first($options);
  71. }
  72. return $options;
  73. }
  74. public function updatedGenerateKpCode(): void
  75. {
  76. // 选择新知识点时清空技能选择
  77. $this->selectedSkills = [];
  78. }
  79. public function toggleAllSkills(): void
  80. {
  81. $skillsOptions = $this->skillsOptions;
  82. $skillsCount = count($skillsOptions);
  83. \Log::info('[ToggleAllSkills] Called', [
  84. 'skillsCount' => $skillsCount,
  85. 'selectedSkillsCount' => count($this->selectedSkills),
  86. 'skillsOptions' => array_column($skillsOptions, 'code'),
  87. 'selectedSkills' => $this->selectedSkills
  88. ]);
  89. if ($skillsCount === 0) {
  90. \Log::info('[ToggleAllSkills] No skills available, returning');
  91. return;
  92. }
  93. // 获取所有技能的编码列表
  94. $allSkillCodes = array_values(array_unique(array_column($skillsOptions, 'code')));
  95. // 检查当前选中的技能是否等于全部技能
  96. if (count($this->selectedSkills) === $skillsCount &&
  97. count(array_intersect($this->selectedSkills, $allSkillCodes)) === $skillsCount) {
  98. // 如果已全选,则清空
  99. $this->selectedSkills = [];
  100. \Log::info('[ToggleAllSkills] Deselecting all skills');
  101. } else {
  102. // 否则全选
  103. $this->selectedSkills = $allSkillCodes;
  104. \Log::info('[ToggleAllSkills] Selecting all skills', ['newSelection' => $this->selectedSkills]);
  105. }
  106. }
  107. public function executeGenerate()
  108. {
  109. if ($this->isGenerating) {
  110. return;
  111. }
  112. if (!$this->generateKpCode) {
  113. Notification::make()->title('请选择知识点')->danger()->send();
  114. return;
  115. }
  116. $skillsOptions = $this->skillsOptions;
  117. $skillsCount = count($skillsOptions);
  118. if ($skillsCount > 0 && empty($this->selectedSkills)) {
  119. Notification::make()->title('请选择至少一个技能')->danger()->send();
  120. return;
  121. }
  122. $this->isGenerating = true;
  123. $this->currentTaskId = null;
  124. try {
  125. $service = app(QuestionServiceApi::class);
  126. $params = [
  127. 'kp_code' => $this->generateKpCode,
  128. 'skills' => $this->selectedSkills,
  129. 'count' => $this->questionCount,
  130. 'difficulty' => $this->generateDifficulty,
  131. 'type' => $this->generateType,
  132. 'prompt_template' => $this->promptTemplate ?? null
  133. ];
  134. $response = $service->generateQuestions($params);
  135. \Log::info("[QuestionGen] 生成请求完成", [
  136. 'response' => $response,
  137. 'kp_code' => $this->generateKpCode,
  138. 'skills' => $this->selectedSkills,
  139. 'count' => $this->questionCount,
  140. ]);
  141. if (!($response['success'] ?? false)) {
  142. $this->isGenerating = false;
  143. Notification::make()
  144. ->title('生成失败')
  145. ->body($response['message'] ?? '生成失败')
  146. ->danger()
  147. ->send();
  148. return;
  149. }
  150. Notification::make()
  151. ->title('生成完成')
  152. ->body('已生成题目并入库')
  153. ->success()
  154. ->send();
  155. $redirectUrl = "/admin/question-management";
  156. $this->js("window.location.href = '{$redirectUrl}';");
  157. return;
  158. } catch (\Illuminate\Http\Client\ConnectionException $e) {
  159. $this->isGenerating = false;
  160. \Log::error("[QuestionGen] 连接异常: " . $e->getMessage());
  161. Notification::make()
  162. ->title('连接AI服务失败')
  163. ->body('请检查AI服务是否正常运行')
  164. ->danger()
  165. ->send();
  166. } catch (\Exception $e) {
  167. $this->isGenerating = false;
  168. \Log::error("[QuestionGen] 生成异常: " . $e->getMessage());
  169. Notification::make()
  170. ->title('请求异常')
  171. ->body($e->getMessage())
  172. ->danger()
  173. ->send();
  174. }
  175. }
  176. public function forceCloseStatusBar(string $taskId): void
  177. {
  178. \Log::info("[QuestionGen] 强制关闭状态栏: {$taskId}");
  179. $this->isGenerating = false;
  180. $this->currentTaskId = null;
  181. Notification::make()
  182. ->title('⚠️ 任务超时')
  183. ->body('生成任务可能已完成,请刷新页面查看')
  184. ->warning()
  185. ->persistent()
  186. ->send();
  187. }
  188. }