AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

OpenAiCompatibleDriver.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Ai\Drivers;
  3. use App\Ai\AiDriver;
  4. use App\Ai\AiException;
  5. use App\Ai\AiRequest;
  6. use App\Ai\AiResponse;
  7. use Illuminate\Http\Client\ConnectionException;
  8. use Illuminate\Support\Facades\Http;
  9. /**
  10. * Any provider that speaks the common "chat completions" format.
  11. * DeepSeek uses it, and so do many others, so a new provider is usually just a config entry.
  12. */
  13. final class OpenAiCompatibleDriver implements AiDriver
  14. {
  15. /**
  16. * @param array<string, mixed> $config
  17. */
  18. public function __construct(
  19. private readonly string $provider,
  20. private readonly array $config,
  21. ) {}
  22. public function complete(AiRequest $request): AiResponse
  23. {
  24. $apiKey = (string) ($this->config['api_key'] ?? '');
  25. $baseUrl = rtrim((string) ($this->config['base_url'] ?? ''), '/');
  26. if ($apiKey === '' || $baseUrl === '') {
  27. throw new AiException("The API key or base URL for [$this->provider] is missing in .env.", AiException::SETUP);
  28. }
  29. try {
  30. $response = Http::withToken($apiKey)
  31. ->timeout((int) config('ai.timeout', 240))
  32. ->acceptJson()
  33. ->post($baseUrl.'/chat/completions', [
  34. 'model' => $request->model,
  35. 'max_tokens' => $request->maxTokens,
  36. 'response_format' => ['type' => 'json_object'],
  37. 'messages' => [
  38. ['role' => 'system', 'content' => $request->system],
  39. ['role' => 'user', 'content' => $request->prompt],
  40. ],
  41. ]);
  42. } catch (ConnectionException $e) {
  43. throw new AiException("Could not reach [$this->provider]: ".$e->getMessage(), AiException::PROVIDER, $e);
  44. }
  45. if ($response->failed()) {
  46. throw new AiException("[$this->provider] answered with HTTP {$response->status()}: ".mb_substr($response->body(), 0, 300));
  47. }
  48. if ($response->json('choices.0.finish_reason') === 'length') {
  49. throw new AiException("[$this->provider] ran out of room before finishing its answer.", AiException::INVALID_RESULT);
  50. }
  51. return new AiResponse(
  52. text: (string) $response->json('choices.0.message.content', ''),
  53. provider: $this->provider,
  54. model: $request->model,
  55. inputTokens: (int) $response->json('usage.prompt_tokens', 0),
  56. outputTokens: (int) $response->json('usage.completion_tokens', 0),
  57. );
  58. }
  59. }