npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gpt-turbo-plugin-stats

v5.0.1

Published

GPT Turbo plugin that calculates your conversation size and cost

Downloads

41

Readme

GPT Turbo - Plugin - Stats

npm i gpt-turbo-plugin-stats License: MIT

A GPT Turbo plugin to augment conversations with usage statistics:

  • Size: The size (in tokens) of the conversation. This includes the system (context) message. It's an indicator of the minimum amount of tokens that will be sent on the next Chat Completion call.
  • Cumulative Size: The cumulative size (in tokens) of the conversation. It's the amount of tokens that have been send/received since the start of the conversation. This is the amount of tokens you've been charged for.
  • Cost: The cost (in $USD) of the conversation. It's an indicator of the minimum amount of money that will be charged on the next Chat Completion call.
  • Cumulative cost: The cumulative cost (in $USD) of the conversation. It's the amount of money that has been charged since the start of the conversation.

Disclaimer

The statistics are based on the tokens given by a third party library: gpt-tokenizer. While its tokenization is usually accurate, it's not guaranteed to be 100% accurate. Stats may differ from OpenAI's bill, but usually not by much.

Installation

gpt-turbo is required to use this plugin and is marked as a peer dependency. You can install it with:

npm i gpt-turbo

Then install this plugin with:

npm i gpt-turbo-plugin-stats

Note: since this plugin is developed alongside the library, both versions must be equal. Your installation will fail otherwise.

Usage

Here are some examples of how to inject the plugin (statsPlugin) in a Conversation and how to retrieve the output ConversationStats instance (stats).

import { Conversation } from "gpt-turbo";
import statsPlugin, { statsPluginName } from "gpt-turbo-plugin-stats";

// As a per-conversation plugin
const conversation = new Conversation({
    plugins: [statsPlugin]
});
const stats = conversation.plugins.getPluginOutput(statsPluginName); // ConversationStats

// As a global plugin
const globalPlugins = [statsPlugin];
declare module "gpt-turbo" {
    interface ConversationGlobalPluginsOverride {
        globalPlugins: typeof globalPlugins;
    }
}
Conversation.globalPlugins = globalPlugins;
const conversation = new Conversation();
const stats = conversation.plugins.getPluginOutput(statsPluginName); // ConversationStats

If you're getting the plugin dynamically (i.e. not from a string literal or the statsPluginName constant), you can use the isStatsPlugin type guard to properly type the plugin:

import { Conversation } from "gpt-turbo";
import statsPlugin, { isStatsPlugin } from "gpt-turbo-plugin-stats";

const conversation = new Conversation({
    plugins: [statsPlugin]
});

const plugin = conversation.plugins.getPlugin("gpt-turbo-plugin" + "-stats");
plugin.out; // any

if (isStatsPlugin(plugin)) {
    const stats = plugin.out; // ConversationStats
}

With the ConversationStats instance, you can retrieve the stats:

console.log(stats.size, stats.cumulativeSize, stats.cost, stats.cumulativeCost);

Finally, you can subscribe to events to get notified when the stats change:

const unsubscribe = stats.onStatsUpdate(() => {
    console.log(stats.size, stats.cumulativeSize, stats.cost, stats.cumulativeCost);
});
setTimeout(unsubscribe, 1000); // Unsubscribe after 1 second