File size: 2,790 Bytes
03a5d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6390a72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pandas as pd
import google.generativeai as genai
import os
from data import eligibility_df

#function for the chatbot functionality
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()
    
# Function to handle chat interaction with Gemini
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.
    """
    # print(conversation)
    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