Update rag_tool.py
Browse files- rag_tool.py +21 -21
rag_tool.py
CHANGED
@@ -81,25 +81,22 @@ class RAGTool(Tool):
|
|
81 |
|
82 |
def _setup_vector_store(self):
|
83 |
"""Set up the vector store with documents if it doesn't exist"""
|
84 |
-
#
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
else:
|
101 |
-
print(f"Vector store already exists at {self.persist_directory}")
|
102 |
-
self._load_vector_store()
|
103 |
|
104 |
def _get_embeddings(self):
|
105 |
"""Get embedding model based on configuration"""
|
@@ -225,17 +222,20 @@ class RAGTool(Tool):
|
|
225 |
print("No documents available. Cannot create vector store.")
|
226 |
self.vector_store = None
|
227 |
|
228 |
-
def forward(self, query: str, top_k: int =
|
229 |
"""
|
230 |
Retrieve relevant documents based on the query.
|
231 |
|
232 |
Args:
|
233 |
query: The search query
|
234 |
-
top_k: Number of results to return
|
235 |
|
236 |
Returns:
|
237 |
String with formatted search results
|
238 |
"""
|
|
|
|
|
|
|
239 |
if not hasattr(self, 'vector_store') or self.vector_store is None:
|
240 |
return "Vector store is not initialized. Please check your configuration."
|
241 |
|
|
|
81 |
|
82 |
def _setup_vector_store(self):
|
83 |
"""Set up the vector store with documents if it doesn't exist"""
|
84 |
+
# Always try to create directories if they don't exist
|
85 |
+
os.makedirs(self.persist_directory, exist_ok=True)
|
86 |
+
|
87 |
+
# Check if documents path exists
|
88 |
+
if not os.path.exists(self.documents_path):
|
89 |
+
print(f"Warning: Documents path {self.documents_path} does not exist.")
|
90 |
+
return
|
91 |
+
|
92 |
+
# Force creation of vector store from documents
|
93 |
+
documents = self._load_documents()
|
94 |
+
if not documents:
|
95 |
+
print("No documents loaded. Vector store not created.")
|
96 |
+
return
|
97 |
+
|
98 |
+
# Create the vector store
|
99 |
+
self._create_vector_store(documents)
|
|
|
|
|
|
|
100 |
|
101 |
def _get_embeddings(self):
|
102 |
"""Get embedding model based on configuration"""
|
|
|
222 |
print("No documents available. Cannot create vector store.")
|
223 |
self.vector_store = None
|
224 |
|
225 |
+
def forward(self, query: str, top_k: int = None) -> str:
|
226 |
"""
|
227 |
Retrieve relevant documents based on the query.
|
228 |
|
229 |
Args:
|
230 |
query: The search query
|
231 |
+
top_k: Number of results to return (default: 3)
|
232 |
|
233 |
Returns:
|
234 |
String with formatted search results
|
235 |
"""
|
236 |
+
# Set default value if None
|
237 |
+
if top_k is None:
|
238 |
+
top_k = 3
|
239 |
if not hasattr(self, 'vector_store') or self.vector_store is None:
|
240 |
return "Vector store is not initialized. Please check your configuration."
|
241 |
|