Applying User Confirmation to Function Calls

I have created a dummy function for querying and registering schedules. When a user requests schedule registration, I want to apply a process of confirming the extracted function and arguments from LLM and finally invoking the function with the correct arguments (title, date_time) by receiving confirmation from the user.

For example, when the user says “Register a team meeting schedule on July 20th,” and LLM asks, “Shall I register with the title ‘Team Meeting’ and date ‘July 20th’?” If the user responds with “Yes,” I want it to be registered accordingly. Another example is when the user says, “Register a team meeting schedule tomorrow,” and LLM asks, “Shall I register with the title ‘Team Meeting’ and date ‘October 21st’?” If the user corrects the date by saying “No, on July 21st instead,” I want it to be registered with the updated date.

I’m not sure how to implement this part. Is there a good approach to achieve this?

Below is the relevant code:

def get_event(date_time):
    event_info = [
        {
            "title": "Team meeting",
            "date_time": date_time,
        },
        {
            "title": "Dinner with friends meeting",
            "date_time": date_time,
        },
    ]
    return json.dumps(event_info)

def insert_event(title, date_time):
    result = "Insert success >>> " + "title: " + title + ", date_time: " + date_time
    return result

functions = [
    {
        'name': 'get_event',
        'description': 'Useful when retrieving schedules from a calendar.',
        'parameters': {
            'type': 'object',
            'properties': {
                'date_time': {
                    'type': 'string',
                    'description': 'Time information. e.g, 2023-07-03',
                },
            },
            'required': ['date_time'],
        },
    },
    {
        'name': 'insert_event',
        'description': 'Useful when adding events to the calendar.',
        'parameters': {
            'type': 'object',
            'properties': {
                'title': {
                    'type': 'string',
                    'description': 'Title for adding an event. e.g, Team meeting',
                },
                'date_time': {
                    'type': 'string',
                    'description': 'Time information. e.g, 2023-07-03',
                },
            },
        'required': ['title', 'date_time'],
        },
    }
]

def function_call(ai_message):
    function_name = ai_message["function_call"]["name"]
    arguments = json.loads(ai_message["function_call"]["arguments"])
    
    if function_name == 'get_event':
        function_response = get_event(arguments["date_time"])
    elif function_name == 'insert_event':
        function_response = insert_event(arguments["title"], arguments["date_time"])
    
    return function_name, function_response


messages = []
system_content = """You are a smart assistant. Your main goals are:
- To find a relevant function and extract the required arguments for function calling.
- When extracting arguments, you must only refer to the user's content. Never make assumptions.
- If you are unable to extract the arguments, you should ask the user for clarification.
- If you can't find relevant functions, engage in a general conversation with the user."""

messages.append({"role": "system", "content": f"{system_content}"})
user_content = input("User : ")

while user_content != 'Q':    
    # user message
    messages.append({"role": "user", "content": f"{user_content}"})
    print(f'user: {user_content}')

    response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            messages=messages,
            functions=functions,
            function_call="auto",
        )
    response_message = response["choices"][0]["message"]

    if response_message.get("function_call"):
        function_name, function_response = function_call(response_message)

        # function message
        messages.append({"role": "function", "name": function_name, "content": function_response})        
        
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            messages=messages,
        )
        response_message = response["choices"][0]["message"]

        # assistant message
        messages.append({"role": "assistant", "content": response_message["content"]})
        print(f'assistant: {response_message["content"]}')

    else:
        # assistant message
        messages.append({"role": "assistant", "content": response_message['content']})
        print(f"assistant: {response_message['content']}")
    
    user_content = input("User : ")
1 Like

You could use the “yes” or “no” response from the user as a boolean value to see whether to trigger the function that actually creates the event or not (which I’m assuming is insert event here). If it is true, only then you create the event

3 Likes