Add chromadb based vector store impl and test case

This commit is contained in:
liyaxing
2023-09-05 20:42:22 +08:00
parent cff34c6aba
commit 188595beb6
8 changed files with 96 additions and 15 deletions
+49
View File
@@ -0,0 +1,49 @@
from .vector_base import VectorBase
from ..object import ObjectID
import chromadb
import logging
import os
class ChromaVectorStore(VectorBase):
def __init__(self, db_url, model_name: str) -> None:
super().__init__(db_url, model_name)
logging.info(
"will init chroma vector store, db={}, model={}".format(db_url, model_name)
)
directory = os.path.join(
os.path.dirname(__file__), "../../../rootfs/data/vector"
)
logging.info("will use vector store: {}".format(directory))
client = chromadb.PersistentClient(
path=directory, settings=chromadb.Settings(anonymized_telemetry=False)
)
# client = chromadb.Client()
collection_name = "coll_{}".format(model_name)
logging.info("will init chroma colletion: %s", collection_name)
collection = client.get_or_create_collection(collection_name)
self.collection = collection
async def insert(self, vector: [float], id: ObjectID):
self.collection.add(
embeddings=vector,
ids=id,
)
async def query(self, vector: [float], top_k: int) -> [ObjectID]:
ret = self.collection.query(
query_embeddings=vector,
n_results=top_k,
)
return ret["ids"]
async def delete(self, id: ObjectID):
self.collection.delete(
ids=id,
)