Skip to content

Commit d10b887

Browse files
committed
add_special option for server tokenize endpoint
1 parent 92139b9 commit d10b887

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

examples/server/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ Notice that each `probs` is an array of length `n_probs`.
319319

320320
`content`: Set the text to tokenize.
321321

322-
Note that a special `BOS` token is never inserted.
322+
`add_special`: Boolean indicating if special tokens, i.e. `BOS`, should be inserted. Default: `false`
323323

324324
- **POST** `/detokenize`: Convert tokens to text.
325325

examples/server/server.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3632,7 +3632,8 @@ int main(int argc, char ** argv) {
36323632

36333633
std::vector<llama_token> tokens;
36343634
if (body.count("content") != 0) {
3635-
tokens = ctx_server.tokenize(body["content"], false);
3635+
const bool add_special = json_value(body, "add_special", false);
3636+
tokens = ctx_server.tokenize(body["content"], add_special);
36363637
}
36373638
const json data = format_tokenizer_response(tokens);
36383639
return res.set_content(data.dump(), "application/json; charset=utf-8");

examples/server/tests/features/server.feature

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,18 @@ Feature: llama.cpp server
9191
"""
9292
What is the capital of France ?
9393
"""
94-
Then tokens can be detokenize
94+
Then tokens can be detokenized
95+
And tokens do not begin with BOS
96+
97+
Scenario: Tokenize w/ BOS
98+
Given adding special tokens
99+
When tokenizing:
100+
"""
101+
What is the capital of Germany?
102+
"""
103+
Then tokens begin with BOS
104+
Given first token is removed
105+
Then tokens can be detokenized
95106

96107
Scenario: Models available
97108
Given available models

examples/server/tests/features/steps/steps.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,21 +656,29 @@ async def all_embeddings_are_generated(context):
656656
assert_embeddings(context.tasks_result.pop().pop())
657657

658658

659+
@step('adding special tokens')
660+
def step_tokenize_set_add_special(context):
661+
context.tokenize_add_special = True
662+
663+
659664
@step('tokenizing')
660665
@async_run_until_complete
661666
async def step_tokenize(context):
662667
context.tokenized_text = context_text(context)
663668
async with aiohttp.ClientSession() as session:
669+
tokenize_args = {
670+
"content": context.tokenized_text,
671+
}
672+
if getattr(context, 'tokenize_add_special', None) is not None:
673+
tokenize_args['add_special'] = context.tokenize_add_special
664674
async with session.post(f'{context.base_url}/tokenize',
665-
json={
666-
"content": context.tokenized_text,
667-
}) as response:
675+
json=tokenize_args) as response:
668676
assert response.status == 200
669677
tokenize_json = await response.json()
670678
context.tokens = tokenize_json['tokens']
671679

672680

673-
@step('tokens can be detokenize')
681+
@step('tokens can be detokenized')
674682
@async_run_until_complete
675683
async def step_detokenize(context):
676684
assert len(context.tokens) > 0
@@ -685,6 +693,21 @@ async def step_detokenize(context):
685693
assert context.tokenized_text == detokenize_json['content'].strip()
686694

687695

696+
@step('tokens begin with BOS')
697+
def step_strings_for_tokenization(context):
698+
assert context.tokens[0] == context.bos
699+
700+
701+
@step('tokens do not begin with BOS')
702+
def step_strings_for_tokenization(context):
703+
assert context.tokens[0] != context.bos
704+
705+
706+
@step('first token is removed')
707+
def step_strings_for_tokenization(context):
708+
context.tokens = context.tokens[1:]
709+
710+
688711
@step('an OPTIONS request is sent from {origin}')
689712
@async_run_until_complete
690713
async def step_options_request(context, origin):
@@ -1289,4 +1312,6 @@ def server_log(in_stream, out_stream):
12891312
thread_stderr = threading.Thread(target=server_log, args=(context.server_process.stderr, sys.stderr))
12901313
thread_stderr.start()
12911314

1315+
context.bos = 1
1316+
12921317
print(f"server pid={context.server_process.pid}, behave pid={os.getpid()}")

0 commit comments

Comments
 (0)