$gpt.createChatCompletion
The built-in $gpt
service lets you access OpenAI’s ChatGPT models from your bot script.
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.
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
- ECMAScript 5
- ECMAScript 6
$gpt.createChatCompletion(messages, temperature, n, max_completion_tokens, model);
In the ECMAScript 6 runtime, the method is asynchronous:
await $gpt.createChatCompletion(messages, temperature, n, max_completion_tokens, model);
The method accepts the following arguments:
Argument | Type | Required | Description |
---|---|---|---|
messages | Array of objects | Yes | Messages that are passed to the model. You can get the history of the dialog with the bot using $jsapi.chatHistoryInLlmFormat . |
messages[].role | String | Yes | Role of the message author: system , user , assistant or function . |
messages[].content | String | Yes | Message text. |
temperature | Number | No | Randomness and creativity of the model response. Takes values from0.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. |
n | Number | No | Number of response options from the model. Default value is 1 , you can display all responses or select the necessary option. |
max_completion_tokens | Integer | No | Maximum number of tokens that the model can generate. Also includes tokens in the model reasoning. |
model | String | No | Model that generates a response. By default: |
You can find a number of sample requests in the OpenAI documentation.
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
}
);
- 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.
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:
- ECMAScript 5
- ECMAScript 6
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);
state: NoMatch
event!: noMatch
scriptEs6:
const userMessage = $request.query;
const assistantResponse = await $gpt.createChatCompletion(
[{ "role": "user", "content": userMessage }]
);
const response = assistantResponse.choices[0].message.content;
$reactions.answer(response);