Function calling not triggered correctly

I am trying to create a chat bot that takes the customer’s booking data and uses it to send an email as soon as the user has provided all the data. When the user provides all the data, ChatGPT responds as if it has already sent the booking email but the function has not triggered. GPT trigger the function only if the user responds to the booking confirmation with “ok” or “thank you”.

How can I get the function to be triggered in the background without the user having to give the final “ok”?

This is my function definition:

{
            "name": "send_reservation_mail",
            "description": "Send a reservation mail.",
            "parameters": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "The name of the person booking",
                    },
                    "qty": {
                        "type": "integer",
                        "description": "The number of people for booking",
                    },
                    "date": {
                        "type": "string",
                        "description": "the booking date of the YYYY-MM-DD format",
                    },
                    "hour": {
                        "type": "string",
                        "description": "The booking hour",
                    },
                    "phone": {
                        "type": "string",
                        "description": "The telephone number for reservation",
                    },
                    "email": {
                        "type": "string",
                        "description": "The email address to send reservation email",
                    },
                    "lang": {
                        "type": "string",
                        "description": "the ISO 2 language the user is talking to",
                    },
                    "note": {
                        "type": "string",
                        "description": "any notes of the reservation",
                    }
                },
                "required": ["name", "qty", "date", "hour", "phone", "email", "lang"],
            },
        }

I tried your function as is and followed your use case. First, here is the result:

Me: hello there
Chat:

{ role: 'assistant', content: 'Hi! How can I assist you today?' }

Me: i want to make reservation pls
Chat:

{
  role: 'assistant',
  content: 'Sure, I can help you with that. Could you please provide me with the following details for your reservation?\n' +
    '\n' +
    '1. Your name\n' +
    '2. Number of people for the reservation\n' +
    '3. Date of the reservation (in YYYY-MM-DD format)\n' +
    '4. Time of the reservation\n' +
    '5. Your phone number\n' +
    '6. Your email address\n' +
    '7. Any additional notes or special requests you may have\n' +
    '\n' +
    'Once I have all this information, I will be able to assist you further.'
}

Me: John Doe, me and my wife, between 2023-08-11 and 13 so 3 days 2 nights, we will check in around 1PM
Chat:

{
  role: 'assistant',
  content: 'Thank you for providing the details. Just to confirm, you would like to make a reservation for John Doe and his wife for a 3-day, 2-night stay starting from August 11th, 2023, and checking in around 1 PM, correct?\n' +
    '\n' +
    'Could you please provide me with your phone number and email address as well?'
}

Me: 0917-1234-5678 and john.doe@acme.com
Chat:

{
  role: 'assistant',
  content: null,
  function_call: {
    name: 'send_reservation_mail',
    arguments: '{\n' +
      '  "name": "John Doe",\n' +
      '  "qty": 2,\n' +
      '  "date": "2023-08-11",\n' +
      '  "hour": "13:00",\n' +
      '  "phone": "0917-1234-5678",\n' +
      '  "email": "john.doe@acme.com",\n' +
      '  "lang": "en",\n' +
      '  "note": "3-day, 2-night stay"\n' +
      '}'
  }
}

I validated the function call response if all required parameters are complete and sent mock api result.

{
  data: {
    name: 'John Doe',
    qty: 2,
    date: '2023-08-11',
    hour: '13:00',
    phone: '0917-1234-5678',
    email: 'john.doe@acme.com',
    lang: 'en',
    note: '3-day, 2-night stay'
  },
  status: 'Reservation mail sent. Please wait for email confirmation. Thank you.'
}

Chat (summary):

{
  role: 'assistant',
  content: 'Thank you for providing your contact information. I have sent a reservation mail to the email address john.doe@acme.com. Please wait for the email confirmation. If you have any additional notes or requests, please let me know.'
}

What I did, first, added a system prompt to direct the AI what to do. Second, and I think this is crucial, added history in first Chat API call. This enables the AI to automatically fill up the parameters in the function call. This actually surprises me. I am ready to handle if the function call response not giving all required parameters. I did not send the complete info in one message so when it remembers, I was glad.

Here is my system prompt which I used on both chat API calls.

let system_prompt = `You are a helpful customer support assistant.\n` +
        `You will guide the user to fill up reservation form.\n` +
        `You will call send_registration mail for this task.`
1 Like