| import pandas as pd |
| import google.generativeai as genai |
| import os |
| from data import eligibility_df |
|
|
| |
| eligibility_lookup = {} |
| for _, row in eligibility_df.iterrows(): |
| card_name = row["Name"].strip() |
| eligibility_info = f""" |
| - Bank: {row['Bank']} |
| - Age: {row['Minimum Age']} to {row['Maximum Age']} |
| - Minimum Income: {row['Minimum Income (LPA)']} LPA |
| - Minimum Credit Score: {row['Minimum Credit Score']} |
| - Joining Fee: ₹{row['Joining fee']} |
| - Annual Fee: ₹{row['Annual fee']} |
| """ |
| eligibility_lookup[card_name] = eligibility_info.strip() |
| |
| |
| def chat_with_gemini(user_query, user_message, chat_history, card_lookup): |
| genai.configure(api_key=os.environ.get("api_key_4")) |
| model4 = genai.GenerativeModel('gemini-2.0-flash') |
| context = "" |
| for name, desc in list(card_lookup.items())[:5]: |
| eligibility_info = eligibility_lookup.get(name, "No eligibility or fee information available.") |
| full_desc = f"{desc}\n\nEligibility & Fees:\n{eligibility_info}" |
| context += f"{name}:\n{full_desc}\n\n" |
| recent_user_messages = [ |
| msg["content"] for msg in chat_history if msg["role"] == "user" |
| ][-5:] |
|
|
| conversation = f""" |
| You are a helpful financial assistant. A user has already shared their overall credit card preferences. |
| |
| ### User’s Requirements: |
| {user_query} |
| |
| ### Credit Card Options: |
| {context} |
| |
| ### User's Follow-up Question: |
| {user_message} |
| |
| ### Recent User Messages: |
| {recent_user_messages} |
| |
| ### Instructions: |
| 1. Answer the user's current question clearly and concisely. |
| 2. Always consider the user's overall requirements above. |
| 3. Use only the card descriptions provided. Do not assume or invent any card benefits. |
| 4. If the user asks which card is best or suitable for their needs, use the user’s requirements above to select and explain. |
| 5. Don't ask the user to restate their requirements — they're already provided above. |
| """ |
| |
| try: |
| response = model4.generate_content(conversation) |
| gemini_response = response.text |
|
|
| chat_history.append({"role": "user", "content": user_message}) |
| chat_history.append({"role": "assistant", "content": gemini_response}) |
|
|
| except Exception as e: |
| error_msg = "Error: Unable to retrieve response from Gemini. Please try again later." |
| chat_history.append({"role": "user", "content": user_message}) |
| chat_history.append({"role": "assistant", "content": error_msg}) |
|
|
| return chat_history, chat_history |