[Rust Frontend] Fix finish reason for named tool choices (#49496)
Signed-off-by: reidliu41 <reid201711@gmail.com>
This commit is contained in:
@@ -131,6 +131,7 @@ async fn collect_chat_completion(
|
|||||||
echo,
|
echo,
|
||||||
return_token_ids,
|
return_token_ids,
|
||||||
return_tokens_as_token_ids,
|
return_tokens_as_token_ids,
|
||||||
|
is_named_tool_choice,
|
||||||
}: ResponseOptions,
|
}: ResponseOptions,
|
||||||
) -> Result<ChatCompletionResponse, ApiError> {
|
) -> Result<ChatCompletionResponse, ApiError> {
|
||||||
let collected = stream.collect_message().await.map_err(|error| {
|
let collected = stream.collect_message().await.map_err(|error| {
|
||||||
@@ -157,7 +158,9 @@ async fn collect_chat_completion(
|
|||||||
// When reasoning is hidden, omit them rather than leaking hidden reasoning
|
// When reasoning is hidden, omit them rather than leaking hidden reasoning
|
||||||
// tokens through per-token metadata.
|
// tokens through per-token metadata.
|
||||||
let include_output_metadata = include_reasoning || reasoning.is_none();
|
let include_output_metadata = include_reasoning || reasoning.is_none();
|
||||||
let finish_reason = chat_finish_reason_to_openai(&finish_reason, saw_tool_calls)?.to_string();
|
let finish_reason =
|
||||||
|
chat_finish_reason_to_openai(&finish_reason, saw_tool_calls && !is_named_tool_choice)?
|
||||||
|
.to_string();
|
||||||
let tool_calls = message
|
let tool_calls = message
|
||||||
.tool_calls()
|
.tool_calls()
|
||||||
.map(|call| ToolCall {
|
.map(|call| ToolCall {
|
||||||
@@ -254,6 +257,7 @@ async fn chat_completion_chunk_stream(
|
|||||||
echo,
|
echo,
|
||||||
return_token_ids,
|
return_token_ids,
|
||||||
return_tokens_as_token_ids,
|
return_tokens_as_token_ids,
|
||||||
|
is_named_tool_choice,
|
||||||
}: ResponseOptions,
|
}: ResponseOptions,
|
||||||
mut y: TryYielder<ChatCompletionStreamResponse, ApiError>,
|
mut y: TryYielder<ChatCompletionStreamResponse, ApiError>,
|
||||||
) -> Result<(), ApiError> {
|
) -> Result<(), ApiError> {
|
||||||
@@ -454,7 +458,7 @@ async fn chat_completion_chunk_stream(
|
|||||||
&response_model,
|
&response_model,
|
||||||
created,
|
created,
|
||||||
finish_reason,
|
finish_reason,
|
||||||
saw_tool_calls,
|
saw_tool_calls && !is_named_tool_choice,
|
||||||
) {
|
) {
|
||||||
Ok(chunk) => yield_chunk!(chunk),
|
Ok(chunk) => yield_chunk!(chunk),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
@@ -787,10 +791,10 @@ fn final_chunk(
|
|||||||
response_model: &str,
|
response_model: &str,
|
||||||
created: u64,
|
created: u64,
|
||||||
finish_reason: FinishReason,
|
finish_reason: FinishReason,
|
||||||
saw_tool_calls: bool,
|
use_tool_calls_finish_reason: bool,
|
||||||
) -> Result<ChatCompletionStreamResponse, ApiError> {
|
) -> Result<ChatCompletionStreamResponse, ApiError> {
|
||||||
let stop_reason = finish_reason.as_stop_reason().map(stop_reason_to_json);
|
let stop_reason = finish_reason.as_stop_reason().map(stop_reason_to_json);
|
||||||
let finish_reason = chat_finish_reason_to_openai(&finish_reason, saw_tool_calls)?;
|
let finish_reason = chat_finish_reason_to_openai(&finish_reason, use_tool_calls_finish_reason)?;
|
||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
finish_reason = %finish_reason,
|
finish_reason = %finish_reason,
|
||||||
@@ -809,10 +813,10 @@ fn final_chunk(
|
|||||||
|
|
||||||
fn chat_finish_reason_to_openai(
|
fn chat_finish_reason_to_openai(
|
||||||
finish_reason: &FinishReason,
|
finish_reason: &FinishReason,
|
||||||
saw_tool_calls: bool,
|
use_tool_calls_finish_reason: bool,
|
||||||
) -> Result<&'static str, ApiError> {
|
) -> Result<&'static str, ApiError> {
|
||||||
match finish_reason {
|
match finish_reason {
|
||||||
FinishReason::Stop(_) if saw_tool_calls => Ok("tool_calls"),
|
FinishReason::Stop(_) if use_tool_calls_finish_reason => Ok("tool_calls"),
|
||||||
FinishReason::Stop(_) => Ok("stop"),
|
FinishReason::Stop(_) => Ok("stop"),
|
||||||
FinishReason::Length => Ok("length"),
|
FinishReason::Length => Ok("length"),
|
||||||
FinishReason::Abort => Ok("abort"),
|
FinishReason::Abort => Ok("abort"),
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ pub(super) struct ResponseOptions {
|
|||||||
pub return_token_ids: bool,
|
pub return_token_ids: bool,
|
||||||
/// Whether to format logprob tokens as `token_id:{id}`.
|
/// Whether to format logprob tokens as `token_id:{id}`.
|
||||||
pub return_tokens_as_token_ids: bool,
|
pub return_tokens_as_token_ids: bool,
|
||||||
|
/// Whether the request forces one named function tool.
|
||||||
|
pub is_named_tool_choice: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Validate and lower one OpenAI chat completion request into the internal chat
|
/// Validate and lower one OpenAI chat completion request into the internal chat
|
||||||
@@ -98,6 +100,7 @@ pub(super) fn prepare_chat_request(
|
|||||||
.and_then(|options| options.continuous_usage_stats)
|
.and_then(|options| options.continuous_usage_stats)
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
let requested_logprobs = request.logprobs;
|
let requested_logprobs = request.logprobs;
|
||||||
|
let is_named_tool_choice = matches!(&request.tool_choice, Some(ToolChoice::Function { .. }));
|
||||||
|
|
||||||
// Auto-enable prompt logprobs for non-streaming echo, matching Python vLLM's
|
// Auto-enable prompt logprobs for non-streaming echo, matching Python vLLM's
|
||||||
// behavior.
|
// behavior.
|
||||||
@@ -180,6 +183,7 @@ pub(super) fn prepare_chat_request(
|
|||||||
echo,
|
echo,
|
||||||
return_token_ids: request.return_token_ids.unwrap_or(false),
|
return_token_ids: request.return_token_ids.unwrap_or(false),
|
||||||
return_tokens_as_token_ids: request.return_tokens_as_token_ids.unwrap_or(false),
|
return_tokens_as_token_ids: request.return_tokens_as_token_ids.unwrap_or(false),
|
||||||
|
is_named_tool_choice,
|
||||||
},
|
},
|
||||||
chat_request,
|
chat_request,
|
||||||
})
|
})
|
||||||
@@ -1068,6 +1072,7 @@ mod tests {
|
|||||||
.expect("request is valid");
|
.expect("request is valid");
|
||||||
|
|
||||||
assert_eq!(prepared.chat_request.tool_choice, ChatToolChoice::Required);
|
assert_eq!(prepared.chat_request.tool_choice, ChatToolChoice::Required);
|
||||||
|
assert!(!prepared.options.is_named_tool_choice);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -1107,6 +1112,7 @@ mod tests {
|
|||||||
name: "get_weather".to_string(),
|
name: "get_weather".to_string(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
assert!(prepared.options.is_named_tool_choice);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -153,6 +153,20 @@ fn default_stream_output_specs() -> Vec<(Vec<u32>, Option<EngineCoreFinishReason
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn weather_tool_call_output_specs() -> Vec<(Vec<u32>, Option<EngineCoreFinishReason>)> {
|
||||||
|
vec![
|
||||||
|
(bytes_to_token_ids(b"<think>Need tool.</think>"), None),
|
||||||
|
(
|
||||||
|
bytes_to_token_ids(b"<tool_call>\n{\"name\":\"get_weather\", "),
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
bytes_to_token_ids(b"\"arguments\":{\"city\":\"Paris\"}}\n</tool_call>"),
|
||||||
|
Some(EngineCoreFinishReason::Stop),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
fn assert_adapter_a_lora_request(request: &EngineCoreRequest) {
|
fn assert_adapter_a_lora_request(request: &EngineCoreRequest) {
|
||||||
let lora = request.lora_request.as_ref().expect("lora request");
|
let lora = request.lora_request.as_ref().expect("lora request");
|
||||||
assert_eq!(lora.lora_name, "adapter-a");
|
assert_eq!(lora.lora_name, "adapter-a");
|
||||||
@@ -4554,17 +4568,7 @@ async fn include_reasoning_false_suppresses_non_stream_output_metadata() {
|
|||||||
async fn tool_calls_are_mapped_to_tool_call_sse_chunks() {
|
async fn tool_calls_are_mapped_to_tool_call_sse_chunks() {
|
||||||
let (app, engine_task) = test_app_with_backend_and_stream_output_specs(
|
let (app, engine_task) = test_app_with_backend_and_stream_output_specs(
|
||||||
Arc::new(FakeChatBackend::with_model_id("Qwen/Qwen3-0.6B")),
|
Arc::new(FakeChatBackend::with_model_id("Qwen/Qwen3-0.6B")),
|
||||||
vec![
|
weather_tool_call_output_specs(),
|
||||||
(bytes_to_token_ids(b"<think>Need tool.</think>"), None),
|
|
||||||
(
|
|
||||||
bytes_to_token_ids(b"<tool_call>\n{\"name\":\"get_weather\", "),
|
|
||||||
None,
|
|
||||||
),
|
|
||||||
(
|
|
||||||
bytes_to_token_ids(b"\"arguments\":{\"city\":\"Paris\"}}\n</tool_call>"),
|
|
||||||
Some(EngineCoreFinishReason::Stop),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
@@ -4613,6 +4617,63 @@ async fn tool_calls_are_mapped_to_tool_call_sse_chunks() {
|
|||||||
assert!(text.contains("\"finish_reason\":\"tool_calls\""), "{text}");
|
assert!(text.contains("\"finish_reason\":\"tool_calls\""), "{text}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
#[serial]
|
||||||
|
async fn named_tool_choice_uses_stop_finish_reason() {
|
||||||
|
for stream in [false, true] {
|
||||||
|
let (app, engine_task) = test_app_with_backend_and_stream_output_specs(
|
||||||
|
Arc::new(FakeChatBackend::with_model_id("Qwen/Qwen3-0.6B")),
|
||||||
|
weather_tool_call_output_specs(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let response = app
|
||||||
|
.clone()
|
||||||
|
.call(
|
||||||
|
Request::builder()
|
||||||
|
.method("POST")
|
||||||
|
.uri("/v1/chat/completions")
|
||||||
|
.header("content-type", "application/json")
|
||||||
|
.body(Body::from(
|
||||||
|
json!({
|
||||||
|
"model": "Qwen/Qwen1.5-0.5B-Chat",
|
||||||
|
"stream": stream,
|
||||||
|
"messages": [{"role": "user", "content": "hello"}],
|
||||||
|
"tools": [{
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "get_weather",
|
||||||
|
"description": "Get weather",
|
||||||
|
"parameters": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {"city": {"type": "string"}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
"tool_choice": {
|
||||||
|
"type": "function",
|
||||||
|
"function": {"name": "get_weather"}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.to_string(),
|
||||||
|
))
|
||||||
|
.expect("build request"),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("call app");
|
||||||
|
|
||||||
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
|
let body = to_bytes(response.into_body(), usize::MAX).await.expect("read body");
|
||||||
|
engine_task.await.expect("mock engine task");
|
||||||
|
let text = String::from_utf8(body.to_vec()).expect("utf8 body");
|
||||||
|
|
||||||
|
assert!(text.contains("\"tool_calls\":"), "{text}");
|
||||||
|
assert!(text.contains("\"name\":\"get_weather\""), "{text}");
|
||||||
|
assert!(text.contains("\"finish_reason\":\"stop\""), "{text}");
|
||||||
|
assert!(!text.contains("\"finish_reason\":\"tool_calls\""), "{text}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
#[serial]
|
#[serial]
|
||||||
async fn tool_call_sse_chunks_can_carry_logprobs() {
|
async fn tool_call_sse_chunks_can_carry_logprobs() {
|
||||||
|
|||||||
Reference in New Issue
Block a user