Skip to main content

🚀 CostEstimator for OpenAI with tqdm integration 💸

The CostEstimator class offers a convenient way to estimate the cost of using OpenAI's gpt-3.5 and gpt-4 models. It is intended to be used in a loop with tqdm to track and display the cost estimation in real-time.

🌟 Features:

Screenshot of an estimation

  • tqdm Integration: Seamlessly integrates into tqdm powered loops and displays the cost of each API call and the accumulated total cost.
  • Model Synonyms: Easily switch between model versions or names. 🔄
  • Mock Responses: Generate fake 🤖 API responses to estimate costs without making actual API requests.
  • Cost Breakdown: Display the estimated 💰 cost per request and the accumulated cost.

🛠 Usage:

The CostEstimator class was designed to be used in Jupyter Notebooks, see the example notebook for a demonstration.

  1. Decorate your API call function:

    import openai 
    from gpt_cost_estimator import CostEstimator
    
    @CostEstimator()
    def query_openai(model, messages, **kwargs):
        args_to_remove = ['mock', 'completion_tokens']
    
        for arg in args_to_remove:
          if arg in kwargs:
              del kwargs[arg]
    
        return openai.ChatCompletion.create(
            model = model,
            messages = messages,
            **kwargs)
    
  2. Call the API from within a loop:

    for message in tqdm(messages):
        response = query_openai(model="gpt-3.5-turbo-0613", messages=[message], mock=False)
    
        # Or if you're mocking the API call:
        response = query_openai(model="gpt-3.5-turbo-0613", messages=[message], mock=True)     
    
    print() # We need to print a newline to show the total cost
    
  3. Reset Total Cost:

    CostEstimator.reset()
    
  4. Read Total Cost:

    CostEstimator.get_total_cost()
    

📌 Dependencies:

  • tiktoken: Used to determine the number of tokens in a string without making an API call.
  • openai: Official OpenAI Python client.
  • tqdm: Provides the real-time progress bar that displays the cost details. Essential for user-friendly cost tracking.
  • lorem_text: Generates mock API responses.

🔧 Installation:

Install via pip

pip install gpt-cost-estimator

Manual Installation

pip install tiktoken openai tqdm lorem_text

Clone this repository and import the CostEstimator in your script.

How tqdm is Integrated:

The progress bar powered by tqdm provides instant feedback for every API call. Whether you're mocking the call or connecting to the real API, you'll see the cost of the current request and the total expenditure so far. It offers a visual and user-friendly way to monitor costs.

📝 Notes:

  • Ensure the pricing details in the estimator match OpenAI's official pricing.
  • Always test the estimator with your specific use cases to ensure accuracy.
  • The progress bar will appear in your Jupyter notebook or console where the script runs.

Change Log:

  • 2024-01-26 Updated prices and added additional models.

📜 License:

The MIT License is a permissive open-source license. It allows for reuse of code with minimal restrictions, while requiring only attribution and inclusion of the original license. 🔄🔓💼

Metadata

Release files for gpt-cost-estimator 0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gpt-cost-estimator 0.6
File Size Uploaded
gpt_cost_estimator-0.6.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gpt-cost-estimator 0.6
File Interpreter ABI Platform
gpt_cost_estimator-0.6-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / gpt_cost_estimator-0.6.tar.gz

Download URL gpt_cost_estimator-0.6.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
bc18ff2dfe0206cd085858dd028d4b74db4db8e25d6e19c17fce7c0c5183225b
BLAKE2b-256 checksum
How to use checksums
9c64188b8fb2e8018703f1ebb965010ea7388d7fb5ccb4880b5fcd25dc4979f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / gpt_cost_estimator-0.6-py3-none-any.whl

Download URL gpt_cost_estimator-0.6-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e98d22ea784398eca273854388e5dc5e91f7b0e44f436bef4e7c4426e8a59e73
BLAKE2b-256 checksum
How to use checksums
b65051294af6c3236f2ec8f240d9c4499f380afa2216fff1e9402d1751066f86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

0.7

2 release files

This release

0.6 This release

2 release files

0.5

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page