2023-09-05 20:42:22 +08:00
|
|
|
from .vector_base import VectorBase
|
|
|
|
|
from ..object import ObjectID
|
|
|
|
|
import chromadb
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChromaVectorStore(VectorBase):
|
2023-09-12 15:28:59 +08:00
|
|
|
def __init__(self, model_name: str) -> None:
|
|
|
|
|
super().__init__(model_name)
|
2023-09-05 20:42:22 +08:00
|
|
|
|
|
|
|
|
logging.info(
|
2023-09-12 15:28:59 +08:00
|
|
|
"will init chroma vector store, model={}".format(model_name)
|
2023-09-05 20:42:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
)
|