zwell/qdrant

Qdrant 的 PHP 客户端

1.0.5 2024-09-02 09:11 UTC

This package is auto-updated.

Last update: 2024-10-02 09:23:15 UTC


README

Test Application codecov

此库是 Qdrant 的 PHP 客户端。

Qdrant 是一个向量相似度引擎和向量数据库。它作为 API 服务提供对最近的高维向量的搜索。使用 Qdrant,可以将嵌入或神经网络编码器转换为完整的匹配、搜索、推荐等应用!

安装

您可以使用 composer 在您的 PHP 项目中安装客户端

composer require zwell/qdrant

创建集合的示例

use Qdrant\Endpoints\Collections;
use Qdrant\Http\GuzzleClient;
use Qdrant\Models\Request\CreateCollection;
use Qdrant\Models\Request\VectorParams;

include __DIR__ . "/../vendor/autoload.php";
include_once 'config.php';

$config = new \Qdrant\Config(QDRANT_HOST);
$config->setApiKey(QDRANT_API_KEY);

$client = new Qdrant(new GuzzleClient($config));

$createCollection = new CreateCollection();
$createCollection->addVector(new VectorParams(1024, VectorParams::DISTANCE_COSINE), 'image');
$response = $client->collections('images')->create($createCollection);

现在,我们可以插入一个点

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

$points = new PointsStruct();
$points->addPoint(
    new PointStruct(
        (int) $imageId,
        new VectorStruct($data['embeddings'][0], 'image'),
        [
            'id' => 1,
            'meta' => 'Meta data'
        ]
    )
);
$client->collections('images')->points()->upsert($points);

在上传数据时,如果您想等待上传实际发生,可以使用查询参数

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

您可以查看更多参数: https://qdrant.github.io/qdrant/redoc/index.html#tag/points/operation/upsert_points

使用过滤器进行搜索

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('images')->points()->search($searchRequest);