midrees2806 commited on
Commit
48ad339
·
verified ·
1 Parent(s): 7cd8518

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +13 -23
rag.py CHANGED
@@ -6,6 +6,7 @@ import os
6
  import pandas as pd
7
  from datasets import load_dataset, Dataset
8
  from dotenv import load_dotenv
 
9
 
10
  # Load environment variables
11
  load_dotenv()
@@ -27,6 +28,14 @@ GREETINGS = [
27
  "hey there", "greetings"
28
  ]
29
 
 
 
 
 
 
 
 
 
30
  # Load local dataset
31
  try:
32
  with open('dataset.json', 'r') as f:
@@ -100,32 +109,18 @@ def get_best_answer(user_input):
100
 
101
  if best_score < 0.65:
102
  manage_unmatched_queries(user_input)
 
103
 
104
- if best_score >= 0.65:
105
- original_answer = dataset_answers[best_match_idx]
106
- prompt = f"""Name is UOE AI Assistant! You are an official assistant for the University of Education Lahore.
107
-
108
  Rephrase the following official answer clearly and professionally.
109
  Use structured formatting (like headings, bullet points, or numbered lists) where appropriate.
110
  DO NOT add any new or extra information. ONLY rephrase and improve the clarity and formatting of the original answer.
111
-
112
  ### Question:
113
  {user_input}
114
-
115
  ### Original Answer:
116
  {original_answer}
117
-
118
  ### Rephrased Answer:
119
- """
120
- else:
121
- prompt = f"""Name is UOE AI Assistant! As an official assistant for University of Education Lahore, provide a helpful response:
122
- Include relevant details about university policies.
123
- If unsure, direct to official channels.
124
-
125
- ### Question:
126
- {user_input}
127
-
128
- ### Official Answer:
129
  """
130
 
131
  llm_response = query_groq_llm(prompt)
@@ -136,9 +131,4 @@ If unsure, direct to official channels.
136
  return llm_response.split(marker)[-1].strip()
137
  return llm_response
138
  else:
139
- return dataset_answers[best_match_idx] if best_score >= 0.65 else (
140
- "For official information:\n"
141
- "📞 +92-42-99262231-33\n"
142
- "✉️ [email protected]\n"
143
- "🌐 https://ue.edu.pk"
144
- )
 
6
  import pandas as pd
7
  from datasets import load_dataset, Dataset
8
  from dotenv import load_dotenv
9
+ import random
10
 
11
  # Load environment variables
12
  load_dotenv()
 
28
  "hey there", "greetings"
29
  ]
30
 
31
+ # Fixed rephrased unmatched query responses
32
+ UNMATCHED_RESPONSES = [
33
+ "Thank you for your query. We’ve forwarded it to our support team and it will be added soon. In the meantime, you can visit the University of Education official website or reach out via the contact details below.\n\n📞 +92-42-99262231-33\n✉️ [email protected]\n🌐 https://ue.edu.pk",
34
+ "We’ve noted your question and it’s in queue for inclusion. For now, please check the University of Education website or contact the administration directly.\n\n📞 +92-42-99262231-33\n✉️ [email protected]\n🌐 https://ue.edu.pk",
35
+ "Your query has been recorded. We’ll update the system with relevant information shortly. Meanwhile, you can visit UE's official site or reach out using the details below:\n\n📞 +92-42-99262231-33\n✉️ [email protected]\n🌐 https://ue.edu.pk",
36
+ "We appreciate your question. It has been forwarded for further processing. Until it’s available here, feel free to visit the official UE website or use the contact options:\n\n📞 +92-42-99262231-33\n✉️ [email protected]\n🌐 https://ue.edu.pk"
37
+ ]
38
+
39
  # Load local dataset
40
  try:
41
  with open('dataset.json', 'r') as f:
 
109
 
110
  if best_score < 0.65:
111
  manage_unmatched_queries(user_input)
112
+ return random.choice(UNMATCHED_RESPONSES)
113
 
114
+ original_answer = dataset_answers[best_match_idx]
115
+ prompt = f"""Name is UOE AI Assistant! You are an official assistant for the University of Education Lahore.
 
 
116
  Rephrase the following official answer clearly and professionally.
117
  Use structured formatting (like headings, bullet points, or numbered lists) where appropriate.
118
  DO NOT add any new or extra information. ONLY rephrase and improve the clarity and formatting of the original answer.
 
119
  ### Question:
120
  {user_input}
 
121
  ### Original Answer:
122
  {original_answer}
 
123
  ### Rephrased Answer:
 
 
 
 
 
 
 
 
 
 
124
  """
125
 
126
  llm_response = query_groq_llm(prompt)
 
131
  return llm_response.split(marker)[-1].strip()
132
  return llm_response
133
  else:
134
+ return dataset_answers[best_match_idx]