$gpt.createChatCompletion
The $gpt
built-in service allows you to access ChatGPT by OpenAI from the bot script.
The $gpt.createChatCompletion
method is based on the corresponding OpenAI method
which returns a response from the gpt-3.5-turbo
model to a given phrase.
You can use it, for example, to generate responses to unrecognized user phrases.
Syntax
- ECMAScript 5
- ECMAScript 6
$gpt.createChatCompletion(messages, temperature, n);
In the ECMAScript 6 runtime, the method is asynchronous:
await $gpt.createChatCompletion(messages, temperature, n);
The method accepts the following arguments:
Argument | Type | Required | Description | Values |
---|---|---|---|---|
messages | Array of objects | Yes | Messages to be passed to the model. | |
messages[].role | String | Yes | The role of the message author. | system , user , assistant , or function . |
messages[].content | String | Yes | The message text. | Arbitrary text. |
temperature | Number | No | Randomness and creativity of the model response. | Numbers from 0 to 2. The default value is 1. At low values, the model’s responses will be more clear and predictable, while at high values they will be unpredictable and creative. |
n | Number | No | The number of response options from the model. | Positive integers. The default value is 1. If the value is greater than 1, you can display all responses or select the necessary option. |
The method returns a response in the following format:
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1234567890,
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "How can I help you?"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
Usage details
Calling this method consumes Caila tokens. They are not equal to OpenAI tokens. The amount of Caila tokens spent depends on the request.
GptAi limit exhausted
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 these 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:
var userMessage = $request.query;
var assistantResponse = await $gpt.createChatCompletion([{ "role": "user", "content": userMessage }]);
var response = assistantResponse.choices[0].message.content;
$reactions.answer(response);