midrees2806 commited on
Commit
1aa7b8e
ยท
verified ยท
1 Parent(s): 26e824b

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +1 -43
rag.py CHANGED
@@ -8,8 +8,6 @@ from PIL import Image, ImageDraw, ImageFont
8
  import numpy as np
9
  from dotenv import load_dotenv
10
  import os
11
- import pandas as pd
12
- from datasets import load_dataset, Dataset
13
 
14
  # Load environment variables
15
  load_dotenv()
@@ -29,34 +27,6 @@ dataset_questions = [item.get("Question", "").lower().strip() for item in datase
29
  dataset_answers = [item.get("Answer", "") for item in dataset]
30
  dataset_embeddings = similarity_model.encode(dataset_questions, convert_to_tensor=True)
31
 
32
- # Greeting list
33
- GREETINGS = [
34
- "hi", "hello", "hey", "good morning", "good afternoon", "good evening",
35
- "assalam o alaikum", "salam", "aoa", "hi there",
36
- "hey there", "greetings"
37
- ]
38
-
39
- # Hugging Face config
40
- HF_DATASET_REPO = "midrees2806/unmatched_queries"
41
- HF_TOKEN = os.getenv("HF_TOKEN")
42
-
43
- # Save unmatched queries to Hugging Face
44
- def manage_unmatched_queries(query: str):
45
- try:
46
- timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
47
- try:
48
- ds = load_dataset(HF_DATASET_REPO, token=HF_TOKEN)
49
- df = ds["train"].to_pandas()
50
- except:
51
- df = pd.DataFrame(columns=["Query", "Timestamp", "Processed"])
52
- if query not in df["Query"].values:
53
- new_entry = {"Query": query, "Timestamp": timestamp, "Processed": False}
54
- df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
55
- updated_ds = Dataset.from_pandas(df)
56
- updated_ds.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN)
57
- except Exception as e:
58
- print(f"Failed to save query: {e}")
59
-
60
  def query_groq_llm(prompt, model_name="llama3-70b-8192"):
61
  try:
62
  chat_completion = groq_client.chat.completions.create(
@@ -76,16 +46,8 @@ def query_groq_llm(prompt, model_name="llama3-70b-8192"):
76
  def get_best_answer(user_input):
77
  user_input_lower = user_input.lower().strip()
78
 
79
- # ๐Ÿ‘‰ Greeting functionality
80
- if any(greet in user_input_lower for greet in GREETINGS):
81
- greeting_response = query_groq_llm(
82
- f"You are an official assistant for University of Education Lahore. "
83
- f"Respond to this greeting in a friendly and professional manner: {user_input}"
84
- )
85
- return greeting_response if greeting_response else "Hello! How can I assist you today?"
86
-
87
  # ๐Ÿ‘‰ Check if question is about fee
88
- if any(keyword in user_input_lower for keyword in ["semester fee", "semester fees"]):
89
  return (
90
  "๐Ÿ’ฐ For complete and up-to-date fee details for this program, we recommend visiting the official University of Education fee structure page.\n"
91
  "Youโ€™ll find comprehensive information regarding tuition, admission charges, and other applicable fees there.\n"
@@ -98,10 +60,6 @@ def get_best_answer(user_input):
98
  best_match_idx = similarities.argmax().item()
99
  best_score = similarities[best_match_idx].item()
100
 
101
- # ๐Ÿ‘‰ Save unmatched query if no close match
102
- if best_score < 0.65:
103
- manage_unmatched_queries(user_input)
104
-
105
  if best_score >= 0.65:
106
  original_answer = dataset_answers[best_match_idx]
107
  prompt = f"""As an official assistant for University of Education Lahore, provide a clear response:
 
8
  import numpy as np
9
  from dotenv import load_dotenv
10
  import os
 
 
11
 
12
  # Load environment variables
13
  load_dotenv()
 
27
  dataset_answers = [item.get("Answer", "") 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(
 
46
  def get_best_answer(user_input):
47
  user_input_lower = user_input.lower().strip()
48
 
 
 
 
 
 
 
 
 
49
  # ๐Ÿ‘‰ Check if question is about fee
50
+ if any(keyword in user_input_lower for keyword in ["fee", "fees", "charges", "semester fee"]):
51
  return (
52
  "๐Ÿ’ฐ For complete and up-to-date fee details for this program, we recommend visiting the official University of Education fee structure page.\n"
53
  "Youโ€™ll find comprehensive information regarding tuition, admission charges, and other applicable fees there.\n"
 
60
  best_match_idx = similarities.argmax().item()
61
  best_score = similarities[best_match_idx].item()
62
 
 
 
 
 
63
  if best_score >= 0.65:
64
  original_answer = dataset_answers[best_match_idx]
65
  prompt = f"""As an official assistant for University of Education Lahore, provide a clear response: