gpt-tokens
Advanced tools
Comparing version 1.2.0 to 1.3.0
{ | ||
"name": "gpt-tokens", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Calculate the token consumption and amount of openai gpt message", | ||
@@ -16,7 +16,14 @@ "keywords": [ | ||
"scripts": { | ||
"build": "yarn && tsc && node test.js" | ||
"test": "node test.js", | ||
"build": "npm i && rm -rf dist && tsc && npm run test" | ||
}, | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"module": "index.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.ts", | ||
"require": "./dist/index.js" | ||
} | ||
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"module": "dist/index.ts", | ||
"repository": "https://github.com/Cainier/gpt-tokens", | ||
@@ -27,4 +34,3 @@ "author": "Cainier <xingrong.dev@gmail.com>", | ||
"decimal.js": "^10.4.3", | ||
"js-tiktoken": "^1.0.7", | ||
"openai": "^4.6.1", | ||
"js-tiktoken": "^1.0.10", | ||
"openai-chat-tokens": "^0.2.8" | ||
@@ -34,6 +40,7 @@ }, | ||
"@types/node": "^12.20.0", | ||
"typescript": "^5.0.4" | ||
"typescript": "^5.0.4", | ||
"openai": "^4.28.0" | ||
}, | ||
"files": [ | ||
"index.ts", | ||
"src/index.ts", | ||
"index.d.ts", | ||
@@ -40,0 +47,0 @@ "index.js", |
70
test.js
@@ -1,4 +0,4 @@ | ||
const fs = require('fs') | ||
const OpenAI = require('openai') | ||
const { GPTTokens, testGPTTokens } = require('./index') | ||
const fs = require('fs') | ||
const OpenAI = require('openai') | ||
const { GPTTokens } = require('./dist/index') | ||
@@ -14,6 +14,66 @@ const [apiKey = process.env.OPENAI_API_KEY] = process.argv.slice(2) | ||
async function testGPTTokens(prompt) { | ||
const messages = [ | ||
{ role: 'user', content: prompt }, | ||
] | ||
const supportModels = GPTTokens.supportModels | ||
.filter(model => !model.startsWith('ft:')) | ||
const { length: modelsNum } = supportModels | ||
for (let i = 0; i < modelsNum; i += 1) { | ||
const model = supportModels[i] | ||
console.info(`[${i + 1}/${modelsNum}]: Testing ${model}...`) | ||
let ignoreModel = false | ||
const chatCompletion = await openai.chat.completions.create({ | ||
model, | ||
messages, | ||
}) | ||
.catch(err => { | ||
ignoreModel = true | ||
console.info(`Ignore model ${model}:`) | ||
console.info(err.message) | ||
}) | ||
const openaiUsage = chatCompletion?.usage | ||
const gptTokens = new GPTTokens({ | ||
model, | ||
messages: [ | ||
...messages, | ||
...[chatCompletion?.choices[0].message], | ||
], | ||
}) | ||
if (ignoreModel) continue | ||
if (!openaiUsage) { | ||
console.error(`Test ${model} failed (openai return usage is null)`) | ||
continue | ||
} | ||
if (gptTokens.promptUsedTokens !== openaiUsage.prompt_tokens) | ||
throw new Error(`Test ${model} promptUsedTokens failed (openai: ${openaiUsage.prompt_tokens}/ gpt-tokens: ${gptTokens.promptUsedTokens})`) | ||
if (gptTokens.completionUsedTokens !== openaiUsage.completion_tokens) | ||
throw new Error(`Test ${model} completionUsedTokens failed (openai: ${openaiUsage.completion_tokens}/ gpt-tokens: ${gptTokens.completionUsedTokens})`) | ||
if (gptTokens.usedTokens !== openaiUsage?.total_tokens) | ||
throw new Error(`Test ${model} usedTokens failed (openai: ${openaiUsage?.total_tokens}/ gpt-tokens: ${gptTokens.usedTokens})`) | ||
console.info('Pass!') | ||
} | ||
console.info('Test success!') | ||
} | ||
async function testBasic(prompt) { | ||
console.info('Testing GPT...') | ||
await testGPTTokens(openai, prompt) | ||
await testGPTTokens(prompt) | ||
} | ||
@@ -80,3 +140,3 @@ | ||
// In production, this could be your backend API or an external API | ||
function getCurrentWeather(location, unit = 'fahrenheit') { | ||
function getCurrentWeather(location) { | ||
if (location.toLowerCase().includes('tokyo')) { | ||
@@ -83,0 +143,0 @@ return JSON.stringify({ location: 'Tokyo', temperature: '10', unit: 'celsius' }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
37226
3
735
1
1
- Removedopenai@^4.6.1
- Removed@types/node@18.19.68(transitive)
- Removed@types/node-fetch@2.6.12(transitive)
- Removedabort-controller@3.0.0(transitive)
- Removedagentkeepalive@4.5.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedevent-target-shim@5.0.1(transitive)
- Removedform-data@4.0.1(transitive)
- Removedform-data-encoder@1.7.2(transitive)
- Removedformdata-node@4.4.1(transitive)
- Removedhumanize-ms@1.2.1(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedms@2.1.3(transitive)
- Removednode-domexception@1.0.0(transitive)
- Removednode-fetch@2.7.0(transitive)
- Removedopenai@4.77.0(transitive)
- Removedtr46@0.0.3(transitive)
- Removedundici-types@5.26.5(transitive)
- Removedweb-streams-polyfill@4.0.0-beta.3(transitive)
- Removedwebidl-conversions@3.0.1(transitive)
- Removedwhatwg-url@5.0.0(transitive)
Updatedjs-tiktoken@^1.0.10