VectorSearchService.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 2

Klassen 1

Funktionen 4

Verwendet von 3

Versionen 1

Code

<?php

declare(strict_types=1);

namespace Infrastructure\AI;

// @responsibility: Einheitliche Schnittstelle für semantische Suche

class VectorSearchService
{
    private OllamaService $ollama;
    private QdrantService $qdrant;

    public function __construct(?OllamaService $ollama = null, ?QdrantService $qdrant = null)
    {
        $this->ollama = $ollama ?? new OllamaService();
        $this->qdrant = $qdrant ?? new QdrantService();
    }

    /**
     * Perform semantic search using embedding + vector search
     *
     * @param string $query Search query text
     * @param string $collection Qdrant collection name
     * @param int $limit Maximum results to return
     * @param string $embeddingModel Ollama model for embeddings
     * @return array Search results with scores and payloads
     */
    public function search(
        string $query,
        string $collection = 'documents',
        int $limit = 10,
        string $embeddingModel = 'mxbai-embed-large'
    ): array {
        // Generate embedding for query
        $embedding = $this->ollama->getEmbedding($query, $embeddingModel);

        if (empty($embedding)) {
            return [];
        }

        // Search in Qdrant
        return $this->qdrant->search($embedding, $collection, $limit);
    }

    /**
     * Check if both services are available
     */
    public function isAvailable(): bool
    {
        return $this->ollama->isAvailable() && $this->qdrant->isAvailable();
    }

    /**
     * Get embedding for text (useful for debugging/testing)
     */
    public function getEmbedding(string $text, string $model = 'mxbai-embed-large'): array
    {
        return $this->ollama->getEmbedding($text, $model);
    }
}
← Übersicht Graph