-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard_advisor_agent.py
More file actions
150 lines (129 loc) · 4.74 KB
/
card_advisor_agent.py
File metadata and controls
150 lines (129 loc) · 4.74 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
Simple Credit Card Advisor Agent
A minimal example showing how to build an AI-powered credit card advisor
using the Koko Finance API. This agent takes natural language questions
and routes them to the appropriate API endpoint.
Requirements:
pip install koko-finance openai
Usage:
export KOKO_API_KEY=your-koko-key
export OPENAI_API_KEY=your-openai-key
python card_advisor_agent.py
"""
import os
import json
from koko_finance import KokoClient
from openai import OpenAI
koko = KokoClient(api_key=os.environ["KOKO_API_KEY"])
llm = OpenAI()
TOOLS = [
{
"type": "function",
"function": {
"name": "compare_cards",
"description": "Compare two or three credit cards side-by-side",
"parameters": {
"type": "object",
"properties": {
"cards": {
"type": "array",
"items": {"type": "string"},
"description": "Card names to compare (2-3)",
}
},
"required": ["cards"],
},
},
},
{
"type": "function",
"function": {
"name": "recommend_card",
"description": "Find the best credit card for a spending category",
"parameters": {
"type": "object",
"properties": {
"category": {
"type": "string",
"description": "Spending category: dining, travel, groceries, gas, online_shopping, general",
}
},
"required": ["category"],
},
},
},
{
"type": "function",
"function": {
"name": "check_renewal",
"description": "Check if a credit card is worth renewing at annual fee time. Pass benefit_selections to specify which benefits the user actually uses (selected count at 100%, unselected at 0%).",
"parameters": {
"type": "object",
"properties": {
"card_name": {
"type": "string",
"description": "Name of the card to check",
},
"benefit_selections": {
"type": "array",
"items": {"type": "string"},
"description": "Benefit keys the user uses, e.g. ['uber', 'airline_fee', 'dining']",
},
},
"required": ["card_name"],
},
},
},
]
SYSTEM_PROMPT = """You are a helpful credit card advisor. You help users compare cards,
find the best card for their needs, and decide whether to keep or cancel cards.
Use the available tools to look up real card data. Give concise, actionable advice."""
def handle_tool_call(name, args):
"""Route tool calls to the Koko Finance API."""
if name == "compare_cards":
return koko.compare_cards(cards=args["cards"])
elif name == "recommend_card":
return koko.recommend_card(category=args["category"])
elif name == "check_renewal":
return koko.check_renewal(
card={"card_name": args["card_name"]},
benefit_selections=args.get("benefit_selections"),
)
return {"error": f"Unknown tool: {name}"}
def chat(user_message, history):
"""Send a message and handle tool calls in a loop."""
history.append({"role": "user", "content": user_message})
while True:
response = llm.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": SYSTEM_PROMPT}] + history,
tools=TOOLS,
)
message = response.choices[0].message
if message.tool_calls:
history.append(message)
for call in message.tool_calls:
args = json.loads(call.function.arguments)
result = handle_tool_call(call.function.name, args)
history.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
})
else:
history.append({"role": "assistant", "content": message.content})
return message.content
def main():
print("Credit Card Advisor (powered by Koko Finance API)")
print("Ask me about credit cards. Type 'quit' to exit.\n")
history = []
while True:
user_input = input("You: ").strip()
if user_input.lower() in ("quit", "exit", "q"):
break
if not user_input:
continue
response = chat(user_input, history)
print(f"\nAdvisor: {response}\n")
if __name__ == "__main__":
main()