midrees2806 commited on
Commit
84f2063
Β·
verified Β·
1 Parent(s): a3081bc

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +12 -46
rag.py CHANGED
@@ -1,50 +1,16 @@
1
- import json
2
- from sentence_transformers import SentenceTransformer, util
3
- from groq import Groq
4
- import datetime
5
- import requests
6
- from io import BytesIO
7
- from PIL import Image, ImageDraw, ImageFont
8
- import numpy as np
9
- from dotenv import load_dotenv
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
- def get_best_answer(user_input):
47
- user_embedding = similarity_model.encode(user_input.lower().strip(), convert_to_tensor=True)
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