openai-gpt-cost-estimator
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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'
}
*/

Package Sidebar

Install

npm i openai-gpt-cost-estimator

Weekly Downloads

10

Version

1.0.0

License

MIT

Unpacked Size

23.6 kB

Total Files

35

Last publish

Collaborators

  • rohailaltaf