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