mirror of
https://github.com/oobabooga/text-generation-webui.git
synced 2026-04-04 22:27:29 +00:00
UI: Add a token counter to the chat tab (counts input + history)
This commit is contained in:
parent
f6ca0ee072
commit
1b0e2d8750
3 changed files with 69 additions and 1 deletions
|
|
@ -1542,3 +1542,10 @@ strong {
|
|||
opacity: 0.8;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.token-display {
|
||||
font-family: monospace;
|
||||
font-size: 13px;
|
||||
color: var(--body-text-color-subdued);
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -230,7 +230,15 @@ def generate_chat_prompt(user_input, state, **kwargs):
|
|||
messages.insert(insert_pos, {"role": "user", "content": enhanced_user_msg})
|
||||
|
||||
user_input = user_input.strip()
|
||||
if user_input and not impersonate and not _continue:
|
||||
|
||||
# Check if we have attachments even with empty input
|
||||
has_attachments = False
|
||||
if not impersonate and not _continue and len(history_data.get('metadata', {})) > 0:
|
||||
current_row_idx = len(history)
|
||||
user_key = f"user_{current_row_idx}"
|
||||
has_attachments = user_key in metadata and "attachments" in metadata[user_key]
|
||||
|
||||
if (user_input or has_attachments) and not impersonate and not _continue:
|
||||
# For the current user input being processed, check if we need to add attachments
|
||||
if not impersonate and not _continue and len(history_data.get('metadata', {})) > 0:
|
||||
current_row_idx = len(history)
|
||||
|
|
@ -350,6 +358,50 @@ def generate_chat_prompt(user_input, state, **kwargs):
|
|||
return prompt
|
||||
|
||||
|
||||
def count_prompt_tokens(text_input, state):
|
||||
"""Count tokens for current history + input including attachments"""
|
||||
if shared.tokenizer is None:
|
||||
return "Tokenizer not available"
|
||||
|
||||
try:
|
||||
# Handle dict format with text and files
|
||||
files = []
|
||||
if isinstance(text_input, dict):
|
||||
files = text_input.get('files', [])
|
||||
text = text_input.get('text', '')
|
||||
else:
|
||||
text = text_input
|
||||
files = []
|
||||
|
||||
# Create temporary history copy to add attachments
|
||||
temp_history = copy.deepcopy(state['history'])
|
||||
if 'metadata' not in temp_history:
|
||||
temp_history['metadata'] = {}
|
||||
|
||||
# Process attachments if any
|
||||
if files:
|
||||
row_idx = len(temp_history['internal'])
|
||||
for file_path in files:
|
||||
add_message_attachment(temp_history, row_idx, file_path, is_user=True)
|
||||
|
||||
# Create temp state with modified history
|
||||
temp_state = copy.deepcopy(state)
|
||||
temp_state['history'] = temp_history
|
||||
|
||||
# Build prompt using existing logic
|
||||
prompt = generate_chat_prompt(text, temp_state)
|
||||
current_tokens = get_encoded_length(prompt)
|
||||
max_tokens = temp_state['truncation_length']
|
||||
|
||||
percentage = (current_tokens / max_tokens) * 100 if max_tokens > 0 else 0
|
||||
|
||||
return f"History + Input:<br/>{current_tokens:,} / {max_tokens:,} tokens ({percentage:.1f}%)"
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error counting tokens: {e}")
|
||||
return f"Error: {str(e)}"
|
||||
|
||||
|
||||
def get_stopping_strings(state):
|
||||
stopping_strings = []
|
||||
renderers = []
|
||||
|
|
|
|||
|
|
@ -95,6 +95,11 @@ def create_ui():
|
|||
with gr.Row():
|
||||
shared.gradio['chat-instruct_command'] = gr.Textbox(value=shared.settings['chat-instruct_command'], lines=12, label='Command for chat-instruct mode', info='<|character|> and <|prompt|> get replaced with the bot name and the regular chat prompt respectively.', visible=shared.settings['mode'] == 'chat-instruct', elem_classes=['add_scrollbar'])
|
||||
|
||||
with gr.Row():
|
||||
shared.gradio['count_tokens'] = gr.Button('Count tokens', size='sm')
|
||||
|
||||
shared.gradio['token_display'] = gr.HTML(value='', elem_classes='token-display')
|
||||
|
||||
# Hidden elements for version navigation and editing
|
||||
with gr.Row(visible=False):
|
||||
shared.gradio['navigate_message_index'] = gr.Number(value=-1, precision=0, elem_id="Navigate-message-index")
|
||||
|
|
@ -360,3 +365,7 @@ def create_event_handlers():
|
|||
None, None, None, js=f'() => {{{ui.switch_tabs_js}; switch_to_notebook()}}')
|
||||
|
||||
shared.gradio['show_controls'].change(None, gradio('show_controls'), None, js=f'(x) => {{{ui.show_controls_js}; toggle_controls(x)}}')
|
||||
|
||||
shared.gradio['count_tokens'].click(
|
||||
ui.gather_interface_values, gradio(shared.input_elements), gradio('interface_state')).then(
|
||||
chat.count_prompt_tokens, gradio('textbox', 'interface_state'), gradio('token_display'), show_progress=False)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue