gpt cost estimator
verride, Updated Prices
CostEstimator 클래스는 OpenAI의 GPT-3.5 및 GPT-4 모델 사용 비용을 추정하는 편리한 방법을 제공합니다. tqdm 있는 루프에서 사용하여 비용 추정을 실시간으로 추적하고 표시해야합니다.
tqdm 전원 루프에 원활하게 통합되며 각 API 통화 비용과 누적 된 총 비용을 표시합니다.CostStimator 클래스는 Jupyter Notebooks에서 사용되도록 설계되었습니다. 시연은 예제 노트를 참조하십시오.
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 : 공식 Openai Python 클라이언트.tqdm : 비용 세부 정보를 표시하는 실시간 진행 상황을 제공합니다. 사용자 친화적 인 비용 추적에 필수적입니다.lorem_text : 모의 API 응답을 생성합니다. PIP를 통해 설치하십시오
pip install gpt-cost-estimator수동 설치
pip install tiktoken openai tqdm lorem_text 이 저장소를 복제하고 스크립트에서 CostEstimator 가져 오십시오.
tqdm 으로 구동되는 진행률 표시 줄은 모든 API 호출에 대한 즉각적인 피드백을 제공합니다. 전화를 조롱하든 실제 API에 연결하든, 현재 요청의 비용과 지금까지 총 지출을 볼 수 있습니다. 비용을 모니터링하는 시각적이고 사용자 친화적 인 방법을 제공합니다.
MIT 라이센스는 허용되는 오픈 소스 라이센스입니다. 최소한의 제한으로 코드를 재사용 할 수 있지만 원래 라이센스의 귀속과 포함이 필요합니다. ?