nimuli/app/Domains/Ai/Services/AiService.php

30 lines
763 B
PHP

<?php
namespace App\Domains\Ai\Services;
use Illuminate\Support\Facades\Http;
class AiService
{
public function complete(string $prompt, string $model = 'claude-sonnet-4-6'): string
{
$apiKey = config('services.anthropic.api_key');
if (! $apiKey) {
return '';
}
$response = Http::withHeaders([
'x-api-key' => $apiKey,
'anthropic-version' => '2023-06-01',
'content-type' => 'application/json',
])->post('https://api.anthropic.com/v1/messages', [
'model' => $model,
'max_tokens' => 256,
'messages' => [['role' => 'user', 'content' => $prompt]],
]);
return trim($response->json('content.0.text') ?? '');
}
}