Spaces:
Running
Running
Update rag.py
Browse files
rag.py
CHANGED
@@ -76,76 +76,73 @@ def query_groq_llm(prompt: str, model_name="llama3-70b-8192") -> str:
|
|
76 |
return ""
|
77 |
|
78 |
# β
Main RAG logic
|
79 |
-
def get_best_answer(user_input
|
80 |
if not user_input.strip():
|
81 |
-
return "
|
82 |
|
83 |
user_input_lower = user_input.lower().strip()
|
84 |
|
85 |
-
# Handle short or vague questions
|
86 |
-
if len(user_input_lower.split()) < 3 and not any(g in user_input_lower for g in GREETINGS):
|
87 |
-
return "π Please provide more details or ask a complete question (at least 3 words)."
|
88 |
-
|
89 |
-
# Handle greetings
|
90 |
if any(greet in user_input_lower for greet in GREETINGS):
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
93 |
|
94 |
-
# Handle direct FAQ (e.g., fee structure)
|
95 |
if any(keyword in user_input_lower for keyword in ["fee structure", "fees structure", "semester fees", "semester fee"]):
|
96 |
return (
|
97 |
-
"π° For complete and up-to-date fee details for this program,
|
98 |
-
"
|
99 |
-
"
|
100 |
)
|
101 |
|
102 |
-
# Semantic search for best matching question
|
103 |
user_embedding = similarity_model.encode(user_input_lower, convert_to_tensor=True)
|
104 |
similarities = util.pytorch_cos_sim(user_embedding, dataset_embeddings)[0]
|
105 |
best_match_idx = similarities.argmax().item()
|
106 |
best_score = similarities[best_match_idx].item()
|
107 |
|
108 |
-
|
109 |
-
|
110 |
|
111 |
-
if best_score >=
|
112 |
original_answer = dataset_answers[best_match_idx]
|
113 |
-
prompt = f"""
|
114 |
-
|
|
|
|
|
|
|
|
|
115 |
### Question:
|
116 |
{user_input}
|
|
|
117 |
### Original Answer:
|
118 |
{original_answer}
|
119 |
-
|
|
|
|
|
120 |
else:
|
121 |
-
|
122 |
-
|
123 |
-
If unsure, direct
|
|
|
124 |
### Question:
|
125 |
{user_input}
|
126 |
-
### Official Answer:"""
|
127 |
|
128 |
-
|
129 |
-
|
|
|
|
|
130 |
|
131 |
-
if
|
132 |
-
for marker in ["
|
133 |
-
if marker in
|
134 |
-
return
|
135 |
-
return
|
136 |
else:
|
137 |
-
return dataset_answers[best_match_idx] if best_score >=
|
138 |
"For official information:\n"
|
139 |
"π +92-42-99262231-33\n"
|
140 |
"βοΈ [email protected]\n"
|
141 |
"π https://ue.edu.pk"
|
142 |
)
|
143 |
|
144 |
-
# β
Example (for direct testing)
|
145 |
-
if __name__ == "__main__":
|
146 |
-
while True:
|
147 |
-
user_input = input("\nπ§βπ You: ")
|
148 |
-
if user_input.lower() in ["exit", "quit"]:
|
149 |
-
break
|
150 |
-
answer = get_best_answer(user_input)
|
151 |
-
print(f"\nπ€ UOE Assistant:\n{answer}")
|
|
|
76 |
return ""
|
77 |
|
78 |
# β
Main RAG logic
|
79 |
+
def get_best_answer(user_input):
|
80 |
if not user_input.strip():
|
81 |
+
return "Please enter a valid question."
|
82 |
|
83 |
user_input_lower = user_input.lower().strip()
|
84 |
|
|
|
|
|
|
|
|
|
|
|
85 |
if any(greet in user_input_lower for greet in GREETINGS):
|
86 |
+
greeting_response = query_groq_llm(
|
87 |
+
f"You are an official assistant for University of Education Lahore. "
|
88 |
+
f"Respond to this greeting in a friendly and professional manner: {user_input}"
|
89 |
+
)
|
90 |
+
return greeting_response if greeting_response else "Hello! How can I assist you today?"
|
91 |
|
|
|
92 |
if any(keyword in user_input_lower for keyword in ["fee structure", "fees structure", "semester fees", "semester fee"]):
|
93 |
return (
|
94 |
+
"π° For complete and up-to-date fee details for this program, we recommend visiting the official University of Education fee structure page.\n"
|
95 |
+
"You'll find comprehensive information regarding tuition, admission charges, and other applicable fees there.\n"
|
96 |
+
"π https://ue.edu.pk/allfeestructure.php"
|
97 |
)
|
98 |
|
|
|
99 |
user_embedding = similarity_model.encode(user_input_lower, convert_to_tensor=True)
|
100 |
similarities = util.pytorch_cos_sim(user_embedding, dataset_embeddings)[0]
|
101 |
best_match_idx = similarities.argmax().item()
|
102 |
best_score = similarities[best_match_idx].item()
|
103 |
|
104 |
+
if best_score < 0.65:
|
105 |
+
manage_unmatched_queries(user_input)
|
106 |
|
107 |
+
if best_score >= 0.65:
|
108 |
original_answer = dataset_answers[best_match_idx]
|
109 |
+
prompt = f"""Name is UOE AI Assistant! You are an official assistant for the University of Education Lahore.
|
110 |
+
|
111 |
+
Rephrase the following official answer clearly and professionally.
|
112 |
+
Use structured formatting (like headings, bullet points, or numbered lists) where appropriate.
|
113 |
+
DO NOT add any new or extra information. ONLY rephrase and improve the clarity and formatting of the original answer.
|
114 |
+
|
115 |
### Question:
|
116 |
{user_input}
|
117 |
+
|
118 |
### Original Answer:
|
119 |
{original_answer}
|
120 |
+
|
121 |
+
### Rephrased Answer:
|
122 |
+
"""
|
123 |
else:
|
124 |
+
prompt = f"""Name is UOE AI Assistant! As an official assistant for University of Education Lahore, provide a helpful response:
|
125 |
+
Include relevant details about university policies.
|
126 |
+
If unsure, direct to official channels.
|
127 |
+
|
128 |
### Question:
|
129 |
{user_input}
|
|
|
130 |
|
131 |
+
### Official Answer:
|
132 |
+
"""
|
133 |
+
|
134 |
+
llm_response = query_groq_llm(prompt)
|
135 |
|
136 |
+
if llm_response:
|
137 |
+
for marker in ["Improved Answer:", "Official Answer:", "Rephrased Answer:"]:
|
138 |
+
if marker in llm_response:
|
139 |
+
return llm_response.split(marker)[-1].strip()
|
140 |
+
return llm_response
|
141 |
else:
|
142 |
+
return dataset_answers[best_match_idx] if best_score >= 0.65 else (
|
143 |
"For official information:\n"
|
144 |
"π +92-42-99262231-33\n"
|
145 |
"βοΈ [email protected]\n"
|
146 |
"π https://ue.edu.pk"
|
147 |
)
|
148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|