Skip to main content

$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

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

The method accepts the following arguments:

ArgumentTypeRequiredDescriptionValues
messagesArray of objectsYesMessages to be passed to the model.
messages[].roleStringYesThe role of the message author.system, user, assistant, or function.
messages[].contentStringYesThe message text.Arbitrary text.
temperatureNumberNoRandomness 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.
nNumberNoThe 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.
tip
You can find a number of sample requests in the OpenAI documentation.

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 Tovie NLP tokens. They are not equal to OpenAI tokens. The amount of Tovie NLP tokens spent depends on the request.

caution
When the Tovie NLP token limit is exhausted, the method will throw a GptAi limit exhausted exception which you can handle in the script. To replenish the limits, purchase an additional package of Tovie NLP 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:

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);