Skip to content

Commit

Permalink
add flush function of vectorstore & remove auto flush
Browse files Browse the repository at this point in the history
  • Loading branch information
ILSparkle committed Sep 2, 2024
1 parent 09cce5e commit b21acc1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
OPENAI_BASE_URL: ${{ secrets.OPENAI_BASE_URL }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
DEFAULT_EMBED_MODEL: text-embedding-ada-002
DEFAULT_CHAT_MODEL: gpt-3.5-turbo
DEFAULT_CHAT_MODEL: gpt-4o-mini
HF_TOKENIZER_PATH: 01-ai/Yi-6B-Chat
DEFAULT_CHUNK_SIZE: 300
DEFAULT_CHUNK_OVERLAP: 100
Expand Down
13 changes: 4 additions & 9 deletions src/cardinal/vectorstore/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ def create(cls, name: str, texts: Sequence[str], data: Sequence[T], drop_old: Op
return _get_vectorstore().create(name, texts, data, drop_old)

def insert(self, texts: Sequence[str], data: Sequence[T]) -> None:
self._vectorstore.insert(texts, data)
return self._flush()
return self._vectorstore.insert(texts, data)

def delete(self, condition: "Condition") -> None:
self._vectorstore.delete(condition)
return self._flush()
return self._vectorstore.delete(condition)

def search(
self, query: str, top_k: Optional[int] = 4, condition: Optional["Condition"] = None
Expand All @@ -47,11 +45,8 @@ def exists(self) -> bool:
def destroy(self) -> None:
return self._vectorstore.destroy()

def _flush(self) -> None:
if isinstance(self._vectorstore, Milvus):
return self._vectorstore.store.flush()
else:
return None
def flush(self) -> None:
return self._vectorstore.flush()


_vectorstores: Dict[str, Type["VectorStore"]] = {}
Expand Down
3 changes: 3 additions & 0 deletions src/cardinal/vectorstore/chroma.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,6 @@ def destroy(self) -> None:
client = _get_chroma_client()
client.delete_collection(self.name)
self.store = None

def flush(self) -> None:
return None
4 changes: 4 additions & 0 deletions src/cardinal/vectorstore/milvus.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,7 @@ def destroy(self):
self._try_init_and_check_exists()
self.store.drop()
self.store = None

def flush(self) -> None:
return self.store.flush()

17 changes: 9 additions & 8 deletions tests/vectorstore/test_vector_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ class Animal(BaseModel):


def test_vector_store():
vectorStore = AutoVectorStore[Animal](name="test")
assert(not vectorStore.exists()) # False
vectorStore.insert(texts=texts, data=data)
vectorStore.delete(AutoCondition(key="name", value="dog", op=Operator.Eq))
assert(vectorStore.search(query="dog", top_k=2)[0][0] == data[2])
assert(vectorStore.search(query="dog", top_k=2)[1][0] == data[1])
vectorstore = AutoVectorStore[Animal](name="test")
assert(not vectorstore.exists()) # False
vectorstore.insert(texts=texts, data=data)
vectorstore.delete(AutoCondition(key="name", value="dog", op=Operator.Eq))
vectorstore.flush()
assert(vectorstore.search(query="dog", top_k=2)[0][0] == data[2])
assert(vectorstore.search(query="dog", top_k=2)[1][0] == data[1])
# [(Animal(name='puppy'), 0.8510237336158752), (Animal(name='llama'), 1.1970627307891846)]
assert(vectorStore.exists()) # True
vectorStore.destroy()
assert(vectorstore.exists()) # True
vectorstore.destroy()

0 comments on commit b21acc1

Please sign in to comment.