Skip to main content

$gpt.createChatCompletion

The built-in $gpt service lets you access OpenAI’s ChatGPT models from your bot script.

note

You can use the $gpt service in JAICP, even though the OpenAI website is not available for Russian IP addresses.

The $gpt.createChatCompletion method is based on the corresponding OpenAI method which returns an LLM response to a user request. You can use it, for example, to generate responses to unrecognized user phrases.

tip

We recommend using $gpt.createChatCompletion in text bots only. To access LLMs with minimal pauses in the phone channel, use the llmRequest reply type.

Models

To access an LLM, the method uses the openai-proxy service on the Caila platform. You can view available models on the service page.

Specify the model in the model argument. By default, the $gpt.createChatCompletion method uses GPT-4o mini.

Syntax

$gpt.createChatCompletion(messages, temperature, n, max_completion_tokens, model);

The method accepts the following arguments:

ArgumentTypeRequiredDescription
messagesArray of objectsYesMessages that are passed to the model. You can get the history of the dialog with the bot using $jsapi.chatHistoryInLlmFormat.
messages[].roleStringYesRole of the message author: system, user, assistant or function.
messages[].contentStringYesMessage text.
temperatureNumberNo

Randomness and creativity of the model response.

Takes values from 0.0 to 2.0. The default value is 1. At low values, the model’s responses are more clear and predictable, while at high values they are unpredictable and creative.
nNumberNo

Number of response options from the model. Default value is 1.

If the value is greater than 1, you can display all responses or select the necessary option.
max_completion_tokensIntegerNoMaximum number of tokens that the model can generate. Also includes tokens in the model reasoning.
modelStringNo

Model that generates a response. By default: gpt-4o-mini.

You can find a number of sample requests in the OpenAI documentation.

note

The OpenAI website is not available for Russian IP addresses.

Call examples

$gpt.createChatCompletion([{ "role": "user", "content": "Hello!" }]);
$gpt.createChatCompletion(
[{ "role": "user", "content": "Hello!" }], 1, 3, 100, "gpt-4o"
);
$gpt.createChatCompletion(
[{ "role": "user", "content": "Hello!" }], undefined, undefined, undefined, "gpt-4o"
);

Other method parameters

If you want to use other parameters for the method, you can pass one object with all the settings as an argument. The object can contain any parameters supported by the OpenAI method.

For example, you can specify top_p and presence_penalty:

$gpt.createChatCompletion(
{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "Tell me about yourself"
}
],
"top_p": 0.1,
"presence_penalty": -1

}
);
Exceptions
  • The "stream": true parameter is not supported. If it is specified, the method does not return the LLM response text.
  • You can use the tools parameter only if you call the method in the ECMAScript 6 runtime.

Return value

The method response is similar to OpenAI API response:

{
"model": "gpt-4o-mini-2024-07-18",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm an AI language model created by OpenAI called ChatGPT…"
},
"finish_reason": "stop"
},
{
"index": 1,
"message": {
"role": "assistant",
"content": "I am an AI language model created by OpenAI…"
},
"finish_reason": "stop"
},
{
"index": 2,
"message": {
"role": "assistant",
"content": "I’m an AI language model developed by OpenAI, known as ChatGPT…"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 11,
"completion_tokens": 60,
"total_tokens": 71
}
}

Billing

Calling this method consumes Caila tokens. They are not equal to OpenAI tokens. The amount of Caila tokens spent depends on the request.

caution
When the Caila token limit is exhausted, the method throws a Not enough MLP tokens exception which you can handle in the script. To replenish the limits, purchase an additional package of Caila tokens.

How to use

For the bot to respond to unrecognized user phrases using ChatGPT, call the $gpt.createChatCompletion method in the state where the bot handles such phrases:

state: NoMatch
event!: noMatch
script:
var userMessage = $request.query;
var assistantResponse = $gpt.createChatCompletion(
[{ "role": "user", "content": userMessage }]
);
var response = assistantResponse.choices[0].message.content;
$reactions.answer(response);