File size: 10,161 Bytes
e029e22 |
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 |
# Adapted from https://github.com/langchain-ai/langgraph/blob/main/examples/rag/langgraph_crag.ipynb?ref=blog.langchain.dev
from typing import List
from typing_extensions import TypedDict
from langgraph.graph import END, StateGraph, START
from modules.chat.base import BaseRAG
from langchain.memory import ChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
class GradeDocuments(BaseModel):
"""Binary score for relevance check on retrieved documents."""
binary_score: str = Field(
description="Documents are relevant to the question, 'yes' or 'no'"
)
class GraphState(TypedDict):
"""
Represents the state of our graph.
Attributes:
question: question
generation: LLM generation
documents: list of documents
"""
question: str
generation: str
documents: List[str]
class Langgraph_RAG(BaseRAG):
def __init__(self, llm, memory, retriever, qa_prompt: str, rephrase_prompt: str):
"""
Initialize the Langgraph_RAG class.
Args:
llm (LanguageModelLike): The language model instance.
memory (BaseChatMessageHistory): The chat message history instance.
retriever (BaseRetriever): The retriever instance.
qa_prompt (str): The QA prompt string.
rephrase_prompt (str): The rephrase prompt string.
"""
self.llm = llm
self.structured_llm_grader = llm.with_structured_output(GradeDocuments)
self.memory = self.add_history_from_list(memory)
self.retriever = retriever
self.qa_prompt = (
"You are an AI Tutor for the course DS598, taught by Prof. Thomas Gardos. Answer the user's question using the provided context. Only use the context if it is relevant. The context is ordered by relevance. "
"If you don't know the answer, do your best without making things up. Keep the conversation flowing naturally. "
"Speak in a friendly and engaging manner, like talking to a friend. Avoid sounding repetitive or robotic.\n\n"
"Context:\n{context}\n\n"
"Answer the student's question below in a friendly, concise, and engaging manner. Use the context and history only if relevant, otherwise, engage in a free-flowing conversation.\n"
"Student: {question}\n"
"AI Tutor:"
)
self.rephrase_prompt = rephrase_prompt
self.store = {}
## Fix below ##
system = """You are a grader assessing relevance of a retrieved document to a user question. \n
If the document contains keyword(s) or semantic meaning related to the question, grade it as relevant. \n
Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question."""
grade_prompt = ChatPromptTemplate.from_messages(
[
("system", system),
(
"human",
"Retrieved document: \n\n {document} \n\n User question: {question}",
),
]
)
self.retrieval_grader = grade_prompt | self.structured_llm_grader
system = """You a question re-writer that converts an input question to a better version that is optimized \n
for web search. Look at the input and try to reason about the underlying semantic intent / meaning."""
re_write_prompt = ChatPromptTemplate.from_messages(
[
("system", system),
(
"human",
"Here is the initial question: \n\n {question} \n Formulate an improved question.",
),
]
)
self.question_rewriter = re_write_prompt | self.llm | StrOutputParser()
# Generate
self.qa_prompt_template = ChatPromptTemplate.from_template(self.qa_prompt)
self.rag_chain = self.qa_prompt_template | self.llm | StrOutputParser()
###
# build the agentic graph
self.app = self.create_agentic_graph()
def retrieve(self, 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"]
# Retrieval
documents = self.retriever.get_relevant_documents(question)
return {"documents": documents, "question": question}
def generate(self, 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"]
# RAG generation
generation = self.rag_chain.invoke({"context": documents, "question": question})
return {"documents": documents, "question": question, "generation": generation}
def transform_query(self, 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"]
# Re-write question
better_question = self.question_rewriter.invoke({"question": question})
return {"documents": documents, "question": better_question}
def grade_documents(self, 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"]
# 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.binary_score
if grade == "yes":
print("---GRADE: DOCUMENT RELEVANT---")
filtered_docs.append(d)
else:
print("---GRADE: DOCUMENT NOT RELEVANT---")
web_search = "Yes"
continue
return {
"documents": filtered_docs,
"question": question,
"web_search": web_search,
}
def decide_to_generate(self, 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"]
web_search = state["web_search"]
state["documents"]
if web_search == "Yes":
# All documents have been filtered check_relevance
# We will re-generate a new query
print(
"---DECISION: ALL DOCUMENTS ARE NOT RELEVANT TO QUESTION, TRANSFORM QUERY---"
)
return "transform_query"
else:
# We have relevant documents, so generate answer
print("---DECISION: GENERATE---")
return "generate"
def create_agentic_graph(self):
"""
Create an agentic graph to answer questions.
Returns:
dict: Agentic graph
"""
self.workflow = StateGraph(GraphState)
self.workflow.add_node("retrieve", self.retrieve)
self.workflow.add_node(
"grade_documents", self.grade_documents
) # grade documents
self.workflow.add_node("generate", self.generate) # generatae
self.workflow.add_node(
"transform_query", self.transform_query
) # transform_query
# build the graph
self.workflow.add_edge(START, "retrieve")
self.workflow.add_edge("retrieve", "grade_documents")
self.workflow.add_conditional_edges(
"grade_documents",
self.decide_to_generate,
{
"transform_query": "transform_query",
"generate": "generate",
},
)
self.workflow.add_edge("transform_query", "generate")
self.workflow.add_edge("generate", END)
# Compile
app = self.workflow.compile()
return app
def invoke(self, user_query, config):
"""
Invoke the chain.
Args:
kwargs: The input variables.
Returns:
dict: The output variables.
"""
inputs = {
"question": user_query["input"],
}
for output in self.app.stream(inputs):
for key, value in output.items():
# Node
print(f"Node {key} returned: {value}")
print("\n\n")
print(value["generation"])
# rename generation to answer
value["answer"] = value.pop("generation")
value["context"] = value.pop("documents")
return value
def add_history_from_list(self, history_list):
"""
Add messages from a list to the chat history.
Args:
messages (list): The list of messages to add.
"""
history = ChatMessageHistory()
for idx, message_pairs in enumerate(history_list):
history.add_user_message(message_pairs[0])
history.add_ai_message(message_pairs[1])
return history
|