4 min read 4 days ago

Function Calling

Function Calling is the idea of letting the AI model know about a list of functions it can call. The model will then wait for the result of the executed function and do (or not) something with the output, if there is one.

Function Calling (also called Tools) needs some functions to be callable so they can be executed. Note that the AI itself does not run code—it can’t execute functions directly. Instead, it knows that it has access to a list of predefined functions that you’ve created.

When the AI model determines that it wants to execute a function, it sends back a reply with the name of the function and any necessary arguments. Then, your server (in the case of AI Engine) runs the function, generates an output, and sends it back to the AI model so it can finalize its response.

Dynamic and Static

There are two types of functions that can be called: Dynamic and Static.

dynamic function is used to enrich the AI’s final reply with additional data. Dynamic functions can also be called in a cascade, depending on the functions you’ve defined and the AI’s decisions. For example, if you ask the AI to “send me an email,” it might first call a function to retrieve your email information, then call another function to actually send the email.

It’s recommended to use dynamic functions, as the final answer will be written by the AI and will therefore flow naturally. You can register your functions using Code Engine or manually.

Code Engine

Code Engine was previously called Snippet Vault, which is now deprecated. So make sure you’re using Code Engine to continue receiving updates.

The first step is to download the Code Engine plugin. This plugin is 100% free and can be used similarly to Code Snippet, but with even more features. If you’re familiar with Code Snippet, you can even import all your code from there for an easy switch. You can find the plugin here: Code Engine.

Code Engine allows you to store PHP and/or JS snippet code inside a dashboard and execute them whenever you want, even using a REST API.

You have a special type of snippet in Code Engine called Callable. As the name suggests, this snippet can only contain one unique PHP function. You can declare its arguments and define them in specialized side settings. Once this is done, the AI model can use them easily, and you don’t have to do anything more on your side.

By using Code Engine, you can declare Callable functions that can be used in Function Calling. Code Engine and AI Engine will then work together automatically to handle the entire pipeline—asking to run a function → running the function → returning the result to the AI → continuing the query—without you needing to do anything. This is very powerful and is the only plugin on WordPress that can do this!

Notion Image

Simply navigate to your chatbot settings and locate the new “Functions” section. Then, select the function you want to use for the current chatbot session. From there, function calling is automatically handled, streamlining the process for you.

The Function section is not appearing? This means the model you have selected is not compatible with function calling. Only OpenAI and Claude currently support this feature, but not all of their models can use function calling. So make sure you select a model that supports it.

This can happen if you selected “Default” as the model in your chatbot settings. Even if your default model supports Function Calling, AI Engine doesn’t know which model it is until a request is made, so it can’t display the “Functions” section. To fix this, explicitly select a model that supports Function Calling in the chatbot settings.

Notion Image
Notion Image

Here, we’re running a PHP callable function—so when the AI wants to run the function, it will be executed on your server. This is really useful if you need to access the database, get user information, fetch posts, and so on.

But it’s not limited to that. You can also create a JavaScript callable function to interact with your website. For instance, you could ask Nyao to change the color of the website—and watch it happen live!