Redis Vector Library (RedisVL) 基于Predis PHP客户端,将 Redis 作为 LLM 应用程序的实时数据库

v0.2.3 2024-02-29 15:33 UTC

README

The Redis Vector Library (RedisVL) 是一个利用 Redis 的 AI 应用程序 PHP 客户端。

设计用于

  • 向量相似度搜索
  • 推荐引擎

适用于基于 Redis 的应用程序的完美工具,集成了基于向量的语义搜索、全文搜索和地理空间搜索等功能。

入门

安装

composer install redis-ventures/redisvl

设置 Redis

从多种 Redis 部署选项中选择

  1. Redis Cloud:托管云数据库(免费层可用)
  2. Redis Stack:开发 Docker 镜像
docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
  1. Redis Enterprise:商业、自托管数据库

包含内容

Redis 索引管理

  1. 使用可用的 Redis 数据结构(HASH、JSON)和可索引字段(例如文本、标签、数值、地理和向量)之一设计您的模式,以建模您的数据集。

将模式作为字典加载

$schema = [
    'index' => [
        'name' => 'products',
        'prefix' => 'product:',
        'storage_type' => 'hash',
    ],
    'fields' => [
        'id' => [
            'type' => 'numeric',
        ],
        'categories' => [
            'type' => 'tag',
        ],
        'description' => [
            'type' => 'text',
        ],
        'description_embedding' => [
             'type' => 'vector',
             'dims' => 3,
             'datatype' => 'float32',
             'algorithm' => 'flat',
             'distance_metric' => 'cosine'
        ],
    ],
];
  1. 使用输入模式和客户端连接创建 SearchIndex 对象,以便能够与您的 Redis 索引交互
use Predis\Client;
use RedisVentures\RedisVl\Index\SearchIndex;

$client = new Client();
$index = new SearchIndex($client, $schema);

// Creates index in the Redis
$index->create();
  1. 从索引中加载/检索您的数据。如果您有哈希索引,数据应作为键值对加载,对于 json 类型数据,则加载为 json 字符串。
$data = ['id' => '1', 'count' => 10, 'id_embeddings' => VectorHelper::toBytes([0.000001, 0.000002, 0.000003])];

// Loads given dataset associated with given key.
$index->load('key', $data);

// Fetch dataset corresponding to given key
$index->fetch('key');

实时搜索

定义查询并在索引上执行高级搜索,包括向量和各种过滤器的组合。

VectorQuery - 具有自定义过滤器的灵活向量相似度语义搜索

use RedisVentures\RedisVl\Query\VectorQuery;

$query = new VectorQuery(
    [0.001, 0.002, 0.03],
    'description_embedding',
    null,
    3
);

// Run vector search against vector field specified in schema.
$results = $index->query($query);

在查询中包含复杂的元数据过滤器

use RedisVentures\RedisVl\Query\Filter\TagFilter;
use RedisVentures\RedisVl\Enum\Condition;

$filter = new TagFilter(
    'categories',
    Condition::equal,
    'foo'
);

$query = new VectorQuery(
    [0.001, 0.002, 0.03],
    'description_embedding',
    null,
    10,
    true,
    2,
    $filter
);

// Results will be filtered by tag field values.
$results = $index->query($query);

过滤类型

数值

数值过滤器可以应用于数值字段。支持适用于标量类型的各种条件(==, !=, <, >, <=, >=)。更多信息 这里

use RedisVentures\RedisVl\Query\Filter\NumericFilter;
use RedisVentures\RedisVl\Enum\Condition;

$equal = new NumericFilter('numeric', Condition::equal, 10);
$notEqual = new NumericFilter('numeric', Condition::notEqual, 10);
$greaterThan = new NumericFilter('numeric', Condition::greaterThan, 10);
$greaterThanOrEqual = new NumericFilter('numeric', Condition::greaterThanOrEqual, 10);
$lowerThan = new NumericFilter('numeric', Condition::lowerThan, 10);
$lowerThanOrEqual = new NumericFilter('numeric', Condition::lowerThanOrEqual, 10);

