-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselfRAGAgentHF.py
executable file
·397 lines (305 loc) · 11.1 KB
/
selfRAGAgentHF.py
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import os
from typing import List
from dotenv import load_dotenv
from langchain.prompts import PromptTemplate
from langchain_chroma import Chroma
from langchain_core.output_parsers import JsonOutputParser, StrOutputParser
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_ollama import ChatOllama
from langgraph.graph import END, START, StateGraph
from typing_extensions import TypedDict
load_dotenv()
ollama_base_url = ""
if os.getenv("BASE_URL"):
ollama_base_url = os.getenv("BASE_URL")
else:
ollama_base_url = None
retrieval_grader_llm = "llama3.1"
rag_chain_llm = "llama3.1"
hallucination_grader_llm = "llama3.1"
answer_grader_llm = "llama3.1"
question_rewriter_llm = "llama3.1"
def retrieval_grader_chain():
prompt = PromptTemplate(
template="""You are a grader assessing relevance of a retrieved document to a user question. \n
Here is the retrieved document: \n\n {document} \n\n
Here is the user question: {question} \n
If the document contains keywords related to the user question, grade it as relevant. \n
Use logic and understand the context of the question and document to make decisions \n
Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question. \n
Provide the binary score as a JSON with a single key 'score' and no premable or explanation.""",
input_variables=["question", "document"],
)
llm = ChatOllama(
base_url=ollama_base_url,
model=retrieval_grader_llm,
format="json",
temperature=0,
)
retrieval_grader = prompt | llm | JsonOutputParser()
return retrieval_grader
def rag_chain():
prompt = PromptTemplate(
template="""You are an legal question-answering agent. Use the following pieces of retrieved context which are part of circulars by the government and your experience to understand to create an answer relevant to the query containing all pertinent information required to answer the question. Make sure the answer is of an appropriate length containing specifics about the query's response. If you don't know the answer, just say that you don't know. Please only provide the answer in the output.
Question: {question}
Context: {context}
Answer:""",
input_variables=["question", "context"],
)
llm = ChatOllama(
base_url=ollama_base_url,
model=rag_chain_llm,
# format="json",
temperature=0,
)
rag = prompt | llm | StrOutputParser()
return rag
def hallucination_grader_chain():
prompt = PromptTemplate(
template="""You are a grader assessing whether an answer is grounded in / supported by a set of facts. \n
Here are the facts:
\n ------- \n
{documents}
\n ------- \n
Here is the answer: {generation}
Give a binary score 'yes' or 'no' score to indicate whether the answer is grounded in / supported by a set of facts. \n
Provide the binary score as a JSON with a single key 'score' and no preamble or explanation.""",
input_variables=["generation", "documents"],
)
llm = ChatOllama(
base_url=ollama_base_url,
model=hallucination_grader_llm,
format="json",
temperature=0,
)
hallucination_grader = prompt | llm | JsonOutputParser()
return hallucination_grader
def answer_grader_chain():
prompt = PromptTemplate(
template="""You are a grader assessing whether an answer is useful to resolve a question. \n
Here is the answer:
\n ------- \n
{generation}
\n ------- \n
Here is the question: {question}
Give a binary score 'yes' or 'no' to indicate whether the answer is useful to resolve a question. \n
Provide the binary score as a JSON with a single key 'score' and no preamble or explanation.""",
input_variables=["generation", "question"],
)
llm = ChatOllama(
base_url=ollama_base_url,
model=answer_grader_llm,
format="json",
temperature=0,
)
answer_grader = prompt | llm | JsonOutputParser()
return answer_grader
def question_rewriter_chain():
prompt = PromptTemplate(
template="""You a question re-writer that converts an input question to a better version that is optimized \n
for vectorstore retrieval. Look at the initial and formulate an improved question. \n
Here is the initial question: \n\n {question}. Improved question with no preamble: \n
Only return the improved question as the output.""",
input_variables=["generation", "question"],
)
llm = ChatOllama(
base_url=ollama_base_url,
model=question_rewriter_llm,
temperature=0,
)
question_rewriter = prompt | llm | StrOutputParser()
return question_rewriter
class GraphState(TypedDict):
"""
Represents the state of our graph.
Attributes:
question: question
generation: LLM generation
documents: list of documents
iterations: number of iterations
"""
question: str
generation: str
documents: List[str]
iterations: int
def retrieve(state):
"""
Retrieve documents
Args:
state (dict): The current graph state
Returns:
state (dict): New key added to state, documents, that contains retrieved documents
"""
print("---RETRIEVE---")
question = state["question"]
embeddings = HuggingFaceEmbeddings(
model_name="nomic-ai/nomic-embed-text-v1.5",
model_kwargs={"device": "cuda", "trust_remote_code": True},
)
docsearch = Chroma(
collection_name="rag-chroma",
persist_directory="./chroma",
embedding_function=embeddings,
)
retriever = docsearch.as_retriever()
documents = retriever.invoke(question)
return {"documents": documents, "question": question}
def generate(state):
"""
Generate answer
Args:
state (dict): The current graph state
Returns:
state (dict): New key added to state, generation, that contains LLM generation
"""
print("---GENERATE---")
question = state["question"]
documents = state["documents"]
state["iterations"] += 1
rag = rag_chain()
generation = rag.invoke({"question": question, "context": documents})
print(generation)
return {
"documents": documents,
"question": question,
"generation": generation,
"iterations": state["iterations"],
}
def grade_documents(state):
"""
Determines whether the retrieved documents are relevant to the question.
Args:
state (dict): The current graph state
Returns:
state (dict): Updates documents key with only filtered relevant documents
"""
print("---CHECK DOCUMENT RELEVANCE TO QUESTION---")
question = state["question"]
documents = state["documents"]
retrieval_grader = retrieval_grader_chain()
filtered_docs = []
for d in documents:
score = retrieval_grader.invoke(
{"question": question, "document": d.page_content}
)
grade = score["score"]
if grade == "yes":
print("---GRADE: DOCUMENT RELEVANT---")
filtered_docs.append(d)
else:
print("---GRADE: DOCUMENT NOT RELEVANT---")
continue
return {"documents": filtered_docs, "question": question}
def transform_query(state):
"""
Transform the query to produce a better question.
Args:
state (dict): The current graph state
Returns:
state (dict): Updates question key with a re-phrased question
"""
print("---TRANSFORM QUERY---")
question = state["question"]
documents = state["documents"]
question_rewriter = question_rewriter_chain()
better_question = question_rewriter.invoke({"question": question})
return {"documents": documents, "question": better_question}
def decide_to_generate(state):
"""
Determines whether to generate an answer, or re-generate a question.
Args:
state (dict): The current graph state
Returns:
str: Binary decision for next node to call
"""
print("---ASSESS GRADED DOCUMENTS---")
state["question"]
filtered_documents = state["documents"]
if not filtered_documents:
print(
"---DECISION: ALL DOCUMENTS ARE NOT RELEVANT TO QUESTION, TRANSFORM QUERY---"
)
return "transform_query"
else:
print("---DECISION: GENERATE---")
return "generate"
def grade_generation_v_documents_and_question(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
"""
print("---CHECK HALLUCINATIONS---")
question = state["question"]
documents = state["documents"]
generation = state["generation"]
hallucination_grader = hallucination_grader_chain()
answer_grader = answer_grader_chain()
score = hallucination_grader.invoke(
{"documents": documents, "generation": generation}
)
grade = score["score"]
if grade == "yes":
print("---DECISION: GENERATION IS GROUNDED IN DOCUMENTS---")
print("---GRADE GENERATION vs QUESTION---")
score = answer_grader.invoke({"question": question, "generation": generation})
grade = score["score"]
if grade == "yes":
print("---DECISION: GENERATION ADDRESSES QUESTION---")
return "useful"
else:
print("---DECISION: GENERATION DOES NOT ADDRESS QUESTION---")
return "not useful"
else:
if state["iterations"] >= 5:
return "stop"
else:
print("---DECISION: GENERATION IS NOT GROUNDED IN DOCUMENTS, RE-TRY---")
print(f"---ITERATION COUNT: {state['iterations']}---")
return "not supported"
def answer(state):
"""
Return the answer
Args:
state (dict): The current graph state
Returns:
str: The answer
TODO: Check the prev state to augment the answer
"""
print("---ANSWER---")
return state
def selfRAGAgent():
workflow = StateGraph(GraphState)
# Define the nodes
workflow.add_node("retrieve", retrieve)
workflow.add_node("grade_documents", grade_documents)
workflow.add_node("generate", generate)
workflow.add_node("transform_query", transform_query)
workflow.add_node("answer", answer)
# Build graph
workflow.add_edge(START, "retrieve")
workflow.add_edge("retrieve", "grade_documents")
workflow.add_conditional_edges(
"grade_documents",
decide_to_generate,
{
"transform_query": "transform_query",
"generate": "generate",
},
)
workflow.add_edge("transform_query", "retrieve")
workflow.add_conditional_edges(
"generate",
grade_generation_v_documents_and_question,
{
"not supported": "generate",
"useful": "answer",
"not useful": "transform_query",
"stop": "answer",
},
)
workflow.add_edge("answer", END)
app = workflow.compile()
return app