69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use App\Models\Setting;
|
|
|
|
trait ChatGPTHelper
|
|
{
|
|
public function setupEnvironment(): array
|
|
{
|
|
$setting = Setting::first();
|
|
$secretKey = $setting->chat_gpt_api_secret;;
|
|
return [
|
|
'secretKey' => $secretKey,
|
|
|
|
];
|
|
}
|
|
|
|
|
|
public function getGptResponse(string $question)
|
|
{
|
|
|
|
$configuration = $this->setupEnvironment();
|
|
|
|
$endpoint = "https://api.openai.com/v1/engines/text-davinci-003/completions";
|
|
$options = [
|
|
"prompt" => $question,
|
|
"max_tokens" => 100,
|
|
"stop" => "",
|
|
"temperature" => 1
|
|
];
|
|
|
|
$options = json_encode($options);
|
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt_array($curl, [
|
|
CURLOPT_URL => $endpoint,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_ENCODING => "",
|
|
CURLOPT_MAXREDIRS => 10,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
|
CURLOPT_CUSTOMREQUEST => "POST",
|
|
CURLOPT_POSTFIELDS => $options,
|
|
CURLOPT_SSL_VERIFYPEER => false, //disabling SSL verification
|
|
CURLOPT_SSL_VERIFYHOST => 0, //disabling SSL verification
|
|
CURLOPT_HTTPHEADER => [
|
|
"Content-Type: application/json",
|
|
"Content-Length: " . strlen($options),
|
|
"Authorization: Bearer " . $configuration['secretKey'],
|
|
],
|
|
]);
|
|
|
|
$response = curl_exec($curl);
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
if ($err) {
|
|
return "cURL Error #:" . $err;
|
|
} else {
|
|
$response = json_decode($response);
|
|
return $response;
|
|
}
|
|
}
|
|
|
|
}
|