Skip to main content

LLM Cost Calculation

Project description

OPEN AI API - PRICE CALCULATOR

Overview

This package is created to calculate cost of OPEN AI API usage.

Pricing based on following url : OPEN AI Pricing API. Source code: Github

Usage

Installation

Install Page

pip install openai-pricing-calc-draft

Without Surrounding Code

from lll_pricing_calculation import calculate_openai_pricing

# Without surrounding Code
costForThousandCurrency,embeddingsCost,promptCost,completionTokenCost,total_cost = calculate_openai_pricing("GPT-3.5 Turbo","4K context",token_counter.total_embedding_token_count,token_counter.prompt_llm_token_count,token_counter.completion_llm_token_count)
print("currency:"+costForThousandCurrency)
print("embeddingsCost:"+str(embeddingsCost))
print("promptCost:"+str(promptCost))
print("completionTokenCost:"+str(completionTokenCost))
print("total cost:"+str(total_cost))

With Surrounding Code Using Llama Index

import tiktoken
from llama_index.callbacks import CallbackManager, TokenCountingHandler
from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
from lll_pricing_calculation import calculate_openai_pricing

sampleQuery = "Sample Query"
token_counter = TokenCountingHandler(
    tokenizer=tiktoken.encoding_for_model("text-davinci-003").encode,
    verbose=False  # set to true to see usage printed to the console
)
callback_manager = CallbackManager([token_counter])
service_context = ServiceContext.from_defaults(callback_manager=callback_manager)

def askQuestion(quest,storage,service_context,token_counter):
    token_counter.reset_counts()
    # index defined outside
    specificindex = index.get_index(dataFolder,"./storage"+storage,service_context)
    print(quest)
    result = query.query_index(specificindex, quest,"./storage"+storage)
    print(result)
    # otherwise, you can access the count directly
    print("Embeddings Token Counter stuff is below (total_embedding_token_count):")
    print(token_counter.total_embedding_token_count)
    print("Detailed ")
    print('Embedding Tokens: ', token_counter.total_embedding_token_count, '\n',
      'LLM Prompt Tokens: ', token_counter.prompt_llm_token_count, '\n',
      'LLM Completion Tokens: ', token_counter.completion_llm_token_count, '\n',
      'Total LLM Token Count: ', token_counter.total_llm_token_count)

    # CALCULATE PRICING TAKES PLACE HERE
    costForThousandCurrency,embeddingsCost,promptCost,completionTokenCost,total_cost = calculate_openai_pricing("GPT-3.5 Turbo","4K context",token_counter.total_embedding_token_count,token_counter.prompt_llm_token_count,token_counter.completion_llm_token_count)
    print("currency:"+costForThousandCurrency)
    print("embeddingsCost:"+str(embeddingsCost))
    print("promptCost:"+str(promptCost))
    print("completionTokenCost:"+str(completionTokenCost))
    print("total cost:"+str(total_cost))

askQuestion(sampleQuery,"4",service_context,token_counter)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openai_pricing_calc_draft-0.3.1.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openai_pricing_calc_draft-0.3.1-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file openai_pricing_calc_draft-0.3.1.tar.gz.

File metadata

File hashes

Hashes for openai_pricing_calc_draft-0.3.1.tar.gz
Algorithm Hash digest
SHA256 547d4f77560630cdf182901f41b9b4fcdb5e8edd81307e26fc8c1275b685d5d7
MD5 c5195498f9d3dfb0a28a37054bb025d2
BLAKE2b-256 9d05ee0670b438bebf8ecf047779a925ab2653d28747ebd7a9e7b1c9608be8e6

See more details on using hashes here.

File details

Details for the file openai_pricing_calc_draft-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_pricing_calc_draft-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6f10d84850a0433ac11e1c9873a208053dadf7756f741a81ba7179ad66cf8be3
MD5 ddc71e5d7dfc998ffdc7b4e9a6fbca45
BLAKE2b-256 8f0bf5e6f21a8fca78da9c2f1d0e772bd21e19043bcc304a9d9260212486198b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page