-
Notifications
You must be signed in to change notification settings - Fork 18
/
store.py
50 lines (38 loc) · 1.59 KB
/
store.py
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
import asyncio
import time
from typing import Any, Optional
from langchain_community.vectorstores.pgvector import PGVector
from langchain_core.documents import Document
from langchain_core.runnables.config import run_in_executor
from sqlalchemy.orm import Session
class ExtendedPgVector(PGVector):
def get_all_ids(self) -> list[str]:
time.sleep(5)
with Session(self._bind) as session:
results = session.query(self.EmbeddingStore.custom_id).all()
return [result[0] for result in results if result[0] is not None]
def get_documents_by_ids(self, ids: list[str]) -> list[Document]:
with Session(self._bind) as session:
results = (
session.query(self.EmbeddingStore)
.filter(self.EmbeddingStore.custom_id.in_(ids))
.all()
)
return [
Document(page_content=result.document, metadata=result.cmetadata or {})
for result in results
if result.custom_id in ids
]
class AsnyPgVector(ExtendedPgVector):
async def get_all_ids(self) -> list[str]:
await asyncio.sleep(5)
return await run_in_executor(None, super().get_all_ids)
async def get_documents_by_ids(self, ids: list[str]) -> list[Document]:
return await run_in_executor(None, super().get_documents_by_ids, ids)
async def delete(
self,
ids: Optional[list[str]] = None,
collection_only: bool = False,
**kwargs: Any
) -> None:
await run_in_executor(None, super().delete, ids, collection_only, **kwargs)