Implemented ChatGPT functionality
This commit is contained in:
@@ -5,41 +5,35 @@ const { createTransaction } = require("./transactionController");
|
||||
|
||||
const { Configuration, OpenAIApi } = require('openai');
|
||||
const configuration = new Configuration({
|
||||
organization: process.env.OPENAI_ORG,
|
||||
apiKey: process.env.OPENAI_KEY
|
||||
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}) {
|
||||
// If the temperature is set to null
|
||||
_temperature = _temperature ?? 0;
|
||||
// If the tokens are set to null
|
||||
_max_tokens = _max_tokens ?? 100;
|
||||
|
||||
// TODO - Get the tokens in the message and subtract that from the max tokens to be sent to the AI
|
||||
|
||||
log.DEBUG("Getting chat with these properties: ", _prompt, _model, _temperature, _max_tokens)
|
||||
try{
|
||||
/*
|
||||
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);
|
||||
});
|
||||
if(!response?.data) return callback(new Error("Error in response data: ", response));
|
||||
return callback(undefined, response.data);
|
||||
} catch (err){
|
||||
return callback(err, undefined);
|
||||
log.ERROR(err);
|
||||
log.ERROR("Error when handing model request");
|
||||
//return callback(err, undefined);
|
||||
}
|
||||
//var responseData = response.data.choices[0].text;
|
||||
}
|
||||
@@ -51,30 +45,25 @@ async function getGeneration(_prompt, callback, { _model = "text-davinci-003", _
|
||||
* @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');
|
||||
exports.submitPromptTransaction = async (prompt, temperature, max_tokens, discord_account_id, callback) => {
|
||||
|
||||
|
||||
if (temperature) params._temperature = temperature;
|
||||
if (maxTokens) params._max_tokens = maxTokens;
|
||||
|
||||
getGeneration(promptText, (err, gptResult) => {
|
||||
getGeneration(prompt, (err, gptResult) => {
|
||||
if (err) callback(err, undefined);
|
||||
|
||||
// TODO - Use the pricing table to calculate discord tokens
|
||||
log.DEBUG("GPT Response", gptResult);
|
||||
const discordTokensUsed = gptResult.usage.total_tokens;
|
||||
|
||||
if (gptResult){
|
||||
createTransaction(gptResult.id, interaction.member.id, discordTokensUsed, gptResult.usage.total_tokens, 1, async (err, transactionResult) => {
|
||||
createTransaction(gptResult.id, discord_account_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}));
|
||||
callback(undefined, ({ promptResult: gptResult.choices[0].text, totalTokens: discordTokensUsed}));
|
||||
}
|
||||
});
|
||||
}
|
||||
}, { _temperature: temperature, _max_tokens: maxTokens });
|
||||
}, { _temperature: temperature, _max_tokens: max_tokens });
|
||||
}
|
||||
Reference in New Issue
Block a user