Creating a dialog/form conversation

Hi,
I would like to create a dialog/form that would be invoked by this question, for example:

  • I would like to order product X.

And then I would like to get some information from the customer, by asking the following questions:

  • Describe what features should have product X.
  • Your name and email
  • Time slot

For that purpose I made a model with the following conversation:

{
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant"
        },
        {
            "role": "user",
            "content": "I have an idea for building a wooden shed, what should I do next?"
        },
        {
            "role": "assistant",
            "tool_calls": [
                {
                    "id": "call_id_display_welcome_message",
                    "type": "function",
                    "function": {
                        "name": "display_welcome_message",
                        "arguments": "{\"message\": \"You're on the right place, we can build it for you. Would you please be so kind and describe your idea in more details?\"}"
                    }
                }
            ]
        },
        { "role": "tool", "tool_call_id": "call_id_display_welcome_message", "content": "You're on the right place, we can build it for you. Would you please be so kind and describe your idea in more details?" },
        {
            "role":"assistant",
            "content":"You're on the right place, we can build it for you. Would you please be so kind and describe your idea in more details?"
		},
		{
            "role": "user",
            "content": "I want to build an wooden shed with door and two windows."
        },
        {
            "role": "assistant",
            "tool_calls": [
                {
                    "id": "call_id0",
                    "type": "function",
                    "function": {
                        "name": "save_customer_idea",
                        "arguments": "{\"idea\": \"I want to build an wooden shed with door and two windows.\"}"
                    }
                }
            ]
        },
        { "role": "tool", "tool_call_id": "call_id0", "content": "I want to build an wooden shed with door and two windows." },
        {
            "role":"assistant",
            "content":"Thank you for the description, please provide your email and name. The first step in a collaboration is a initial call to discuss requirements, expectations and timelines in more detail. We'll get back to you."
		},
		{
            "role": "user",
            "content": "Martin, test@test.com"
        },
        {
            "role": "assistant",
            "tool_calls": [
                {
                    "id": "call_id",
                    "type": "function",
                    "function": {
                        "name": "save_customer_name_and_email",
                        "arguments": "{\"name\": \"Martin\", \"email\": \"test@test.com\"}"
                    }
                }
            ]
        },
        { "role": "tool", "tool_call_id": "call_id", "content": "Martin, test@test.com" },
		{
            "role":"assistant",
            "content":"Thank you, are you available on Monday 3PM or alternatively Tuesday 2PM?"
		},
		{
            "role": "user",
            "content": "Monday works"
        },
        {
            "role": "assistant",
            "tool_calls": [
                {
                    "id": "call_id_selected_time_slot",
                    "type": "function",
                    "function": {
                        "name": "save_selected_time_slot",
                        "arguments": "{\"time_slot\": \"Monday\"}"
                    }
                }
            ]
        },
        { "role": "tool", "tool_call_id": "call_id_selected_time_slot", "content": "Monday" },
        {
            "role": "assistant",
            "content": "An offer has been sent to your e-mail address, thank you."
        }
    ]

},

When in model response the following function calls are detected:

  • call_id_display_welcome_message
  • save_customer_name_and_email
  • save_selected_time_slot

I’m displaying appropriate messages to the user, from a Python custom functions:

  • You’re on the right place, we can build it for you. Would you please be so kind and describe your idea in more details?
  • Thank you for the description, please provide your email and name. The first step in a collaboration is a initial call to discuss requirements, expectations and timelines in more detail. We’ll get back to you.
  • Thank you, are you available on Monday 3PM or alternatively Tuesday 2PM?

This is the code for executing the dialog:

async def get_custom_function_openai_response(self, message):
    openai.api_key = settings.OPENAI_API_KEY
    system_context = "You are a helpful assistant!"

    if len(self.conversation_history_for_custom_function) <= 1:
        self.conversation_history_for_custom_function.insert(
            0,
            {"role": "system", "content": system_context})

    self.conversation_history_for_custom_function.append({"role": "user", "content": message})

    try:
        tools = self.get_tools_for_custom_function()

        response = openai.chat.completions.create(
            model=self.model,
            messages=self.conversation_history_for_custom_function,
            max_tokens=150,
            temperature=self.temperature,
            tools=tools,
            tool_choice="auto",
        )
        response_message = response.choices[0].message
        tool_calls = response_message.tool_calls
        print(f"response_message : {response_message}")

        first_message = ({
            "role": response.choices[0].message.role,
            "content": response.choices[0].message.content,
            "function_call": response.choices[0].message.function_call,
        })
        if response.choices[0].message.tool_calls:
            first_message["tool_calls"] = [{
                "id": response.choices[0].message.tool_calls[0].id,
                "function": response.choices[0].message.tool_calls[0].function,
                "type": response.choices[0].message.tool_calls[0].type
            }]

        print(f"first_message : {first_message}")
        self.conversation_history_for_custom_function.append(first_message)

        if tool_calls:
            tool_call_response = self.invoke_function_in_tool_calls(tool_calls)
            tool_response = tool_call_response["tool_response"]
            function_response = tool_call_response["function_response"]

            if tool_response["content"]:
                tool_response_content = tool_response["content"]

                tool_response_json = {
                    "tool_call_id": tool_response["tool_call_id"],
                    "role": tool_response["role"],
                    "content": tool_response["content"]["message"]
                }

                self.conversation_history_for_custom_function.append(tool_response_json)

                print(f"tool_response_content: {tool_response_content}")
                print(f"conversation_history_v2: {self.conversation_history_for_custom_function}")

                second_response = openai.chat.completions.create(
                    model=self.model,
                    messages=self.conversation_history_for_custom_function,
                    temperature=0.1,
                )  # get a new response from the model where it can see the function response

                second_response_json = {
                    "role": second_response.choices[0].message.role,
                    "content": second_response.choices[0].message.content,
                }

                second_response_message = second_response.choices[0].message.content
                self.conversation_history_for_custom_function.append(second_response_json)

                print(f"second_response_message: {second_response_message}")
                print(f"conversation_history_v2: {self.conversation_history_for_custom_function}")

                return second_response_message
        else:
            return first_message["content"]
    except Exception as e:
        return f"Error: {str(e)}"

The questions are:

  • Can I make this dialog (for getting customer idea, name & email, timeslot) only when first user message is like ‘I would like to order product X’?
    I believe in Rasa (Conversation AI Platform) this is called a form, but in my testing I’ve noticed when I entered my name and email at the start of conversation it detected it, but I didn’t wanted for that to be detected because user didn’t said ‘I would like to order product X’ before.

  • Can you also please check if the flow is correct, like I used flow with user input, then tool call, then setting tool_call_id and then returning response in assistant - is that ok?

  • Also, sometimes first user message ‘I have an idea for building a wooden shed’ is detected in second tool call ‘save_customer_idea’ which is not intended to be - can I somehow force that only when first tool call is detected i.e. ‘display_welcome_message’, only then other tool calls are executed?

Tnx and sorry for a long post.