-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api): fix indexing files with api key auth (#852)
* add migration for api key auth on storage.objects * move get_user_id in crud_base to a static method for other classes to use * fix issues with database calls from index.py * add conformance testing --------- Co-authored-by: Jonathan Perry <[email protected]>
- Loading branch information
Showing
7 changed files
with
311 additions
and
124 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
packages/api/supabase/migrations/20240729193626_v0.10.0_api_keys_storage_objects.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
create policy "Individuals can CRUD storage.objects via API key." | ||
on storage.objects for all | ||
to anon | ||
using | ||
( | ||
exists ( | ||
select 1 | ||
from api_keys | ||
where api_keys.api_key_hash = crypt(current_setting('request.headers')::json->>'x-custom-api-key', api_keys.api_key_hash) | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
"""CRUD Operations for VectorStore.""" | ||
|
||
from pydantic import BaseModel | ||
from supabase import AClient as AsyncClient | ||
from leapfrogai_api.data.crud_base import get_user_id | ||
import ast | ||
|
||
|
||
class Vector(BaseModel): | ||
id: str = "" | ||
vector_store_id: str | ||
file_id: str | ||
content: str | ||
metadata: dict | ||
embedding: list[float] | ||
|
||
|
||
class CRUDVectorContent: | ||
"""CRUD Operations for VectorStore""" | ||
|
||
def __init__(self, db: AsyncClient): | ||
self.db = db | ||
self.table_name = "vector_content" | ||
|
||
async def add_vectors(self, object_: list[Vector]) -> list[Vector]: | ||
"""Create new row.""" | ||
|
||
user_id = await get_user_id(self.db) | ||
|
||
rows = [] | ||
|
||
for vector in object_: | ||
dict_ = vector.model_dump() | ||
dict_["user_id"] = user_id | ||
if "id" in dict_: | ||
del dict_["id"] | ||
|
||
rows.append(dict_) | ||
|
||
data, _count = await self.db.table(self.table_name).insert(dict_).execute() | ||
|
||
_, response = data | ||
|
||
final_response = [] | ||
try: | ||
for item in response: | ||
if "user_id" in item: | ||
del item["user_id"] | ||
if isinstance(item["embedding"], str): | ||
item["embedding"] = self.string_to_float_list(item["embedding"]) | ||
final_response.append( | ||
Vector( | ||
id=item["id"], | ||
vector_store_id=item["vector_store_id"], | ||
file_id=item["file_id"], | ||
content=item["content"], | ||
metadata=item["metadata"], | ||
embedding=item["embedding"], | ||
) | ||
) | ||
|
||
return final_response | ||
except Exception as e: | ||
raise e | ||
|
||
async def delete_vectors(self, vector_store_id: str, file_id: str) -> bool: | ||
"""Delete a vector store file by its ID.""" | ||
data, _count = ( | ||
await self.db.table(self.table_name) | ||
.delete() | ||
.eq("vector_store_id", vector_store_id) | ||
.eq("file_id", file_id) | ||
.execute() | ||
) | ||
|
||
_, response = data | ||
|
||
return bool(response) | ||
|
||
async def similarity_search(self, query: list[float], vector_store_id: str, k: int): | ||
user_id = await get_user_id(self.db) | ||
|
||
params = { | ||
"query_embedding": query, | ||
"match_limit": k, | ||
"vs_id": vector_store_id, | ||
"user_id": user_id, | ||
} | ||
|
||
return await self.db.rpc("match_vectors", params).execute() | ||
|
||
@staticmethod | ||
def string_to_float_list(s: str) -> list[float]: | ||
try: | ||
# Remove any whitespace and convert to a Python list | ||
cleaned_string = s.strip() | ||
python_list = ast.literal_eval(cleaned_string) | ||
|
||
# Convert all elements to float | ||
return [float(x) for x in python_list] | ||
except (ValueError, SyntaxError) as e: | ||
raise e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.