implemeted step 13 by Gemini
- added configuracion .env - added test script for DAI API client
This commit is contained in:
72
src/Services/DAIClient.php
Normal file
72
src/Services/DAIClient.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Exception;
|
||||
|
||||
class DAIClient
|
||||
{
|
||||
private string $apiUrl;
|
||||
private int $timeout = 60;
|
||||
|
||||
public function __construct(?string $apiUrl = null)
|
||||
{
|
||||
if ($apiUrl !== null) {
|
||||
$this->apiUrl = $apiUrl;
|
||||
} else {
|
||||
$this->apiUrl = \App\Utils\Config::get('DAIAPI_URL', 'http://192.168.122.10:9001/run');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a prompt to the DAIAPI and returns the raw answer string.
|
||||
*/
|
||||
public function sendRequest(string $prompt): ?string
|
||||
{
|
||||
$payload = json_encode([
|
||||
'prompt' => $prompt
|
||||
]);
|
||||
|
||||
if ($payload === false) {
|
||||
error_log("DAIClient Error: Failed to encode JSON payload.");
|
||||
return null;
|
||||
}
|
||||
|
||||
$ch = curl_init($this->apiUrl);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($payload)
|
||||
],
|
||||
CURLOPT_POSTFIELDS => $payload,
|
||||
CURLOPT_TIMEOUT => $this->timeout,
|
||||
]);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$error = curl_error($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($response === false) {
|
||||
error_log("DAIClient Error: cURL error: $error");
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
error_log("DAIClient Error: API returned HTTP code $httpCode. Response: $response");
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = json_decode($response, true);
|
||||
if (!is_array($data) || empty($data['success']) || !isset($data['answer'])) {
|
||||
error_log("DAIClient Error: Invalid API response format: " . $response);
|
||||
return null;
|
||||
}
|
||||
|
||||
return $data['answer'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user