Skip to main content

A cost estimator for OpenAI API calls in tqdm loops.

Project description

🚀 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. 🔄🔓💼

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

gpt_cost_estimator-0.4.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

gpt_cost_estimator-0.4-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file gpt_cost_estimator-0.4.tar.gz.

File metadata

  • Download URL: gpt_cost_estimator-0.4.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for gpt_cost_estimator-0.4.tar.gz
Algorithm Hash digest
SHA256 f530e2ae07d42fa966577535487e9d812611ffb6a7de58c98423246ea9f237ac
MD5 094d710cb81475baffded2803d4a0b9c
BLAKE2b-256 1de8e8c3106bb7674d34b7d112db37c189167b2351d4d7f2af7fab720911194f

See more details on using hashes here.

File details

Details for the file gpt_cost_estimator-0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for gpt_cost_estimator-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e07be6ec3f11d769c83f7b787404aef05b699db778ba060b9258d9f9887a06f7
MD5 e5094e1ebddc9a1e40e24ddf8ee347f7
BLAKE2b-256 33b593d707fa1ddc4c2c65434552d48845bc1917838ec5ee82e0921275b8b2d3

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