|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Literal, Optional, Annotated |
| 4 | + |
| 5 | +from cyclopts import Parameter |
| 6 | +from rich.markup import escape as escape_rich_markup |
| 7 | + |
| 8 | +from together import omit |
| 9 | +from together._utils._json import openapi_dumps |
| 10 | +from together.lib.cli.utils.config import CLIConfigParameter |
| 11 | +from together.lib.cli.utils._console import console |
| 12 | +from together.lib.cli.components.list import ListTable |
| 13 | +from together.lib.cli.components.loader import show_loading_status |
| 14 | +from together.types.fine_tune_preview_row import FineTunePreviewRow |
| 15 | + |
| 16 | +_TOKEN_PREVIEW_LIMIT = 32 |
| 17 | + |
| 18 | + |
| 19 | +def _format_token(token: str) -> str: |
| 20 | + return escape_rich_markup(token.encode("unicode_escape").decode("ascii")) |
| 21 | + |
| 22 | + |
| 23 | +def _format_tokens(row: FineTunePreviewRow) -> str: |
| 24 | + tokens = row.tokens[:_TOKEN_PREVIEW_LIMIT] |
| 25 | + labels = row.labels[:_TOKEN_PREVIEW_LIMIT] |
| 26 | + formatted: list[str] = [] |
| 27 | + for token, label in zip(tokens, labels): |
| 28 | + token_text = _format_token(token) |
| 29 | + formatted.append(f"[dim]{token_text}[/dim]" if label == -100 else token_text) |
| 30 | + |
| 31 | + if len(row.tokens) > _TOKEN_PREVIEW_LIMIT: |
| 32 | + formatted.append("[dim]...[/dim]") |
| 33 | + |
| 34 | + return " ".join(formatted) |
| 35 | + |
| 36 | + |
| 37 | +def _format_spans(row: FineTunePreviewRow) -> str: |
| 38 | + if not row.trained_spans: |
| 39 | + return "-" |
| 40 | + return ", ".join(f"{start}-{end}" for start, end in row.trained_spans) |
| 41 | + |
| 42 | + |
| 43 | +async def preview( |
| 44 | + training_file: Annotated[ |
| 45 | + str, |
| 46 | + Parameter( |
| 47 | + alias="-t", |
| 48 | + help="Training file ID from the Files API to sample for preview", |
| 49 | + ), |
| 50 | + ], |
| 51 | + model: Annotated[ |
| 52 | + str, |
| 53 | + Parameter( |
| 54 | + alias="-M", |
| 55 | + help="Name of the base model whose tokenizer and chat template will be used", |
| 56 | + ), |
| 57 | + ], |
| 58 | + top_k: Annotated[ |
| 59 | + Optional[int], |
| 60 | + Parameter(help="Maximum number of rows from the start of the training file to tokenize"), |
| 61 | + ] = None, |
| 62 | + train_on_inputs: Annotated[ |
| 63 | + Optional[bool], |
| 64 | + Parameter(help="Whether prompt or user-message tokens should contribute to training loss"), |
| 65 | + ] = None, |
| 66 | + training_method: Annotated[ |
| 67 | + Literal["sft"], |
| 68 | + Parameter(help="Fine-tuning method to preview; only supervised fine-tuning is currently supported"), |
| 69 | + ] = "sft", |
| 70 | + *, |
| 71 | + config: CLIConfigParameter, |
| 72 | +) -> None: |
| 73 | + """Preview how a fine-tuning training file will be tokenized.""" |
| 74 | + response = await show_loading_status( |
| 75 | + "Loading fine-tuning preview...", |
| 76 | + config.client.fine_tuning.preview( |
| 77 | + model=model, |
| 78 | + training_file=training_file, |
| 79 | + top_k=top_k if top_k is not None else omit, |
| 80 | + train_on_inputs=train_on_inputs if train_on_inputs is not None else omit, |
| 81 | + training_method=training_method, |
| 82 | + ), |
| 83 | + ) |
| 84 | + |
| 85 | + if config.json: |
| 86 | + console.print_json(openapi_dumps(response).decode("utf-8")) |
| 87 | + return |
| 88 | + |
| 89 | + console.print(f"[dim][primary]Model:[/primary][/dim]\t\t[bold]{escape_rich_markup(response.model)}[/bold]") |
| 90 | + console.print(f"[dim][primary]Dataset format:[/primary][/dim]\t{response.dataset_format}") |
| 91 | + console.print(f"[dim][primary]Max sequence:[/primary][/dim]\t{response.max_seq_length}") |
| 92 | + console.print(f"[dim][primary]Train inputs:[/primary][/dim]\t{response.train_on_inputs}") |
| 93 | + |
| 94 | + table = ListTable("Preview Rows", empty_message="No preview rows returned") |
| 95 | + table.add_primary_column("Row") |
| 96 | + table.add_column("Tokens", justify="right") |
| 97 | + table.add_column("Trained", justify="right") |
| 98 | + table.add_column("Truncated") |
| 99 | + table.add_column("Trained Spans") |
| 100 | + table.add_column("Token Preview", ratio=4) |
| 101 | + |
| 102 | + for index, row in enumerate(response.rows, start=1): |
| 103 | + table.add_row( |
| 104 | + str(index), |
| 105 | + str(row.num_tokens), |
| 106 | + str(row.num_trained_tokens), |
| 107 | + "yes" if row.truncated else "no", |
| 108 | + _format_spans(row), |
| 109 | + _format_tokens(row), |
| 110 | + ) |
| 111 | + |
| 112 | + console.print(table) |
0 commit comments