Build Laravel 13 ticket assistant with Docker, Livewire admin, and helpdesk scraper command
This commit is contained in:
47
app/Services/EmbeddingService.php
Normal file
47
app/Services/EmbeddingService.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\OllamaUnavailableException;
|
||||
use App\Models\EmbeddingCache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Throwable;
|
||||
|
||||
class EmbeddingService
|
||||
{
|
||||
public function embed(string $text): array
|
||||
{
|
||||
$hash = hash('sha256', $text);
|
||||
$cached = EmbeddingCache::query()->where('text_hash', $hash)->first();
|
||||
|
||||
if ($cached !== null) {
|
||||
return $cached->embedding;
|
||||
}
|
||||
|
||||
$baseUrl = rtrim((string) config('services.ollama.base_url'), '/');
|
||||
|
||||
try {
|
||||
$response = Http::timeout((int) config('services.ollama.timeout', 30))
|
||||
->post($baseUrl.'/api/embeddings', [
|
||||
'model' => config('services.ollama.embed_model', 'nomic-embed-text'),
|
||||
'prompt' => $text,
|
||||
])
|
||||
->throw()
|
||||
->json();
|
||||
} catch (Throwable $e) {
|
||||
throw new OllamaUnavailableException('Ollama embedding endpoint is unavailable', 0, $e);
|
||||
}
|
||||
|
||||
$embedding = $response['embedding'] ?? [];
|
||||
if (!is_array($embedding) || $embedding === []) {
|
||||
throw new OllamaUnavailableException('Ollama embedding response did not include a valid embedding');
|
||||
}
|
||||
|
||||
EmbeddingCache::query()->updateOrCreate(
|
||||
['text_hash' => $hash],
|
||||
['text' => $text, 'embedding' => $embedding]
|
||||
);
|
||||
|
||||
return $embedding;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user