hkulekci/qdrant

Qdrant 的 PHP 客户端

v0.5.6 2024-08-21 19:21 UTC

This package is auto-updated.

Last update: 2024-09-09 09:17:22 UTC


README

Test Application codecov

此库是 Qdrant 的 PHP 客户端。

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

安装

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

composer require hkulekci/qdrant

连接到 Qdrant

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

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

您可以检查更多参数: 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('contents')->points()->search($searchRequest);

使用 OpenAI 嵌入在点上搜索

$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;
}