29 lines
745 B
PHP
29 lines
745 B
PHP
<?php
|
|
|
|
namespace App\Domains\Ai\Services;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class AiService
|
|
{
|
|
public function complete(string $prompt, string $model = 'gpt-4o-mini'): string
|
|
{
|
|
$apiKey = config('services.openai.api_key');
|
|
|
|
if (! $apiKey) {
|
|
return '';
|
|
}
|
|
|
|
$response = Http::withHeaders([
|
|
'Authorization' => 'Bearer ' . $apiKey,
|
|
'Content-Type' => 'application/json',
|
|
])->post('https://api.openai.com/v1/chat/completions', [
|
|
'model' => $model,
|
|
'max_tokens' => 800,
|
|
'messages' => [['role' => 'user', 'content' => $prompt]],
|
|
]);
|
|
|
|
return trim($response->json('choices.0.message.content') ?? '');
|
|
}
|
|
}
|