Add functionality to create transactions
- Transactions work against the account balance - Needs helper functions for users - Needs more testing
This commit is contained in:
@@ -36,6 +36,16 @@ exports.createAccount = (_discordAccountId, callback) => {
|
||||
})
|
||||
}
|
||||
|
||||
exports.withdrawBalance = async (_withdrawAmount, _accountId, callback) => {
|
||||
userStorage.updateBalance('withdraw', _withdrawAmount, _accountId, async (err, result) => {
|
||||
if (err) return callback(err, undefined);
|
||||
|
||||
if(result) return callback(undefined, result);
|
||||
|
||||
return callback(undefined, undefined);
|
||||
})
|
||||
}
|
||||
|
||||
exports.verifyBalance = (_tokensToBeUsed, _accountId, callback) => {
|
||||
userStorage.checkBalance(_tokensToBeUsed, _accountId, (err, results) => {
|
||||
if (err) return callback(err, undefined);
|
||||
|
||||
@@ -1,3 +1,49 @@
|
||||
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
|
||||
*
|
||||
@@ -5,18 +51,30 @@
|
||||
* @param {*} param1 Default parameters can be modified
|
||||
* @returns
|
||||
*/
|
||||
exports.getChat = async (_prompt, { _model = "text-davinci-003", _temperature = 0, _max_tokens = 100 }) => {
|
||||
log.DEBUG("Getting chat with these properties: ", _prompt, _model, _temperature, _max_tokens)
|
||||
return
|
||||
/*
|
||||
const response = await openai.createCompletion({
|
||||
model: _model,
|
||||
prompt: _prompt,
|
||||
temperature: _temperature,
|
||||
max_tokens: _max_tokens
|
||||
});
|
||||
|
||||
var responseData = response.data.choices[0].text;
|
||||
return responseData;
|
||||
*/
|
||||
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.user, 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 });
|
||||
}
|
||||
35
controllers/transactionController.js
Normal file
35
controllers/transactionController.js
Normal file
@@ -0,0 +1,35 @@
|
||||
// Controller for managing transactions
|
||||
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder");
|
||||
const log = new DebugBuilder("server", "transactionController");
|
||||
|
||||
const { TransactionStorage } = require("../libStorage");
|
||||
const transactionStorage = new TransactionStorage();
|
||||
|
||||
const { BaseTransaction } = require("../utilities/recordHelper");
|
||||
const { withdrawBalance } = require("./accountController");
|
||||
|
||||
exports.createTransaction = async (_provider_transaction_id, _account_id, _discord_tokens_used, _provider_tokens_used, _provider_id, callback) => {
|
||||
if (!_provider_transaction_id && !_account_id && !_discord_tokens_used && !_provider_id) return callback(new Error("Invalid vars when creating transaction", {vars: [_provider_transaction_id, _account_id, _discord_tokens_used, _provider_id, callback]}))
|
||||
|
||||
const newTransaction = new BaseTransaction(_provider_transaction_id, _account_id, _discord_tokens_used, _provider_tokens_used, _provider_id, callback);
|
||||
log.DEBUG("New Transaction Object: ", newTransaction);
|
||||
withdrawBalance(newTransaction.discord_tokens_used, newTransaction.account_id, (err, withdrawResult) => {
|
||||
if (err) return callback(err, undefined);
|
||||
|
||||
if (withdrawResult){
|
||||
log.DEBUG("New withdraw result: ", withdrawResult);
|
||||
transactionStorage.createTransaction(newTransaction, async (err, transactionResult) =>{
|
||||
if (err) return callback(err, undefined);
|
||||
|
||||
if(transactionResult){
|
||||
log.DEBUG("New transaction result: ", transactionResult);
|
||||
return callback(undefined, transactionResult);
|
||||
}
|
||||
})
|
||||
}
|
||||
else {
|
||||
return callback(undefined, undefined);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user