2023-09-20 20:30:31 -06:00
|
|
|
import requests
|
|
|
|
import tiktoken
|
|
|
|
|
|
|
|
from llm_server import opts
|
2023-09-30 19:41:50 -06:00
|
|
|
from llm_server.cluster.cluster_config import cluster_config
|
2023-09-20 20:30:31 -06:00
|
|
|
|
2023-09-27 14:36:49 -06:00
|
|
|
|
2023-09-30 19:41:50 -06:00
|
|
|
def tokenize(prompt: str, backend_url: str) -> int:
|
2023-09-25 22:32:48 -06:00
|
|
|
if not prompt:
|
|
|
|
# The tokenizers have issues when the prompt is None.
|
|
|
|
return 0
|
2023-09-28 03:54:20 -06:00
|
|
|
tokenizer = tiktoken.get_encoding("cl100k_base")
|
2023-09-30 19:41:50 -06:00
|
|
|
token_limit = cluster_config.get_backend(backend_url)['model_config']['max_position_embeddings']
|
2023-09-28 03:54:20 -06:00
|
|
|
|
|
|
|
# First we tokenize it locally to determine if it's worth sending it to the backend.
|
|
|
|
initial_estimate = len(tokenizer.encode(prompt))
|
2023-09-30 19:41:50 -06:00
|
|
|
if initial_estimate <= token_limit + 200:
|
2023-09-28 03:54:20 -06:00
|
|
|
try:
|
2023-09-30 19:41:50 -06:00
|
|
|
r = requests.post(f'{backend_url}/tokenize', json={'input': prompt}, verify=opts.verify_ssl, timeout=opts.backend_generate_request_timeout)
|
2023-09-28 03:54:20 -06:00
|
|
|
j = r.json()
|
|
|
|
return j['length']
|
|
|
|
except Exception as e:
|
|
|
|
print(f'Failed to tokenize using VLLM -', f'{e.__class__.__name__}: {e}')
|
|
|
|
return len(tokenizer.encode(prompt)) + 10
|
|
|
|
else:
|
|
|
|
# If the result was greater than our context size, return the estimate.
|
|
|
|
# We won't be sending it through the backend so it does't need to be accurage.
|
|
|
|
return initial_estimate
|