Spaces:
Running
Running
Update ragagent.py
Browse files- ragagent.py +14 -16
ragagent.py
CHANGED
@@ -120,7 +120,7 @@ class RAGAgent():
|
|
120 |
documents: List[str]
|
121 |
|
122 |
def retrieve(state):
|
123 |
-
RAGAgent.logs += "---RETRIEVE
|
124 |
question = state["question"]
|
125 |
|
126 |
# Retrieval
|
@@ -128,7 +128,7 @@ class RAGAgent():
|
|
128 |
return {"documents": documents, "question": question}
|
129 |
|
130 |
def generate(state):
|
131 |
-
RAGAgent.logs += "---GENERATE
|
132 |
question = state["question"]
|
133 |
documents = state["documents"]
|
134 |
|
@@ -139,7 +139,7 @@ class RAGAgent():
|
|
139 |
|
140 |
def grade_documents(state):
|
141 |
|
142 |
-
|
143 |
question = state["question"]
|
144 |
documents = state["documents"]
|
145 |
|
@@ -153,11 +153,11 @@ class RAGAgent():
|
|
153 |
grade = score["score"]
|
154 |
# Document relevant
|
155 |
if grade.lower() == "yes":
|
156 |
-
|
157 |
filtered_docs.append(d)
|
158 |
# Document not relevant
|
159 |
else:
|
160 |
-
|
161 |
# We do not include the document in filtered_docs
|
162 |
# We set a flag to indicate that we want to run web search
|
163 |
web_search = "Yes"
|
@@ -167,7 +167,7 @@ class RAGAgent():
|
|
167 |
|
168 |
def web_search(state):
|
169 |
|
170 |
-
|
171 |
question = state["question"]
|
172 |
documents = state["documents"]
|
173 |
|
@@ -184,7 +184,7 @@ class RAGAgent():
|
|
184 |
|
185 |
def decide_to_generate(state):
|
186 |
|
187 |
-
|
188 |
question = state["question"]
|
189 |
web_search = state["web_search"]
|
190 |
filtered_documents = state["documents"]
|
@@ -192,18 +192,16 @@ class RAGAgent():
|
|
192 |
if web_search == "Yes":
|
193 |
# All documents have been filtered check_relevance
|
194 |
# We will re-generate a new query
|
195 |
-
|
196 |
-
"---DECISION: ALL DOCUMENTS ARE NOT RELEVANT TO QUESTION, INCLUDE WEB SEARCH---"
|
197 |
-
)
|
198 |
return "websearch"
|
199 |
else:
|
200 |
# We have relevant documents, so generate answer
|
201 |
-
|
202 |
return "generate"
|
203 |
|
204 |
def grade_generation_v_documents_and_question(state):
|
205 |
|
206 |
-
|
207 |
question = state["question"]
|
208 |
documents = state["documents"]
|
209 |
generation = state["generation"]
|
@@ -215,19 +213,19 @@ class RAGAgent():
|
|
215 |
|
216 |
# Check hallucination
|
217 |
if grade == "yes":
|
218 |
-
|
219 |
# Check question-answering
|
220 |
print("---GRADE GENERATION vs QUESTION---")
|
221 |
score = RAGAgent.answer_grader.invoke({"question": question, "generation": generation})
|
222 |
grade = score["score"]
|
223 |
if grade == "yes":
|
224 |
-
|
225 |
return "useful"
|
226 |
else:
|
227 |
-
|
228 |
return "not useful"
|
229 |
else:
|
230 |
-
|
231 |
return "not supported"
|
232 |
|
233 |
workflow = StateGraph(GraphState)
|
|
|
120 |
documents: List[str]
|
121 |
|
122 |
def retrieve(state):
|
123 |
+
RAGAgent.logs += "---RETRIEVE---\n"
|
124 |
question = state["question"]
|
125 |
|
126 |
# Retrieval
|
|
|
128 |
return {"documents": documents, "question": question}
|
129 |
|
130 |
def generate(state):
|
131 |
+
RAGAgent.logs += "---GENERATE---\n"
|
132 |
question = state["question"]
|
133 |
documents = state["documents"]
|
134 |
|
|
|
139 |
|
140 |
def grade_documents(state):
|
141 |
|
142 |
+
RAGAgent.logs += "---CHECK DOCUMENT RELEVANCE TO QUESTION---\n"
|
143 |
question = state["question"]
|
144 |
documents = state["documents"]
|
145 |
|
|
|
153 |
grade = score["score"]
|
154 |
# Document relevant
|
155 |
if grade.lower() == "yes":
|
156 |
+
RAGAgent.logs += "---GRADE: DOCUMENT RELEVANT---\n"
|
157 |
filtered_docs.append(d)
|
158 |
# Document not relevant
|
159 |
else:
|
160 |
+
RAGAgent.logs += "---GRADE: DOCUMENT NOT RELEVANT---\n"
|
161 |
# We do not include the document in filtered_docs
|
162 |
# We set a flag to indicate that we want to run web search
|
163 |
web_search = "Yes"
|
|
|
167 |
|
168 |
def web_search(state):
|
169 |
|
170 |
+
RAGAgent.logs += "---WEB SEARCH---\n"
|
171 |
question = state["question"]
|
172 |
documents = state["documents"]
|
173 |
|
|
|
184 |
|
185 |
def decide_to_generate(state):
|
186 |
|
187 |
+
RAGAgent.logs += "---ASSESS GRADED DOCUMENTS---\n"
|
188 |
question = state["question"]
|
189 |
web_search = state["web_search"]
|
190 |
filtered_documents = state["documents"]
|
|
|
192 |
if web_search == "Yes":
|
193 |
# All documents have been filtered check_relevance
|
194 |
# We will re-generate a new query
|
195 |
+
RAGAgent.logs += "---DECISION: ALL DOCUMENTS ARE NOT RELEVANT TO QUESTION, INCLUDE WEB SEARCH---\n"
|
|
|
|
|
196 |
return "websearch"
|
197 |
else:
|
198 |
# We have relevant documents, so generate answer
|
199 |
+
RAGAgent.logs += "---DECISION: GENERATE---\n"
|
200 |
return "generate"
|
201 |
|
202 |
def grade_generation_v_documents_and_question(state):
|
203 |
|
204 |
+
RAGAgent.logs += "---CHECK HALLUCINATIONS---\n"
|
205 |
question = state["question"]
|
206 |
documents = state["documents"]
|
207 |
generation = state["generation"]
|
|
|
213 |
|
214 |
# Check hallucination
|
215 |
if grade == "yes":
|
216 |
+
RAGAgent.logs += "---DECISION: GENERATION IS GROUNDED IN DOCUMENTS---\n"
|
217 |
# Check question-answering
|
218 |
print("---GRADE GENERATION vs QUESTION---")
|
219 |
score = RAGAgent.answer_grader.invoke({"question": question, "generation": generation})
|
220 |
grade = score["score"]
|
221 |
if grade == "yes":
|
222 |
+
RAGAgent.logs += "---DECISION: GENERATION ADDRESSES QUESTION---\n"
|
223 |
return "useful"
|
224 |
else:
|
225 |
+
RAGAgent.logs += "---DECISION: GENERATION DOES NOT ADDRESS QUESTION---\n"
|
226 |
return "not useful"
|
227 |
else:
|
228 |
+
RAGAgent.logs += "---DECISION: GENERATION IS NOT GROUNDED IN DOCUMENTS, RE-TRY---\n"
|
229 |
return "not supported"
|
230 |
|
231 |
workflow = StateGraph(GraphState)
|