Skip to content

Commit 33f0d83

Browse files
chore: Move vector store kvstore implementation into openai_vector_store_mixin.py (llamastack#2748)
1 parent 6b8a8c1 commit 33f0d83

File tree

14 files changed

+203
-234
lines changed

14 files changed

+203
-234
lines changed

docs/source/providers/vector_io/remote_pgvector.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ See [PGVector's documentation](https://github.com/pgvector/pgvector) for more de
4040
| `db` | `str \| None` | No | postgres | |
4141
| `user` | `str \| None` | No | postgres | |
4242
| `password` | `str \| None` | No | mysecretpassword | |
43+
| `kvstore` | `utils.kvstore.config.RedisKVStoreConfig \| utils.kvstore.config.SqliteKVStoreConfig \| utils.kvstore.config.PostgresKVStoreConfig \| utils.kvstore.config.MongoDBKVStoreConfig, annotation=NoneType, required=False, default='sqlite', discriminator='type'` | No | | Config for KV store backend (SQLite only for now) |
4344

4445
## Sample Configuration
4546

@@ -49,6 +50,9 @@ port: ${env.PGVECTOR_PORT:=5432}
4950
db: ${env.PGVECTOR_DB}
5051
user: ${env.PGVECTOR_USER}
5152
password: ${env.PGVECTOR_PASSWORD}
53+
kvstore:
54+
type: sqlite
55+
db_path: ${env.SQLITE_STORE_DIR:=~/.llama/dummy}/pgvector_registry.db
5256

5357
```
5458

docs/source/providers/vector_io/remote_weaviate.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ See [Weaviate's documentation](https://weaviate.io/developers/weaviate) for more
3636
## Sample Configuration
3737

3838
```yaml
39-
{}
39+
kvstore:
40+
type: sqlite
41+
db_path: ${env.SQLITE_STORE_DIR:=~/.llama/dummy}/weaviate_registry.db
4042

4143
```
4244

llama_stack/providers/inline/vector_io/faiss/faiss.py

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ async def initialize(self) -> None:
181181
)
182182
self.cache[vector_db.identifier] = index
183183

184-
# Load existing OpenAI vector stores using the mixin method
185-
self.openai_vector_stores = await self._load_openai_vector_stores()
184+
# Load existing OpenAI vector stores into the in-memory cache
185+
await self.initialize_openai_vector_stores()
186186

187187
async def shutdown(self) -> None:
188188
# Cleanup if needed
@@ -261,42 +261,6 @@ async def query_chunks(
261261

262262
return await index.query_chunks(query, params)
263263

264-
# OpenAI Vector Store Mixin abstract method implementations
265-
async def _save_openai_vector_store(self, store_id: str, store_info: dict[str, Any]) -> None:
266-
"""Save vector store metadata to kvstore."""
267-
assert self.kvstore is not None
268-
key = f"{OPENAI_VECTOR_STORES_PREFIX}{store_id}"
269-
await self.kvstore.set(key=key, value=json.dumps(store_info))
270-
self.openai_vector_stores[store_id] = store_info
271-
272-
async def _load_openai_vector_stores(self) -> dict[str, dict[str, Any]]:
273-
"""Load all vector store metadata from kvstore."""
274-
assert self.kvstore is not None
275-
start_key = OPENAI_VECTOR_STORES_PREFIX
276-
end_key = f"{OPENAI_VECTOR_STORES_PREFIX}\xff"
277-
stored_openai_stores = await self.kvstore.values_in_range(start_key, end_key)
278-
279-
stores = {}
280-
for store_data in stored_openai_stores:
281-
store_info = json.loads(store_data)
282-
stores[store_info["id"]] = store_info
283-
return stores
284-
285-
async def _update_openai_vector_store(self, store_id: str, store_info: dict[str, Any]) -> None:
286-
"""Update vector store metadata in kvstore."""
287-
assert self.kvstore is not None
288-
key = f"{OPENAI_VECTOR_STORES_PREFIX}{store_id}"
289-
await self.kvstore.set(key=key, value=json.dumps(store_info))
290-
self.openai_vector_stores[store_id] = store_info
291-
292-
async def _delete_openai_vector_store_from_storage(self, store_id: str) -> None:
293-
"""Delete vector store metadata from kvstore."""
294-
assert self.kvstore is not None
295-
key = f"{OPENAI_VECTOR_STORES_PREFIX}{store_id}"
296-
await self.kvstore.delete(key)
297-
if store_id in self.openai_vector_stores:
298-
del self.openai_vector_stores[store_id]
299-
300264
async def _save_openai_vector_store_file(
301265
self, store_id: str, file_id: str, file_info: dict[str, Any], file_contents: list[dict[str, Any]]
302266
) -> None:

llama_stack/providers/inline/vector_io/sqlite_vec/sqlite_vec.py

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,8 @@ async def initialize(self) -> None:
452452
)
453453
self.cache[vector_db.identifier] = VectorDBWithIndex(vector_db, index, self.inference_api)
454454

455-
# load any existing OpenAI vector stores
456-
self.openai_vector_stores = await self._load_openai_vector_stores()
455+
# Load existing OpenAI vector stores into the in-memory cache
456+
await self.initialize_openai_vector_stores()
457457

458458
async def shutdown(self) -> None:
459459
# nothing to do since we don't maintain a persistent connection
@@ -501,41 +501,6 @@ async def unregister_vector_db(self, vector_db_id: str) -> None:
501501
await self.cache[vector_db_id].index.delete()
502502
del self.cache[vector_db_id]
503503

504-
# OpenAI Vector Store Mixin abstract method implementations
505-
async def _save_openai_vector_store(self, store_id: str, store_info: dict[str, Any]) -> None:
506-
"""Save vector store metadata to SQLite database."""
507-
assert self.kvstore is not None
508-
key = f"{OPENAI_VECTOR_STORES_PREFIX}{store_id}"
509-
await self.kvstore.set(key=key, value=json.dumps(store_info))
510-
self.openai_vector_stores[store_id] = store_info
511-
512-
async def _load_openai_vector_stores(self) -> dict[str, dict[str, Any]]:
513-
"""Load all vector store metadata from SQLite database."""
514-
assert self.kvstore is not None
515-
start_key = OPENAI_VECTOR_STORES_PREFIX
516-
end_key = f"{OPENAI_VECTOR_STORES_PREFIX}\xff"
517-
stored_openai_stores = await self.kvstore.values_in_range(start_key, end_key)
518-
stores = {}
519-
for store_data in stored_openai_stores:
520-
store_info = json.loads(store_data)
521-
stores[store_info["id"]] = store_info
522-
return stores
523-
524-
async def _update_openai_vector_store(self, store_id: str, store_info: dict[str, Any]) -> None:
525-
"""Update vector store metadata in SQLite database."""
526-
assert self.kvstore is not None
527-
key = f"{OPENAI_VECTOR_STORES_PREFIX}{store_id}"
528-
await self.kvstore.set(key=key, value=json.dumps(store_info))
529-
self.openai_vector_stores[store_id] = store_info
530-
531-
async def _delete_openai_vector_store_from_storage(self, store_id: str) -> None:
532-
"""Delete vector store metadata from SQLite database."""
533-
assert self.kvstore is not None
534-
key = f"{OPENAI_VECTOR_STORES_PREFIX}{store_id}"
535-
await self.kvstore.delete(key)
536-
if store_id in self.openai_vector_stores:
537-
del self.openai_vector_stores[store_id]
538-
539504
async def _save_openai_vector_store_file(
540505
self, store_id: str, file_id: str, file_info: dict[str, Any], file_contents: list[dict[str, Any]]
541506
) -> None:

llama_stack/providers/remote/vector_io/milvus/milvus.py

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ async def initialize(self) -> None:
179179
uri = os.path.expanduser(self.config.db_path)
180180
self.client = MilvusClient(uri=uri)
181181

182-
self.openai_vector_stores = await self._load_openai_vector_stores()
182+
# Load existing OpenAI vector stores into the in-memory cache
183+
await self.initialize_openai_vector_stores()
183184

184185
async def shutdown(self) -> None:
185186
self.client.close()
@@ -248,36 +249,6 @@ async def query_chunks(
248249

249250
return await index.query_chunks(query, params)
250251

251-
async def _save_openai_vector_store(self, store_id: str, store_info: dict[str, Any]) -> None:
252-
"""Save vector store metadata to persistent storage."""
253-
assert self.kvstore is not None
254-
key = f"{OPENAI_VECTOR_STORES_PREFIX}{store_id}"
255-
await self.kvstore.set(key=key, value=json.dumps(store_info))
256-
self.openai_vector_stores[store_id] = store_info
257-
258-
async def _update_openai_vector_store(self, store_id: str, store_info: dict[str, Any]) -> None:
259-
"""Update vector store metadata in persistent storage."""
260-
assert self.kvstore is not None
261-
key = f"{OPENAI_VECTOR_STORES_PREFIX}{store_id}"
262-
await self.kvstore.set(key=key, value=json.dumps(store_info))
263-
self.openai_vector_stores[store_id] = store_info
264-
265-
async def _delete_openai_vector_store_from_storage(self, store_id: str) -> None:
266-
"""Delete vector store metadata from persistent storage."""
267-
assert self.kvstore is not None
268-
key = f"{OPENAI_VECTOR_STORES_PREFIX}{store_id}"
269-
await self.kvstore.delete(key)
270-
if store_id in self.openai_vector_stores:
271-
del self.openai_vector_stores[store_id]
272-
273-
async def _load_openai_vector_stores(self) -> dict[str, dict[str, Any]]:
274-
"""Load all vector store metadata from persistent storage."""
275-
assert self.kvstore is not None
276-
start_key = OPENAI_VECTOR_STORES_PREFIX
277-
end_key = f"{OPENAI_VECTOR_STORES_PREFIX}\xff"
278-
stored = await self.kvstore.values_in_range(start_key, end_key)
279-
return {json.loads(s)["id"]: json.loads(s) for s in stored}
280-
281252
async def _save_openai_vector_store_file(
282253
self, store_id: str, file_id: str, file_info: dict[str, Any], file_contents: list[dict[str, Any]]
283254
) -> None:

llama_stack/providers/remote/vector_io/pgvector/config.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
from pydantic import BaseModel, Field
1010

11+
from llama_stack.providers.utils.kvstore.config import (
12+
KVStoreConfig,
13+
SqliteKVStoreConfig,
14+
)
1115
from llama_stack.schema_utils import json_schema_type
1216

1317

@@ -18,15 +22,27 @@ class PGVectorVectorIOConfig(BaseModel):
1822
db: str | None = Field(default="postgres")
1923
user: str | None = Field(default="postgres")
2024
password: str | None = Field(default="mysecretpassword")
25+
kvstore: KVStoreConfig | None = Field(description="Config for KV store backend (SQLite only for now)", default=None)
2126

2227
@classmethod
2328
def sample_run_config(
2429
cls,
30+
__distro_dir__: str,
2531
host: str = "${env.PGVECTOR_HOST:=localhost}",
2632
port: int = "${env.PGVECTOR_PORT:=5432}",
2733
db: str = "${env.PGVECTOR_DB}",
2834
user: str = "${env.PGVECTOR_USER}",
2935
password: str = "${env.PGVECTOR_PASSWORD}",
3036
**kwargs: Any,
3137
) -> dict[str, Any]:
32-
return {"host": host, "port": port, "db": db, "user": user, "password": password}
38+
return {
39+
"host": host,
40+
"port": port,
41+
"db": db,
42+
"user": user,
43+
"password": password,
44+
"kvstore": SqliteKVStoreConfig.sample_run_config(
45+
__distro_dir__=__distro_dir__,
46+
db_name="pgvector_registry.db",
47+
),
48+
}

0 commit comments

Comments
 (0)