AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ClaudeDriver.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Ai\Drivers;
  3. use Anthropic\Client;
  4. use Anthropic\Core\Exceptions\AnthropicException;
  5. use Anthropic\Messages\TextBlock;
  6. use App\Ai\AiDriver;
  7. use App\Ai\AiException;
  8. use App\Ai\AiRequest;
  9. use App\Ai\AiResponse;
  10. use Psr\Http\Client\ClientInterface;
  11. /**
  12. * Claude, through Anthropic's official PHP SDK.
  13. */
  14. final class ClaudeDriver implements AiDriver
  15. {
  16. /**
  17. * @param array<string, mixed> $config
  18. * @param ClientInterface|null $transporter Only set in tests, to avoid real network calls.
  19. */
  20. public function __construct(
  21. private readonly string $provider,
  22. private readonly array $config,
  23. private readonly ?ClientInterface $transporter = null,
  24. ) {}
  25. public function complete(AiRequest $request): AiResponse
  26. {
  27. $apiKey = (string) ($this->config['api_key'] ?? '');
  28. if ($apiKey === '') {
  29. throw new AiException('ANTHROPIC_API_KEY is missing in .env.', AiException::SETUP);
  30. }
  31. $options = ['timeout' => (float) config('ai.timeout', 240)];
  32. if ($this->transporter !== null) {
  33. $options['transporter'] = $this->transporter;
  34. $options['maxRetries'] = 0;
  35. }
  36. $client = new Client(apiKey: $apiKey, requestOptions: $options);
  37. try {
  38. $message = $client->messages->create(
  39. model: $request->model,
  40. maxTokens: $request->maxTokens,
  41. system: $request->system,
  42. messages: [['role' => 'user', 'content' => $request->prompt]],
  43. thinking: ($this->config['thinking'] ?? true) ? ['type' => 'adaptive'] : null,
  44. );
  45. } catch (AnthropicException $e) {
  46. throw new AiException('Claude request failed: '.$e->getMessage(), AiException::PROVIDER, $e);
  47. }
  48. if ($message->stopReason === 'refusal') {
  49. throw new AiException('Claude declined this request.', AiException::PROVIDER);
  50. }
  51. if ($message->stopReason === 'max_tokens') {
  52. throw new AiException('Claude ran out of room before finishing its answer.', AiException::INVALID_RESULT);
  53. }
  54. $text = '';
  55. // The answer can hold several blocks (thinking, text). Only text blocks are the answer.
  56. foreach ($message->content as $block) {
  57. if ($block instanceof TextBlock) {
  58. $text .= $block->text;
  59. }
  60. }
  61. return new AiResponse(
  62. text: $text,
  63. provider: $this->provider,
  64. model: $request->model,
  65. inputTokens: (int) $message->usage->inputTokens,
  66. outputTokens: (int) $message->usage->outputTokens,
  67. );
  68. }
  69. }