> For the complete documentation index, see [llms.txt](https://docs.servoy.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.servoy.com/reference/servoyextensions/server-plugins/ai/openaichatbuilder.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
