ttok

repository·main·Indexed 18 days ago

https://github.com/simonw/ttok

A CLI tool used to count and truncate text based on tokens for Large Language Models (LLMs) such as GPT-3.5 and GPT-4. It supports specifying tokenizer models via the -m/--model option, encoding text to integer token IDs, decoding IDs back to text, and truncating output to a specific token limit.

Tokens
1.5K
Snippets
8
Records
11
Agent score
14%

What's inside ttok

  1. Count tokens in text

    main

    ttok can count tokens for text provided as command-line arguments or via standard input (stdin).

    Using arguments

    ttok Hello world

    Using stdin

    When piping text, use echo -n to prevent adding a newline character, which would otherwise increase the token count.

    echo -n "Hello world" | ttok

    Appending text to piped input

    To pipe in text and then append additional tokens from arguments, use the -i - option:

    echo -n "Hello world" | ttok more text -i -
  2. View and decode token IDs

    main

    ttok provides utilities to inspect the integer token IDs used by models.

    View integer token IDs

    Use --encode to see the integer IDs for the input text:

    ttok Hello world --encode

    View detailed token breakdown

    Add the --tokens flag to see the actual byte strings for each token:

    ttok Hello world --encode --tokens

    Convert token IDs back to text

    Use the --decode option to reverse the process:

    ttok 9906 1917 --decode
  3. Handle special tokens with --allow-special

    main
    By default, ttok will raise an error if it encounters disallowed special tokens during encoding. To bypass this error and allow special tokens, use the --allow-special flag. Note that this flag must be used in conjunction with --encode or --tokens.
  4. Use the ttok CLI to count and truncate tokens

    main

    The ttok command-line tool allows you to count tokens in text or truncate text to a specific token limit. You can provide text as positional arguments or via stdin using the --input flag or a pipe.

    Basic Usage

    Count tokens from arguments:

    ttok one two three

    Count tokens from stdin:

    cat input.txt | ttok

    Truncate text to a specific token count:

    cat input.txt | ttok -t 100

    Truncate using a specific model:

    cat input.txt | ttok -t 100 -m gpt2
    ttok one two three
    cat input.txt | ttok
    cat input.txt | ttok -t 100
    cat input.txt | ttok -t 100 -m gpt2
  5. Reference: ttok CLI options

    main

    The following options are available for the ttok command:

    • --version: Show the version and exit.
    • -i, --input FILENAME: Specify an input file.
    • -t, --truncate INTEGER: Truncate to this many tokens.
    • -m, --model TEXT: Which model to use.
    • --encode, --tokens: Output token integers.
    • --decode: Convert token integers to text.
    • --tokens: Output full tokens.
    • --allow-special: Do not error on special tokens.
    • --help: Show this message and exit.
  6. Reference: Available models for -m/--model

    main

    The following models are supported via the -m or --model option. The names listed are the valid tokens to use with the CLI.

    • gpt-4 (cl100k_base)
    • gpt-3.5-turbo (cl100k_base)
    • gpt-3.5 (cl100k_base)
    • gpt-35-turbo (cl100k_base)
    • davinci-002 (cl100k_base)
    • babbage-002 (cl100k_base)
    • text-embedding-ada-002 (cl100k_base)
    • text-embedding-3-small (cl100k_base)
    • text-embedding-3-large (cl100k_base)
    • text-davinci-003 (p50k_base)
    • text-davinci-002 (p50k_base)
    • text-davinci-001 (r50k_base)
    • text-curie-001 (r50k_base)
    • text-babbage-001 (r50k_base)
    • text-ada-001 (r50k_base)
    • davinci (r50k_base)
    • curie (r50k_base)
    • babbage (r50k_base)
    • ada (r50k_base)
    • code-davinci-002 (p50k_base)
    • code-davinci-001 (p50k_base)
    • code-cushman-002 (p50k_base)
    • code-cushman-001 (p50k_base)
    • davinci-codex (p50k_base)
    • cushman-codex (p50k_base)
    • text-davinci-edit-001 (p50k_edit)
    • code-davinci-edit-001 (p50k_edit)
    • text-similarity-davinci-001 (r50k_base)
    • text-similarity-curie-001 (r50k_base)
    • text-similarity-babbage-001 (r50k_base)
    • text-similarity-ada-001 (r50k_base)
    • text-search-davinci-doc-001 (r50k_base)
    • text-search-curie-doc-001 (r50k_base)
    • text-search-babbage-doc-001 (r50k_base)
    • text-search-ada-doc-001 (r50k_base)
    • code-search-babbage-code-001 (r50k_base)
    • code-search-ada-code-001 (r50k_base)
    • gpt2 (gpt2)
    • gpt-2 (gpt2)
  7. View token integers and token details with ttok

    main

    You can use ttok to inspect the underlying tokenization of a string.

    To view token integers: Use --encode to see the integer representation of the tokens.

    cat input.txt | ttok --encode

    To see the details of the tokens (as bytes): Use --tokens to see the actual token content (e.g., [b'hello', b' world']).

    ttok "hello world" --tokens

    To convert token integers back to text: Provide a string of integers and use the --decode flag.

    ttok 9906 1917 --decode
    cat input.txt | ttok --encode
    ttok "hello world" --tokens
    ttok 9906 1917 --decode
  8. Specify a tokenizer model

    main

    By default, ttok uses the tokenizer model for GPT-3.5 and GPT-4. You can specify different models using the -m or --model option. For example, to use the GPT-2 model:

    ttok boo Hello there this is -m gpt2
  9. Reference the ttok CLI options and flags

    main

    The following options are available for the ttok command:

    FlagLong FlagTypeDescription
    -i--inputFileRead input from a file
    -t--truncateIntegerTruncate the output to this many tokens
    -m--modelStringWhich model to use (default: gpt-3.5-turbo)
    --encodeFlagOutput token integers
    --tokensFlagOutput full tokens (as bytes/text)
    --decodeFlagConvert token integers back to text
    --allow-specialFlagDo not error on special tokens

    Constraints

    • You cannot use --decode and --encode together.
    • --allow-special requires either --encode or --tokens to be set.
    ttok --help