fehmikaya commited on
Commit
1e04250
·
verified ·
1 Parent(s): 82ce72b

Update ragagent.py

Browse files
Files changed (1) hide show
  1. ragagent.py +16 -4
ragagent.py CHANGED
@@ -17,6 +17,7 @@ from typing_extensions import TypedDict
17
  from typing import List
18
  from langchain_core.documents import Document
19
  import os
 
20
 
21
 
22
  class RAGAgent():
@@ -94,12 +95,23 @@ class RAGAgent():
94
  )
95
  doc_splits = text_splitter.split_documents(docs_list)
96
  embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
 
 
 
 
 
 
 
 
 
 
97
  # Add to vectorDB
98
  vectorstore = Chroma.from_documents(
99
- documents=doc_splits,
100
- collection_name="rag-chroma",
101
  embedding=embedding_function,
102
  )
 
103
  RAGAgent.retriever = vectorstore.as_retriever()
104
  RAGAgent.reset_chains()
105
  RAGAgent.logs=""
@@ -220,7 +232,7 @@ class RAGAgent():
220
  RAGAgent.add_log("---DECISION: GENERATION DOES NOT ADDRESS QUESTION---")
221
  return "not useful"
222
  else:
223
- RAGAgent.add_log("---DECISION: GENERATION IS NOT GROUNDED IN DOCUMENTS, RE-TRY---")
224
  return "not supported"
225
 
226
  workflow = StateGraph(GraphState)
@@ -248,7 +260,7 @@ class RAGAgent():
248
  "generate",
249
  grade_generation_v_documents_and_question,
250
  {
251
- "not supported": "generate",
252
  "useful": END,
253
  "not useful": "websearch",
254
  },
 
17
  from typing import List
18
  from langchain_core.documents import Document
19
  import os
20
+ import re
21
 
22
 
23
  class RAGAgent():
 
95
  )
96
  doc_splits = text_splitter.split_documents(docs_list)
97
  embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
98
+
99
+ collection_name = re.sub(r'[^a-zA-Z0-9]', '', self.doc_splits[0].metadata.get('source'))
100
+
101
+ try:
102
+ # If it exists, delete the existing collection
103
+ Chroma()._client.delete_collection(name=collection_name)
104
+ print(f"Collection {collection_name} deleted successfully.")
105
+ except Exception as e:
106
+ pass
107
+
108
  # Add to vectorDB
109
  vectorstore = Chroma.from_documents(
110
+ documents=self.doc_splits,
111
+ collection_name=collection_name,
112
  embedding=embedding_function,
113
  )
114
+
115
  RAGAgent.retriever = vectorstore.as_retriever()
116
  RAGAgent.reset_chains()
117
  RAGAgent.logs=""
 
232
  RAGAgent.add_log("---DECISION: GENERATION DOES NOT ADDRESS QUESTION---")
233
  return "not useful"
234
  else:
235
+ RAGAgent.add_log("---DECISION: GENERATION IS NOT GROUNDED IN DOCUMENTS, ENDING---")
236
  return "not supported"
237
 
238
  workflow = StateGraph(GraphState)
 
260
  "generate",
261
  grade_generation_v_documents_and_question,
262
  {
263
+ "not supported": END, # "generate",
264
  "useful": END,
265
  "not useful": "websearch",
266
  },