# OpenAIChatBuilder

## Overview

OpenAiChatBuilder is a builder for configuring and creating OpenAI chat clients.\
Allows setting API key, model name, temperature, and memory token limits for the OpenAI model.

## Methods Summarized

| Type                                                                                    | Name                                                                                     | Summary                                                                              |
| --------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
| void                                                                                    | [addSystemMessage(message)](#addsystemmessage-message)                                   | Adds a system message to the chat context.                                           |
| [OpenAiChatBuilder](/reference/servoyextensions/server-plugins/ai/openaichatbuilder.md) | [apiKey(key)](#apikey-key)                                                               | Sets the OpenAI API key.                                                             |
| [ChatClient](/reference/servoyextensions/server-plugins/ai/chatclient.md)               | [build()](#build)                                                                        | Builds and returns a ChatClient configured with the specified OpenAI model settings. |
| [ToolBuilder](/reference/servoyextensions/server-plugins/ai/toolbuilder.md)             | [createTool(toolFunction, name, description)](#createtool-toolfunction-name-description) | Creates a new ToolBuilder for defining a custom tool for the AI agent.               |
| [OpenAiChatBuilder](/reference/servoyextensions/server-plugins/ai/openaichatbuilder.md) | [maxMemoryTokens(tokens)](#maxmemorytokens-tokens)                                       | Sets the maximum number of memory tokens for chat history.                           |
| [OpenAiChatBuilder](/reference/servoyextensions/server-plugins/ai/openaichatbuilder.md) | [modelName(modelName)](#modelname-modelname)                                             | Sets the OpenAI model name.                                                          |
| [OpenAiChatBuilder](/reference/servoyextensions/server-plugins/ai/openaichatbuilder.md) | [temperature(temperature)](#temperature-temperature)                                     | Sets the temperature for the OpenAI model.                                           |
| void                                                                                    | [useBuiltInTools(useBuiltInTools)](#usebuiltintools-usebuiltintools)                     | Injects the built-in Servoy tools (such as user info) into the AI agent.             |

## Methods Detailed

### addSystemMessage(message)

Adds a system message to the chat context.\
This can be a message how the model must behave for this chat session.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **message** The system message to add.

**Returns:** voidThis builder instance for chaining.

### apiKey(key)

Sets the OpenAI API key.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **key** The API key.

**Returns:** [OpenAiChatBuilder](/reference/servoyextensions/server-plugins/ai/openaichatbuilder.md) This builder instance.

### build()

Builds and returns a ChatClient configured with the specified OpenAI model settings.

**Returns:** [ChatClient](/reference/servoyextensions/server-plugins/ai/chatclient.md) A configured ChatClient instance.

### createTool(toolFunction, name, description)

Creates a new ToolBuilder for defining a custom tool for the AI agent.

**Parameters**

* [Function](/reference/servoycore/dev-api/js-lib/function.md) **toolFunction** the function representing the tool's logic
* [String](/reference/servoycore/dev-api/js-lib/string.md) **name** the name of the tool
* [String](/reference/servoycore/dev-api/js-lib/string.md) **description** the description of the tool

**Returns:** [ToolBuilder](/reference/servoyextensions/server-plugins/ai/toolbuilder.md) a ToolBuilder instance for further configuration

### maxMemoryTokens(tokens)

Sets the maximum number of memory tokens for chat history.

**Parameters**

* [Number](/reference/servoycore/dev-api/js-lib/number.md) **tokens** The maximum number of tokens.

**Returns:** [OpenAiChatBuilder](/reference/servoyextensions/server-plugins/ai/openaichatbuilder.md) This builder instance.

### modelName(modelName)

Sets the OpenAI model name.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **modelName** The model name.

**Returns:** [OpenAiChatBuilder](/reference/servoyextensions/server-plugins/ai/openaichatbuilder.md) This builder instance.

### temperature(temperature)

Sets the temperature for the OpenAI model.

**Parameters**

* [Number](/reference/servoycore/dev-api/js-lib/number.md) **temperature** The temperature value.

**Returns:** [OpenAiChatBuilder](/reference/servoyextensions/server-plugins/ai/openaichatbuilder.md) This builder instance.

### useBuiltInTools(useBuiltInTools)

Injects the built-in Servoy tools (such as user info) into the AI agent.

**Parameters**

* [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md) **useBuiltInTools** Boolean to indicate whether to use built-in tools.

**Returns:** voidThis builder instance for chaining.

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.servoy.com/reference/servoyextensions/server-plugins/ai/openaichatbuilder.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
