This commit is contained in:
ct
2025-06-13 11:35:18 +08:00
parent 7ffa3d9ca0
commit 53690753c6
42 changed files with 4638 additions and 991 deletions

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Helpers\FirstParty\AI;
use Exception;
use Illuminate\Support\Facades\Http;
use Pgvector\Laravel\Vector;
class CloudflareAI
{
public static function getVectorEmbeddingBgeSmall($embedding_query)
{
$embedding_query = substr(trim(preg_replace('/[^a-zA-Z0-9\s,\.]/', '', $embedding_query)), 0, 500);
if (empty($embedding_query)) {
throw new Exception('Empty embedding query.');
}
$maxRetries = 3;
$currentAttempt = 0;
while ($currentAttempt < $maxRetries) {
try {
// Use the new API endpoint
$response = Http::withHeaders([])->withOptions(['verify' => (app()->environment() == 'local') ? false : true])->timeout(800)
->get(
'hhttps://worker-embedding-mag.prime-42b.workers.dev',
[
'query' => $embedding_query,
]
);
if (! $response->successful()) {
throw new Exception('Embedding response failed, API error');
}
$embedding_response = json_decode($response->body(), true);
try {
return new Vector($embedding_response['response']['data'][0]);
} catch (Exception $e) {
throw new Exception('Embedding response failed, null response');
}
} catch (Exception $e) {
$currentAttempt++;
if ($currentAttempt >= $maxRetries) {
// we can throw exception here
}
// Optional: Add sleep for a few seconds if you want to delay the next attempt
// sleep(1);
}
}
}
}

View File

@@ -44,6 +44,7 @@ class MemeMedia extends Model
protected $fillable = [
'type',
'sub_type',
'original_id',
'name',
'description',
'keywords',