标签

标签过滤器可以应用于标签字段。可以提供单个或多个值,单个值仅支持相等条件(==, !==),对于多个标签,可以指定额外的合取(AND、OR)。更多信息 这里

use RedisVentures\RedisVl\Query\Filter\TagFilter;
use RedisVentures\RedisVl\Enum\Condition;
use RedisVentures\RedisVl\Enum\Logical;

$singleTag = new TagFilter('tag', Condition::equal, 'value')
$multipleTags = new TagFilter('tag', Condition::notEqual, [
    'conjunction' => Logical::or,
    'tags' => ['value1', 'value2']
])

文本

文本过滤器可以应用于文本字段。值可以是单个词或多个词,并指定条件。空值对应于所有值(*)。更多信息 这里

use RedisVentures\RedisVl\Query\Filter\TextFilter;
use RedisVentures\RedisVl\Enum\Condition;

$single = new TextFilter('text', Condition::equal, 'foo');

// Matching foo AND bar
$multipleAnd = new TextFilter('text', Condition::equal, 'foo bar');

// Matching foo OR bar
$multipleOr = new TextFilter('text', Condition::equal, 'foo|bar');

// Perform fuzzy search
$fuzzy = new TextFilter('text', Condition::equal, '%foobaz%');

地理

地理过滤器可以应用于地理字段。仅支持相等条件,值应指定为特定形状数组。更多信息 这里

use RedisVentures\RedisVl\Query\Filter\GeoFilter;
use RedisVentures\RedisVl\Enum\Condition;
use RedisVentures\RedisVl\Enum\Unit;

$geo = new GeoFilter('geo', Condition::equal, [
    'lon' => 10.111,
    'lat' => 11.111,
    'radius' => 100,
    'unit' => Unit::kilometers
]);

聚合

要向单个查询应用多个过滤器,请使用 AggregateFilter。如果您要对每个过滤器应用相同的逻辑运算符,则可以在构造函数中传递值,
如果您需要特定的组合,请使用 and()or() 方法创建组合过滤器。

use RedisVentures\RedisVl\Query\Filter\AggregateFilter;
use RedisVentures\RedisVl\Query\Filter\TextFilter;
use RedisVentures\RedisVl\Query\Filter\NumericFilter;
use RedisVentures\RedisVl\Enum\Condition;
use RedisVentures\RedisVl\Enum\Logical;

$aggregate = new AggregateFilter([
    new TextFilter('text', Condition::equal, 'value'),
    new NumericFilter('numeric', Condition::greaterThan, 10)
], Logical::or);

$combinedAggregate = new AggregateFilter();
$combinedAggregate
    ->and(
        new TextFilter('text', Condition::equal, 'value'),
        new NumericFilter('numeric', Condition::greaterThan, 10)
    )->or(
        new NumericFilter('numeric', Condition::lowerThan, 100)
    );

向量器

为了能够有效地为索引数据或查询创建向量表示,您必须使用 LLM。有多种向量器提供与流行嵌入模型的集成。

唯一必需的选项是您指定的 API 密钥,该密钥作为环境变量或配置选项。

OpenAI

use RedisVentures\RedisVl\Vectorizer\Factory;

putenv('OPENAI_API_TOKEN=your_token');

$factory = new Factory();
$vectorizer = $factory->createVectorizer('openai');

// Creates vector representation of given text.
$embedding = $vectorizer->embed('your_text')

// Creates a single vector representation from multiple chunks.
$mergedEmbedding = $vectorizer->batchEmbed(['first_chunk', 'second_chunk']);

VectorHelper

当您对Redis执行向量查询或将包含向量字段数据的哈希数据加载到索引中时,您的向量应以blob字符串的形式表示。VectorHelper 允许您从表示为浮点数数组的向量创建blob表示形式。

use RedisVentures\RedisVl\VectorHelper;

$blobVector = VectorHelper::toBytes([0.001, 0.002, 0.003]);