-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWorkflow.py
More file actions
364 lines (314 loc) · 11.7 KB
/
Workflow.py
File metadata and controls
364 lines (314 loc) · 11.7 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import logging
from typing import List
from langchain_core.documents import Document
from langgraph.graph import END, StateGraph
from typing_extensions import TypedDict
logger = logging.getLogger(__name__)
class Workflow:
"""
This class represents a workflow for processing information retrieval tasks.
It takes a set of tools and models and builds a state graph to execute them
in a specific order based on the results of previous steps.
Args:
retriever (object): An object responsible for retrieving documents from a vector store.
docs (list[Document], optional): A list of documents to process (used for summaries).
Defaults to None.
summary_chain (object): An object responsible for generating summaries of documents.
rag_chain (object): An object responsible for generating answers using retrieved documents.
retrieval_grader (object): An object responsible for grading the relevance of retrieved
documents.
hallucination_grader (object): An object responsible for checking if the generated answer
is grounded in the documents.
answer_grader (object): An object responsible for checking if the generated answer addresses
the question.
web_search_tool (object): An object responsible for searching the web for relevant
information.
"""
def __init__(
self,
retriever,
docs,
summary_chain,
rag_chain,
retrieval_grader,
hallucination_grader,
answer_grader,
web_search_tool,
):
self.retriever = retriever
self.rag_chain = rag_chain
self.retrieval_grader = retrieval_grader
self.hallucination_grader = hallucination_grader
self.answer_grader = answer_grader
self.web_search_tool = web_search_tool
self.docs = docs
self.summary_chain = summary_chain
# return self.build()
def build(self):
"""
Builds a state graph representing the workflow for processing information retrieval tasks.
Returns:
StateGraph: A compiled state graph object.
"""
workflow = StateGraph(self.GraphState)
workflow.add_node("websearch", self.web_search)
workflow.add_node("retrieve", self.retrieve)
workflow.add_node("grade_documents", self.grade_documents)
workflow.add_node("generate", self.generate)
workflow.add_node("summary", self.generate_summary)
workflow.add_edge("summary", END)
workflow.add_node("rag", self.init_rag)
workflow.add_edge("rag", "retrieve")
workflow.add_edge("retrieve", "grade_documents")
workflow.add_conditional_edges(
"grade_documents",
self.decide_to_generate,
{
"websearch": "websearch",
"generate": "generate",
},
)
workflow.add_edge("websearch", "generate")
workflow.add_conditional_edges(
"generate",
self.grade_generation_v_documents_and_question,
{
"not supported": "generate",
"useful": END,
"not useful": "websearch",
},
)
workflow.set_conditional_entry_point(
self.route_chain,
{
"summary": "summary",
"rag": "rag",
},
)
return workflow.compile()
class GraphState(TypedDict):
"""
Represents the state of our graph.
Attributes:
question: question
generation: LLM generation
web_search: whether to add search
documents: list of documents
count: # of time generate step executed
"""
question: str
generation: str
web_search: str
documents: List[str]
count: int
retries: int
### Nodes
def retrieve(self, state):
"""
Retrieve documents from vectorstore
Args:
state (dict): The current graph state
Returns:
state (dict): New key added to state, documents, that contains retrieved documents
"""
logger.info("---RETRIEVE---")
question = state["question"]
# Retrieval
documents = self.retriever.invoke(question)
return {
"documents": documents,
"question": question,
"retries": state["retries"],
"count": state["count"],
}
def init_rag(self, state):
"""
Initialize the RAG chain
Args:
state (dict): The current graph state
Returns:
state (dict): Initialized elements to state
"""
return state
def generate(self, state):
"""
Generate answer using RAG on retrieved documents
Args:
state (dict): The current graph state
Returns:
state (dict): New key added to state, generation, that contains LLM generation
"""
logger.info("---GENERATE---")
question = state["question"]
documents = state["documents"]
count = 0 if state["count"] is None else (state["count"] + 1)
# RAG generation
generation = self.rag_chain.invoke({"context": documents, "question": question})
return {
"documents": documents,
"question": question,
"generation": generation,
"count": count,
"retries": state["retries"],
}
def generate_summary(self, _):
"""
Generate summary of all documents using RAG on retrieved documents
Args:
state (dict): The current graph state
Returns:
state (dict): generation, that contains LLM generation
"""
logger.info("---SUMMARY---")
return {"generation": self.summary_chain.run(self.docs)}
def grade_documents(self, state):
"""
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
Args:
state (dict): The current graph state
Returns:
state (dict): Filtered out irrelevant documents and updated web_search state
"""
logger.info("---CHECK DOCUMENT RELEVANCE TO QUESTION---")
question = state["question"]
documents = state["documents"]
# Score each doc
filtered_docs = []
web_search = "No"
for d in documents:
score = self.retrieval_grader.invoke(
{"question": question, "document": d.page_content}
)
grade = score["score"]
# Document relevant
if grade.lower() == "yes":
logger.info("---GRADE: DOCUMENT RELEVANT---")
filtered_docs.append(d)
# Document not relevant
else:
logger.info("---GRADE: DOCUMENT NOT RELEVANT---")
# We do not include the document in filtered_docs
# We set a flag to indicate that we want to run web search
web_search = "Yes"
continue
return {
"documents": filtered_docs,
"question": question,
"web_search": web_search,
"retries": state["retries"],
}
def web_search(self, state):
"""
Web search based based on the question
Args:
state (dict): The current graph state
Returns:
state (dict): Appended web results to documents
"""
logger.info("---WEB SEARCH---")
question = state["question"]
documents = state["documents"]
# Web search
docs = self.web_search_tool.invoke({"query": question})
# Not needed in Google Search
# docs = "\n".join([d["content"] for d in docs])
web_results = Document(page_content=docs)
if documents is not None:
documents.append(web_results)
else:
documents = [web_results]
return {
"documents": documents,
"question": question,
"retries": state["retries"],
"count": state["count"],
}
def route_chain(self, state):
"""
Routes the workflow execution based on the current question in the state.
This method checks the question in the state and determines the appropriate entry
point for the workflow.
If the question is "summary", the workflow execution starts at the "summary"
node. Otherwise,
it starts at the "rag" node.
Args:
state (GraphState): The current state of the workflow.
Returns:
str: The name of the node to start execution at ("summary" or "rag").
"""
logger.info("---ROUTE CHAIN---")
question = state["question"]
if question == "summary":
logger.info("---ROUTE QUESTION TO SUMMARY---")
return "summary"
else:
logger.info("---ROUTE QUESTION TO RAG---")
return "rag"
def decide_to_generate(self, state):
"""
Determines whether to generate an answer, or add web search
Args:
state (dict): The current graph state
Returns:
str: Binary decision for next node to call
"""
logger.info("---ASSESS GRADED DOCUMENTS---")
web_search = state["web_search"]
if web_search == "Yes":
# All documents have been filtered check_relevance
# We will re-generate a new query
logger.info(
"---DECISION: ALL DOCUMENTS ARE NOT RELEVANT TO QUESTION, INCLUDE WEB SEARCH---"
)
return "websearch"
else:
# We have relevant documents, so generate answer
logger.info("---DECISION: GENERATE---")
return "generate"
### Conditional edge
def grade_generation_v_documents_and_question(self, state):
"""
Determines whether the generation is grounded in the document and answers question.
Args:
state (dict): The current graph state
Returns:
str: Decision for next node to call
"""
logger.info("---CHECK HALLUCINATIONS---")
question = state["question"]
documents = state["documents"]
generation = state["generation"]
generation_count = state["count"]
if generation_count > state["retries"]:
logger.info("---DECISION: GENERATION ADDRESSES QUESTION---")
return "useful"
score = self.hallucination_grader.invoke(
{"documents": documents, "generation": generation}
)
grade = score["score"]
# Check hallucination
if grade == "yes":
logger.info("---DECISION: GENERATION IS GROUNDED IN DOCUMENTS---")
# Check question-answering
logger.info("---GRADE GENERATION vs QUESTION---")
score = self.answer_grader.invoke(
{"question": question, "generation": generation}
)
grade = score["score"]
if grade == "yes":
logger.info("---DECISION: GENERATION ADDRESSES QUESTION---")
return "useful"
else:
logger.info("---DECISION: GENERATION DOES NOT ADDRESS QUESTION---")
return "not useful"
else:
if generation_count == state["retries"]:
logger.info(
"---DECISION: GENERATION IS NOT GROUNDED IN DOCUMENTS, WEB SEARCH---"
)
return "not useful"
logger.info(
"---DECISION: GENERATION IS NOT GROUNDED IN DOCUMENTS, RE-TRY---"
)
return "not supported"