Spaces:
Sleeping
Sleeping
Update rag.py
Browse files
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.
|
| 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 "
|
| 106 |
|
| 107 |
-
#
|
| 108 |
if len(user_input_lower.split()) < 3 and not any(greet in user_input_lower for greet in GREETINGS):
|
| 109 |
-
return "Please
|
| 110 |
-
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 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 |
-
#
|
| 127 |
original_answer = dataset_answers[best_match_idx]
|
|
|
|
| 128 |
prompt = f"""You are the official University of Education (UOE) Assistant.
|
| 129 |
-
I
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
5. Maintain a polite and welcoming tone.
|
| 137 |
|
| 138 |
User Question: {user_input}
|
| 139 |
Verified Data: {original_answer}
|
| 140 |
-
|
| 141 |
-
|
| 142 |
else:
|
| 143 |
-
#
|
| 144 |
manage_unmatched_queries(user_input)
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
Instructions:
|
| 149 |
-
1. Answer based on your
|
| 150 |
-
2.
|
| 151 |
-
3.
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
| 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)
|