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:
- tqdm Integration: Seamlessly integrates into
tqdmpowered 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.
-
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)
-
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
-
Reset Total Cost:
CostEstimator.reset()
-
Read Total Cost:
CostEstimator.get_total_cost()
-
** NEW Price Override**
from cost_estimator import CostEstimator # Define custom prices for models custom_prices = { "gpt-4o-mini": {"input": 0.0002, "output": 0.0007}, } # Instantiate the CostEstimator with custom prices estimator = CostEstimator(price_overrides=custom_prices) # Use the estimator as usual @estimator 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)
📌 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.
- 2024-11-25 Updated prices, added new models. Added price override.
📜 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gpt_cost_estimator-0.7.tar.gz.
File metadata
- Download URL: gpt_cost_estimator-0.7.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca51e4bec42fbf190bf34fa0662c5d1047776156797f26fcd3bd8901b1e4c148
|
|
| MD5 |
78c08ce51c5c546741dc7bc475a7cd1a
|
|
| BLAKE2b-256 |
4c8c9cc7c57cceee42f4bdad66382458b51ad4af2899308ed6a10a9c9d0d056c
|
Provenance
The following attestation bundles were made for gpt_cost_estimator-0.7.tar.gz:
Publisher:
python-publish.yml on michaelachmann/gpt-cost-estimator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gpt_cost_estimator-0.7.tar.gz -
Subject digest:
ca51e4bec42fbf190bf34fa0662c5d1047776156797f26fcd3bd8901b1e4c148 - Sigstore transparency entry: 151308107
- Sigstore integration time:
-
Permalink:
michaelachmann/gpt-cost-estimator@469a54132ab26c3a13d770fceed968f5d3de89c2 -
Branch / Tag:
refs/tags/v0.7 - Owner: https://github.com/michaelachmann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@469a54132ab26c3a13d770fceed968f5d3de89c2 -
Trigger Event:
release
-
Statement type:
File details
Details for the file gpt_cost_estimator-0.7-py3-none-any.whl.
File metadata
- Download URL: gpt_cost_estimator-0.7-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
692544be4ab44ec177497ced19fb29dc8baaba6ba9e3ec91cfc85b0b3c344394
|
|
| MD5 |
53e4c77650333ff99ed6135c8a88716e
|
|
| BLAKE2b-256 |
7cae875531d47f7fce1108257a6b82025f7a293e74ca6b098a545946c993890a
|
Provenance
The following attestation bundles were made for gpt_cost_estimator-0.7-py3-none-any.whl:
Publisher:
python-publish.yml on michaelachmann/gpt-cost-estimator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gpt_cost_estimator-0.7-py3-none-any.whl -
Subject digest:
692544be4ab44ec177497ced19fb29dc8baaba6ba9e3ec91cfc85b0b3c344394 - Sigstore transparency entry: 151308109
- Sigstore integration time:
-
Permalink:
michaelachmann/gpt-cost-estimator@469a54132ab26c3a13d770fceed968f5d3de89c2 -
Branch / Tag:
refs/tags/v0.7 - Owner: https://github.com/michaelachmann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@469a54132ab26c3a13d770fceed968f5d3de89c2 -
Trigger Event:
release
-
Statement type: