> 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/chatclient.md).

# ChatClient

## Overview

This is the ChatClient class that wraps around a LLM to chat with it.\
It has support for adding files (images, videos, audio, pdf, text) to the chat.

## Methods Summarized

| Type                                                                      | Name                                                                                                            | Summary                                            |
| ------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- | -------------------------------------------------- |
| [ChatClient](/reference/servoyextensions/server-plugins/ai/chatclient.md) | [addBytes(bytes)](#addbytes-bytes)                                                                              | Adds a file by bytes to the chat message.          |
| [ChatClient](/reference/servoyextensions/server-plugins/ai/chatclient.md) | [addBytes(bytes, contentType)](#addbytes-bytes-contenttype)                                                     | Adds a file by bytes to the chat message.          |
| [ChatClient](/reference/servoyextensions/server-plugins/ai/chatclient.md) | [addFile(file)](#addfile-file)                                                                                  | Add a file (String or JSFile) to the chat message. |
| [ChatClient](/reference/servoyextensions/server-plugins/ai/chatclient.md) | [addFile(file, contentType)](#addfile-file-contenttype)                                                         | Add a file (String or JSFile) to the chat message. |
| [Promise](/reference/servoycore/dev-api/js-lib/promise.md)                | [chat(userMessage)](#chat-usermessage)                                                                          | Send a userMessage to the ai.                      |
| void                                                                      | [chat(userMessage, partialResponse, onComplete, onError)](#chat-usermessage-partialresponse-oncomplete-onerror) | Send a userMessage to the ai.                      |

## Methods Detailed

### addBytes(bytes)

Adds a file by bytes to the chat message.\
The bytes must be a image/\*, video/\*, audio/\*, application/pdf or text/ content type

**Parameters**

* [Array](/reference/servoycore/dev-api/js-lib/array.md) **bytes** The bytes of that must be send. The content type will be detected automatically.

**Returns:** [ChatClient](/reference/servoyextensions/server-plugins/ai/chatclient.md) The this

### addBytes(bytes, contentType)

Adds a file by bytes to the chat message.\
The bytes must be a image/\*, video/\*, audio/\*, application/pdf or text/ content type

**Parameters**

* [Array](/reference/servoycore/dev-api/js-lib/array.md) **bytes** The bytes of that must be send. The content type will be detected automatically.
* [String](/reference/servoycore/dev-api/js-lib/string.md) **contentType** the content the file, must be a image/\*, video/\*, audio/\*, application/pdf or text/ content type

**Returns:** [ChatClient](/reference/servoyextensions/server-plugins/ai/chatclient.md) The this

### addFile(file)

Add a file (String or JSFile) to the chat message.\
The file must be a image/\*, video/\*, audio/\*, application/pdf or text/ content type\
This will be guessed based on the file extension or content.

**Parameters**

* [Object](/reference/servoycore/dev-api/js-lib/object.md) **file** A JSFile or String (path to a file on the server)

**Returns:** [ChatClient](/reference/servoyextensions/server-plugins/ai/chatclient.md) The this

### addFile(file, contentType)

Add a file (String or JSFile) to the chat message.\
The file must be a image/\*, video/\*, audio/\*, application/pdf or text/ content type

**Parameters**

* [Object](/reference/servoycore/dev-api/js-lib/object.md) **file** A JSFile or String (path to a file on the server)
* [String](/reference/servoycore/dev-api/js-lib/string.md) **contentType** the content the file, must be a image/\*, video/\*, audio/\*, application/pdf or text/ content type

**Returns:** [ChatClient](/reference/servoyextensions/server-plugins/ai/chatclient.md) The this

### chat(userMessage)

Send a userMessage to the ai. This will return a promise that will be resolved with the response.\
This response is a Promise that will get a ChatResponse object in the then or the promise will be rejected with an error.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **userMessage** The user message

**Returns:** [Promise](/reference/servoycore/dev-api/js-lib/promise.md) {Promise\<plugins.ai.ChatResponse>} A promise that will be resolved with the assistant response.

### chat(userMessage, partialResponse, onComplete, onError)

Send a userMessage to the ai. This will call the provided functions on partial response, complete response and error.\
So this can be used for streaming responses.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **userMessage** The user message send to the ai.
* [Function](/reference/servoycore/dev-api/js-lib/function.md) **partialResponse** {(partialResponse:String)=>void} A function that will be called with each partial string response from the ai.
* [Function](/reference/servoycore/dev-api/js-lib/function.md) **onComplete** {(response:plugins.ai.ChatResponse)=>void} A function that will be called when the response is complete, it will be called with the ChatResponse object.
* [Function](/reference/servoycore/dev-api/js-lib/function.md) **onError** {(error:Exception)=>void} A function that will be called when an error occurs.

**Returns:** void

***


---

# 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/chatclient.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.
