Spaces:
Running
Running
Update rag.py
Browse files
rag.py
CHANGED
@@ -6,7 +6,6 @@ import os
|
|
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,14 +27,6 @@ GREETINGS = [
|
|
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:
|
@@ -85,7 +76,7 @@ def query_groq_llm(prompt, model_name="llama3-70b-8192"):
|
|
85 |
print(f"Error querying Groq API: {e}")
|
86 |
return ""
|
87 |
|
88 |
-
# Main logic function to be called from Gradio
|
89 |
def get_best_answer(user_input):
|
90 |
if not user_input.strip():
|
91 |
return "Please enter a valid question."
|
@@ -109,10 +100,33 @@ def get_best_answer(user_input):
|
|
109 |
|
110 |
if best_score < 0.65:
|
111 |
manage_unmatched_queries(user_input)
|
112 |
-
return random.choice(UNMATCHED_RESPONSES)
|
113 |
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
@@ -123,12 +137,12 @@ DO NOT add any new or extra information. ONLY rephrase and improve the clarity a
|
|
123 |
### Rephrased Answer:
|
124 |
"""
|
125 |
|
126 |
-
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
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 |
"hey there", "greetings"
|
28 |
]
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
# Load local dataset
|
31 |
try:
|
32 |
with open('dataset.json', 'r') as f:
|
|
|
76 |
print(f"Error querying Groq API: {e}")
|
77 |
return ""
|
78 |
|
79 |
+
# Main logic function to be called from Gradio or other interface
|
80 |
def get_best_answer(user_input):
|
81 |
if not user_input.strip():
|
82 |
return "Please enter a valid question."
|
|
|
100 |
|
101 |
if best_score < 0.65:
|
102 |
manage_unmatched_queries(user_input)
|
|
|
103 |
|
104 |
+
# Rephrased fixed message
|
105 |
+
official_contact_message = (
|
106 |
+
"**Note:** We've noted your query and will update our system soon.\n\n"
|
107 |
+
"๐ You may revisit later to receive an official response based on updated information.\n\n"
|
108 |
+
"For urgent queries, contact the University of Education directly:\n"
|
109 |
+
"๐ +92-42-99262231-33\n"
|
110 |
+
"โ๏ธ [email protected]\n"
|
111 |
+
"๐ [https://ue.edu.pk](https://ue.edu.pk)\n\n"
|
112 |
+
"_The response below is based on pretrained knowledge and may not reflect official policy._\n\n"
|
113 |
+
)
|
114 |
+
|
115 |
+
# Prompt for pretrained response
|
116 |
+
prompt = f"""You are UOE AI Assistant, an intelligent helper for the University of Education Lahore.
|
117 |
+
Provide a helpful and informative response based on general and pretrained knowledge.
|
118 |
+
Mention clearly that this answer is based on pretrained knowledge and may not reflect the latest official information.
|
119 |
+
Do NOT fabricate policies or details if unsure.
|
120 |
+
### Question:
|
121 |
+
{user_input}
|
122 |
+
### Answer:
|
123 |
+
"""
|
124 |
+
llm_response = query_groq_llm(prompt)
|
125 |
+
return official_contact_message + (llm_response or "")
|
126 |
+
|
127 |
+
else:
|
128 |
+
original_answer = dataset_answers[best_match_idx]
|
129 |
+
prompt = f"""Name is UOE AI Assistant! You are an official assistant for the University of Education Lahore.
|
130 |
Rephrase the following official answer clearly and professionally.
|
131 |
Use structured formatting (like headings, bullet points, or numbered lists) where appropriate.
|
132 |
DO NOT add any new or extra information. ONLY rephrase and improve the clarity and formatting of the original answer.
|
|
|
137 |
### Rephrased Answer:
|
138 |
"""
|
139 |
|
140 |
+
llm_response = query_groq_llm(prompt)
|
141 |
|
142 |
+
if llm_response:
|
143 |
+
for marker in ["Improved Answer:", "Official Answer:", "Rephrased Answer:"]:
|
144 |
+
if marker in llm_response:
|
145 |
+
return llm_response.split(marker)[-1].strip()
|
146 |
+
return llm_response
|
147 |
+
else:
|
148 |
+
return original_answer
|