Spaces:
Running
Running
Update rag.py
Browse files
rag.py
CHANGED
@@ -1,50 +1,16 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
import os
|
11 |
-
|
12 |
-
# Load environment variables
|
13 |
-
load_dotenv()
|
14 |
-
|
15 |
-
# Initialize Groq client
|
16 |
-
groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
17 |
-
|
18 |
-
# Load models and dataset
|
19 |
-
similarity_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
20 |
-
|
21 |
-
# Load dataset (automatically using the path)
|
22 |
-
with open('dataset.json', 'r') as f:
|
23 |
-
dataset = json.load(f)
|
24 |
-
|
25 |
-
# Precompute embeddings
|
26 |
-
dataset_questions = [item.get("input", "").lower().strip() for item in dataset]
|
27 |
-
dataset_answers = [item.get("response", "") for item in dataset]
|
28 |
-
dataset_embeddings = similarity_model.encode(dataset_questions, convert_to_tensor=True)
|
29 |
-
|
30 |
-
def query_groq_llm(prompt, model_name="llama3-70b-8192"):
|
31 |
-
try:
|
32 |
-
chat_completion = groq_client.chat.completions.create(
|
33 |
-
messages=[{
|
34 |
-
"role": "user",
|
35 |
-
"content": prompt
|
36 |
-
}],
|
37 |
-
model=model_name,
|
38 |
-
temperature=0.7,
|
39 |
-
max_tokens=500
|
40 |
)
|
41 |
-
return chat_completion.choices[0].message.content.strip()
|
42 |
-
except Exception as e:
|
43 |
-
print(f"Error querying Groq API: {e}")
|
44 |
-
return ""
|
45 |
|
46 |
-
|
47 |
-
user_embedding = similarity_model.encode(
|
48 |
similarities = util.pytorch_cos_sim(user_embedding, dataset_embeddings)[0]
|
49 |
best_match_idx = similarities.argmax().item()
|
50 |
best_score = similarities[best_match_idx].item()
|
@@ -79,4 +45,4 @@ def get_best_answer(user_input):
|
|
79 |
βοΈ [email protected]
|
80 |
π ue.edu.pk"""
|
81 |
|
82 |
-
return response
|
|
|
1 |
+
def get_best_answer(user_input):
|
2 |
+
user_input_lower = user_input.lower().strip()
|
3 |
+
|
4 |
+
# π Check if question is about fee
|
5 |
+
if any(keyword in user_input_lower for keyword in ["fee", "fees", "charges", "semester fee"]):
|
6 |
+
return (
|
7 |
+
"π° For complete and up-to-date fee details for all programs, "
|
8 |
+
"please visit the official University of Education fee structure page:\n"
|
9 |
+
"π https://ue.edu.pk/allfeestructure.php"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
)
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# π Continue with normal similarity-based logic
|
13 |
+
user_embedding = similarity_model.encode(user_input_lower, convert_to_tensor=True)
|
14 |
similarities = util.pytorch_cos_sim(user_embedding, dataset_embeddings)[0]
|
15 |
best_match_idx = similarities.argmax().item()
|
16 |
best_score = similarities[best_match_idx].item()
|
|
|
45 |
βοΈ [email protected]
|
46 |
π ue.edu.pk"""
|
47 |
|
48 |
+
return response
|