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.
tqdm powered loops and displays the cost of each API call and the accumulated total cost.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 costReset 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)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.Install via pip
pip install gpt-cost-estimatorManual Installation
pip install tiktoken openai tqdm lorem_textClone this repository and import the CostEstimator in your script.
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.
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. ?