Qdrant PHP Client

repository·main·Indexed 41 days ago

https://github.com/hkulekci/qdrant-php

A PHP client for Qdrant, a vector similarity engine and vector database. It enables developers to manage collections, upsert vector points, perform similarity searches, and handle cluster operations. The client provides endpoints for managing points, payloads, snapshots, and indexing, with support for complex queries and integration with OpenAI embeddings.

Tokens
8.3K
Snippets
50
Records
51
Agent score
50%

What's inside Qdrant PHP

  1. Connect to Qdrant

    main

    To connect to a Qdrant instance, initialize a Config object with the host, optionally set an API key, build a transport layer using the Builder, and instantiate the Qdrant client.

    use Qdrant\Qdrant;
    use Qdrant\Config;
    use Qdrant\Http\Builder;
    
    $config = new Config(QDRANT_HOST);
    $config->setApiKey(QDRANT_API_KEY);
    
    $transport = (new Builder())->build($config);
    $client = new Qdrant($transport);
  2. Manage points in a collection using the Points endpoint

    main

    The Points endpoint provides methods to interact with individual points or groups of points within a specific Qdrant collection. You can use it to upsert, delete, retrieve, count, and search for points.

    Key operations include:

    • Upserting: Use upsert() to insert or update points.
    • Batching: Use batch() for high-performance batch operations.
    • Retrieving: Use id() to get a single point or ids() to get multiple points by their IDs.
    • Deleting: Use delete() for specific IDs or deleteByFilter() to remove points matching a Filter.
    • Counting: Use count() to get the number of points, optionally filtered.
  3. Search using OpenAI Embeddings

    main

    To perform a semantic search, first generate an embedding for your query string using an OpenAI client, then pass that embedding into a SearchRequest via a VectorStruct.

    $openai = OpenAI::client(OPENAI_API_KEY);
    
    $query = 'lorem ipsum dolor sit amed';
    $response = $openai->embeddings()->create([
        'model' => 'text-embedding-ada-002',
        'input' => $query,
    ]);
    $embedding = array_values($response->embeddings[0]->embedding);
    
    $searchRequest = (new SearchRequest(new VectorStruct($embedding, 'content')))
        ->setLimit(10)
        ->setParams([
            'hnsw_ef' => 128,
            'exact' => false,
        ])
        ->setWithPayload(true);
    
    $response = $client->collections('contents')->points()->search($searchRequest);
    
    foreach ($response['result'] as $item) {
        echo $item['score'] . ';' . $item['payload']['id'] . ';' . $item['payload']['meta_data'] . PHP_EOL;
    }
  4. Wait for Upsert Acknowledgement

    main

    When performing an upsert operation, you can pass an options array to ensure the operation is acknowledged before the request returns. Use ['wait' => 'true'] to wait for the data to be processed.

    $client->collections('contents')->points()->upsert($points, ['wait' => 'true']);
  5. Insert Points into a Collection

    main

    To insert data, wrap your points in a PointsStruct. Each point is represented by a PointStruct containing an integer ID, a VectorStruct (mapping the embedding to a specific vector name), and an optional payload array. Use the upsert() method on the collection's points endpoint to save them.

    use Qdrant\Models\PointsStruct;
    use Qdrant\Models\PointStruct;
    use Qdrant\Models\VectorStruct;
    
    // Assuming $embedding is an array of floats from an embedding model
    $points = new PointsStruct();
    $points->addPoint(
        new PointStruct(
            (int) $imageId,
            new VectorStruct($embedding, 'content'),
            [
                'id' => 1,
                'meta' => 'Meta data'
            ]
        )
    );
    
    $client->collections('contents')->points()->upsert($points);
  6. Search for Points

    main

    Perform a vector search using a SearchRequest. You can configure the search by:

    • Providing a VectorStruct for the query vector.
    • Adding a Filter with conditions like MatchString.
    • Setting a limit for the number of results.
    • Passing additional parameters via setParams() (e.g., hnsw_ef, exact).
    • Enabling payload retrieval with setWithPayload(true).
    use Qdrant\Models\Filter\Condition\MatchString;
    use Qdrant\Models\Filter\Filter;
    use Qdrant\Models\Request\SearchRequest;
    use Qdrant\Models\VectorStruct;
    
    $searchRequest = (new SearchRequest(new VectorStruct($embedding, 'elev_pitch')))
        ->setFilter(
            (new Filter())->addMust(
                new MatchString('name', 'Palm')
            )
        )
        ->setLimit(10)
        ->setParams([
            'hnsw_ef' => 128,
            'exact' => false,
        ])
        ->setWithPayload(true);
    
    $response = $client->collections('contents')->points()->search($searchRequest);
  7. Create a Collection

    main

    Use the collections() method on the client to create a new collection. You must define VectorParams specifying the vector size and the distance metric (e.g., VectorParams::DISTANCE_COSINE).

    use Qdrant\Endpoints\Collections;
    use Qdrant\Models\Request\CreateCollection;
    use Qdrant\Models\Request\VectorParams;
    
    $createCollection = new CreateCollection();
    $createCollection->addVector(new VectorParams(1536, VectorParams::DISTANCE_COSINE), 'content');
    $response = $client->collections('contents')->create($createCollection);
  8. Configure collection optimizer settings with OptimizersConfig

    main

    Use the OptimizersConfig class to tune how Qdrant optimizes segments within a collection. This configuration allows you to control segment sizes, memory usage (memmap), indexing thresholds, and the frequency of optimization tasks.

    Key Configuration Parameters:

    • deleted_threshold (setDeletedThreshold): The minimal fraction of deleted vectors in a segment required to trigger optimization.
    • vacuum_min_vector_number (setVacuumMinVectorNumber): The minimal number of vectors in a segment required to trigger optimization.
    • default_segment_number (setDefaultSegmentNumber): The target number of segments. Setting this to 0 allows Qdrant to automatically select the number based on available CPUs. It is recommended to choose a factor of your search threads.
    • max_segment_size (setMaxSegmentSize): Limits the size of segments in KiloBytes. Lower values prioritize indexation speed; higher values prioritize search speed.
    • memmap_threshold (setMemmapThreshold): Maximum size (in KiloBytes) of vectors to store in-memory per segment. Segments exceeding this will be stored as read-only memmaped files.
    • indexing_threshold (setIndexingThreshold): Maximum size (in KiloBytes) of vectors allowed for a plain index.
    • flush_interval_sec (setFlushIntervalSec): The minimum interval between forced flushes.
    • max_optimization_threads (setMaxOptimizationThreads): The maximum number of threads available for optimization workers.

    Note on Units: For size-related parameters, 1Kb is approximately equal to 1 vector of size 256.

    use Qdrant\Models\Request\CollectionConfig\OptimizersConfig;
    
    $optimizersConfig = (new OptimizersConfig())
        ->setDeletedThreshold(0.1)
        ->setMaxSegmentSize(50000)
        ->setDefaultSegmentNumber(0)
        ->setMemmapThreshold(10000);
  9. Delete points by ID or Filter

    main

    You can remove points from a collection in two ways:

    1. By specific IDs: Use delete(array $points) passing an array of IDs.
    2. By Filter: Use deleteByFilter(Filter $filter) to remove all points that match a specific criteria.
    // Delete by IDs
    $collection->points()->delete([1, 2, 3]);
    
    // Delete by Filter
    $filter = new Filter([...]);
    $collection->points()->deleteByFilter($filter);
  10. Create a new collection

    main

    Create a new collection by passing a CreateCollection model instance to the create() method. You can optionally provide query parameters as an array.

    use Qdrant\Models\Request\CreateCollection;
    
    $params = new CreateCollection([
        // collection configuration parameters
    ]);
    
    $response = $collections->create($params);
  11. Retrieve multiple points by IDs

    main

    Use the ids() method to retrieve multiple points at once. You can control whether to include the payload or the vector in the response.

    Parameters:

    • array $ids: An array of point IDs.
    • bool $withPayload: Whether to include the payload (default false).
    • bool $withVector: Whether to include the vector (default true).
    $ids = [1, "uuid-string", 3];
    $response = $collection->points()->ids($ids, true, true);