This commit is contained in:
ct
2025-06-20 20:16:34 +08:00
parent b502120091
commit 8f6fb3787a
14 changed files with 493 additions and 22 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Helpers\FirstParty\AI;
use App\Models\KeywordEmbedding;
use Exception;
use Illuminate\Support\Facades\Http;
use Pgvector\Laravel\Vector;
@@ -16,10 +17,18 @@ public static function getVectorEmbeddingBgeSmall($embedding_query)
throw new Exception('Empty embedding query.');
}
$keyword_embedding = KeywordEmbedding::where('keyword', $embedding_query)->first();
if (! is_null($keyword_embedding)) {
return $keyword_embedding->embedding;
}
$maxRetries = 3;
$currentAttempt = 0;
while ($currentAttempt < $maxRetries) {
$embedding = null;
while ($currentAttempt < $maxRetries && $embedding === null) {
try {
// Use the new API endpoint
$response = Http::withHeaders([])->withOptions(['verify' => (app()->environment() == 'local') ? false : true])->timeout(800)
@@ -37,7 +46,17 @@ public static function getVectorEmbeddingBgeSmall($embedding_query)
$embedding_response = json_decode($response->body(), true);
try {
return new Vector($embedding_response['response']['data'][0]);
$embedding = new Vector($embedding_response['response']['data'][0]);
// dump($embedding);
// dump($embedding_query);
KeywordEmbedding::create([
'keyword' => $embedding_query,
'embedding' => $embedding
]);
break;
} catch (Exception $e) {
throw new Exception('Embedding response failed, null response');
}