28594_usage_tracking
Showing
1 changed file
with
4 additions
and
15 deletions
| ... | @@ -6,7 +6,7 @@ require('dotenv').config() | ... | @@ -6,7 +6,7 @@ require('dotenv').config() |
| 6 | const rateLimit = require('express-rate-limit') | 6 | const rateLimit = require('express-rate-limit') |
| 7 | const anchorme = require("anchorme").default; | 7 | const anchorme = require("anchorme").default; |
| 8 | const axios = require('axios'); | 8 | const axios = require('axios'); |
| 9 | const tiktoken = require('@dqbd/tiktoken'); | 9 | const { encodingForModel } = require('js-tiktoken'); |
| 10 | const tiktokenModels = [ | 10 | const tiktokenModels = [ |
| 11 | 'text-davinci-003', | 11 | 'text-davinci-003', |
| 12 | 'text-davinci-002', | 12 | 'text-davinci-002', |
| ... | @@ -45,7 +45,6 @@ const tiktokenModels = [ | ... | @@ -45,7 +45,6 @@ const tiktokenModels = [ |
| 45 | 'gpt-3.5-turbo', | 45 | 'gpt-3.5-turbo', |
| 46 | 'gpt-3.5-turbo-0301' | 46 | 'gpt-3.5-turbo-0301' |
| 47 | ]; | 47 | ]; |
| 48 | const encoding_for_model = tiktoken.encoding_for_model; | ||
| 49 | 48 | ||
| 50 | // Open AI Configuration | 49 | // Open AI Configuration |
| 51 | // console.log(process.env.OPENAI_API_ORG) | 50 | // console.log(process.env.OPENAI_API_ORG) |
| ... | @@ -70,16 +69,6 @@ app.use(cors()) | ... | @@ -70,16 +69,6 @@ app.use(cors()) |
| 70 | app.use(require('morgan')('dev')) | 69 | app.use(require('morgan')('dev')) |
| 71 | app.use(rateLimiter) | 70 | app.use(rateLimiter) |
| 72 | 71 | ||
| 73 | const cleanString = async (input) => { | ||
| 74 | var output = ""; | ||
| 75 | for (var i=0; i<input.length; i++) { | ||
| 76 | if (input.charCodeAt(i) <= 127) { | ||
| 77 | output += input.charAt(i); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | return output; | ||
| 81 | }; | ||
| 82 | |||
| 83 | // Routing | 72 | // Routing |
| 84 | 73 | ||
| 85 | // Primary Open AI Route | 74 | // Primary Open AI Route |
| ... | @@ -132,9 +121,9 @@ app.post('/api', async (req, res) => { | ... | @@ -132,9 +121,9 @@ app.post('/api', async (req, res) => { |
| 132 | let usage = {}; | 121 | let usage = {}; |
| 133 | let enc = null; | 122 | let enc = null; |
| 134 | try { | 123 | try { |
| 135 | enc = encoding_for_model(tiktokenModels.includes(currentModel) ? currentModel : 'gpt-3.5-turbo'); | 124 | enc = encodingForModel(tiktokenModels.includes(currentModel) ? currentModel : 'gpt-3.5-turbo'); |
| 136 | usage.prompt_tokens = (enc.encode(await cleanString(query_prompt))).length; | 125 | usage.prompt_tokens = (enc.encode(query_prompt)).length; |
| 137 | usage.completion_tokens = (enc.encode(await cleanString(input))).length; | 126 | usage.completion_tokens = (enc.encode(input)).length; |
| 138 | usage.total_tokens = usage.prompt_tokens + usage.completion_tokens; | 127 | usage.total_tokens = usage.prompt_tokens + usage.completion_tokens; |
| 139 | } catch (e) { | 128 | } catch (e) { |
| 140 | console.log('Error encoding prompt text', e); | 129 | console.log('Error encoding prompt text', e); | ... | ... |
-
Please register or sign in to post a comment