PHP code example of redis-ventures / redisvl

1. Go to this page and download the library: Download redis-ventures/redisvl 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/ */

    

redis-ventures / redisvl example snippets


$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'
        ],
    ],
];

use Predis\Client;
use RedisVentures\RedisVl\Index\SearchIndex;

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

// Creates index in the Redis
$index->create();

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

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

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

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

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

use RedisVentures\RedisVl\VectorHelper;

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