guidellm.mock_server.handlers.tokenizer
HTTP request handler for vLLM tokenization API endpoints in the mock server.
This module provides the TokenizerHandler class that implements vLLM-compatible tokenization and detokenization endpoints for testing and development purposes. It handles text-to-token conversion, token-to-text reconstruction, request validation, and error responses with proper HTTP status codes and JSON formatting.
TokenizerHandler
HTTP request handler for vLLM tokenization and detokenization endpoints.
Provides mock implementations of vLLM's tokenization API endpoints including /tokenize for converting text to tokens and /detokenize for reconstructing text from token sequences. Handles request validation, error responses, and JSON serialization with proper HTTP status codes.
Example: :: handler = TokenizerHandler(config) response = await handler.tokenize(request) response = await handler.detokenize(request)
Source code in src/guidellm/mock_server/handlers/tokenizer.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
__init__(config)
Initialize the tokenizer handler with configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config | MockServerConfig | Server configuration object containing tokenizer settings | required |
Source code in src/guidellm/mock_server/handlers/tokenizer.py
detokenize(request) async
Convert token IDs back to text via the /detokenize endpoint.
Validates the request payload, extracts token sequences, and returns a JSON response containing the reconstructed text. Handles validation errors and malformed JSON with appropriate HTTP error responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request | Request | Sanic HTTP request containing JSON payload with tokens field | required |
Returns:
| Type | Description |
|---|---|
HTTPResponse | JSON response with reconstructed text, or error response |
Source code in src/guidellm/mock_server/handlers/tokenizer.py
tokenize(request) async
Convert input text to token IDs via the /tokenize endpoint.
Validates the request payload, extracts text content, and returns a JSON response containing the token sequence and count. Handles validation errors and malformed JSON with appropriate HTTP error responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request | Request | Sanic HTTP request containing JSON payload with text field | required |
Returns:
| Type | Description |
|---|---|
HTTPResponse | JSON response with tokens list and count, or error response |