MagicAI.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Webkul\MagicAI;
  3. use Webkul\MagicAI\Services\Gemini;
  4. use Webkul\MagicAI\Services\GroqAI;
  5. use Webkul\MagicAI\Services\Ollama;
  6. use Webkul\MagicAI\Services\OpenAI;
  7. class MagicAI
  8. {
  9. /**
  10. * LLM model.
  11. */
  12. protected string $model;
  13. /**
  14. * LLM agent.
  15. */
  16. protected string $agent;
  17. /**
  18. * Stream Response.
  19. */
  20. protected bool $stream = false;
  21. /**
  22. * Raw Response.
  23. */
  24. protected bool $raw = true;
  25. /**
  26. * Raw Response.
  27. */
  28. protected float $temperature = 0.7;
  29. /**
  30. * LLM prompt text.
  31. */
  32. protected string $prompt;
  33. /**
  34. * Set LLM model
  35. */
  36. public function setModel(string $model): self
  37. {
  38. $this->model = $model;
  39. return $this;
  40. }
  41. /**
  42. * Set LLM agent
  43. */
  44. public function setAgent(string $agent): self
  45. {
  46. $this->agent = $agent;
  47. return $this;
  48. }
  49. /**
  50. * Set stream response.
  51. */
  52. public function setStream(bool $stream): self
  53. {
  54. $this->stream = $stream;
  55. return $this;
  56. }
  57. /**
  58. * Set response raw.
  59. */
  60. public function setRaw(bool $raw): self
  61. {
  62. $this->raw = $raw;
  63. return $this;
  64. }
  65. /**
  66. * Set LLM prompt text.
  67. */
  68. public function setTemperature(float $temperature): self
  69. {
  70. $this->temperature = $temperature;
  71. return $this;
  72. }
  73. /**
  74. * Set LLM prompt text.
  75. */
  76. public function setPrompt(string $prompt): self
  77. {
  78. $this->prompt = $prompt;
  79. return $this;
  80. }
  81. /**
  82. * Set LLM prompt text.
  83. */
  84. public function ask(): string
  85. {
  86. return $this->getModelInstance()->ask();
  87. }
  88. /**
  89. * Generate Images
  90. */
  91. public function images(array $options): array
  92. {
  93. return $this->getModelInstance()->images($options);
  94. }
  95. /**
  96. * Get LLM model instance.
  97. */
  98. public function getModelInstance(): OpenAI|Ollama|Gemini|GroqAI
  99. {
  100. if (in_array($this->model, ['gpt-4-turbo', 'gpt-4o', 'gpt-4o-mini', 'dall-e-2', 'dall-e-3'])) {
  101. return new OpenAI(
  102. $this->model,
  103. $this->prompt,
  104. $this->temperature,
  105. $this->stream,
  106. );
  107. }
  108. if (in_array($this->model, ['llama3-8b-8192'])) {
  109. return new GroqAI(
  110. $this->model,
  111. $this->prompt,
  112. $this->temperature,
  113. $this->stream,
  114. );
  115. }
  116. if (in_array($this->model, ['gemini-2.0-flash'])) {
  117. return new Gemini(
  118. $this->model,
  119. $this->prompt,
  120. $this->stream,
  121. $this->raw,
  122. );
  123. }
  124. return new Ollama(
  125. $this->model,
  126. $this->prompt,
  127. $this->temperature,
  128. $this->stream,
  129. $this->raw,
  130. );
  131. }
  132. }