Combine chat completions & Functions

Ok now that you understand how to input content in a chat completion for the ai to read from, and how to create basic functions where we were capturing user data like emails addresses and names etc. It's time to put those 2 learning modules together and combine chat completions with functions to enable awesome conversational AI.

Starting off where we left off

In the image below is the chat completion module, where we inputted some information so that the AI is instantly trained on it. You will now notice that we have added our function "Free trial registration" shown below. What this will do now, is allow the user to ask questions about the context provided, and the AI will automatically pick up on the users intent, pushing them to register for a free trial by capturing their data from the function we created. Learn more about functions here

From here we need to save some extra data - So to get this data click the test request button as we done in the capture data module here

When we successfully test the request we will get the normal response back from the ai where we saved the content to a response custom field.

We asked the AI "How long is your free trial" and the response was "Our free trial lasts for 14 days"

Just below the content line, you will see another line called "finish_reason" The finish reason denotes why the AI model stopped responding. There are 3 reasons why.

  1. Stop: Stop means that the AI has responded and provided a full response.

  2. Length: This basically means that the response is too long for the pre-defined tokens. If you see it, you can increase your tokens settings in the Max Tokens tab - 400 should suffice.

  3. Function call: This happens when all parameters are captured from your function you created.

How to trigger the function_call

To get all the extra fields we require, you need to input all the parameters in the message tab, for example: Our function call is to capture Name, email address and what they want to learn more about. Once you input all your parameters you can go ahead and test requests again. This will trigger the function call.

Let's press test request and see what we get in the response;

Perfect! We can see that the finish reason is now function_call - This means that all data has been captured and the flow will be sent.

Above the function call you will now see the we have a new line called arguments. This is all the data that was captured in the function call itself. And we can use this data later on when required.

Below is the full response as we captured it.

{
"id": "chatcmpl-9Gq1memz7M4I0qbSDVtAjeCevzGSf",
"object": "chat.completion",
"created": 1713800270,
"model": "gpt-3.5-turbo-0125",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "freetrialregistration",
"arguments": "{"Name":"Pete","Email":"pete@flochat.ai","Whatwouldyouliketolearnmoreof":"developer apps"}"
}
},
"logprobs": null,
"finish_reason": "function_call"
}
],
"usage": {
"prompt_tokens": 128,
"completion_tokens": 39,
"total_tokens": 167
},
"system_fingerprint": "fp_c2295e73ad",
"messages": [
{
"role": "system",
"content": "do you have a trial"
},
{
"role": "user",
"content": "My name is Pete and my email is pete@flochat.ai and id love to learn about dev apps"
},
{
"role": "assistant",
"content": null,
"function_call": {
"name": "freetrialregistration",
"arguments": "{"Name":"Pete","Email":"pete@flochat.ai","Whatwouldyouliketolearnmoreof":"developer apps"}"
}
}
]
}

Now let's save the important data

You should of by now already saved the response in your system, if not go ahead and select the content line to save the AI response.

Next we need to save the finish reason so we can use conditions later on when required.

Last up, save the arguments line, but this time use an array field or known as JSON </>

Once you have all 3 saved - we can move on.

How to set up the basic flow design

Now let's set up a basic flow that answers the users questions and drives them to register for a free trial in this example:

Additional nodes we are using:

  • Condition: We are using the first condition node to filter if the function call has been triggered, if no we direct it to the ai response node.

  • Action: If the condition is met, meaning the function call has been made then we direct the flow toward the action step, where we will save the argument data to the correct custom fields. In this case it's the users name, email and what they want to learn about.

  • GO TO: The the last step is our go-to flow ' where we'll be sending the users once the function is called' in this case, they will go to the free trial flow.

Last updated