MCPcopy
hub / github.com/openai/tiktoken / encode_single_token

Method encode_single_token

tiktoken/core.py:245–259  ·  view source on GitHub ↗

Encodes text corresponding to a single token to its token value. NOTE: this will encode all special tokens. Raises `KeyError` if the token is not in the vocabulary. ``` >>> enc.encode_single_token("hello") 31373 ```

(self, text_or_bytes: str | bytes)

Source from the content-addressed store, hash-verified

243 return self._core_bpe.encode_with_unstable(text, allowed_special)
244
245 def encode_single_token(self, text_or_bytes: str | bytes) -> int:
246 """Encodes text corresponding to a single token to its token value.
247
248 NOTE: this will encode all special tokens.
249
250 Raises `KeyError` if the token is not in the vocabulary.
251
252 ```
253 >>> enc.encode_single_token("hello")
254 31373
255 ```
256 """
257 if isinstance(text_or_bytes, str):
258 text_or_bytes = text_or_bytes.encode("utf-8")
259 return self._core_bpe.encode_single_token(text_or_bytes)
260
261 # ====================
262 # Decoding

Callers 4

test_simpleFunction · 0.80
test_special_tokenFunction · 0.80
test_simpleFunction · 0.80

Calls 1

encodeMethod · 0.45

Tested by 4

test_simpleFunction · 0.64
test_special_tokenFunction · 0.64
test_simpleFunction · 0.64