
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
openai-chat-tokens
Advanced tools
Estimate the number of tokens an OpenAI chat completion request will use
A TypeScript / JavaScript library for estimating the number of tokens an OpenAI chat completion request will use.
Estimating token usage for chat completions isn't quite as easy as it sounds.
For regular chat messages, you need to consider how the messages are formatted by OpenAI when they're provided to the model, as they don't simply dump the JSON messages they receive via the API into the model.
For function calling, things are even more complex, as the OpenAPI-style function definitions get rewritten into TypeScript type definitions.
This library handles both of those cases, as well as a minor adjustment needed for handling the results of function calling. tiktoken is used to do the tokenization.
import { promptTokensEstimate } from "openai-chat-tokens";
const estimate = promptTokensEstimate({
messages: [
{ role: "system", content: "These aren't the droids you're looking for" },
{ role: "user", content: "You can go about your business. Move along." },
],
functions: [
{
name: "activate_hyperdrive",
description: "Activate the hyperdrive",
parameters: {
type: "object",
properties: {
destination: { type: "string" },
},
},
},
],
});
Built in TypeScript, tested with Jest.
$ npm install
$ npm test
When adding new test cases or debugging token count mismatches, it can be helpful to validate the estimated tokens in the tests against the live OpenAI API. To do this:
OPENAI_API_KEY
environment variable with a live API keyvalidate: true
to one of the test examples, or set validateAll
to true
in token-counts.test.ts
, then run the testsFAQs
Estimate the number of tokens an OpenAI chat completion request will use
The npm package openai-chat-tokens receives a total of 31,916 weekly downloads. As such, openai-chat-tokens popularity was classified as popular.
We found that openai-chat-tokens demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.