Spaces:
Sleeping
Sleeping
Update rag.py
Browse files
rag.py
CHANGED
|
@@ -63,19 +63,37 @@ def manage_unmatched_queries(query: str):
|
|
| 63 |
|
| 64 |
def query_groq_llm(prompt):
|
| 65 |
try:
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 88 |
|
| 89 |
-
|
| 90 |
-
if any(keyword in user_input_lower for keyword in ["fee structure", "fees structure", "semester fees", "semester fee"]):
|
| 91 |
return (
|
| 92 |
-
"π° **Fee Structure
|
| 93 |
-
"
|
| 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.
|
| 107 |
-
|
|
|
|
| 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 |
-
|
| 119 |
-
1. Answer
|
| 120 |
-
2.
|
| 121 |
-
"π’ *Note:
|
| 122 |
-
3.
|
| 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 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|