Update app.py
Browse files
app.py
CHANGED
|
@@ -94,21 +94,31 @@ for doc in data:
|
|
| 94 |
# -------------------------------
|
| 95 |
|
| 96 |
# Create a Chroma vector store using the document splits.
|
| 97 |
-
persist_directory = "
|
| 98 |
-
if os.path.exists(persist_directory):
|
| 99 |
-
print(f"Clearing existing vector store at {persist_directory}...")
|
| 100 |
-
shutil.rmtree(persist_directory)
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
vectorstore = Chroma.
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# Create a BM25 retriever from the document splits.
|
| 114 |
bm25_retriever = BM25Retriever.from_documents(all_splits)
|
|
|
|
| 94 |
# -------------------------------
|
| 95 |
|
| 96 |
# Create a Chroma vector store using the document splits.
|
| 97 |
+
persist_directory = "path_to_persist_directory"
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
# Check if the directory exists and contains a persisted vector store
|
| 100 |
+
if os.path.exists(persist_directory):
|
| 101 |
+
# Attempt to load the existing vector store
|
| 102 |
+
try:
|
| 103 |
+
vectorstore = Chroma.load(persist_directory, embedding_function=OpenAIEmbeddings())
|
| 104 |
+
print("Loaded existing vector store from persisted directory.")
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"Error loading vector store: {e}. Proceeding to create a new one.")
|
| 107 |
+
# Fallback to creating a new vector store if loading fails
|
| 108 |
+
vectorstore = Chroma.from_documents(
|
| 109 |
+
documents=all_splits,
|
| 110 |
+
embedding=OpenAIEmbeddings(),
|
| 111 |
+
persist_directory=persist_directory
|
| 112 |
+
)
|
| 113 |
+
print("Created new vector store and persisted embeddings.")
|
| 114 |
+
else:
|
| 115 |
+
# Create a new vector store if the directory doesn't exist
|
| 116 |
+
vectorstore = Chroma.from_documents(
|
| 117 |
+
documents=all_splits,
|
| 118 |
+
embedding=OpenAIEmbeddings(),
|
| 119 |
+
persist_directory=persist_directory
|
| 120 |
+
)
|
| 121 |
+
print("Created new vector store and persisted embeddings.")
|
| 122 |
|
| 123 |
# Create a BM25 retriever from the document splits.
|
| 124 |
bm25_retriever = BM25Retriever.from_documents(all_splits)
|