The chat helper allow to keep a list of messages in memory and make consecutive prompt.
| Property Name | Type | Default Value | Description |
|---|---|---|---|
chat |
OpenAIChatAPI | - | The chat API instance used for communication with OpenAI. |
systemPrompt |
OpenAIMessage | - | The system prompt message that guides the chat assistant's responses. |
numberOfMessages |
Integer | 15 | The maximum number of messages to retain in the chat history. |
parameters |
OpenAIChatCompletionsParameters | - | The parameters for the OpenAI chat completion request. |
messages |
Collection of OpenAIMessage | [] | The collection of messages exchanged in the chat session. |
tools |
Collection of OpenAITool | [] | List of registered OpenAI tools for function calling. |
autoHandleToolCalls |
Boolean | True | Boolean indicating whether tool calls are handled automatically using registered tools. |
lastErrors |
Collection | - | Collection containing the last errors encountered during chat operations. |
To create a new OpenAIChatHelper instance, it's best to use the create() method from the OpenAI client's chat API:
var $chatHelper:=$client.chat.create("You are a helpful assistant.")This method creates a new chat helper with the specified system prompt and initializes it with default parameters. The system prompt defines the assistant's role and behavior throughout the conversation.
prompt(prompt : Variant) : OpenAIChatCompletionsResult
| Parameter | Type | Description |
|---|---|---|
| prompt | Text or OpenAIMessage | The text prompt to send to OpenAI chat, or an OpenAIMessage object for more complex messages (e.g., with images or files). |
| Function result | OpenAIChatCompletionsResult | The completion result returned by the chat. |
Sends a user prompt to the chat and returns the corresponding completion result. You can pass either a simple text string or an OpenAIMessage object for more advanced scenarios like including images or files.
// Simple text prompt
var $result:=$chatHelper.prompt("Hello, how can I help you today?")
$result:=$chatHelper.prompt("Why 42?")
// Using OpenAIMessage for advanced scenarios (e.g., with images)
var $message:=cs.AIKit.OpenAIMessage.new({role: "user"; content: "What's in this image?"})
$message.addImageURL("https://example.com/photo.jpg"; "high")
$result:=$chatHelper.prompt($message)
// Using OpenAIMessage with files
var $fileMessage:=cs.AIKit.OpenAIMessage.new({role: "user"; content: "Analyze this document"})
$fileMessage.addFileId($uploadedFile.id)
$result:=$chatHelper.prompt($fileMessage)reset()
Resets the chat context by clearing all messages and unregistering all tools. This effectively starts a fresh conversation while keeping the system prompt and parameters intact.
$chatHelper.prompt("Hello!")
$chatHelper.reset() // Clear all previous messages and toolsregisterTool(tool : Object; handler : Variant)
| Parameter | Type | Description |
|---|---|---|
| tool | Object | The tool definition object (or OpenAITool instance) |
| handler | Object | The function to handle tool calls (4D.Function or Object), optional if defined inside tool as handler property |
Registers a tool with its handler function for automatic tool call handling.
The handler parameter can be:
- A 4D.Function: Direct handler function
- An Object: An object containing a formula property matching the tool function name
The handler function receives an object containing the parameters passed from the OpenAI tool call. This object contains key-value pairs where the keys match the parameter names defined in the tool's schema, and the values are the actual arguments provided by the AI model.
// Example 1: Simple registration with direct handler
var $tool:={type: "function"; function: {name: "get_weather"; description: "Get current weather"; parameters: {type: "object"; properties: {location: {type: "string"; description: "City name"}}}}}
var $handler:=Formula(return "Sunny, 25°C in "+$1.location)
$chatHelper.registerTool($tool; $handler)
// Example 2: Tool with handler property (no second parameter needed)
var $tool:={name: "calculate"; description: "Perform calculations"; handler: Formula(return String(Num($1.expression)))}
$chatHelper.registerTool($tool)
// Example 3: Using object notation
$chatHelper.registerTool({tool: $tool; handler: $handler})
// Example 4: Handler as object with formula matching tool name
var $tool:={name: "getTime"; description: "Get current time"}
var $handlerObj:=cs.MyTimeTool.new() // class with a getTime function
$chatHelper.registerTool($tool; $handlerObj)registerTools(toolsWithHandlers : Variant)
| Parameter | Type | Description |
|---|---|---|
| toolsWithHandlers | Variant | Object or Collection containing tools and their handlers |
Registers multiple tools at once. The parameter can be:
- Collection: Array of tool objects (with handlers embedded or separate)
- Object: Object with function names as keys mapping to tool definitions
- Object with
toolsattribute: Object containing atoolscollection and formula properties matching tool names
var $weatherTool:={name: "getWeather"; description: "Get current weather"; handler: Formula(return "Sunny, 25°C in "+$1.location)}
var $calculatorTool:={name: "calculate"; description: "Perform calculations"; handler: Formula(return String(Num($1.expression)))}
$chatHelper.registerTools([$weatherTool; $calculatorTool])var $toolsWithSeparateHandlers:={}
$toolsWithSeparateHandlers.getWeather:={tool: $weatherToolDefinition; handler: $weatherHandler}
$toolsWithSeparateHandlers.calculate:={tool: $calculatorToolDefinition; handler: $calculatorHandler}
$chatHelper.registerTools($toolsWithSeparateHandlers)MyTools class:
Class constructor
this.tools:=[{name: "getWeather"; description: "Get current weather"}; \
{name: "getTime"; description: "Get current time"}] // Collection of tool definitions
Function getWeather($parameters: Object)
return "Sunny, 25°C"
Function getTime($parameters: Object)
return String(Current time)$chatHelper.registerTools(cs.MyTools.new())var $tools:={}
$tools.getWeather:=$weatherTool // Tool with handler property
$tools.calculate:=$calculatorTool // Tool with handler property
$chatHelper.registerTools($tools)unregisterTool(functionName : Text)
| Parameter | Type | Description |
|---|---|---|
| functionName | Text | The name of the function tool to unregister |
Unregisters a specific tool by its function name. This removes the tool from the registered tools collection, clears its handler, and removes it from the parameters.
$chatHelper.registerTool($weatherTool; $weatherHandler)
$chatHelper.unregisterTool("get_weather") // Remove the weather toolunregisterTools()
Unregisters all tools at once. This clears all tool handlers, empties the tools collection, and removes all tools from the parameters.
$chatHelper.registerTools($multipleTools)
$chatHelper.unregisterTools() // Remove all tools