midrees2806 commited on
Commit
58dc6f1
Β·
verified Β·
1 Parent(s): 9765f7f

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +42 -47
rag.py CHANGED
@@ -63,19 +63,37 @@ def manage_unmatched_queries(query: str):
63
 
64
  def query_groq_llm(prompt):
65
  try:
66
- # Temperature 0.7 rakha hai taake har baar response rephrase ho kar aaye
67
- chat_completion = groq_client.chat.completions.create(
68
- messages=[{"role": "user", "content": prompt}],
69
- model="llama3-70b-8192",
70
- temperature=0.7,
71
- max_tokens=800
 
 
 
 
 
 
 
 
 
 
 
 
72
  )
73
- return chat_completion.choices[0].message.content.strip()
74
- except Exception as e:
75
- print(f"Error querying Groq API: {e}")
76
- return None # None return karega agar API fail hui
77
 
 
 
 
 
 
 
 
78
 
 
 
 
79
 
80
  def get_best_answer(user_input):
81
  if not user_input.strip():
@@ -84,13 +102,12 @@ def get_best_answer(user_input):
84
  user_input_lower = user_input.lower().strip()
85
 
86
  if len(user_input_lower.split()) < 3 and not any(greet in user_input_lower for greet in GREETINGS):
87
- return "Please ask your question properly with at least 3 words."
88
 
89
- # Fee Check
90
- if any(keyword in user_input_lower for keyword in ["fee structure", "fees structure", "semester fees", "semester fee"]):
91
  return (
92
- "πŸ’° **Fee Structure Information**\n\n"
93
- "University of Education Lahore ki up-to-date fee maloomat ke liye niche diye gaye official link par click karen:\n"
94
  "πŸ”— https://ue.edu.pk/allfeestructure.php"
95
  )
96
 
@@ -101,44 +118,22 @@ def get_best_answer(user_input):
101
  best_score = similarities[best_match_idx].item()
102
 
103
  if best_score >= 0.65:
104
- # PATH 1: Dataset Match
105
  original_answer = dataset_answers[best_match_idx]
106
- prompt = f"""You are the official UOE AI Assistant. Rephrase the following verified answer into a professional and attractive format.
107
- Use headings and bullet points. Do not add external facts.
 
108
 
109
  Question: {user_input}
110
  Verified Answer: {original_answer}"""
111
  else:
112
- # PATH 2: No Dataset Match - LLM Knowledge + Precise Instruction
113
  manage_unmatched_queries(user_input)
114
-
115
- prompt = f"""You are the UOE AI Assistant for University of Education (UE) Lahore.
116
  The user asked: "{user_input}".
117
 
118
- Task:
119
- 1. Answer the question using your knowledge about University of Education Lahore.
120
- 2. At the end, add this exact notice:
121
- "πŸ“’ *Note: Aapki ye query hamari support team ko forward kar di gayi hai kyunke hamare pas abhi users ki queries zyada hain. Support team isay jald verified database mein shamil kar degi taake next time aapko mazeed behtar jawab mil sakay.*"
122
- 3. Provide official contact details:
123
- 🌐 Website: https://ue.edu.pk
124
- πŸ“ž Phone: +92-42-99262231-33
125
- βœ‰οΈ Email: info@ue.edu.pk
126
-
127
- Format the response with professional headings and bold text."""
128
 
129
- llm_response = query_groq_llm(prompt)
130
-
131
- # Agar Groq ne jawab diya to wo dikhao
132
- if llm_response:
133
- return llm_response
134
-
135
- # Bilkul aakhri fallback agar Groq API down ho
136
- if best_score >= 0.65:
137
- return f"Verified Answer: {dataset_answers[best_match_idx]}"
138
- else:
139
- return (
140
- "I'm sorry, I'm having trouble connecting to my brain right now. πŸ˜…\n\n"
141
- "Lekin maine aapki query support team ko bhej di hai. Official maloomat ke liye:\n"
142
- "πŸ“ž +92-42-99262231-33\n"
143
- "βœ‰οΈ info@ue.edu.pk"
144
- )
 
63
 
64
  def query_groq_llm(prompt):
65
  try:
66
+ # Using the updated Llama 3.3 70B model with your specific parameters
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. Provide professional, structured responses."
73
+ },
74
+ {
75
+ "role": "user",
76
+ "content": prompt
77
+ }
78
+ ],
79
+ temperature=1,
80
+ max_completion_tokens=1024,
81
+ top_p=1,
82
+ stream=True, # As requested
83
+ stop=None
84
  )
 
 
 
 
85
 
86
+ full_response = ""
87
+ for chunk in completion:
88
+ content = chunk.choices[0].delta.content or ""
89
+ full_response += content
90
+ # Optional: print(content, end="") # Keeps the terminal streaming effect
91
+
92
+ return full_response.strip()
93
 
94
+ except Exception as e:
95
+ print(f"Groq API Error: {e}")
96
+ return None
97
 
98
  def get_best_answer(user_input):
99
  if not user_input.strip():
 
102
  user_input_lower = user_input.lower().strip()
103
 
104
  if len(user_input_lower.split()) < 3 and not any(greet in user_input_lower for greet in GREETINGS):
105
+ return "Please provide more details. I need at least 3 words to understand your query properly."
106
 
107
+ if any(kw in user_input_lower for kw in ["fee structure", "fees structure", "semester fees", "semester fee"]):
 
108
  return (
109
+ "πŸ’° **Official Fee Structure**\n\n"
110
+ "For the most accurate and up-to-date fee details, please visit the official University of Education link:\n"
111
  "πŸ”— https://ue.edu.pk/allfeestructure.php"
112
  )
113
 
 
118
  best_score = similarities[best_match_idx].item()
119
 
120
  if best_score >= 0.65:
 
121
  original_answer = dataset_answers[best_match_idx]
122
+ prompt = f"""You are the official UOE AI Assistant.
123
+ Rephrase and improve the following verified university answer to be professional and attractive.
124
+ Use bullet points and bold text where necessary.
125
 
126
  Question: {user_input}
127
  Verified Answer: {original_answer}"""
128
  else:
 
129
  manage_unmatched_queries(user_input)
130
+ prompt = f"""You are the UOE AI Assistant for University of Education Lahore.
 
131
  The user asked: "{user_input}".
132
 
133
+ Instructions:
134
+ 1. Answer using your knowledge about this university.
135
+ 2. MUST add this exact notice at the end:
136
+ "πŸ“’ *Note: Your query has been forwarded to our support team. We are currently updating our verified database to include this information soon.*"
137
+ 3. Official Contacts: Website: https://ue.edu.pk | Phone: +92-42-99262231-33"""
 
 
 
 
 
138
 
139
+ return query_groq_llm(prompt)