-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathOpenAIPostgreSQLSchemaManager.php
83 lines (68 loc) · 2.18 KB
/
OpenAIPostgreSQLSchemaManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace Schranz\Search\SEAL\Adapter\OpenAIPostgreSQL;
use OpenAI\Client;
use Schranz\Search\SEAL\Adapter\SchemaManagerInterface;
use Schranz\Search\SEAL\Schema\Index;
use Schranz\Search\SEAL\Task\SyncTask;
use Schranz\Search\SEAL\Task\TaskInterface;
final class OpenAIPostgreSQLSchemaManager implements SchemaManagerInterface
{
public function __construct(
private readonly Client $openAiClient,
private readonly \PDO $pdoClient,
) {
}
public function existIndex(Index $index): bool
{
$statement = $this->pdoClient->query(
<<<SQL
SELECT EXISTS (
SELECT FROM
pg_tables
WHERE
schemaname = 'public' AND
tablename = '{$index->name}'
);
SQL
);
/** @var bool $exists */
$exists = $statement->fetchColumn();
return $exists;
}
public function dropIndex(Index $index, array $options = []): ?TaskInterface
{
$this->pdoClient->exec(
<<<SQL
DROP TABLE {$index->name}
SQL
);
if (true !== ($options['return_slow_promise_result'] ?? false)) {
return null;
}
return new SyncTask(null);
}
public function createIndex(Index $index, array $options = []): ?TaskInterface
{
$this->pdoClient->exec(
<<<SQL
CREATE EXTENSION IF NOT EXISTS vector;
SQL
);
$this->pdoClient->exec(
<<<SQL
CREATE TABLE IF NOT EXISTS {$index->name} (
identifier VARCHAR(48) PRIMARY KEY,
document JSONB,
embedding vector(1536)
);
SQL
); // OpenAI's text-embedding-ada-002 model outputs 1536 dimensions, so we will use that for our vector size.
// TODO optimize index: https://github.com/pgvector/pgvector/tree/v0.4.1#indexing
// TODO make a filterable columns
if (true !== ($options['return_slow_promise_result'] ?? false)) {
return null;
}
return new SyncTask(null);
}
}