midrees2806 commited on
Commit
7b27360
Β·
verified Β·
1 Parent(s): 038c54c

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +16 -12
rag.py CHANGED
@@ -27,10 +27,10 @@ GREETINGS = [
27
  "hey there", "greetings"
28
  ]
29
 
30
- # Normalize user input for internal processing
31
  def normalize_input(text):
32
  text = text.lower().strip()
33
- text = text.replace("which", "what")
34
  return text
35
 
36
  # Load local dataset
@@ -82,12 +82,17 @@ def query_groq_llm(prompt, model_name="llama3-70b-8192"):
82
  print(f"Error querying Groq API: {e}")
83
  return ""
84
 
85
- # Main logic function to be called from Gradio or elsewhere
86
  def get_best_answer(user_input):
87
  if not user_input.strip():
88
  return "Please enter a valid question."
89
 
90
- user_input_lower = user_input.lower().strip()
 
 
 
 
 
91
 
92
  if len(user_input_lower.split()) < 3 and not any(greet in user_input_lower for greet in GREETINGS):
93
  return "Please ask your question properly with at least 3 words."
@@ -95,7 +100,7 @@ def get_best_answer(user_input):
95
  if any(greet in user_input_lower for greet in GREETINGS):
96
  greeting_response = query_groq_llm(
97
  f"You are an official assistant for University of Education Lahore. "
98
- f"Respond to this greeting in a friendly and professional manner: {user_input}"
99
  )
100
  return greeting_response if greeting_response else "Hello! How can I assist you today?"
101
 
@@ -106,15 +111,14 @@ def get_best_answer(user_input):
106
  "πŸ”— https://ue.edu.pk/allfeestructure.php"
107
  )
108
 
109
- # Normalize input for similarity
110
- normalized_input = normalize_input(user_input_lower)
111
- user_embedding = similarity_model.encode(normalized_input, convert_to_tensor=True)
112
  similarities = util.pytorch_cos_sim(user_embedding, dataset_embeddings)[0]
113
  best_match_idx = similarities.argmax().item()
114
  best_score = similarities[best_match_idx].item()
115
 
116
  if best_score < 0.65:
117
- manage_unmatched_queries(user_input)
118
 
119
  if best_score >= 0.65:
120
  original_answer = dataset_answers[best_match_idx]
@@ -123,7 +127,7 @@ Rephrase the following official answer clearly and professionally.
123
  Use structured formatting (like headings, bullet points, or numbered lists) where appropriate.
124
  DO NOT add any new or extra information. ONLY rephrase and improve the clarity and formatting of the original answer.
125
  ### Question:
126
- {user_input}
127
  ### Original Answer:
128
  {original_answer}
129
  ### Rephrased Answer:
@@ -133,7 +137,7 @@ DO NOT add any new or extra information. ONLY rephrase and improve the clarity a
133
  Include relevant details about university policies.
134
  If unsure, direct to official channels.
135
  ### Question:
136
- {user_input}
137
  ### Official Answer:
138
  """
139
 
@@ -150,4 +154,4 @@ If unsure, direct to official channels.
150
  "πŸ“ž +92-42-99262231-33\n"
151
  "βœ‰οΈ [email protected]\n"
152
  "🌐 https://ue.edu.pk"
153
- )
 
27
  "hey there", "greetings"
28
  ]
29
 
30
+ # Normalize user input for internal processing (with 'which' to 'what' replacement)
31
  def normalize_input(text):
32
  text = text.lower().strip()
33
+ text = text.replace("which", "what") # Add your requested replacement
34
  return text
35
 
36
  # Load local dataset
 
82
  print(f"Error querying Groq API: {e}")
83
  return ""
84
 
85
+ # Main logic function (with hidden 'which' to 'what' replacement)
86
  def get_best_answer(user_input):
87
  if not user_input.strip():
88
  return "Please enter a valid question."
89
 
90
+ # Preserve original input for display
91
+ original_input = user_input
92
+
93
+ # Normalize input for processing (with hidden replacement)
94
+ processed_input = normalize_input(user_input)
95
+ user_input_lower = processed_input # Use normalized version for processing
96
 
97
  if len(user_input_lower.split()) < 3 and not any(greet in user_input_lower for greet in GREETINGS):
98
  return "Please ask your question properly with at least 3 words."
 
100
  if any(greet in user_input_lower for greet in GREETINGS):
101
  greeting_response = query_groq_llm(
102
  f"You are an official assistant for University of Education Lahore. "
103
+ f"Respond to this greeting in a friendly and professional manner: {original_input}"
104
  )
105
  return greeting_response if greeting_response else "Hello! How can I assist you today?"
106
 
 
111
  "πŸ”— https://ue.edu.pk/allfeestructure.php"
112
  )
113
 
114
+ # Use normalized input for similarity matching
115
+ user_embedding = similarity_model.encode(user_input_lower, convert_to_tensor=True)
 
116
  similarities = util.pytorch_cos_sim(user_embedding, dataset_embeddings)[0]
117
  best_match_idx = similarities.argmax().item()
118
  best_score = similarities[best_match_idx].item()
119
 
120
  if best_score < 0.65:
121
+ manage_unmatched_queries(original_input) # Store original query
122
 
123
  if best_score >= 0.65:
124
  original_answer = dataset_answers[best_match_idx]
 
127
  Use structured formatting (like headings, bullet points, or numbered lists) where appropriate.
128
  DO NOT add any new or extra information. ONLY rephrase and improve the clarity and formatting of the original answer.
129
  ### Question:
130
+ {original_input} # Show original to user
131
  ### Original Answer:
132
  {original_answer}
133
  ### Rephrased Answer:
 
137
  Include relevant details about university policies.
138
  If unsure, direct to official channels.
139
  ### Question:
140
+ {original_input} # Show original to user
141
  ### Official Answer:
142
  """
143
 
 
154
  "πŸ“ž +92-42-99262231-33\n"
155
  "βœ‰οΈ [email protected]\n"
156
  "🌐 https://ue.edu.pk"
157
+ )