|
3 | 3 | from pathlib import Path |
4 | 4 |
|
5 | 5 | from botocore.exceptions import NoCredentialsError |
| 6 | +from chromadb.api import ClientAPI |
6 | 7 | from haystack import Pipeline |
7 | 8 | from haystack.components.converters import MultiFileConverter |
8 | 9 | from haystack.components.embedders import SentenceTransformersDocumentEmbedder |
9 | 10 | from haystack.components.preprocessors import DocumentPreprocessor |
10 | 11 | from haystack.components.writers import DocumentWriter |
| 12 | +from haystack.document_stores.errors.errors import DocumentStoreError |
11 | 13 | from haystack_integrations.document_stores.chroma import ChromaDocumentStore |
12 | 14 |
|
13 | 15 | from src.app_config import config |
|
16 | 18 | logger = logging.getLogger(__name__) |
17 | 19 |
|
18 | 20 |
|
| 21 | +def delete_preview_collections(chroma_client: ClientAPI) -> None: |
| 22 | + collections = chroma_client.list_collections() |
| 23 | + for collection in collections: |
| 24 | + name = collection.name |
| 25 | + if name.startswith(f"{config.collection_name_prefix}_p-"): |
| 26 | + logger.info("Deleting preview collection: %s", name) |
| 27 | + chroma_client.delete_collection(name) |
| 28 | + |
| 29 | + |
19 | 30 | def populate_vector_db() -> None: |
20 | 31 | logging.basicConfig(format="%(levelname)s - %(name)s - %(message)s", level=logging.INFO) |
21 | 32 |
|
22 | 33 | chroma_client = config.chroma_client() |
| 34 | + delete_preview_collections(chroma_client) |
| 35 | + |
23 | 36 | logger.info("ChromaDB collections: %s", chroma_client.list_collections()) |
24 | 37 | doc_store = config.chroma_document_store() |
25 | 38 | collection_name = doc_store._collection_name |
26 | 39 |
|
27 | 40 | # Clear existing collection if any |
28 | 41 | if doc_store.count_documents() > 0: |
29 | | - # Don't delete collection since it's referenced by existing pipelines upon their startup |
30 | | - logger.info("Clearing out existing vector DB collection=%r", collection_name) |
31 | | - doc_store.delete_all_documents() |
| 42 | + try: |
| 43 | + # Don't delete collection since it's referenced by existing pipelines upon their startup |
| 44 | + logger.info("Clearing out existing vector DB collection=%r", collection_name) |
| 45 | + # recreate_index=True results in a new id for the collection, which breaks existing pipelines |
| 46 | + doc_store.delete_all_documents(recreate_index=False) |
| 47 | + except DocumentStoreError as e: |
| 48 | + # Ignore this error from haystack.logging, which is okay since logging is the last step in delete_all_documents |
| 49 | + if "overwrite 'name' in LogRecord" in str(e): |
| 50 | + logger.info("Ignoring expected DocumentStoreError: %s", e) |
| 51 | + else: |
| 52 | + logger.warning("Unexpected DocumentStoreError: %s", e) |
| 53 | + raise |
| 54 | + assert doc_store.count_documents() == 0, "Documents should be deleted from collection" |
32 | 55 |
|
33 | 56 | # Download files from S3 |
34 | 57 | local_folder = download_s3_folder_to_local() |
|
0 commit comments