Update app.py
Browse files
app.py
CHANGED
|
@@ -2844,13 +2844,13 @@ with gr.Blocks(
|
|
| 2844 |
|
| 2845 |
# ==================== TAB 6: ASK AI ====================
|
| 2846 |
with gr.Tab("π¬ Ask AI"):
|
| 2847 |
-
gr.Markdown("## π€ Chat with RewardPilot AI (Powered by
|
| 2848 |
-
|
| 2849 |
# Add status indicator
|
| 2850 |
-
if
|
| 2851 |
-
gr.Markdown("β
**Status:**
|
| 2852 |
else:
|
| 2853 |
-
gr.Markdown("β οΈ **Status:**
|
| 2854 |
|
| 2855 |
gr.Markdown("---")
|
| 2856 |
gr.Markdown("*Ask questions about credit cards, rewards, and your spending patterns*")
|
|
@@ -2969,23 +2969,24 @@ with gr.Blocks(
|
|
| 2969 |
messages.append({"role": "user", "content": message})
|
| 2970 |
|
| 2971 |
try:
|
| 2972 |
-
# β
Call GPT-4 with function calling
|
| 2973 |
response = openai_client.chat.completions.create(
|
| 2974 |
model="gpt-4-turbo-preview",
|
| 2975 |
messages=messages,
|
| 2976 |
-
|
| 2977 |
-
|
| 2978 |
temperature=0.7,
|
| 2979 |
max_tokens=500
|
| 2980 |
)
|
| 2981 |
|
| 2982 |
response_message = response.choices[0].message
|
| 2983 |
|
| 2984 |
-
# Check if GPT-4 wants to call a function
|
| 2985 |
-
if response_message.
|
| 2986 |
-
|
| 2987 |
-
|
| 2988 |
-
|
|
|
|
| 2989 |
# Execute the function
|
| 2990 |
if function_name == "get_card_recommendation":
|
| 2991 |
# Call your existing recommendation system
|
|
@@ -3014,11 +3015,11 @@ with gr.Blocks(
|
|
| 3014 |
else:
|
| 3015 |
function_response = "Function not implemented yet."
|
| 3016 |
|
| 3017 |
-
|
| 3018 |
messages.append(response_message)
|
| 3019 |
messages.append({
|
| 3020 |
-
"role": "function"
|
| 3021 |
-
"
|
| 3022 |
"content": function_response
|
| 3023 |
})
|
| 3024 |
|
|
|
|
| 2844 |
|
| 2845 |
# ==================== TAB 6: ASK AI ====================
|
| 2846 |
with gr.Tab("π¬ Ask AI"):
|
| 2847 |
+
gr.Markdown("## π€ Chat with RewardPilot AI (Powered by OpenAI GPT-4)")
|
| 2848 |
+
|
| 2849 |
# Add status indicator
|
| 2850 |
+
if os.getenv("OPENAI_API_KEY"):
|
| 2851 |
+
gr.Markdown("β
**Status:** GPT-4 Turbo Active | **Mode:** Function Calling Enabled")
|
| 2852 |
else:
|
| 2853 |
+
gr.Markdown("β οΈ **Status:** OpenAI API key not configured")
|
| 2854 |
|
| 2855 |
gr.Markdown("---")
|
| 2856 |
gr.Markdown("*Ask questions about credit cards, rewards, and your spending patterns*")
|
|
|
|
| 2969 |
messages.append({"role": "user", "content": message})
|
| 2970 |
|
| 2971 |
try:
|
| 2972 |
+
# β
Call GPT-4 with function calling (NEW SYNTAX)
|
| 2973 |
response = openai_client.chat.completions.create(
|
| 2974 |
model="gpt-4-turbo-preview",
|
| 2975 |
messages=messages,
|
| 2976 |
+
tools=[{"type": "function", "function": func} for func in functions],
|
| 2977 |
+
tool_choice="auto", # Changed from function_call
|
| 2978 |
temperature=0.7,
|
| 2979 |
max_tokens=500
|
| 2980 |
)
|
| 2981 |
|
| 2982 |
response_message = response.choices[0].message
|
| 2983 |
|
| 2984 |
+
# Check if GPT-4 wants to call a function (NEW SYNTAX)
|
| 2985 |
+
if response_message.tool_calls:
|
| 2986 |
+
tool_call = response_message.tool_calls[0]
|
| 2987 |
+
function_name = tool_call.function.name
|
| 2988 |
+
function_args = json.loads(tool_call.function.arguments)
|
| 2989 |
+
|
| 2990 |
# Execute the function
|
| 2991 |
if function_name == "get_card_recommendation":
|
| 2992 |
# Call your existing recommendation system
|
|
|
|
| 3015 |
else:
|
| 3016 |
function_response = "Function not implemented yet."
|
| 3017 |
|
| 3018 |
+
|
| 3019 |
messages.append(response_message)
|
| 3020 |
messages.append({
|
| 3021 |
+
"role": "tool", # Changed from "function"
|
| 3022 |
+
"tool_call_id": tool_call.id, # Required field
|
| 3023 |
"content": function_response
|
| 3024 |
})
|
| 3025 |
|