sammy786 commited on
Commit
58dd0f3
Β·
verified Β·
1 Parent(s): 3946542

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
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 Gemini)")
2848
-
2849
  # Add status indicator
2850
- if config.USE_GEMINI and gemini.enabled:
2851
- gr.Markdown("βœ… **Status:** Gemini 2.0 Flash Active | **Mode:** Conversational Assistant")
2852
  else:
2853
- gr.Markdown("⚠️ **Status:** Fallback Mode (Gemini unavailable)")
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
- functions=functions,
2977
- function_call="auto",
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.function_call:
2986
- function_name = response_message.function_call.name
2987
- function_args = json.loads(response_message.function_call.arguments)
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
- # Send function response back to GPT-4 for natural language formatting
3018
  messages.append(response_message)
3019
  messages.append({
3020
- "role": "function",
3021
- "name": function_name,
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