JSPM

openai-gpt-cost-estimator

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q33476F
  • License MIT

Estimate the cost for various OpenAI API operations.

Package Exports

  • openai-gpt-cost-estimator
  • openai-gpt-cost-estimator/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (openai-gpt-cost-estimator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

openai-gpt-cost-estimator

Easily calculate the estimated cost of various operations for OpenAI's GPT API

Note: Currently this supports the completion and embedding commands. More to be added soon. Pricing is accurate as of 2022/12/28. This is NOT an official package by OpenAI.

Usage

Install

yarn add openai-gpt-cost-estimator

OR

npm i openai-gpt-cost-estimator

Example 1: Completion

import { estimate, Operation, CompletionModel } from 'openai-gpt-cost-estimator'

const prompt = "The quick brown fox jumps over the lazy dog"
const config = {
  operation: Operation.COMPLETION,
  model: CompletionModel.DAVINCI,
  prompt,
  n: 3,
  bestOf: 5,
  maxTokens: 32
}
const result = estimate(config)

console.log(result)

/*
{
  promptTokens: 9,
  completionTokens: 160,
  totalTokens: 169,
  totalCost: 0.0034,
  totalCostDisplay: '$0.0034'
}
*/

Example 2: Embedding

import { estimate, Operation, EmbeddingModel } from 'openai-gpt-cost-estimator'

const prompt = "[SOME LONG PROMPT OF 8000 TOKENS]"
const config = {
  operation: Operation.EMBEDDING,
  model: EmbeddingModel.ADA_V2,
  prompt
}
const result = estimate(config)

console.log(result)

/*
{
  promptTokens: 8000,
  totalTokens: 8000,
  totalCost: 0.0032,
  totalCostDisplay: '$0.0032'
}
*/