Update recommender/graph_retrieval_vectordb.py
Browse files
recommender/graph_retrieval_vectordb.py
CHANGED
|
@@ -3,8 +3,8 @@ from neo4j import GraphDatabase
|
|
| 3 |
import os
|
| 4 |
import numpy as np
|
| 5 |
import faiss
|
| 6 |
-
from data import card_descriptions,eligibility_df
|
| 7 |
from neo4j.exceptions import ServiceUnavailable, TransientError
|
|
|
|
| 8 |
|
| 9 |
#neo4j credentials
|
| 10 |
NEO4J_URI = os.environ.get("NEO4J_URI")
|
|
@@ -19,7 +19,8 @@ def get_driver():
|
|
| 19 |
return _driver
|
| 20 |
|
| 21 |
#generating cypher query
|
| 22 |
-
|
|
|
|
| 23 |
genai.configure(api_key='AIzaSyAHoi9xbYAThtjXlyF_IKFtruoWYoUCjJQ')
|
| 24 |
model3 = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 25 |
print("inside cypher query gen")
|
|
@@ -27,7 +28,6 @@ def generate_cypher(user_query, query_intent, include_cobranded):
|
|
| 27 |
context_note = f"""
|
| 28 |
Contextual Flags:
|
| 29 |
- FD Card intent: {query_intent}
|
| 30 |
-
- Include co-branded cards: {include_cobranded}
|
| 31 |
"""
|
| 32 |
|
| 33 |
cypher_prompt = f"""
|
|
@@ -54,7 +54,6 @@ def generate_cypher(user_query, query_intent, include_cobranded):
|
|
| 54 |
- If fuel is mentioned, include both `Fuel Benefits` and `Fuel Surcharge Waiver`.
|
| 55 |
- **ALWAYS** match features using: `f.name IN [...]` β even if there is only **one** feature.
|
| 56 |
|
| 57 |
-
|
| 58 |
Valid values:
|
| 59 |
- card_type: 'FD Card' or 'Regular'
|
| 60 |
- premium: true (no concept of false β just include it if applicable)
|
|
@@ -62,11 +61,12 @@ def generate_cypher(user_query, query_intent, include_cobranded):
|
|
| 62 |
|
| 63 |
MANDATORY Condition Rules:
|
| 64 |
- If FD Card intent is true β include: `c.card_type = 'FD Card'`
|
| 65 |
-
- Else β include: `c.card_type = 'Regular'`
|
| 66 |
- If the query is based on beginners or students or people with no or low credit history then use FD Card.
|
| 67 |
- If the query uses words like "premium", "elite", "luxury", "exclusive", "infinia", "black", etc. β include: `AND c.premium = true`
|
| 68 |
- If the query includes low spending, without high spending or budget β include: `(c.premium IS NULL OR c.premium = false)`
|
| 69 |
-
- If
|
|
|
|
| 70 |
- Use exact values for `bank_name` as in the database: ["SBI", "HDFC", "Axis", "ICICI", "YES", "HSBC", "IDFC", "American Express", "SMB", "Federal Bank", "AU Bank", "IDBI", "Kotak Mahindra Bank","IndusInd","RBL"]
|
| 71 |
- Do not add bank after the name of the bank if it is not mentioned in the datase list.
|
| 72 |
- These conditions are **MANDATORY**. If they apply, include them in the `WHERE` clause. Do not skip them.
|
|
@@ -74,15 +74,16 @@ def generate_cypher(user_query, query_intent, include_cobranded):
|
|
| 74 |
---
|
| 75 |
|
| 76 |
Available features:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
---
|
| 88 |
|
|
@@ -131,6 +132,7 @@ def generate_cypher(user_query, query_intent, include_cobranded):
|
|
| 131 |
|
| 132 |
return cypher_code
|
| 133 |
|
|
|
|
| 134 |
#generating embeddings (run only once)
|
| 135 |
def chunk_text(text, chunk_size=1):
|
| 136 |
sentences = text.split("; ")
|
|
@@ -194,12 +196,12 @@ def eligibility_filter(cards, user_income, user_cibil, user_age,min_joining_fee,
|
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
-
#function for retrieving cards from knowledge graph
|
| 198 |
class Neo4jConnectionError(Exception):
|
| 199 |
pass
|
| 200 |
|
| 201 |
def run_cypher_query(user_query, query, use_eligibility, user_income, user_cibil, user_age,
|
| 202 |
-
min_joining_fee, max_joining_fee, min_annual_fee, max_annual_fee,
|
| 203 |
|
| 204 |
attempt = 0
|
| 205 |
while attempt < max_retries:
|
|
|
|
| 3 |
import os
|
| 4 |
import numpy as np
|
| 5 |
import faiss
|
|
|
|
| 6 |
from neo4j.exceptions import ServiceUnavailable, TransientError
|
| 7 |
+
from data import card_descriptions,eligibility_df
|
| 8 |
|
| 9 |
#neo4j credentials
|
| 10 |
NEO4J_URI = os.environ.get("NEO4J_URI")
|
|
|
|
| 19 |
return _driver
|
| 20 |
|
| 21 |
#generating cypher query
|
| 22 |
+
|
| 23 |
+
def generate_cypher(user_query, query_intent):
|
| 24 |
genai.configure(api_key='AIzaSyAHoi9xbYAThtjXlyF_IKFtruoWYoUCjJQ')
|
| 25 |
model3 = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 26 |
print("inside cypher query gen")
|
|
|
|
| 28 |
context_note = f"""
|
| 29 |
Contextual Flags:
|
| 30 |
- FD Card intent: {query_intent}
|
|
|
|
| 31 |
"""
|
| 32 |
|
| 33 |
cypher_prompt = f"""
|
|
|
|
| 54 |
- If fuel is mentioned, include both `Fuel Benefits` and `Fuel Surcharge Waiver`.
|
| 55 |
- **ALWAYS** match features using: `f.name IN [...]` β even if there is only **one** feature.
|
| 56 |
|
|
|
|
| 57 |
Valid values:
|
| 58 |
- card_type: 'FD Card' or 'Regular'
|
| 59 |
- premium: true (no concept of false β just include it if applicable)
|
|
|
|
| 61 |
|
| 62 |
MANDATORY Condition Rules:
|
| 63 |
- If FD Card intent is true β include: `c.card_type = 'FD Card'`
|
| 64 |
+
- Else β include: `c.card_type = 'Regular'`
|
| 65 |
- If the query is based on beginners or students or people with no or low credit history then use FD Card.
|
| 66 |
- If the query uses words like "premium", "elite", "luxury", "exclusive", "infinia", "black", etc. β include: `AND c.premium = true`
|
| 67 |
- If the query includes low spending, without high spending or budget β include: `(c.premium IS NULL OR c.premium = false)`
|
| 68 |
+
- If the user query does not mentions about co-branded cards then don't consider that property.
|
| 69 |
+
- If user says no need co-branded cards only then include β include: `AND (c.co_branded IS NULL OR c.co_branded = false)`
|
| 70 |
- Use exact values for `bank_name` as in the database: ["SBI", "HDFC", "Axis", "ICICI", "YES", "HSBC", "IDFC", "American Express", "SMB", "Federal Bank", "AU Bank", "IDBI", "Kotak Mahindra Bank","IndusInd","RBL"]
|
| 71 |
- Do not add bank after the name of the bank if it is not mentioned in the datase list.
|
| 72 |
- These conditions are **MANDATORY**. If they apply, include them in the `WHERE` clause. Do not skip them.
|
|
|
|
| 74 |
---
|
| 75 |
|
| 76 |
Available features:
|
| 77 |
+
'Fuel Surcharge Waiver','Insurance','Shopping Benefits','Airport Lounge Access','Co-Branded',
|
| 78 |
+
'Daily Spends (Grocery)','Dining Benefits','Domestic Travel Benefits','Entertainment',
|
| 79 |
+
'General Reward Points','Movie Benefits','Rupay Network Support','Student',
|
| 80 |
+
'UPI Transaction Support','Welcome Bonus','International Travel Benefits','premium',
|
| 81 |
+
'Flight Discounts','Hotel Benefits','Travel Benefits','Railway Benefits','Railway Lounge',
|
| 82 |
+
'Utility','Beginners (Entry Level)','E-commerce Platform Benefits','Air Miles',
|
| 83 |
+
'Jewellery Spends','Concierge Services','Food Delivery Benefits','Lifestyle & Luxury Perks',
|
| 84 |
+
'Spa Access Benefits','Golf Access & Perks','Super Premium','Frequent Flyer Benefits',
|
| 85 |
+
'Health Benefits','Rent Payment Benefits','Education','Lifetime Free','Roadside Assistance',
|
| 86 |
+
'EMI Conversion Options','No Forex Markup Fee','Secured FD Based','Cashback','Fuel Benefits','Business'
|
| 87 |
|
| 88 |
---
|
| 89 |
|
|
|
|
| 132 |
|
| 133 |
return cypher_code
|
| 134 |
|
| 135 |
+
|
| 136 |
#generating embeddings (run only once)
|
| 137 |
def chunk_text(text, chunk_size=1):
|
| 138 |
sentences = text.split("; ")
|
|
|
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
+
#function for retrieving cards from knowledge graph and put into vector storage
|
| 200 |
class Neo4jConnectionError(Exception):
|
| 201 |
pass
|
| 202 |
|
| 203 |
def run_cypher_query(user_query, query, use_eligibility, user_income, user_cibil, user_age,
|
| 204 |
+
min_joining_fee, max_joining_fee, min_annual_fee, max_annual_fee,excluded_cards, max_retries=3, retry_delay=2):
|
| 205 |
|
| 206 |
attempt = 0
|
| 207 |
while attempt < max_retries:
|