🚀 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.
-
Initialization:
from cost_estimator import CostEstimator estimator = CostEstimator()
-
Decorate your API call function:
@estimator() def call_openai_api(model="gpt-3.5-turbo-0613", temperature=0.0, messages=[...], mock=True): # Your API call logic here return openai.ChatCompletion.create( model=model, temperature=temperature, messages=messages, ) return response
-
Call the API from within a loop:
for message in tqdm(messages): response = call_openai_api(messages=[message], mock=False) # Or if you're mocking the API call: response = call_openai_api(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()
📌 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 the necessary packages:
pip install tiktoken openai tqdm lorem_text
Clone this repository and import the CostEstimator in your script.
pip package coming soon! 📦
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.
📜 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.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| gpt_cost_estimator-0.1.tar.gz | 4.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| gpt_cost_estimator-0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.7 kB
Release files / gpt_cost_estimator-0.1.tar.gz
| Download URL | gpt_cost_estimator-0.1.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f469a13960f1b451ea65ed2e629a3a76f3ab93c380c9db6fdabe04f13f45ae27
|
|
BLAKE2b-256 checksum How to use checksums |
749bb530a83884efe60aae5726ee1e7508bf1cb23dcddc087d5c36cd49a3d5b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / gpt_cost_estimator-0.1-py3-none-any.whl
| Download URL | gpt_cost_estimator-0.1-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
114f745c4ad378d7ea21af58865bfad5da57e7f4c59cc6541c5786e4c2af6887
|
|
BLAKE2b-256 checksum How to use checksums |
fdc0f1a6d723a21f9f5afa5295dc0c960349b9a890fe80df5fadd1dfb0d0d280
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|