Sulaiman8 commited on
Commit
03a5d34
·
verified ·
1 Parent(s): ca95ebe

Update agents/chat.py

Browse files
Files changed (1) hide show
  1. agents/chat.py +67 -67
agents/chat.py CHANGED
@@ -1,68 +1,68 @@
1
- import pandas as pd
2
- import google.generativeai as genai
3
- import os
4
- from data import eligibility_df
5
-
6
- #function for the chatbot functionality
7
- eligibility_lookup = {}
8
- for _, row in eligibility_df.iterrows():
9
- card_name = row["Name"].strip()
10
- eligibility_info = f"""
11
- - Bank: {row['Bank']}
12
- - Age: {row['Minimum Age']} to {row['Maximum Age']}
13
- - Minimum Income: {row['Minimum Income (LPA)']} LPA
14
- - Minimum Credit Score: {row['Minimum Credit Score']}
15
- - Joining Fee: ₹{row['Joining fee']}
16
- - Annual Fee: ₹{row['Annual fee']}
17
- """
18
- eligibility_lookup[card_name] = eligibility_info.strip()
19
-
20
- # Function to handle chat interaction with Gemini
21
- def chat_with_gemini(user_query, user_message, chat_history, card_lookup):
22
- genai.configure(api_key=os.environ.get("api_key_4"))
23
- model4 = genai.GenerativeModel('gemini-1.5-flash-latest')
24
- context = ""
25
- for name, desc in list(card_lookup.items())[:5]:
26
- eligibility_info = eligibility_lookup.get(name, "No eligibility or fee information available.")
27
- full_desc = f"{desc}\n\nEligibility & Fees:\n{eligibility_info}"
28
- context += f"{name}:\n{full_desc}\n\n"
29
- recent_user_messages = [
30
- msg["content"] for msg in chat_history if msg["role"] == "user"
31
- ][-5:]
32
-
33
- conversation = f"""
34
- You are a helpful financial assistant. A user has already shared their overall credit card preferences.
35
-
36
- ### User’s Requirements:
37
- {user_query}
38
-
39
- ### Credit Card Options:
40
- {context}
41
-
42
- ### User's Follow-up Question:
43
- {user_message}
44
-
45
- ### Recent User Messages:
46
- {recent_user_messages}
47
-
48
- ### Instructions:
49
- 1. Answer the user's current question clearly and concisely.
50
- 2. Always consider the user's overall requirements above.
51
- 3. Use only the card descriptions provided. Do not assume or invent any card benefits.
52
- 4. If the user asks which card is best or suitable for their needs, use the user’s requirements above to select and explain.
53
- 5. Don't ask the user to restate their requirements — they're already provided above.
54
- """
55
- # print(conversation)
56
- try:
57
- response = model4.generate_content(conversation)
58
- gemini_response = response.text
59
-
60
- chat_history.append({"role": "user", "content": user_message})
61
- chat_history.append({"role": "assistant", "content": gemini_response})
62
-
63
- except Exception as e:
64
- error_msg = "Error: Unable to retrieve response from Gemini. Please try again later."
65
- chat_history.append({"role": "user", "content": user_message})
66
- chat_history.append({"role": "assistant", "content": error_msg})
67
-
68
  return chat_history, chat_history
 
1
+ import pandas as pd
2
+ import google.generativeai as genai
3
+ import os
4
+ from data import eligibility_df
5
+
6
+ #function for the chatbot functionality
7
+ eligibility_lookup = {}
8
+ for _, row in eligibility_df.iterrows():
9
+ card_name = row["Name"].strip()
10
+ eligibility_info = f"""
11
+ - Bank: {row['Bank']}
12
+ - Age: {row['Minimum Age']} to {row['Maximum Age']}
13
+ - Minimum Income: {row['Minimum Income (LPA)']} LPA
14
+ - Minimum Credit Score: {row['Minimum Credit Score']}
15
+ - Joining Fee: ₹{row['Joining fee']}
16
+ - Annual Fee: ₹{row['Annual fee']}
17
+ """
18
+ eligibility_lookup[card_name] = eligibility_info.strip()
19
+
20
+ # Function to handle chat interaction with Gemini
21
+ def chat_with_gemini(user_query, user_message, chat_history, card_lookup):
22
+ genai.configure(api_key=os.environ.get("api_key_4"))
23
+ model4 = genai.GenerativeModel('gemini-2.0-flash')
24
+ context = ""
25
+ for name, desc in list(card_lookup.items())[:5]:
26
+ eligibility_info = eligibility_lookup.get(name, "No eligibility or fee information available.")
27
+ full_desc = f"{desc}\n\nEligibility & Fees:\n{eligibility_info}"
28
+ context += f"{name}:\n{full_desc}\n\n"
29
+ recent_user_messages = [
30
+ msg["content"] for msg in chat_history if msg["role"] == "user"
31
+ ][-5:]
32
+
33
+ conversation = f"""
34
+ You are a helpful financial assistant. A user has already shared their overall credit card preferences.
35
+
36
+ ### User’s Requirements:
37
+ {user_query}
38
+
39
+ ### Credit Card Options:
40
+ {context}
41
+
42
+ ### User's Follow-up Question:
43
+ {user_message}
44
+
45
+ ### Recent User Messages:
46
+ {recent_user_messages}
47
+
48
+ ### Instructions:
49
+ 1. Answer the user's current question clearly and concisely.
50
+ 2. Always consider the user's overall requirements above.
51
+ 3. Use only the card descriptions provided. Do not assume or invent any card benefits.
52
+ 4. If the user asks which card is best or suitable for their needs, use the user’s requirements above to select and explain.
53
+ 5. Don't ask the user to restate their requirements — they're already provided above.
54
+ """
55
+ # print(conversation)
56
+ try:
57
+ response = model4.generate_content(conversation)
58
+ gemini_response = response.text
59
+
60
+ chat_history.append({"role": "user", "content": user_message})
61
+ chat_history.append({"role": "assistant", "content": gemini_response})
62
+
63
+ except Exception as e:
64
+ error_msg = "Error: Unable to retrieve response from Gemini. Please try again later."
65
+ chat_history.append({"role": "user", "content": user_message})
66
+ chat_history.append({"role": "assistant", "content": error_msg})
67
+
68
  return chat_history, chat_history