Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server : add "tokens" output #10853

Merged
merged 5 commits into from
Dec 18, 2024
Merged

server : add "tokens" output #10853

merged 5 commits into from
Dec 18, 2024

Conversation

ggerganov
Copy link
Owner

@ggerganov ggerganov commented Dec 16, 2024

The llama-server now also outputs raw token ids in its responses. This is useful for various applications, including TTS.

curl --request POST --url http://localhost:8033/completion --header "Content-Type: application/json" --data '{"prompt": "Hello, my name is", "n_predict": 8 }' | jq

{
  "index": 0,
  "content": " Aki, and I am the owner",
  "tokens": [
    362,
    6642,
    11,
    323,
    358,
    1079,
    279,
    6372
  ],
  "id_slot": 0,
  "stop": true,
  "model": "gpt-3.5-turbo",
  "tokens_predicted": 8,
  "tokens_evaluated": 5,
...
}

Copy link
Collaborator

@ngxson ngxson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This added tokens field can also increase the response JSON size quite a lot, if the returned text is long. Do you think we should only return it if user explicitly request? We can add a field like "return_tokens" and default it to false (see example of how "timings_per_token" works)

std::string content;

std::string content;
llama_tokens tokens;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can also replace these 2 fields with completion_token_output. Then inside send_partial_response, we can std::move it to res

Copy link
Collaborator

@ngxson ngxson Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P/s: we cannot std::move it, because inside process_token, result is still being used after send_partial_response

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure that the "return_tokens" logic is necessary. The tokens array should be similar in JSON length to the content string, though I am not sure performance wise how much slower it is to serialize an array of integers compared to a string. Anyway, I've added the flag and added tests.

Note that with "stream": true we always return the tokens field in the partial responses (i.e. this is not affected by the "return_tokens" flag).

Copy link
Collaborator

@ngxson ngxson Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm thinking is that this should not degrade the performance of JSON serializing/parsing. But I'm just thinking about the bandwidth, because it seems like in most cases, we're now using double the bandwidth.

For stream, I don't think it's a problem because time to serialize/send/receive/parse is minor compared to the time a token is generated.

But I think for now we can keep it this way. The non-OAI /completion is a playground anw so it's fine to expose everything. The OAI compat /v1/completions that I'm planning to do next will be more prod-ready, thus it won't have these data in the response.

Edit: I didn't notice that you implemented return_tokens, that's good then, let's keep it 👍

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think for now we can keep it this way. The non-OAI /completion is a playground anw so it's fine to expose everything. The OAI compat /v1/completions that I'm planning to do next will be more prod-ready, thus it won't have these data in the response.

Yes, I agree we can keep /v1/completions strongly OAI-compat (i.e. not even have extra fields like tokens) and only have these in the non-OAI endpoints like /completions.

Copy link
Contributor

@isaac-mcfadyen isaac-mcfadyen Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you implemented return_tokens, that's good then, let's keep it 👍

This is great to see, thank you.

I sometimes use the /completions API on a bandwidth-constrained network (Wireguard over a bad WAN connection) so having an option to disable tokens if I don't need them is perfect.

examples/server/tests/unit/test_completion.py Outdated Show resolved Hide resolved
@github-actions github-actions bot added the python python script changes label Dec 17, 2024
@ggerganov ggerganov force-pushed the gg/server-content-tokens branch from 4d7771b to 5bf29af Compare December 18, 2024 08:04
@ggerganov ggerganov merged commit 0e70ba6 into master Dec 18, 2024
56 of 58 checks passed
@ggerganov ggerganov deleted the gg/server-content-tokens branch December 18, 2024 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
examples python python script changes server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants