This commit is contained in:
2023-11-26 18:56:40 +08:00
parent be14f5fdb1
commit 64431e7a73
144 changed files with 497072 additions and 3730 deletions

View File

@@ -0,0 +1,54 @@
<?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);
}
}
}
}