Tokencost는 대형 언어 모델(LLM, Large Language Model) API를 사용할 때 발생하는 비용을 추정하는 도구입니다. 이 도구는 클라이언트 측에서 토큰을 계산하고, 이를 기반으로 프롬프트 및 응답(completion) 처리에 따른 비용을 예측합니다. 주로 AI 에이전트 및 LLM 기반 애플리케이션을 구축하는 개발자들에게 유용한 기능을 제공합니다.
주요 기능:
- LLM 가격 추적: 주요 LLM 제공업체는 자주 새로운 모델을 추가하고, 가격 정책도 업데이트합니다. Tokencost는 이러한 변경 사항을 추적하여 최신 가격 정보를 제공합니다.
- 토큰 계산: OpenAI와 같은 LLM API를 사용하기 전에 프롬프트에 포함된 토큰 수를 정확하게 계산할 수 있습니다. 이를 통해 API 요청 전에 예상 비용을 알 수 있습니다.
- 쉬운 통합: Tokencost는 간단한 함수 호출로 프롬프트나 응답의 비용을 계산할 수 있게 해주며, 이를 통해 개발자들이 빠르고 쉽게 비용을 관리할 수 있습니다.
Tokencost의 사용법은 매우 간단하며, 주로 LLM API 요청 시 토큰 수와 이에 따른 비용을 계산하는 데 활용됩니다. 아래에서는 Tokencost의 사용법을 단계별로 자세히 설명하겠습니다.
1. 설치
먼저 Tokencost 라이브러리를 설치해야 합니다. Python의 패키지 매니저인 pip를 사용하여 간단히 설치할 수 있습니다.
2. 기본 사용법
설치가 완료되면, Tokencost 라이브러리를 이용해 특정 모델에 대해 프롬프트와 응답의 비용을 계산할 수 있습니다.
3. 모델 및 가격 업데이트
Tokencost는 주요 LLM 제공업체에서 새로 추가된 모델이나 가격 변경 사항을 추적합니다. 따라서 사용자는 항상 최신 모델과 가격 정보를 바탕으로 비용을 계산할 수 있습니다.
4. 통합 및 응용
Tokencost는 간단한 함수 호출만으로 통합할 수 있어, AI 애플리케이션의 다양한 부분에 쉽게 적용할 수 있습니다. 이 도구를 활용해 애플리케이션의 운영 비용을 최적화하고, 예산 관리에 큰 도움을 줄 수 있습니다.
5. 사용 사례
- 비용 예측: 특정 작업에 대한 예상 비용을 사전에 계산해 예산을 효율적으로 관리할 수 있습니다.
- 실시간 비용 계산: 애플리케이션에서 사용자가 입력한 프롬프트에 대해 실시간으로 비용을 계산해 알려줄 수 있습니다.
이와 같은 방법으로 Tokencost를 사용해 LLM API의 비용을 손쉽게 관리할 수 있습니다.
- 설치
$ pip install tokencost
- openai requests 비용 계산
# Calculating the cost of prompts and completions from OpenAI requests
from openai import OpenAI
client = OpenAI()
model = "gpt-3.5-turbo"
prompt = [{ "role": "user", "content": "Say this is a test"}]
chat_completion = client.chat.completions.create(
messages=prompt, model=model
)
completion = chat_completion.choices[0].message.content
# "This is a test."
prompt_cost = calculate_prompt_cost(prompt, model)
completion_cost = calculate_completion_cost(completion, model)
print(f"{prompt_cost} + {completion_cost} = {prompt_cost + completion_cost}")
- 문자열로 비용계산
# Calculating cost using string prompts instead of messages
from tokencost import calculate_prompt_cost
prompt_string = "Hello world"
response = "How may I assist you today?"
model= "gpt-3.5-turbo"
prompt_cost = calculate_prompt_cost(prompt_string, model)
print(f"Cost: ${prompt_cost}")
# Cost: $3e-06
- 토큰 계산
# Counting tokens
from tokencost import count_message_tokens, count_string_tokens
message_prompt = [{ "role": "user", "content": "Hello world"}]
# Counting tokens in prompts formatted as message lists
print(count_message_tokens(message_prompt, model="gpt-3.5-turbo"))
# 9
# Alternatively, counting tokens in string prompts
print(count_string_tokens(prompt="Hello world", model="gpt-3.5-turbo"))
# 2
GitHub - AgentOps-AI/tokencost: Easy token price estimates for 400+ LLMs. TokenOps.
GitHub - AgentOps-AI/tokencost: Easy token price estimates for 400+ LLMs. TokenOps.
Easy token price estimates for 400+ LLMs. TokenOps. - AgentOps-AI/tokencost
github.com
'AI > LLM' 카테고리의 다른 글
AI 엔지니어와 데이터 과학자를 위한 Phoenix (1) | 2024.08.29 |
---|---|
AI로 미래의 과학을 여는 길: 독립적인 연구와 논문 작성이 가능한 'The AI Scientist'(sakana ai) (7) | 2024.08.26 |
LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS 논문 요약 (1) | 2024.08.22 |
오픈소스 프롬프트 엔지니어링 도구, Promptfoo 사용법 (3) | 2024.08.21 |
Efficient Memory Management for Large LanguageModel Serving with PagedAttention(vllm) 논문 요약 (2) | 2024.08.21 |