Function Calling not happening

Hello, No matter what I do my api is not calling the function. Instead every time I am getting “finish_reason”: “stop”. I am sharing my code. Can anyone identify what I am doing wrong?

def extract_discount_details(max_discount, bank_name, min_swipe):
    '''Get the discount details like discount amount, bank name and Minimum swipe/pucrhase from the offer which is resulting in maximum discount
    
    Inputs:
        max_discount (integer): The discount amount from the best offer
        bank_name (str): The name of the bank which is offereing the best offer
        min_swipe (int): The minimum swipe or purchase amount from the best offer
    '''
    discount_info = {
        "Maximum Discount" : max_discount,
        "Bank Name" : bank_name,
        "Minimum Swipe" : min_swipe 
    }
    return json.dumps(discount_info)

def run_idenification(messages_2):
    # Step 1: send the conversation and available functions to GPT
    messages = messages_2
    functions = [
        {
            "name": "extract_discount_details",
            "description": "Get the discount details like discount amount, bank name and Minimum swipe/pucrhase from the offer which is resulting in maximum discount",
            "parameters": {
                "type": "object",
                "properties": {
                    "max_discount": {
                        "type": "integer",
                        "description": "The discount amount from the best offer. e.g. 2000, 500"
                    },
                    "bank_name": {
                        "type": "string",
                        "description": "The name of the bank which is offereing the best offer. e.g HDFC Bank Credit card, ICICI Bank"
                    },
                    "min_swipe": {
                        "type": "integer",
                        "description": "The minimum swipe or purchase amount from the best offer. e.g. 17899, 19999"
                    }
                },
                "required": ["max_discount"]
            }
        }
    ]
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=messages,
        functions=functions,
        function_call="auto",  # auto is default, but we'll be explicit
        temperature=0.1
    )
    response_message = response["choices"][0]["message"]

messages_3 = [
    {"role": "user", "content": "The Price of the SKU is 19999\n\nOffers:\n\n1. Flat INR 2100 Instant Discount on HDFC Bank Credit CardTxn. Minimum purchase value INR 17999\n\n2. Flat INR 2100 Instant Discount on ICICI Bank Credit Cards (excluding Amazon Pay ICICI Credit Card) Credit CardTxn. Minimum purchase value INR 17999\n\n3. Flat INR 2000 Instant Discount on HDFC Bank Debit Card EMI Txn. Minimum purchase value INR 19999\n\n4. 5% Instant Discount up to INR 250 on HSBC Cashback Card Credit Card Transactions. Minimum purchase value INR 1000\n\n5. Get 5% back with Amazon Pay ICICI Bank credit card for Prime members. 3% back for others. Not applicable on Amazon business transactions.\n\n6. ₹100 cashback & ₹500 welcome rewards on Amazon Pay Later.\n\nWhich is the best Offer?"}
    ]
print(run_idenification(messages_3))

Output:
{
  "id": "chatcmpl-82aWHMj7SS5NZq25CnrzQCLjfQa7a",
  "object": "chat.completion",
  "created": 1695627729,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The best offer would be offer number 1: Flat INR 2100 Instant Discount on HDFC Bank Credit CardTxn. Minimum purchase value INR 17999. This offer provides the highest discount amount of INR 2100."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 251,
    "completion_tokens": 50,
    "total_tokens": 301
  }
}

Instruction: “which is the best offer”.

Data object, of several listed:

  1. 5% Instant Discount up to INR 250 on HSBC Cashback Card Credit Card Transactions. Minimum purchase value INR 1000

Function:
“name”: “extract_discount_details”,
“description”: “Get the discount details like discount amount, bank name and Minimum swipe/pucrhase from the offer which is resulting in maximum discount”,
“parameters”: {
“type”: “object”,
“properties”: {
max_discount”: {
“type”: “integer”,
“description”: “The discount amount from the best offer. e.g. 2000, 500”
},
bank_name”: {
“type”: “string”,
“description”: “The name of the bank which is offereing the best offer. e.g HDFC Bank Credit card, ICICI Bank”
},
min_swipe”: {
“type”: “integer”,
“description”: “The minimum swipe or purchase amount from the best offer. e.g. 17899, 19999”
}
},

I don’t understand the purpose of this function either. Get the discount details? AI would already have needed to make a choice about “best” to submit to the function, and it already extracted its details in order to do so.

The function doesn’t seem to serve to inform the AI better.

The purpose of the function is to get the output in the standard format. Before function calling was introduced I have to use regex to get the maximum discount & other details from the text.

I think you need to provide a more generic function that describes what it does. Then instruct in the user space. The AI will see that it needs to use the function for output and not direct speech to the user.

   functions = [
        {
            "name": "offer_formatted_output",
            "description": "Generate all AI responses about discounted offerings only to this API",
            "parameters": {
                "type": "object",
                "properties": {
                    "discount_bank": {
                        "type": "string",
                        "description": "Bank providing offer"
                    },
                    "discount_amount": {
                        "type": "integer",
                        "description": "An offer's discount amount (example: 2000, 500)"
                    },
                    "discount_minimum_purchase": {
                        "type": "integer",
                        "description": "minimum purchase required to obtain discount offer"
                    }
                },
                "required": ["discount_bank"]
            }
        }
    ]

Then you can prime it for operation with the system prompt:
“you are a backend data processor that determines best consumer value from scraped data and outputs single decision only to an API function. Follow provided decision-making instructions.”

Have you tried this code? It did not work for me. Same output.