Add return_token_ids parameter to OpenAI API endpoints (#22587)

Signed-off-by: Yuge Zhang <scottyugochang@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Simon Mo <simon.mo@hey.com>
This commit is contained in:
Yuge Zhang
2025-08-19 09:48:31 -07:00
committed by GitHub
co-authored by Claude Simon Mo
parent 4f510bc2a1
commit 24f4d1a224
5 changed files with 477 additions and 27 deletions
+18 -4
View File
@@ -568,12 +568,17 @@ class OpenAIServingChat(OpenAIServing):
),
logprobs=None,
finish_reason=None)
# return prompt_token_ids at the first chunk ever
chunk = ChatCompletionStreamResponse(
id=request_id,
object=chunk_object_type,
created=created_time,
choices=[choice_data],
model=model_name)
model=model_name,
prompt_token_ids=(res.prompt_token_ids
if request.return_token_ids else
None))
# if continuous usage stats are requested, add it
if include_continuous_usage:
@@ -912,7 +917,9 @@ class OpenAIServingChat(OpenAIServing):
index=i,
delta=delta_message,
logprobs=logprobs,
finish_reason=None)
finish_reason=None,
token_ids=(as_list(output.token_ids)
if request.return_token_ids else None))
# if the model is finished generating
else:
@@ -973,7 +980,9 @@ class OpenAIServingChat(OpenAIServing):
logprobs=logprobs,
finish_reason=output.finish_reason
if not auto_tools_called else "tool_calls",
stop_reason=output.stop_reason)
stop_reason=output.stop_reason,
token_ids=(as_list(output.token_ids)
if request.return_token_ids else None))
finish_reason_sent[i] = True
@@ -1260,7 +1269,10 @@ class OpenAIServingChat(OpenAIServing):
logprobs=logprobs,
finish_reason="tool_calls" if auto_tools_called else
output.finish_reason if output.finish_reason else "stop",
stop_reason=output.stop_reason)
stop_reason=output.stop_reason,
token_ids=(as_list(output.token_ids)
if request.return_token_ids else None),
)
choices.append(choice_data)
@@ -1301,6 +1313,8 @@ class OpenAIServingChat(OpenAIServing):
choices=choices,
usage=usage,
prompt_logprobs=clamp_prompt_logprobs(final_res.prompt_logprobs),
prompt_token_ids=(final_res.prompt_token_ids
if request.return_token_ids else None),
kv_transfer_params=final_res.kv_transfer_params,
)