Skip to content

Commit 8ac4cd3

Browse files
authored
Implement aget_nodes and adelete in PGVectorStore (#18515)
1 parent 65a26eb commit 8ac4cd3

File tree

2 files changed

+76
-1
lines changed
  • llama-index-integrations/vector_stores/llama-index-vector-stores-postgres

2 files changed

+76
-1
lines changed

llama-index-integrations/vector_stores/llama-index-vector-stores-postgres/llama_index/vector_stores/postgres/base.py

+75
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ class HybridAbstractData(base): # type: ignore
100100
model.text_search_tsv, # type: ignore
101101
postgresql_using="gin",
102102
)
103+
Index(
104+
f"{indexname}_1",
105+
model.metadata_["ref_doc_id"].astext, # type: ignore
106+
postgresql_using="btree",
107+
)
103108
else:
104109

105110
class AbstractData(base): # type: ignore
@@ -116,6 +121,12 @@ class AbstractData(base): # type: ignore
116121
{"__tablename__": tablename, "__table_args__": {"schema": schema_name}},
117122
)
118123

124+
Index(
125+
f"{indexname}_1",
126+
model.metadata_["ref_doc_id"].astext, # type: ignore
127+
postgresql_using="btree",
128+
)
129+
119130
return model
120131

121132

@@ -953,6 +964,18 @@ def delete(self, ref_doc_id: str, **delete_kwargs: Any) -> None:
953964
session.execute(stmt)
954965
session.commit()
955966

967+
async def adelete(self, ref_doc_id: str, **delete_kwargs: Any) -> None:
968+
from sqlalchemy import delete
969+
970+
self._initialize()
971+
async with self._async_session() as session, session.begin():
972+
stmt = delete(self._table_class).where(
973+
self._table_class.metadata_["doc_id"].astext == ref_doc_id
974+
)
975+
976+
await session.execute(stmt)
977+
await session.commit()
978+
956979
def delete_nodes(
957980
self,
958981
node_ids: Optional[List[str]] = None,
@@ -1087,6 +1110,58 @@ def get_nodes(
10871110

10881111
return nodes
10891112

1113+
async def aget_nodes(
1114+
self,
1115+
node_ids: Optional[List[str]] = None,
1116+
filters: Optional[MetadataFilters] = None,
1117+
) -> List[BaseNode]:
1118+
"""Get nodes asynchronously from vector store."""
1119+
assert (
1120+
node_ids is not None or filters is not None
1121+
), "Either node_ids or filters must be provided"
1122+
1123+
self._initialize()
1124+
from sqlalchemy import select
1125+
1126+
stmt = select(
1127+
self._table_class.node_id,
1128+
self._table_class.text,
1129+
self._table_class.metadata_,
1130+
self._table_class.embedding,
1131+
)
1132+
1133+
if node_ids:
1134+
stmt = stmt.where(self._table_class.node_id.in_(node_ids))
1135+
1136+
if filters:
1137+
filter_clause = self._recursively_apply_filters(filters)
1138+
stmt = stmt.where(filter_clause)
1139+
1140+
nodes: List[BaseNode] = []
1141+
1142+
async with self._async_session() as session, session.begin():
1143+
res = (await session.execute(stmt)).fetchall()
1144+
for item in res:
1145+
node_id = item.node_id
1146+
text = item.text
1147+
metadata = item.metadata_
1148+
embedding = item.embedding
1149+
1150+
try:
1151+
node = metadata_dict_to_node(metadata)
1152+
node.set_content(str(text))
1153+
node.embedding = embedding
1154+
except Exception:
1155+
node = TextNode(
1156+
id_=node_id,
1157+
text=text,
1158+
metadata=metadata,
1159+
embedding=embedding,
1160+
)
1161+
nodes.append(node)
1162+
1163+
return nodes
1164+
10901165

10911166
def _dedup_results(results: List[DBEmbeddingRow]) -> List[DBEmbeddingRow]:
10921167
seen_ids = set()

llama-index-integrations/vector_stores/llama-index-vector-stores-postgres/pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ exclude = ["**/BUILD"]
2727
license = "MIT"
2828
name = "llama-index-vector-stores-postgres"
2929
readme = "README.md"
30-
version = "0.5.0"
30+
version = "0.5.1"
3131

3232
[tool.poetry.dependencies]
3333
python = ">=3.9,<4.0"

0 commit comments

Comments
 (0)