Skip to content

Commit

Permalink
refactor: make /tokens command use log_costs function
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Dec 20, 2024
1 parent f65e75e commit 1fd407c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
15 changes: 3 additions & 12 deletions gptme/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import Literal

from . import llm
from .util.export import export_chat_to_html
from .logmanager import LogManager, prepare_messages
from .message import (
Message,
Expand All @@ -16,10 +15,11 @@
print_msg,
toml_to_msgs,
)
from .llm.models import get_model
from .tools import ToolUse, execute_msg, loaded_tools
from .tools.base import ConfirmFunc, get_tool_format
from .util.export import export_chat_to_html
from .util.useredit import edit_text_with_editor
from .util.cost import log_costs

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -134,16 +134,7 @@ def handle_cmd(
yield from execute_msg(msg, confirm=lambda _: True)
case "tokens":
manager.undo(1, quiet=True)
model = get_model()
n_tokens = len_tokens(
manager.log.messages, model.model if model else "gpt-4"
)
print(f"Tokens used: {n_tokens}")
model = get_model()
if model:
print(f"Model: {model.model}")
if model.price_input:
print(f"Cost (input): ${n_tokens * model.price_input / 1_000_000}")
log_costs(manager.log.messages)
case "tools":
manager.undo(1, quiet=True)
print("Available tools:")
Expand Down
2 changes: 1 addition & 1 deletion gptme/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_tokenizer(model: str):
except KeyError:
global _warned_models
if model not in _warned_models:
logger.info(
logger.debug(
f"No tokenizer for '{model}'. Using tiktoken cl100k_base. Use results only as estimates."
)
_warned_models |= {model}
Expand Down

0 comments on commit 1fd407c

Please sign in to comment.