midrees2806 commited on
Commit
04356f4
Β·
verified Β·
1 Parent(s): 3e83acd

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +37 -40
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: str) -> str:
80
  if not user_input.strip():
81
- return "⚠️ Please enter a valid question."
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
- prompt = f"You are an official assistant for University of Education Lahore. Respond to this greeting in a professional and friendly tone: {user_input}"
92
- return query_groq_llm(prompt) or "πŸ‘‹ Hello! How can I assist you today?"
 
 
 
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, please visit:\n"
98
- "πŸ”— https://ue.edu.pk/allfeestructure.php\n"
99
- "It contains all relevant information including tuition, admission, and semester-wise fees."
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
- # Threshold to determine match quality
109
- SIMILARITY_THRESHOLD = 0.65
110
 
111
- if best_score >= SIMILARITY_THRESHOLD:
112
  original_answer = dataset_answers[best_match_idx]
113
- prompt = f"""You are UOE AI Assistant! As an official assistant for University of Education Lahore, rephrase the following answer clearly and professionally.
114
- Use bullet points or headings if helpful. Do NOT add extra information.
 
 
 
 
115
  ### Question:
116
  {user_input}
 
117
  ### Original Answer:
118
  {original_answer}
119
- ### Rephrased Answer:"""
 
 
120
  else:
121
- manage_unmatched_queries(user_input)
122
- prompt = f"""You are UOE AI Assistant. As an official assistant for University of Education Lahore, provide a helpful response to this query.
123
- If unsure, direct the user to the official university contact options.
 
124
  ### Question:
125
  {user_input}
126
- ### Official Answer:"""
127
 
128
- # Get the response from LLM
129
- response = query_groq_llm(prompt)
 
 
130
 
131
- if response:
132
- for marker in ["Rephrased Answer:", "Official Answer:", "Improved Answer:"]:
133
- if marker in response:
134
- return response.split(marker)[-1].strip()
135
- return response # if no marker found
136
  else:
137
- return dataset_answers[best_match_idx] if best_score >= SIMILARITY_THRESHOLD else (
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