OpenAIChatHelper
The chat helper allow to keep a list of messages in memory and make consecutive prompt.
Propriedades
Nome da propriedade | Tipo | Valor padrão | Descrição |
---|---|---|---|
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. |
parâmetros | OpenAIChatCompletionsParameters | - | The parameters for the OpenAI chat completion request. |
messages | Coleção de OpenAIMessage | [] | The collection of messages exchanged in the chat session. |
tools | Collection of OpenAITool | [] | List of registered OpenAI tools for function calling. |
autoHandleToolCalls | Parâmetros | True | Boolean indicating whether tool calls are handled automatically using registered tools. |
lastErrors | Collection | - | Collection containing the last errors encountered during chat operations. |
Constructor
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.
Funções
prompt()
prompt(prompt : Text) : OpenAIChatCompletionsResult
Parâmetro | Tipo | Descrição |
---|---|---|
prompt | Text | The text prompt to send to OpenAI chat. |
Resultado | OpenAIChatCompletionsResult | The completion result returned by the chat. |
Sends a user prompt to the chat and returns the corresponding completion result.
Exemplo de uso
var $result:=$chatHelper.prompt("Hello, how can I help you today?")
$result:=$chatHelper.prompt("Why 42?")
reset()
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.
Reset Example
$chatHelper.prompt("Hello!")
$chatHelper.reset() // Clear all previous messages and tools
registerTool()
registerTool(tool : Object; handler : Object)
Parâmetro | Tipo | Descrição |
---|---|---|
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.
Register Tool Example
// 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()
registerTools(toolsWithHandlers : Variant)
Parâmetro | Tipo | Descrição |
---|---|---|
toolsWithHandlers | Diferente de | 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
tools
attribute: Object containing atools
collection and formula properties matching tool names
Register Multiple Tools Example
Example 1: Collection format with handlers in tools
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])
Example 2: Object format with separate tool and handler
var $toolsWithSeparateHandlers:={}
$toolsWithSeparateHandlers.getWeather:={tool: $weatherToolDefinition; handler: $weatherHandler}
$toolsWithSeparateHandlers.calculate:={tool: $calculatorToolDefinition; handler: $calculatorHandler}
$chatHelper.registerTools($toolsWithSeparateHandlers)
Example 3: Object with tools collection attribute and formula properties
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())
Example 4: Simple object format with tools as properties
var $tools:={}
$tools.getWeather:=$weatherTool // Tool with handler property
$tools.calculate:=$calculatorTool // Tool with handler property
$chatHelper.registerTools($tools)
unregisterTool()
unregisterTool(functionName : Text)
Parâmetro | Tipo | Descrição |
---|---|---|
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.
Unregister Tool Example
$chatHelper.registerTool($weatherTool; $weatherHandler)
$chatHelper.unregisterTool("get_weather") // Remove the weather tool
unregisterTools()
unregisterTools()
Unregisters all tools at once. This clears all tool handlers, empties the tools collection, and removes all tools from the parameters.
Unregister All Tools Example
$chatHelper.registerTools($multipleTools)
$chatHelper.unregisterTools() // Remove all tools