비용 계산하기
AI 기능을 대규모로 배포하기 전에 비용을 미리 추정하는 것이 중요합니다. 비용은 사용된 입력 및 출력 토큰 수와 선택한 모델에 따라 달라집니다.
이번 과제에서는 고객 채팅 기록을 요약하는 데 드는 비용을 계산합니다.
OpenAI client와 text, prompt, max_completion_tokens는 미리 로드되어 있습니다.
이 연습은 강의의 일부입니다
OpenAI API 활용하기
연습 안내
response에서 입력 토큰 사용량을 추출하세요.- 출력 토큰 비용을 더하여 비용 계산을 완성하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
client = OpenAI(api_key="")
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
max_completion_tokens=max_completion_tokens
)
input_token_price = 0.15 / 1_000_000
output_token_price = 0.6 / 1_000_000
# Extract token usage
input_tokens = response.____.____
output_tokens = max_completion_tokens
# Calculate cost
cost = (input_tokens * input_token_price + ____ * ____)
print(f"Estimated cost: ${cost}")