Spaces:
Sleeping
Sleeping
Update app/rag_setup.py
Browse files- app/rag_setup.py +16 -12
app/rag_setup.py
CHANGED
|
@@ -95,20 +95,24 @@ class TFIDFEmbeddingFunction:
|
|
| 95 |
|
| 96 |
# ChromaDB setup - using in-memory storage for simplicity and compatibility with Hugging Face.
|
| 97 |
logger.info("Initializing ChromaDB with in-memory storage.")
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
embedding_function=embedding_function
|
| 105 |
)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
except Exception as e:
|
| 110 |
-
logger.error(f"Error initializing ChromaDB: {e}")
|
| 111 |
-
raise RuntimeError(f"Failed to initialize ChromaDB: {e}")
|
| 112 |
|
| 113 |
class OpenRouterLLM:
|
| 114 |
"""A wrapper for interacting with the OpenRouter LLM API."""
|
|
|
|
| 95 |
|
| 96 |
# ChromaDB setup - using in-memory storage for simplicity and compatibility with Hugging Face.
|
| 97 |
logger.info("Initializing ChromaDB with in-memory storage.")
|
| 98 |
+
client = chromadb.Client()
|
| 99 |
+
embedding_function = TFIDFEmbeddingFunction()
|
| 100 |
+
COLLECTION_NAME = "context_aware_collection"
|
| 101 |
+
|
| 102 |
+
def clear_and_recreate_collection():
|
| 103 |
+
"""Deletes and re-creates the ChromaDB collection to ensure it's empty."""
|
| 104 |
+
logger.info(f"Deleting and re-creating collection '{COLLECTION_NAME}'...")
|
| 105 |
+
try:
|
| 106 |
+
client.delete_collection(name=COLLECTION_NAME)
|
| 107 |
+
except Exception as e:
|
| 108 |
+
logger.warning(f"Failed to delete collection (it may not exist): {e}")
|
| 109 |
+
return client.get_or_create_collection(
|
| 110 |
+
name=COLLECTION_NAME,
|
| 111 |
embedding_function=embedding_function
|
| 112 |
)
|
| 113 |
+
|
| 114 |
+
collection = clear_and_recreate_collection()
|
| 115 |
+
logger.info("ChromaDB collection initialized successfully.")
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
class OpenRouterLLM:
|
| 118 |
"""A wrapper for interacting with the OpenRouter LLM API."""
|