token pricing

This commit is contained in:
Paul Gauthier 2024-04-17 15:34:59 -07:00
parent c9bb22d6d5
commit 3662a4680b
3 changed files with 20 additions and 16 deletions

View file

@ -508,7 +508,7 @@ class Coder:
messages += self.cur_messages messages += self.cur_messages
# Add the reminder prompt if we still have room to include it. # Add the reminder prompt if we still have room to include it.
if total_tokens < self.main_model.max_context_tokens: if total_tokens < self.main_model.info.get("max_input_tokens", 0):
messages += reminder_message messages += reminder_message
return messages return messages
@ -707,10 +707,10 @@ class Coder:
completion_tokens = completion.usage.completion_tokens completion_tokens = completion.usage.completion_tokens
tokens = f"{prompt_tokens} prompt tokens, {completion_tokens} completion tokens" tokens = f"{prompt_tokens} prompt tokens, {completion_tokens} completion tokens"
if self.main_model.prompt_price: if self.main_model.info.get("input_cost_per_token"):
cost = prompt_tokens * self.main_model.prompt_price / 1000 cost = prompt_tokens * self.main_model.info.get("input_cost_per_token")
if self.main_model.completion_price: if self.main_model.info.get("output_cost_per_token"):
cost += completion_tokens * self.main_model.completion_price / 1000 cost += completion_tokens * self.main_model.info.get("output_cost_per_token")
tokens += f", ${cost:.6f} cost" tokens += f", ${cost:.6f} cost"
self.total_cost += cost self.total_cost += cost

View file

@ -176,7 +176,7 @@ class Commands:
self.io.tool_output() self.io.tool_output()
width = 8 width = 8
cost_width = 7 cost_width = 9
def fmt(v): def fmt(v):
return format(int(v), ",").rjust(width) return format(int(v), ",").rjust(width)
@ -188,13 +188,13 @@ class Commands:
total_cost = 0.0 total_cost = 0.0
for tk, msg, tip in res: for tk, msg, tip in res:
total += tk total += tk
cost = tk * (self.coder.main_model.prompt_price / 1000) cost = tk * self.coder.main_model.info.get("input_cost_per_token", 0)
total_cost += cost total_cost += cost
msg = msg.ljust(col_width) msg = msg.ljust(col_width)
self.io.tool_output(f"${cost:5.2f} {fmt(tk)} {msg} {tip}") self.io.tool_output(f"${cost:7.4f} {fmt(tk)} {msg} {tip}")
self.io.tool_output("=" * (width + cost_width + 1)) self.io.tool_output("=" * (width + cost_width + 1))
self.io.tool_output(f"${total_cost:5.2f} {fmt(total)} tokens total") self.io.tool_output(f"${total_cost:7.4f} {fmt(total)} tokens total")
# only switch to image model token count if gpt4 and openai and image in files # only switch to image model token count if gpt4 and openai and image in files
image_in_chat = False image_in_chat = False
@ -203,7 +203,7 @@ class Commands:
is_image_file(relative_fname) is_image_file(relative_fname)
for relative_fname in self.coder.get_inchat_relative_files() for relative_fname in self.coder.get_inchat_relative_files()
) )
limit = 128000 if image_in_chat else self.coder.main_model.max_context_tokens limit = 128000 if image_in_chat else self.coder.main_model.info.get("max_input_tokens")
remaining = limit - total remaining = limit - total
if remaining > 1024: if remaining > 1024:

View file

@ -4,22 +4,26 @@ import math
import litellm import litellm
from PIL import Image from PIL import Image
from aider.dump import dump
class Model: class Model:
name = None name = None
edit_format = "whole" edit_format = "whole"
max_context_tokens = 0
max_chat_history_tokens = 1024
always_available = False
use_repo_map = False use_repo_map = False
send_undo_reply = False send_undo_reply = False
max_chat_history_tokens = 1024
prompt_price = None
completion_price = None
def __init__(self, model): def __init__(self, model):
self.name = model self.name = model
self.info = litellm.get_model_info(model)
dump(self.info)
if self.info.get("max_input_tokens", 0) < 32 * 1024:
self.max_chat_history_tokens = 1024
else:
self.max_chat_history_tokens = 2 * 1024
def __str__(self): def __str__(self):
return self.name return self.name