Combine chat completions & Functions
Last updated
Last updated
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.
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.
Stop: Stop means that the AI has responded and provided a full response.
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.
Function call: This happens when all parameters are captured from your function you created.
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.
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 </>
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.