gpt cost estimator
verride, Updated Prices
CostEstimatorクラスは、OpenAIのGPT-3.5およびGPT-4モデルを使用するコストを推定する便利な方法を提供します。 tqdmを使用してループで使用して、コストの見積もりをリアルタイムで追跡および表示することを目的としています。
tqdm搭載ループにシームレスに統合され、各API呼び出しのコストと累積総コストを表示します。Costestimatorクラスは、Jupyterノートブックで使用するように設計されています。デモンストレーションについては、ノートブックの例を参照してください。
APIコール機能を飾ります:
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 )ループ内からAPIを呼び出します。
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総コストをリセットします:
CostEstimator . reset ()総コストを読む:
CostEstimator . get_total_cost ()**新しい価格オーバーライド**
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 :API呼び出しを行わずに文字列内のトークンの数を決定するために使用されます。openai :公式Pothai Pythonクライアント。tqdm :コストの詳細を表示するリアルタイムの進捗バーを提供します。ユーザーフレンドリーなコスト追跡に不可欠です。lorem_text :モックAPI応答を生成します。 PIP経由でインストールします
pip install gpt-cost-estimator手動インストール
pip install tiktoken openai tqdm lorem_textこのリポジトリをクローンして、スクリプトにCostEstimatorをインポートします。
tqdmを搭載したプログレスバーは、すべてのAPI呼び出しにインスタントフィードバックを提供します。コールをock笑するか、実際のAPIに接続するかにかかわらず、現在のリクエストのコストとこれまでの総支出が表示されます。コストを監視するための視覚的でユーザーフレンドリーな方法を提供します。
MITライセンスは、許容されるオープンソースライセンスです。最小限の制限でコードを再利用することができますが、元のライセンスの属性と含めることのみが必要です。 ?