|
1 | 1 | import sqlite3 |
2 | 2 | from importlib import resources |
3 | 3 |
|
| 4 | +import asyncpg |
4 | 5 | import numpy as np |
5 | 6 | import sqlite_vec |
| 7 | +from pgvector.asyncpg import register_vector |
6 | 8 |
|
7 | 9 |
|
8 | 10 | class Database: |
@@ -106,11 +108,65 @@ def search_function(self, embedding, top_n=25): |
106 | 108 |
|
107 | 109 |
|
108 | 110 | class PostgreSQLDatabase: |
109 | | - def __init__(self): |
110 | | - raise TypeError |
| 111 | + def __init__(self, connection): |
| 112 | + self.connection = connection |
111 | 113 |
|
112 | | - def add_function(self, name, cfg, embedding, binary_name, binary_sha256, model_identifier=None): |
113 | | - raise NotImplementedError |
| 114 | + @classmethod |
| 115 | + async def connect(cls, **kwargs): |
| 116 | + connection = await asyncpg.connect(**kwargs) |
| 117 | + await connection.execute('CREATE EXTENSION IF NOT EXISTS vector') |
| 118 | + await register_vector(connection) |
| 119 | + await connection.execute(resources.read_text('citatio', 'schema-postgresql.sql')) |
| 120 | + return cls(connection) |
114 | 121 |
|
115 | | - def search_function(self, embedding, top_n=25): |
116 | | - raise NotImplementedError |
| 122 | + async def _insert_or_get_function(self, cfg, embedding): |
| 123 | + cfg = str(cfg) |
| 124 | + try: |
| 125 | + return await self.connection.fetchval( |
| 126 | + 'INSERT INTO functions (cfg, embedding) VALUES ($1, $2) RETURNING id', |
| 127 | + cfg, |
| 128 | + embedding, |
| 129 | + ) |
| 130 | + except asyncpg.IntegrityConstraintViolationError: |
| 131 | + return await self.connection.fetchval('SELECT id FROM functions WHERE cfg = $1', cfg) |
| 132 | + |
| 133 | + async def _insert_label(self, function_id, name, binary_name, binary_sha256): |
| 134 | + await self.connection.execute( |
| 135 | + 'INSERT INTO labels (function_id, label, binary_name, binary_sha256) VALUES ($1, $2, $3, $4)', |
| 136 | + function_id, |
| 137 | + name, |
| 138 | + binary_name, |
| 139 | + binary_sha256, |
| 140 | + ) |
| 141 | + |
| 142 | + async def add_function(self, name, cfg, embedding, binary_name, binary_sha256, model_identifier=None): |
| 143 | + function_id = await self._insert_or_get_function(cfg, embedding) |
| 144 | + await self._insert_label(function_id, name, binary_name, binary_sha256) |
| 145 | + return function_id |
| 146 | + |
| 147 | + async def _search_near(self, embedding, top_n): |
| 148 | + return await self.connection.fetch( |
| 149 | + """ |
| 150 | + SELECT label, (2 - (embedding <-> $1)) / 2 AS similarity, binary_name, binary_sha256 |
| 151 | + FROM labels |
| 152 | + JOIN functions ON labels.function_id = functions.id |
| 153 | + ORDER BY similarity DESC |
| 154 | + LIMIT $2 |
| 155 | + """, |
| 156 | + embedding, |
| 157 | + top_n, |
| 158 | + ) |
| 159 | + |
| 160 | + async def search_function(self, embedding, top_n=25): |
| 161 | + results = await self._search_near(embedding, top_n) |
| 162 | + |
| 163 | + return [ |
| 164 | + dict( |
| 165 | + zip( |
| 166 | + ['function', 'similarity', 'binary_name', 'binary_sha256'], |
| 167 | + result, |
| 168 | + strict=True, |
| 169 | + ) |
| 170 | + ) |
| 171 | + for result in results |
| 172 | + ] |
0 commit comments