-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditional_RAG.py
More file actions
190 lines (140 loc) · 5.7 KB
/
Copy pathconditional_RAG.py
File metadata and controls
190 lines (140 loc) · 5.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
import os
from typing import TypedDict, Annotated
from langgraph.graph.message import add_messages
from langgraph.graph import StateGraph , START , END
from langchain_groq import ChatGroq
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from dotenv import load_dotenv
load_dotenv()
#Step 1 - Building the RAG retrievers
embeddings = HuggingFaceEmbeddings(model_name = "sentence-transformers/all-MiniLM-L6-v2" )
def build_retriver(pdf_path : str):
loader = PyPDFLoader(pdf_path)
document = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size = 800,
chunk_overlap = 100)
chunks = splitter.split_documents(document)
vectorstore = FAISS.from_documents(chunks,embeddings)
return vectorstore.as_retriever(search_kwargs = {"k":4})
acedemic_retriever = build_retriver("academics_handbook.pdf")
fee_retriever = build_retriver("fee_structure.pdf")
llm = ChatGroq(model="llama-3.3-70b-versatile", temperature=0.4)
#step2 - State
class State(TypedDict):
programme : str
messages : Annotated[list,add_messages]
query_type : str
retrieved_context : str
#Step 3 - Nodes generation
def classifier_node(state : State) -> dict:
"""Look at the latest user message and decide which path to take."""
last_message = state['messages'][-1].content
prompt = (
"Classify the following student query into exactly one category: "
"'academic', 'fee', or 'general'.\n\n"
"Use 'academic' for questions about attendance, exams, grading, credits, "
"promotion, course structure, summer training, or degree requirements.\n"
"Use 'fee' for questions about tuition, payment, refund, late charges, "
"scholarships, or any money-related topic.\n"
"Use 'general' for greetings, casual talk, or anything not related to "
"the college rules or fee.\n\n"
f"Query: {last_message}\n\n"
"Return only one word: academic, fee, or general."
)
response = llm.invoke(prompt)
category = response.content.strip().lower()
if "academic" in category:
category = "academic"
elif "fee" in category:
category = "fee"
else:
category = "general"
return {"query_type" : category}
def academic_rag_node(state: State) -> dict:
"""Retrieves relevant chunks from the academics handbook."""
query = state["messages"][-1].content
docs = acedemic_retriever.invoke(query)
context = "\n\n".join([doc.page_content for doc in docs])
return {"retrieved_context": context}
def fee_rag_node(state: State) -> dict:
"""Retrieves relevant chunks from the fee structure PDF."""
query = state["messages"][-1].content
docs = fee_retriever.invoke(query)
context = "\n\n".join([doc.page_content for doc in docs])
return {"retrieved_context": context}
def general_node(state: State) -> dict:
"""Answers directly using the LLM's own knowledge, no retrieval needed."""
return {"retrieved_context": "NO_RETRIEVAL_NEEDED"}
def response_node(state: State) -> dict:
"""Generates the final answer, personalized using the student's programme."""
query = state["messages"][-1].content
programme = state.get("programme", "Unknown")
context = state["retrieved_context"]
if context == "NO_RETRIEVAL_NEEDED":
prompt = (
f"You are a friendly college assistant talking to a {programme} student. "
f"Answer this question using your own general knowledge:\n\n{query}"
)
else:
prompt = (
f"You are a college assistant helping a {programme} student. "
f"Use the following context from the official college documents to answer "
f"the question accurately. If the context mentions specific figures for "
f"different programmes, highlight the one relevant to {programme} if possible.\n\n"
f"Context:\n{context}\n\n"
f"Question: {query}\n\n"
f"Give a clear, friendly, and precise answer."
)
response = llm.invoke(prompt)
return {"messages": [("ai", response.content.strip())]}
#step 4 - router function
def route_query(state:State):
if state['query_type'] == 'academic':
return "academic_rag"
elif state['query_type'] == "fee":
return "fee_rag"
else:
return "general"
#step 5 - Building the graph
graph = StateGraph(State)
graph.add_node("classifier",classifier_node)
graph.add_node("academic_rag",academic_rag_node)
graph.add_node("fee_rag",fee_rag_node)
graph.add_node("general",general_node)
graph.add_node("response",response_node)
#edges
graph.add_edge(START,"classifier")
graph.add_conditional_edges(
"classifier",route_query
)
graph.add_edge("academic_rag","response")
graph.add_edge("fee_rag","response")
graph.add_edge("general","response")
graph.add_edge("response",END)
app = graph.compile()
#step 6 - Run the code
print("welcome to the College assistant \n\n")
print("which programe are you in ")
print("1. BCA")
print("2. BBA")
print("3. B.com (H)")
choice = input("\nEnter 1, 2 or 3 ")
programme_map = {
"1": "BCA",
"2": "BBA",
"3": "B.Com (H)"
}
student_programme = programme_map.get(choice, "BCA")
print(f"\nGreat! You're set as a {student_programme} student.")
while True:
user_query = input("You: ")
if user_query.lower() in ["exit","quit"]:
break
result = app.invoke({
"programme": student_programme,
"messages": [("human",user_query)]
})
print(f"Assistant : {result['messages'][-1].content}")