AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AiManager.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Ai;
  3. /**
  4. * The single door to AI. No other code talks to an AI provider directly.
  5. *
  6. * It looks up which provider and model a task uses in config/ai.php, builds that
  7. * provider's driver, and returns the answer in one common shape.
  8. */
  9. class AiManager
  10. {
  11. /**
  12. * Ask the model configured for the given task.
  13. *
  14. * @throws AiException
  15. */
  16. public function ask(string $task, string $system, string $prompt): AiResponse
  17. {
  18. $settings = config("ai.tasks.$task");
  19. if (! is_array($settings) || empty($settings['provider']) || empty($settings['model'])) {
  20. throw new AiException("AI task [$task] is not configured.", AiException::SETUP);
  21. }
  22. $provider = (string) $settings['provider'];
  23. $maxTokens = (int) ($settings['max_tokens'] ?? 4000);
  24. $providerLimit = (int) config("ai.providers.$provider.max_output_tokens", 0);
  25. if ($providerLimit > 0) {
  26. $maxTokens = min($maxTokens, $providerLimit);
  27. }
  28. return $this->driver($provider)->complete(new AiRequest(
  29. task: $task,
  30. model: (string) $settings['model'],
  31. system: $system,
  32. prompt: $prompt,
  33. maxTokens: $maxTokens,
  34. ));
  35. }
  36. /**
  37. * Build the driver for a provider named in config/ai.php.
  38. *
  39. * @throws AiException
  40. */
  41. public function driver(string $provider): AiDriver
  42. {
  43. $config = config("ai.providers.$provider");
  44. $class = is_array($config) ? ($config['driver'] ?? null) : null;
  45. if (! is_string($class) || ! is_subclass_of($class, AiDriver::class)) {
  46. throw new AiException("AI provider [$provider] is not configured.", AiException::SETUP);
  47. }
  48. return app()->make($class, ['provider' => $provider, 'config' => $config]);
  49. }
  50. }