Assistants API function calling with user confirmation

Hello there,

I successfully use the assistants API with function calls to process user requests. In my use case, they ask to register the clock-out time using natural language.

Now, I have two scenarios that I haven’t been able to identify how to develop, and also couldn’t find something in the forum:

  1. Before I store the information in the DB, I need to ask the user for a confirmation (example: Do you want to register Today 11pm? Confirm with yes or no). The key point is, I show the data to register and then ask for confirmation. How do I manage this step?

I’m using assistant API and threads.

This is my function now:

{
“type”: “function”,
“function”: {
“name”: “registerClockOut”,
“description”: “Register the exit time of a user on current or previous day”,
“parameters”: {
“type”: “object”,
“properties”: {
“clockOutTime”: {
“type”: “string”,
“pattern”: “^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$”, // YYYY-MM-DD HH:mm format
“description”: “Clock-out time like 2023-12-24 23:05”
}
},
“required”: [“clockOutTime”]
}
}
}

1 Like

Welcome to the forum.

Here’s a GPT-4 answer, so you might want to check it.

To implement a confirmation step in your scenario using the assistant API and threads, you can follow a two-step approach. Firstly, you modify your registerClockOut function to return a confirmation prompt instead of directly storing the data. Then, you handle the user’s response to this confirmation prompt in a follow-up interaction.

Here’s how you can modify your current setup:

Step 1: Modify registerClockOut Function

Alter the registerClockOut function to include a step that returns a confirmation message. Instead of immediately saving the clock-out time to the database, the function should generate a message like “Do you want to register [clock-out time]? Confirm with yes or no.”

The modified function might look something like this:

{
  "type": "function",
  "function": {
    "name": "registerClockOut",
    "description": "Generate a confirmation prompt for registering the exit time of a user",
    "parameters": {
      "type": "object",
      "properties": {
        "clockOutTime": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}$", // YYYY-MM-DD HH:mm format
          "description": "Clock-out time like 2023-12-24 23:05"
        }
      },
      "required": ["clockOutTime"]
    },
    "output": {
      "type": "object",
      "properties": {
        "confirmationMessage": {
          "type": "string",
          "description": "Message asking user to confirm the clock-out time"
        }
      }
    }
  }
}

In this version, the function’s output is a confirmation message instead of directly processing the clock-out time.
Step 2: Handle User Response

After the user receives the confirmation prompt, you need a mechanism to handle their response (yes or no). This might involve another function or a logic branch in your application that:

Listens for the user's response.
Validates the response (e.g., checks if it's a 'yes' or 'no').
If the user confirms (says 'yes'), proceed to save the data to the database.
If the user denies (says 'no'), cancel the action or ask for the correct time again.

This part depends heavily on how your application’s flow is structured and might involve state management to remember the context of the user’s initial request.
Considerations

Ensure that the user's response is correctly linked to the confirmation prompt. This might involve tracking the conversation's context or using thread IDs.
Handle edge cases, such as ambiguous responses or the user not responding at all.
Consider implementing a timeout after which the confirmation request expires if not answered.

This approach separates the data confirmation from the data storage process, allowing for a more interactive and user-validated experience.

Dear Paul, thanks for the reply.

I tried many times with GPT4 but never could get a correct answer or something I could work on. Thanks for this.

I’m looking at the assistants API reference and couldn’t find any output parameter for the tool object, have you seen this somewhere?

Anyway, I’ll keep looking. Thanks again.

I think you will have to do this with prompting and just make sure the function gets called only after the explicit confirmation is received.
OR, since you are in control over the bot flow - let the function interject a button or something in the chat flow.

1 Like

Thanks jlvanhulst. I’ll check out this option.

I tried to implement your requirement by breaking down the function into two: first, get the clock-out time and then saving it in the database. I also added instruction to ask for user confirmation when the clock-out time is displayed, if the user wants to save it or not before calling the second function.

Here are the sample conversations:

  1. Save check-out time

  2. Do not save check-out time

Here are the sample functions:

{
  "name": "getClockOut",
  "parameters": {
    "type": "object",
    "properties": {
      "clockOutTime": {
        "type": "string",
        "description": "Clock-out time in YYYY-MM-DD HH:mm format"
      }
    },
    "required": [
      "clockOutTime"
    ]
  },
  "description": "get the exit time of a user on current or previous day"
}

{
  "name": "saveClockOut",
  "parameters": {
    "type": "object",
    "properties": {
      "clockOutTime": {
        "type": "string",
        "description": "Clock-out time in YYYY-MM-DD HH:mm format"
      }
    },
    "required": [
      "clockOutTime"
    ]
  },
  "description": "Save the exit time of a user on current or previous day in the database."
}

Here’s the Instructions:

You are a helpful personal assistant.
You have the following tools you can invoke depending on user request.
- getClockout, when the user wants to get the clock-out time.
- saveClockout, when the user confirmed to save the clock-out time in the database.
When you display the clock-out time after calling getClockout, ask the user for confirmation if they want to save the clock-out time before calling saveClockout.
1 Like

@cdsuze

Is it OK if a moderator closes this topic?

Thanks supershaneski, this worked perfectly.