Fix to handle user queries already having a credit card
Browse files- recommender/retrieval_ranking.py +68 -27
recommender/retrieval_ranking.py
CHANGED
|
@@ -14,46 +14,87 @@ def convert_to_direct_query_gradio(user_query,preferences):
|
|
| 14 |
preferences_text = ""
|
| 15 |
if preferences:
|
| 16 |
preferences_text = "User selected preferences: " + ", ".join(preferences) + "."
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
prompt = f"""
|
| 19 |
-
You are an AI assistant that refines user queries to make them optimized for information retrieval while
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
-
|
| 24 |
-
-
|
| 25 |
-
- Use
|
| 26 |
-
-
|
| 27 |
-
- Do NOT introduce
|
| 28 |
-
-
|
| 29 |
-
- If the
|
| 30 |
-
- If the
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
Example 1
|
| 35 |
- User Query: "I drive a lot for work and want a credit card with good fuel rewards and travel perks."
|
| 36 |
-
- Optimized Query: "Category: Fuel Rewards | Best credit cards for high fuel spending with maximum rewards & fuel surcharge waiver. Travel perks preferred but secondary."
|
| 37 |
-
|
| 38 |
Example 2
|
| 39 |
- User Query: "I mostly shop online and want a card that gives high cashback on e-commerce purchases. Food delivery perks would be nice."
|
| 40 |
-
- Optimized Query: "Category: Online Shopping | Credit cards with best cashback on e-commerce platforms like Amazon, Flipkart. Food delivery benefits secondary."
|
| 41 |
-
|
| 42 |
Example 3
|
| 43 |
- User Query: "I eat out a lot and also order food from Swiggy/Zomato. I want the best dining discounts and food delivery cashback."
|
| 44 |
-
- Optimized Query: "Category: Dining & Food Delivery | Top credit cards offering the best dining discounts at restaurants and cashback on Swiggy/Zomato orders."
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
Preferences: "{preferences_text}"
|
| 49 |
-
User Query: "{user_query}"
|
|
|
|
| 50 |
"""
|
| 51 |
|
| 52 |
print("rewriting")
|
| 53 |
response = model2.generate_content(prompt)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Function to generate multiple focused subqueries from a user query
|
| 59 |
def generate_multi_queries(direct_query, n=3):
|
|
|
|
| 14 |
preferences_text = ""
|
| 15 |
if preferences:
|
| 16 |
preferences_text = "User selected preferences: " + ", ".join(preferences) + "."
|
| 17 |
+
|
| 18 |
+
card_list_str = ", ".join(all_card_names)
|
| 19 |
+
feature_list_str = ", ".join(feature_list)
|
| 20 |
+
|
| 21 |
prompt = f"""
|
| 22 |
+
You are an AI assistant that refines user queries to make them optimized for credit card information retrieval while strictly preserving the user's original intent.
|
| 23 |
+
Your task is to restructure queries in a way that enables accurate and relevant credit card recommendations.
|
| 24 |
+
|
| 25 |
+
Instructions:
|
| 26 |
+
- Identify the main intent or feature of the query (e.g., cashback, lounge access, air miles).
|
| 27 |
+
- Format the query clearly and concisely, with the primary benefit mentioned first.
|
| 28 |
+
- Use direct and specific keywords that match real credit card benefits.
|
| 29 |
+
- Respect and incorporate any stated user preferences.
|
| 30 |
+
- Do NOT introduce any benefits not explicitly requested by the user.
|
| 31 |
+
- Retain and highlight any mention of a specific bank or card brand if provided.
|
| 32 |
+
- If the user uses vague terms like "vacation", interpret it as related to travel benefits, including: airport lounge access, hotel privileges, travel offers, international usage, and forex fee waivers.
|
| 33 |
+
- If the user mentions "beginner", "entry-level", or "low credit score", focus on basic features like cashback, reward points, and easy approvals.
|
| 34 |
+
- If the query is empty, generate a structured, useful query based entirely on preferences.
|
| 35 |
+
|
| 36 |
+
**Card & Feature Awareness:**
|
| 37 |
+
Only exclude cards if the user clearly states they already have them. Match this against the known cards: [{card_list_str}].
|
| 38 |
+
|
| 39 |
+
Do not guess or assume exclusions based on the features the user mentions — only exclude when there is a direct statement like:
|
| 40 |
+
|
| 41 |
+
"I already have <card name>"
|
| 42 |
+
|
| 43 |
+
"I use <card name>"
|
| 44 |
+
|
| 45 |
+
"I have a card with <specific benefit>"
|
| 46 |
+
|
| 47 |
+
If the user does not explicitly mention owning a specific card, leave the excluded_cards array empty.
|
| 48 |
+
- If the user already has a card covering certain benefits (e.g., airline miles), do NOT recommend similar benefits again.
|
| 49 |
+
- Instead, refocus the query on alternative features that support the same intent. For example:
|
| 50 |
+
- If the user already has airline miles but asks for "frequent flyer" cards, suggest lounge access, hotel perks, or concierge services instead.
|
| 51 |
+
- Use the following set of all possible benefits to guide replacements: [{feature_list_str}].
|
| 52 |
+
- Do NOT remove or alter any request for a specific bank or product if explicitly mentioned by the user.
|
| 53 |
+
|
| 54 |
+
Output:
|
| 55 |
+
- Return a JSON object with:
|
| 56 |
+
{{
|
| 57 |
+
"optimized_query": "Category: <Primary Feature> | <Refined, clean version of the user's query using precise benefit terms. Avoid mentioning excluded cards.>",
|
| 58 |
+
"excluded_cards": ["<card1>", "<card2>"]
|
| 59 |
+
}}
|
| 60 |
+
- Do NOT include phrases like “Exclude: <Card Name>” in the `optimized_query`.
|
| 61 |
+
|
| 62 |
+
**Examples:**
|
| 63 |
+
|
| 64 |
Example 1
|
| 65 |
- User Query: "I drive a lot for work and want a credit card with good fuel rewards and travel perks."
|
| 66 |
+
- Optimized Query: "Category: Fuel Rewards | Best credit cards for high fuel spending with maximum rewards & fuel surcharge waiver. Travel perks preferred but secondary."
|
| 67 |
+
|
| 68 |
Example 2
|
| 69 |
- User Query: "I mostly shop online and want a card that gives high cashback on e-commerce purchases. Food delivery perks would be nice."
|
| 70 |
+
- Optimized Query: "Category: Online Shopping | Credit cards with best cashback on e-commerce platforms like Amazon, Flipkart. Food delivery benefits secondary."
|
| 71 |
+
|
| 72 |
Example 3
|
| 73 |
- User Query: "I eat out a lot and also order food from Swiggy/Zomato. I want the best dining discounts and food delivery cashback."
|
| 74 |
+
- Optimized Query: "Category: Dining & Food Delivery | Top credit cards offering the best dining discounts at restaurants and cashback on Swiggy/Zomato orders."
|
| 75 |
+
|
| 76 |
+
Example 4
|
| 77 |
+
- User Query: "I already have a credit card with airline miles. Are there better options for frequent flyers?"
|
| 78 |
+
- Optimized Query: "Category: Travel Benefits | Credit cards with premium travel perks like airport lounge access, hotel discounts, and concierge services. Avoid repeating airline mile features already covered."
|
| 79 |
+
|
| 80 |
Preferences: "{preferences_text}"
|
| 81 |
+
User Query: "{user_query}"
|
| 82 |
+
|
| 83 |
"""
|
| 84 |
|
| 85 |
print("rewriting")
|
| 86 |
response = model2.generate_content(prompt)
|
| 87 |
|
| 88 |
+
clean_text = re.sub(r"^```json|```$", "", response.text.strip(), flags=re.MULTILINE).strip()
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
result = json.loads(clean_text)
|
| 92 |
+
print(result["optimized_query"])
|
| 93 |
+
print(result.get("excluded_cards", []))
|
| 94 |
+
return result["optimized_query"], result.get("excluded_cards", [])
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print("Error parsing JSON:", e)
|
| 97 |
+
return clean_text, []
|
| 98 |
|
| 99 |
# Function to generate multiple focused subqueries from a user query
|
| 100 |
def generate_multi_queries(direct_query, n=3):
|