Spaces:
Sleeping
Sleeping
File size: 1,317 Bytes
7a4d005 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | from typing import Dict, Any
from graph.chains.retrieval_grader import retrieval_grader
from graph.state import GraphState
def grade_documents(state: GraphState) -> Dict[str, Any]:
"""
Determines whether the retrieved documents are relevant to the question
If any document is not relevant, we will set a flag to run web search
Arguments:
state (dict): The current state of the graph
Returns:
state (dict): Filtered out irrelevant documents and updated web_search state
"""
print("----CHECK DOCUMENT RELEVANT TO QUESTION----")
question = state["question"]
documents = state.get("documents") or []
filtered_docs = []
web_search = False
for d in documents:
score = retrieval_grader.invoke(
{"question": question, "document": d.page_content}
)
grade = score.binary_score
if isinstance(grade, str) and grade.lower() == "yes":
print("-----GRADE: DOCUMENT RELEVANT TO QUESTION----")
filtered_docs.append(d)
else:
print("-----GRADE: DOCUMENT NOT RELEVANT TO QUESTION----")
web_search = True
continue
if not filtered_docs:
web_search = True
return {"question": question, "documents": filtered_docs, "web_search": web_search} |