With 4D 21, AI takes a giant leap forward. At the heart of this evolution is AI Kit’s tool calling, a massive addition that transforms the way you integrate AI into your applications.
Tool calling allows you to extend the model’s capabilities by registering your own methods or functions, which the AI can call automatically when relevant. This means that instead of manually handling every interaction, the chat helper automatically invokes your handlers, giving you both flexibility and control.
Why This Matters
Our implementation puts developers in control. You decide what tools exist and what data is shared, there is no direct access for the AI to your database. You declare tools, define their parameters, and determine exactly what data to return. This safeguards your data while allowing the AI to work with the outputs of your choice.
Tool calling also opens the door to Retrieval-Augmented Generation (RAG). In RAG, the model doesn’t rely solely on its own knowledge. Instead, it can dynamically retrieve information from external sources, such as your 4D database, before generating a response. The result is answers that are not only intelligent and natural, but grounded in your real-world business context.
AI tool calling Invocation in Conversations
A key innovation in AI Kit is letting the assistant automatically invoke tools during conversations. A tool is a custom function registered by the developer that can be triggered by the model when certain conditions are met. This makes your assistant far more dynamic, capable of performing specialized tasks without manual intervention.
For example, you could create a tool to return information about a person, cancel invoices, send emails, validate orders — essentially turning your assistant into a new interface for interacting with the services your 4D app provides. And the best part? This can happen purely through a prompt-based UI, which is exactly how the next generation of users expects to interact with software.
Example: Creating a “GetPeopleInfo” Tool
Here’s a practical example showing how to register and use a tool with AI Kit:
- First, create a Tool_GetPeopleInfo class that describes the tool:
// --- Tool Definition ---
property tool:={}
Class constructor
// We describe the "GetPeopleInfo" tool in json format:
This.tool.name:="GetPeopleInfo"
This.tool.description:="It returns information about the person based on their first and last name."
This.tool.parameters:={type: "object"; properties: {}}
This.tool.parameters.properties.firstname:={type: "string"; description: "First name of the person sought"}
This.tool.parameters.properties.lastname:={type: "string"; description: "Last name of the person sought"}
Function handler($info : Object) : Text
$result:=ds.People.query("Firstname=:1 and Lastname=:2"; $info.firstname; $info.lastname)
// Returns the person information
return $result.length>0 ? JSON Stringify($result[0].toObject()) : "This person is unknown"
- Then register the tool with the chat helper:
var $client:=cs.AIKit.OpenAI.new($openAIKey)
var $chatHelper:=$client.chat.create("you are an HR assistant.")
// --- Tool Registration ---
// We register the tool with the chat helper.
// This allows the model to automatically invoke it when a request matches.
$chatHelper.registerTool(cs.Tool_GetPeopleInfo.new())
var $result:=$chatHelper.prompt("What is Faye Back's address?")
//$result.choice.message.text="Faye Back's address is Earl Boulevard (178), Lanesville, 12450, USA.
When the user asks, “What is John Smith’s address?“, the model decides to call the “GetPeopleInfo” tool because it has access to the tool definition. The handler function is automatically called by AIKit with the provided parameters. The model receives the response, integrates it into the conversation, and returns the formatted response. All of this happens without manual coding for each query.

Beyond the Example: Expanding Use Cases
The potential of tool calling goes far beyond retrieving data. Here are a few examples of what you can do:
-
Cancel invoices based on a user request.
-
Send automated emails with custom content.
-
Validate orders before processing.
-
Trigger workflows within your 4D app.
These tools allow your assistant to become a powerful interface for your app’s functionality, a natural language UI for your services. This isn’t just a nice-to-have: it’s how the next generation expects to interact with software.
conclusion
The AI tool calling in 4D 21’s AI Kit empowers you to seamlessly extend the model’s capabilities with your own logic. By bridging AI with your 4D methods or functions, you can give the assistant direct access to your data and processes, making its responses not only smarter but also perfectly adapted to your business needs. Note that not all models support this functionality.
