AiSolutionService.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Services;
  3. class AiSolutionService
  4. {
  5. public function generateSolution(string $questionText, array $context = []): array
  6. {
  7. $questionType = $context['question_type'] ?? null;
  8. $needsSteps = in_array($questionType, ['short', 'answer', 'calc', 'calculation', 'word_problem', 'proof'], true);
  9. if ($needsSteps) {
  10. return $this->generateSolutionSteps($questionText);
  11. }
  12. return [
  13. 'solution' => '',
  14. 'steps' => [],
  15. 'source' => 'placeholder',
  16. ];
  17. }
  18. public function generateStepRubric(string $questionText, array $context = []): array
  19. {
  20. return [
  21. 'steps' => [],
  22. 'total_score' => $context['total_score'] ?? null,
  23. ];
  24. }
  25. public function validateAnswer(string $questionText, string $answer, array $context = []): array
  26. {
  27. return [
  28. 'is_correct' => null,
  29. 'confidence' => null,
  30. 'notes' => [],
  31. ];
  32. }
  33. public function generateSolutionSteps(string $questionText): array
  34. {
  35. $prompt = app(QuestionPromptService::class)->buildSolutionStepsPrompt($questionText);
  36. try {
  37. $result = app(AiClientService::class)->callJson($prompt);
  38. } catch (\Throwable $e) {
  39. return [
  40. 'solution' => '',
  41. 'steps' => [],
  42. 'source' => 'placeholder',
  43. ];
  44. }
  45. return [
  46. 'solution' => $result['solution'] ?? '',
  47. 'steps' => $result['steps'] ?? [],
  48. 'source' => 'ai',
  49. ];
  50. }
  51. }