PHP code example of hkulekci / qdrant

1. Go to this page and download the library: Download hkulekci/qdrant library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hkulekci / qdrant example snippets




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);

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);

use Qdrant\Models\PointsStruct;
use Qdrant\Models\PointStruct;
use Qdrant\Models\VectorStruct;

$openai = OpenAI::client(OPENAI_API_KEY);

$query = 'sustainable agricultural startups';
$response = $openai->embeddings()->create([
    'model' => 'text-embedding-ada-002',
    'input' => $query,
]);
$embedding = array_values($response->embeddings[0]->embedding);

$points = new PointsStruct();
$points->addPoint(
    new PointStruct(
        (int) $imageId,
        new VectorStruct($embedding, 'content'),
        [
            'id' => 1,
            'meta' => 'Meta data'
        ]
    )
);
$client->collections('contents')->points()->upsert($points);

$client->collections('contents')->points()->upsert($points, ['wait' => 'true']);

use Qdrant\Models\Filter\Condition\MatchString;
use Qdrant\Models\Filter\Filter;
use Qdrant\Models\Request\Points\QueryRequest;

$request = (new QueryRequest())
    ->setQuery(['nearest' => $embedding])
    ->setUsing('content')
    ->setFilter((new Filter())->addMust(new MatchString('name', 'Palm')))
    ->setLimit(10)
    ->setParams(['hnsw_ef' => 128, 'exact' => false])
    ->setWithPayload(true);

$response = $client->collections('contents')->points()->query()->query($request);

foreach ($response['result']['points'] as $item) {
    echo $item['score'] . ';' . $item['payload']['id'] . PHP_EOL;
}

$request = (new QueryRequest())
    ->setPrefetch([
        ['query' => $denseEmbedding, 'using' => 'dense', 'limit' => 50],
        ['query' => ['indices' => [1, 42], 'values' => [0.3, 0.7]], 'using' => 'keywords', 'limit' => 50],
    ])
    ->setQuery(['rrf' => ['k' => 60, 'weights' => [1.0, 0.5]]])
    ->setLimit(10);

use Qdrant\Models\Request\CollectionConfig\Memory;
use Qdrant\Models\Request\CollectionConfig\TurboQuantization;

$createCollection = (new CreateCollection())
    ->addVector(new VectorParams(1536, VectorParams::DISTANCE_COSINE), 'content')
    ->setQuantizationConfig(new TurboQuantization(TurboQuantization::BITS_4, Memory::PINNED));

$createCollection->addVector(
    (new VectorParams(1536, VectorParams::DISTANCE_COSINE))->setDatatype(VectorParams::DATATYPE_TURBO4),
    'content'
);

use Qdrant\Models\Request\CollectionConfig\HnswConfig;

$createCollection = (new CreateCollection())
    ->addVector((new VectorParams(1536, VectorParams::DISTANCE_COSINE))->setMemory(Memory::COLD), 'content')
    ->setHnswConfig((new HnswConfig())->setMemory(Memory::CACHED))
    ->setQuantizationConfig(new TurboQuantization(memory: Memory::PINNED))
    ->setPayloadMemory(Memory::COLD);

use Qdrant\Models\Request\CreateVector;
use Qdrant\Models\Request\SparseVectorParams;

$createCollection->addSparseVector('keywords', (new SparseVectorParams())->setModifier(SparseVectorParams::MODIFIER_IDF));

// Add or remove named vectors on an existing collection (Qdrant 1.18+)
$client->collections('contents')->vectors()->create('summary', CreateVector::dense(384, VectorParams::DISTANCE_COSINE));
$client->collections('contents')->vectors()->create('bm25', CreateVector::sparse(SparseVectorParams::MODIFIER_IDF));
$client->collections('contents')->vectors()->delete('summary');

use Qdrant\Models\Filter\Condition\MatchInt;
use Qdrant\Models\Request\UpdateMode;

// Only insert points that do not exist yet (Qdrant 1.17+)
$client->collections('contents')->points()->upsert($points, ['wait' => 'true'], UpdateMode::INSERT_ONLY);

// Only update existing points that match the filter (Qdrant 1.16+)
$client->collections('contents')->points()->upsert(
    $points,
    updateFilter: (new Filter())->addMust(new MatchInt('version', 1))
);

use Qdrant\Models\Filter\Condition\MatchPrefix;
use Qdrant\Models\Filter\Condition\Slice;
use Qdrant\Models\Request\CreateIndex;

$client->collections('contents')->index()->create(new CreateIndex('category', ['type' => 'keyword', 'prefix' => true]));

$filter = (new Filter())
    ->addMust(new MatchPrefix('category', 'elec'))
    ->addMust(new Slice(0, 4)); // first of 4 slices

use Qdrant\Models\Request\QuotaConfig;

$client->collections('contents')->optimizations(['with' => 'queued,completed']); // optimization progress (1.17+)
$client->collections('contents')->shards()->list();                                // shard keys (1.17+)
$client->cluster()->telemetry();                                                   // cluster-wide telemetry (1.17+)

// Global resource quotas (1.19+)
$client->quotas()->get();
$client->quotas()->update((new QuotaConfig())->setMaxDiskUsagePercent(90));