midrees2806 commited on
Commit
47f4327
·
verified ·
1 Parent(s): 20ecec9

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +34 -34
rag.py CHANGED
@@ -63,13 +63,13 @@ def manage_unmatched_queries(query: str):
63
 
64
  def query_groq_llm(prompt):
65
  try:
66
- # Llama-3.3-70b-versatile with streaming enabled
67
  completion = groq_client.chat.completions.create(
68
  model="llama-3.3-70b-versatile",
69
  messages=[
70
  {
71
  "role": "system",
72
- "content": "You are the official UOE AI Assistant. Your goal is to provide highly structured, professional, and easy-to-read information about the University of Education Lahore."
73
  },
74
  {
75
  "role": "user",
@@ -100,56 +100,56 @@ def get_best_answer(user_input):
100
 
101
  user_input_lower = user_input.lower().strip()
102
 
103
- # Greeting Handling
104
  if any(greet == user_input_lower for greet in GREETINGS):
105
- return "Walaikum Assalam! I am the UOE AI Assistant. How can I help you regarding admissions, fees, or programs today?"
106
 
107
- # Length check
108
  if len(user_input_lower.split()) < 3 and not any(greet in user_input_lower for greet in GREETINGS):
109
- return "Please provide more details. I need at least 3 words to understand your query properly."
110
-
111
- # Direct Fee Link
112
- if any(kw in user_input_lower for kw in ["fee structure", "fees structure", "semester fees", "semester fee"]):
113
- return (
114
- "💰 **Official Fee Structure**\n\n"
115
- "For the most accurate and up-to-date fee details, please visit the official University of Education link:\n"
116
- "🔗 https://ue.edu.pk/allfeestructure.php"
117
- )
118
 
119
- # Similarity Calculation
120
  user_embedding = similarity_model.encode(user_input_lower, convert_to_tensor=True)
121
  similarities = util.pytorch_cos_sim(user_embedding, dataset_embeddings)[0]
122
  best_match_idx = similarities.argmax().item()
123
  best_score = similarities[best_match_idx].item()
124
 
125
  if best_score >= 0.65:
126
- # --- PATH 1: DATASET MATCH (Enhanced Formatting) ---
127
  original_answer = dataset_answers[best_match_idx]
 
128
  prompt = f"""You are the official University of Education (UOE) Assistant.
129
- I have found a verified answer in our database. Please rephrase it to make it extremely professional and user-friendly.
130
 
131
- STRICT GUIDELINES:
132
- 1. Use clear **Headings**.
133
- 2. Use **Bullet Points** for lists.
134
- 3. If there is data like timing, criteria, or contact info, present it in a **Markdown Table**.
135
- 4. Bold important keywords.
136
- 5. Maintain a polite and welcoming tone.
137
 
138
  User Question: {user_input}
139
  Verified Data: {original_answer}
140
-
141
- Enhanced Response:"""
142
  else:
143
- # --- PATH 2: GENERAL KNOWLEDGE (with Forwarding Note) ---
144
  manage_unmatched_queries(user_input)
145
- prompt = f"""You are the UOE AI Assistant for University of Education Lahore.
146
- The user asked: "{user_input}". This query is not in our verified database.
147
-
148
  Instructions:
149
- 1. Answer based on your general knowledge about UOE Lahore.
150
- 2. Format the response with headings and bold text.
151
- 3. At the end, you MUST add this exact text:
152
- "📢 *Note: Your query has been forwarded to our support team. We are currently updating our verified database to provide a verified answer next time.*"
153
- 4. List official contacts: Website: https://ue.edu.pk | Phone: +92-42-99262231-33"""
 
 
 
154
 
155
  return query_groq_llm(prompt)
 
63
 
64
  def query_groq_llm(prompt):
65
  try:
66
+ # Using Llama-3.3-70b-versatile with streaming enabled
67
  completion = groq_client.chat.completions.create(
68
  model="llama-3.3-70b-versatile",
69
  messages=[
70
  {
71
  "role": "system",
72
+ "content": "You are the official UOE AI Assistant. You provide professional, structured information about University of Education Lahore using headings, bold text, and tables."
73
  },
74
  {
75
  "role": "user",
 
100
 
101
  user_input_lower = user_input.lower().strip()
102
 
103
+ # 1. Greeting Handling
104
  if any(greet == user_input_lower for greet in GREETINGS):
105
+ return "Hello! I am the UOE AI Assistant. How can I assist you today regarding University of Education Lahore's admissions, programs, or fee structures?"
106
 
107
+ # 2. Basic Validation
108
  if len(user_input_lower.split()) < 3 and not any(greet in user_input_lower for greet in GREETINGS):
109
+ return "Please ask your question with at least 3 words so I can provide a detailed answer."
110
+
111
+ # 3. Fee Keyword Detection (Strict)
112
+ fee_keywords = ["fee", "fees", "fee structure", "fees structure", "semester fee", "admission fee", "dues"]
113
+ needs_fee_link = any(kw in user_input_lower for kw in fee_keywords)
114
+ OFFICIAL_FEE_LINK = "https://ue.edu.pk/allfeestructure.php"
 
 
 
115
 
116
+ # 4. Similarity Calculation
117
  user_embedding = similarity_model.encode(user_input_lower, convert_to_tensor=True)
118
  similarities = util.pytorch_cos_sim(user_embedding, dataset_embeddings)[0]
119
  best_match_idx = similarities.argmax().item()
120
  best_score = similarities[best_match_idx].item()
121
 
122
  if best_score >= 0.65:
123
+ # PATH 1: Verified Data Match
124
  original_answer = dataset_answers[best_match_idx]
125
+
126
  prompt = f"""You are the official University of Education (UOE) Assistant.
127
+ I found a verified answer. Please rephrase it to be professional, attractive, and structured.
128
 
129
+ GUIDELINES:
130
+ - Use clear Headings and Bullet Points.
131
+ - Create a Markdown Table if the data involves timings, eligibility, or numeric values.
132
+ - {'CRITICAL: The user mentioned fees. You MUST prominently include and rephrase this link: ' + OFFICIAL_FEE_LINK if needs_fee_link else 'Always mention this official fee link at the end: ' + OFFICIAL_FEE_LINK}
133
+ - Bold important keywords.
 
134
 
135
  User Question: {user_input}
136
  Verified Data: {original_answer}
137
+
138
+ Response:"""
139
  else:
140
+ # PATH 2: No Dataset Match (LLM Knowledge + Forwarding)
141
  manage_unmatched_queries(user_input)
142
+
143
+ prompt = f"""You are the UOE AI Assistant. I don't have a verified record for this specific question.
144
+
145
  Instructions:
146
+ 1. Answer based on your knowledge of University of Education Lahore.
147
+ 2. Use professional formatting (headings/bold).
148
+ 3. {'The user is asking about fees, so emphasize this link: ' + OFFICIAL_FEE_LINK if needs_fee_link else 'Mention that fee details are available at: ' + OFFICIAL_FEE_LINK}
149
+ 4. Add this exact note: "📢 *Note: Your query has been forwarded to our support team. We are currently updating our verified database to include this information soon.*"
150
+ 5. Provide official contact info: Website: https://ue.edu.pk | Phone: +92-42-99262231-33
151
+
152
+ User Question: {user_input}
153
+ Response:"""
154
 
155
  return query_groq_llm(prompt)