be097ced by Jonille Arreglo

Merge branch '28594_usage_tracking' into '1DEVT'

28594_usage_tracking

See merge request !70
2 parents fbad6f2a 17eb87aa
......@@ -49,7 +49,8 @@ function App() {
let model = data.models.data[i];
if( !(model.id == "whisper-1"
|| model.id == "gpt-4"
|| model.id == "gpt-4-0314") ) model_list.push(model);
|| model.id == "gpt-4-0314"
|| model.id == "gpt-4-0613") ) model_list.push(model);
}
setModels(model_list)
})
......@@ -61,6 +62,12 @@ function App() {
}
async function submitPrompt() {
const TPLogicRun = window.TPLogicRun;
if (typeof TPLogicRun === 'function') {
TPLogicRun();
}
const userInput = ['what', 'why', 'when', 'where' , 'which', 'did', 'do', 'how', 'can', 'are', 'who'];
const userInputRegex = new RegExp(`\\b(${userInput.join('|')})\\b`, 'gi');
const inputMatches = chatInput.match(userInputRegex);
......
......@@ -5,6 +5,47 @@ const cors = require('cors')
require('dotenv').config()
const rateLimit = require('express-rate-limit')
const anchorme = require("anchorme").default;
const axios = require('axios');
const tiktoken = require('@dqbd/tiktoken');
const tiktokenModels = [
'text-davinci-003',
'text-davinci-002',
'text-davinci-001',
'text-curie-001',
'text-babbage-001',
'text-ada-001',
'davinci',
'curie',
'babbage',
'ada',
'code-davinci-002',
'code-davinci-001',
'code-cushman-002',
'code-cushman-001',
'davinci-codex',
'cushman-codex',
'text-davinci-edit-001',
'code-davinci-edit-001',
'text-embedding-ada-002',
'text-similarity-davinci-001',
'text-similarity-curie-001',
'text-similarity-babbage-001',
'text-similarity-ada-001',
'text-search-davinci-doc-001',
'text-search-curie-doc-001',
'text-search-babbage-doc-001',
'text-search-ada-doc-001',
'code-search-babbage-code-001',
'code-search-ada-code-001',
'gpt2',
'gpt-4',
'gpt-4-0314',
'gpt-4-32k',
'gpt-4-32k-0314',
'gpt-3.5-turbo',
'gpt-3.5-turbo-0301'
];
const encoding_for_model = tiktoken.encoding_for_model;
// Open AI Configuration
// console.log(process.env.OPENAI_API_ORG)
......@@ -67,6 +108,23 @@ app.post('/api', async (req, res) => {
temperature,
});
let input = response.data.choices[0].text;
let usage = {};
let enc = null;
try {
enc = encoding_for_model(tiktokenModels.includes(currentModel) ? currentModel : 'gpt-3.5-turbo');
usage.prompt_tokens = (enc.encode(query_prompt)).length;
usage.completion_tokens = (enc.encode(input)).length;
usage.total_tokens = usage.prompt_tokens + usage.completion_tokens;
} catch (e) {
console.log('Error encoding prompt text', e);
}
// TOKEN USAGE
axios.post(`${process.env.API_URL}e/set-chat-usage`,
{ app: 'chatbot', prompt_token: usage.prompt_tokens, total_token: usage.total_tokens },
{ headers: { 'content-type': 'application/x-www-form-urlencoded' }
});
res.json({
message: anchorme({
input,
......@@ -96,6 +154,8 @@ async function runGPTTurbo(req, res) {
// "gpt-3.5-turbo"
const { message, currentModel, temperature } = req.body;
var input = '';
const message_history = JSON.parse(message);
const query_prompt = message_history.length ? message_history[message_history.length - 1].content : "";
try {
const response = await openai.createChatCompletion({
model: `${currentModel}`,
......@@ -112,6 +172,24 @@ async function runGPTTurbo(req, res) {
console.log(e.response);
}
} finally {
let usage = {};
let enc = null;
try {
enc = encoding_for_model(tiktokenModels.includes(currentModel) ? currentModel : 'gpt-3.5-turbo');
usage.prompt_tokens = (enc.encode(query_prompt)).length;
usage.completion_tokens = (enc.encode(input)).length;
usage.total_tokens = usage.prompt_tokens + usage.completion_tokens;
} catch (e) {
console.log('Error encoding prompt text', e);
}
// TOKEN USAGE
axios.post(`${process.env.API_URL}e/set-chat-usage`,
{ app: 'chatbot', prompt_token: usage.prompt_tokens, total_token: usage.total_tokens },
{ headers: { 'content-type': 'application/x-www-form-urlencoded' }
});
res.json({
prompt: JSON.parse(message),
message: anchorme({
......
......@@ -9,7 +9,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"@dqbd/tiktoken": "^1.0.7",
"anchorme": "^2.1.2",
"axios": "^1.5.1",
"body-parser": "^1.20.1",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!