[Rust Frontend] Integrate xgrammar-structural-tag for strict and required tool calling (#46057)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Signed-off-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
co-authored by
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
6af0559ddb
commit
4ed8eaafb0
@@ -361,6 +361,13 @@ fn convert_tool_choice(tool_choice: Option<&ToolChoice>) -> Result<ChatToolChoic
|
||||
match tool_choice {
|
||||
None | Some(ToolChoice::Value(ToolChoiceValue::Auto)) => Ok(ChatToolChoice::Auto),
|
||||
Some(ToolChoice::Value(ToolChoiceValue::None)) => Ok(ChatToolChoice::None),
|
||||
Some(ToolChoice::Value(ToolChoiceValue::Required)) => Ok(ChatToolChoice::Required),
|
||||
Some(ToolChoice::Function {
|
||||
tool_type,
|
||||
function,
|
||||
}) if tool_type == "function" => Ok(ChatToolChoice::Function {
|
||||
name: function.name.clone(),
|
||||
}),
|
||||
_ => bail_invalid_request!("tool_choice={:?} is not supported yet.", tool_choice),
|
||||
}
|
||||
}
|
||||
@@ -962,6 +969,74 @@ mod tests {
|
||||
assert_eq!(prepared.chat_request.tool_choice, ChatToolChoice::None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prepare_chat_request_lowers_required_tool_choice() {
|
||||
let request = ChatCompletionRequest {
|
||||
tools: Some(vec![Tool {
|
||||
tool_type: "function".to_string(),
|
||||
function: Function {
|
||||
name: "get_weather".to_string(),
|
||||
description: Some("Get weather".to_string()),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {"city": {"type": "string"}},
|
||||
}),
|
||||
strict: None,
|
||||
},
|
||||
}]),
|
||||
tool_choice: Some(ToolChoice::Value(ToolChoiceValue::Required)),
|
||||
..base_request()
|
||||
};
|
||||
|
||||
let prepared = prepare_chat_request(
|
||||
request,
|
||||
&served(&["Qwen/Qwen1.5-0.5B-Chat"]),
|
||||
ResolvedRequestContext::default(),
|
||||
)
|
||||
.expect("request is valid");
|
||||
|
||||
assert_eq!(prepared.chat_request.tool_choice, ChatToolChoice::Required);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prepare_chat_request_lowers_named_function_tool_choice() {
|
||||
let request = ChatCompletionRequest {
|
||||
tools: Some(vec![Tool {
|
||||
tool_type: "function".to_string(),
|
||||
function: Function {
|
||||
name: "get_weather".to_string(),
|
||||
description: Some("Get weather".to_string()),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {"city": {"type": "string"}},
|
||||
}),
|
||||
strict: None,
|
||||
},
|
||||
}]),
|
||||
tool_choice: Some(ToolChoice::Function {
|
||||
tool_type: "function".to_string(),
|
||||
function: crate::routes::openai::utils::types::FunctionChoice {
|
||||
name: "get_weather".to_string(),
|
||||
},
|
||||
}),
|
||||
..base_request()
|
||||
};
|
||||
|
||||
let prepared = prepare_chat_request(
|
||||
request,
|
||||
&served(&["Qwen/Qwen1.5-0.5B-Chat"]),
|
||||
ResolvedRequestContext::default(),
|
||||
)
|
||||
.expect("request is valid");
|
||||
|
||||
assert_eq!(
|
||||
prepared.chat_request.tool_choice,
|
||||
ChatToolChoice::Function {
|
||||
name: "get_weather".to_string(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prepare_chat_request_lowers_logprobs_fields() {
|
||||
let request = ChatCompletionRequest {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::types::ChatCompletionRequest;
|
||||
use crate::error::{ApiError, bail_invalid_request};
|
||||
use crate::routes::openai::utils::types::{ChatMessage, Tool, ToolChoice, ToolChoiceValue};
|
||||
use crate::routes::openai::utils::types::{ChatMessage, Tool};
|
||||
|
||||
/// Enforce the minimal compatibility contract for the Rust OpenAI server.
|
||||
pub(super) fn validate_request_compat(
|
||||
@@ -58,30 +58,6 @@ pub(super) fn validate_request_compat(
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(tool_choice) = &request.tool_choice {
|
||||
match tool_choice {
|
||||
ToolChoice::Value(ToolChoiceValue::Auto | ToolChoiceValue::None) => {}
|
||||
ToolChoice::Value(ToolChoiceValue::Required) => {
|
||||
bail_invalid_request!(
|
||||
param = "tool_choice",
|
||||
"tool_choice=required is not supported yet."
|
||||
);
|
||||
}
|
||||
ToolChoice::Function { .. } => {
|
||||
bail_invalid_request!(
|
||||
param = "tool_choice",
|
||||
"Named function tool_choice is not supported yet."
|
||||
);
|
||||
}
|
||||
ToolChoice::AllowedTools { .. } => {
|
||||
bail_invalid_request!(
|
||||
param = "tool_choice",
|
||||
"allowed_tools tool_choice is not supported yet."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if request.use_beam_search {
|
||||
bail_invalid_request!(
|
||||
param = "use_beam_search",
|
||||
@@ -159,8 +135,7 @@ mod tests {
|
||||
use crate::routes::openai::chat_completions::types::ChatCompletionRequest;
|
||||
use crate::routes::openai::utils::structured_outputs::ResponseFormat;
|
||||
use crate::routes::openai::utils::types::{
|
||||
ChatMessage, Function, FunctionChoice, MessageContent, StringOrArray, Tool, ToolChoice,
|
||||
ToolChoiceValue, ToolReference,
|
||||
ChatMessage, Function, MessageContent, StringOrArray, Tool, ToolChoice, ToolChoiceValue,
|
||||
};
|
||||
|
||||
fn served(names: &[&str]) -> Vec<String> {
|
||||
@@ -357,38 +332,4 @@ mod tests {
|
||||
validate_request_compat(&request, &served(&["Qwen/Qwen1.5-0.5B-Chat"]))
|
||||
.expect("tool_choice=none is ok");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_request_compat_rejects_required_and_named_tool_choices() {
|
||||
let required = ChatCompletionRequest {
|
||||
tool_choice: Some(ToolChoice::Value(ToolChoiceValue::Required)),
|
||||
..base_request()
|
||||
};
|
||||
assert!(validate_request_compat(&required, &served(&["Qwen/Qwen1.5-0.5B-Chat"])).is_err());
|
||||
|
||||
let named = ChatCompletionRequest {
|
||||
tool_choice: Some(ToolChoice::Function {
|
||||
tool_type: "function".to_string(),
|
||||
function: FunctionChoice {
|
||||
name: "tool".to_string(),
|
||||
},
|
||||
}),
|
||||
..base_request()
|
||||
};
|
||||
assert!(validate_request_compat(&named, &served(&["Qwen/Qwen1.5-0.5B-Chat"])).is_err());
|
||||
|
||||
let allowed_tools = ChatCompletionRequest {
|
||||
tool_choice: Some(ToolChoice::AllowedTools {
|
||||
tool_type: "allowed_tools".to_string(),
|
||||
mode: "auto".to_string(),
|
||||
tools: vec![ToolReference::Function {
|
||||
name: "tool".to_string(),
|
||||
}],
|
||||
}),
|
||||
..base_request()
|
||||
};
|
||||
assert!(
|
||||
validate_request_compat(&allowed_tools, &served(&["Qwen/Qwen1.5-0.5B-Chat"])).is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user