定制 ankane/pgvector 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ankane/pgvector

最新稳定版本:v0.1.3

Composer 安装命令:

composer require ankane/pgvector

包简介

pgvector support for PHP

README 文档

README

pgvector support for PHP

Build Status

Getting Started

Follow the instructions for your database library:

Or check out some examples:

Laravel

Install the package

composer require ankane/pgvector

Enable the extension

php artisan vendor:publish --tag="pgvector-migrations"
php artisan migrate

You can now use the vector type in future migrations

Schema::create('items', function (Blueprint $table) {
    $table->vector('embedding', 3);
});

Update your model

use Pgvector\Laravel\Vector;

class Item extends Model
{
    use HasNeighbors;

    protected $casts = ['embedding' => Vector::class];
}

Insert a vector

$item = new Item();
$item->embedding = [1, 2, 3];
$item->save();

Get the nearest neighbors to a record

use Pgvector\Laravel\Distance;

$neighbors = $item->nearestNeighbors('embedding', Distance::L2)->take(5)->get();

Also supports InnerProduct and Cosine distance

Get the nearest neighbors to a vector

$neighbors = Item::query()->nearestNeighbors('embedding', [1, 2, 3], Distance::L2)->take(5)->get();

Get the distances

$neighbors->pluck('neighbor_distance');

Add an approximate index in a migration

public function up()
{
    DB::statement('CREATE INDEX my_index ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');
    // or
    DB::statement('CREATE INDEX my_index ON items USING hnsw (embedding vector_l2_ops)');
}

public function down()
{
    DB::statement('DROP INDEX my_index');
}

Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance

PHP

Enable the extension

pg_query($db, 'CREATE EXTENSION IF NOT EXISTS vector');

Create a table

pg_query($db, 'CREATE TABLE items (embedding vector(3))');

Insert a vector

use Pgvector\Vector;

$embedding = new Vector([1, 2, 3]);
pg_query_params($db, 'INSERT INTO items (embedding) VALUES ($1)', [$embedding]);

Get the nearest neighbors to a vector

$embedding = new Vector([1, 2, 3]);
$result = pg_query_params($db, 'SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5', [$embedding]);

Add an approximate index

pg_query($db, 'CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');
// or
pg_query($db, 'CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');

See a full example

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/pgvector/pgvector-php.git
cd pgvector-php
composer install
createdb pgvector_php_test
composer test

统计信息

  • 总下载量: 96.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 61
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 61
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-05