Files
aibuddytool/app/Helpers/FirstParty/Aictio/Aictio.php
2023-11-26 18:56:40 +08:00

55 lines
1.7 KiB
PHP

<?php
namespace App\Helpers\FirstParty\Aictio;
use Exception;
use Http;
use Pgvector\Laravel\Vector;
class Aictio
{
public static function getVectorEmbedding($embedding_query)
{
$embedding_query = trim(preg_replace('/[^a-zA-Z0-9\s,\.]/', '', $embedding_query));
if (is_empty($embedding_query)) {
throw new Exception('Empty embedding query.');
}
$maxRetries = 3;
$currentAttempt = 0;
while ($currentAttempt < $maxRetries) {
try {
$response = Http::withHeaders([
'CtioApp' => 'hireblitz260823',
])->withOptions(['verify' => (app()->environment() == 'local') ? false : false])->timeout(800)
->get('https://aictio.applikuapp.com/api/embeddings',
[
'input' => $embedding_query,
]
);
$embedding_response = json_decode($response->body(), true);
if (is_null($embedding_response)) {
throw new Exception('Embedding response failed, null response');
}
if (isset($embedding_response['error'])) {
throw new Exception($embedding_response['error']);
}
return new Vector(array_values($embedding_response)[0]);
} catch (Exception $e) {
$currentAttempt++;
if ($currentAttempt >= $maxRetries) {
throw $e;
}
// Optional: Add sleep for a few seconds if you want to delay the next attempt
// sleep(1);
}
}
}
}