80 lines
2.8 KiB
JavaScript
80 lines
2.8 KiB
JavaScript
const { DebugBuilder } = require("../utilities/debugBuilder");
|
|
const log = new DebugBuilder("server", "chatGptController");
|
|
|
|
const { createTransaction } = require("./transactionController");
|
|
|
|
const { Configuration, OpenAIApi } = require('openai');
|
|
const configuration = new Configuration({
|
|
organization: process.env.OPENAI_ORG,
|
|
apiKey: process.env.OPENAI_KEY
|
|
});
|
|
|
|
const openai = new OpenAIApi(configuration);
|
|
|
|
|
|
async function getGeneration(_prompt, callback, { _model = "text-davinci-003", _temperature = 0, _max_tokens = 100}) {
|
|
log.DEBUG("Getting chat with these properties: ", _prompt, _model, _temperature, _max_tokens)
|
|
try{
|
|
/*
|
|
const response = await openai.createCompletion({
|
|
model: _model,
|
|
prompt: _prompt,
|
|
temperature: _temperature,
|
|
max_tokens: _max_tokens
|
|
});
|
|
*/
|
|
|
|
var response = {
|
|
"id": "ABD123",
|
|
"usage": {
|
|
"total_tokens": _max_tokens
|
|
},
|
|
"data": {
|
|
"choices": [
|
|
{
|
|
"text": "ASKLDJHASLDJALSKDJAKLSDJLASKDJALSKD"
|
|
}
|
|
]
|
|
}
|
|
};
|
|
return callback(undefined, response);
|
|
} catch (err){
|
|
return callback(err, undefined);
|
|
}
|
|
//var responseData = response.data.choices[0].text;
|
|
}
|
|
|
|
/**
|
|
* Use ChatGPT to generate a response
|
|
*
|
|
* @param {*} _prompt The use submitted text prompt
|
|
* @param {*} param1 Default parameters can be modified
|
|
* @returns
|
|
*/
|
|
exports.submitPromptTransaction = async (interaction, callback) => {
|
|
var params = {};
|
|
var promptText = interaction.options.getString('prompt');
|
|
var temperature = interaction.options.getNumber('temperature');
|
|
var maxTokens = interaction.options.getNumber('tokens');
|
|
|
|
if (temperature) params._temperature = temperature;
|
|
if (maxTokens) params._max_tokens = maxTokens;
|
|
|
|
getGeneration(promptText, (err, gptResult) => {
|
|
if (err) callback(err, undefined);
|
|
|
|
// TODO - Use the pricing table to calculate discord tokens
|
|
const discordTokensUsed = gptResult.usage.total_tokens;
|
|
|
|
if (gptResult){
|
|
createTransaction(gptResult.id, interaction.member.id, discordTokensUsed, gptResult.usage.total_tokens, 1, async (err, transactionResult) => {
|
|
if (err) callback(err, undefined);
|
|
|
|
if (transactionResult){
|
|
log.DEBUG("Transaction Created: ", transactionResult);
|
|
callback(undefined, ({ promptResult: gptResult.data.choices[0].text, totalTokens: discordTokensUsed}));
|
|
}
|
|
});
|
|
}
|
|
}, { _temperature: temperature, _max_tokens: maxTokens });
|
|
} |