From 1fd407c109b37b050df3fa2956e414af5f7c1dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Fri, 20 Dec 2024 13:05:38 +0100 Subject: [PATCH] refactor: make /tokens command use log_costs function --- gptme/commands.py | 15 +++------------ gptme/util/__init__.py | 2 +- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/gptme/commands.py b/gptme/commands.py index c7613db2..ce157d41 100644 --- a/gptme/commands.py +++ b/gptme/commands.py @@ -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, @@ -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__) @@ -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:") diff --git a/gptme/util/__init__.py b/gptme/util/__init__.py index 6bd845f7..f90ef50c 100644 --- a/gptme/util/__init__.py +++ b/gptme/util/__init__.py @@ -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}