[Hy4] support Hy4-preview model (#54160)
Signed-off-by: chengvjiang <chengvjiang@tencent.com> Signed-off-by: Jee Jee Li <jeejeelee@inferact.ai> Signed-off-by: Bugen Zhao <i@bugenzhao.com> Signed-off-by: Jee Jee Li <pandaleefree@gmail.com> Co-authored-by: chengvjiang <chengvjiang@tencent.com> Co-authored-by: Jee Jee Li <jeejeelee@inferact.ai> Co-authored-by: Bugen Zhao <i@bugenzhao.com> Co-authored-by: russellfeng <russellfeng@tencent.com> Co-authored-by: Jee Jee Li <pandaleefree@gmail.com>
This commit is contained in:
co-authored by
chengvjiang
Jee Jee Li
Bugen Zhao
russellfeng
Jee Jee Li
parent
6c18a54648
commit
b2f685834a
@@ -5,13 +5,13 @@ use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use super::{DelimitedReasoningParser, ReasoningDelta, ReasoningError, ReasoningParser, Result};
|
||||
|
||||
/// Internal HY3 reasoning stage used by the unified HY3 parser.
|
||||
pub(crate) struct HyV3ReasoningParser {
|
||||
/// Internal HY reasoning stage used by the unified HY parsers.
|
||||
pub(crate) struct HyReasoningParser {
|
||||
inner: DelimitedReasoningParser,
|
||||
}
|
||||
|
||||
impl HyV3ReasoningParser {
|
||||
/// Create a HY3 reasoning parser for the tokenizer-specific marker suffix.
|
||||
impl HyReasoningParser {
|
||||
/// Create a HY reasoning parser for the tokenizer-specific marker suffix.
|
||||
pub(crate) fn new(tokenizer: DynTokenizer, suffix: &str) -> Result<Self> {
|
||||
Ok(Self {
|
||||
inner: DelimitedReasoningParser::new(
|
||||
@@ -24,15 +24,15 @@ impl HyV3ReasoningParser {
|
||||
}
|
||||
}
|
||||
|
||||
impl ReasoningParser for HyV3ReasoningParser {
|
||||
// Suffix discovery belongs to `HyV3UnifiedParser` so its reasoning and
|
||||
impl ReasoningParser for HyReasoningParser {
|
||||
// Suffix discovery belongs to the unified HY parser so its reasoning and
|
||||
// tool delimiters always use the same tokenizer-derived value.
|
||||
fn create(_tokenizer: DynTokenizer) -> Result<Box<dyn ReasoningParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Err(ReasoningError::DummyUnifiedParser {
|
||||
name: "hy_v3".to_string(),
|
||||
name: "hy".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
mod cohere_cmd;
|
||||
mod deepseek_r1;
|
||||
mod delimited;
|
||||
mod hy_v3;
|
||||
mod hy;
|
||||
mod kimi;
|
||||
mod minimax_m3;
|
||||
mod qwen3;
|
||||
@@ -33,7 +33,7 @@ use vllm_tokenizer::DynTokenizer;
|
||||
pub use self::cohere_cmd::CohereCmdReasoningParser;
|
||||
pub use self::deepseek_r1::DeepSeekR1ReasoningParser;
|
||||
pub(crate) use self::delimited::{DelimitedReasoningParser, last_reasoning_boundary};
|
||||
pub(crate) use self::hy_v3::HyV3ReasoningParser;
|
||||
pub(crate) use self::hy::HyReasoningParser;
|
||||
pub use self::kimi::KimiReasoningParser;
|
||||
pub use self::minimax_m3::MiniMaxM3ReasoningParser;
|
||||
pub use self::qwen3::Qwen3ReasoningParser;
|
||||
|
||||
@@ -11,33 +11,56 @@ use winnow::prelude::*;
|
||||
use winnow::stream::Partial;
|
||||
use winnow::token::{literal, rest, take_until};
|
||||
|
||||
use self::structural_tag::HyV3StructuralTagBuilder;
|
||||
use self::structural_tag::HyStructuralTagBuilder;
|
||||
use super::parameters::ToolSchemas;
|
||||
use super::utils::{MarkerScanState, parse_buffered_event, safe_text_len, take_until_marker};
|
||||
use super::{Result, ToolCallDelta, ToolParser, ToolParserError, ToolParserOutput};
|
||||
use crate::tool::{StructuralTagBuilder, Tool};
|
||||
|
||||
/// Wire-level HY tool-call dialect.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum HyDialect {
|
||||
V3,
|
||||
V4,
|
||||
}
|
||||
|
||||
impl HyDialect {
|
||||
fn separator(self) -> &'static str {
|
||||
match self {
|
||||
Self::V3 => "\n",
|
||||
Self::V4 => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::V3 => "HY3",
|
||||
Self::V4 => "HY4",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct HyV3ToolMarkers {
|
||||
pub(crate) struct HyToolMarkers {
|
||||
tool_calls_start: String,
|
||||
tool_calls_end: String,
|
||||
tool_call_start: String,
|
||||
tool_call_end: String,
|
||||
tool_sep: String,
|
||||
tool_sep: Option<String>,
|
||||
arg_key_start: String,
|
||||
arg_key_end: String,
|
||||
arg_value_start: String,
|
||||
arg_value_end: String,
|
||||
}
|
||||
|
||||
impl HyV3ToolMarkers {
|
||||
pub(crate) fn new(suffix: &str) -> Self {
|
||||
impl HyToolMarkers {
|
||||
pub(crate) fn new(suffix: &str, dialect: HyDialect) -> Self {
|
||||
Self {
|
||||
tool_calls_start: format!("<tool_calls{suffix}>"),
|
||||
tool_calls_end: format!("</tool_calls{suffix}>"),
|
||||
tool_call_start: format!("<tool_call{suffix}>"),
|
||||
tool_call_end: format!("</tool_call{suffix}>"),
|
||||
tool_sep: format!("<tool_sep{suffix}>"),
|
||||
tool_sep: (dialect == HyDialect::V3).then(|| format!("<tool_sep{suffix}>")),
|
||||
arg_key_start: format!("<arg_key{suffix}>"),
|
||||
arg_key_end: format!("</arg_key{suffix}>"),
|
||||
arg_value_start: format!("<arg_value{suffix}>"),
|
||||
@@ -47,31 +70,32 @@ impl HyV3ToolMarkers {
|
||||
|
||||
pub(crate) fn iter(&self) -> impl Iterator<Item = &str> {
|
||||
[
|
||||
self.tool_calls_start.as_str(),
|
||||
self.tool_calls_end.as_str(),
|
||||
self.tool_call_start.as_str(),
|
||||
self.tool_call_end.as_str(),
|
||||
self.tool_sep.as_str(),
|
||||
self.arg_key_start.as_str(),
|
||||
self.arg_key_end.as_str(),
|
||||
self.arg_value_start.as_str(),
|
||||
self.arg_value_end.as_str(),
|
||||
Some(self.tool_calls_start.as_str()),
|
||||
Some(self.tool_calls_end.as_str()),
|
||||
Some(self.tool_call_start.as_str()),
|
||||
Some(self.tool_call_end.as_str()),
|
||||
self.tool_sep.as_deref(),
|
||||
Some(self.arg_key_start.as_str()),
|
||||
Some(self.arg_key_end.as_str()),
|
||||
Some(self.arg_value_start.as_str()),
|
||||
Some(self.arg_value_end.as_str()),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
}
|
||||
}
|
||||
|
||||
type HyV3Input<'i> = Partial<&'i str>;
|
||||
type HyInput<'i> = Partial<&'i str>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum HyV3Mode {
|
||||
enum HyMode {
|
||||
Text,
|
||||
ToolBlock { tool_call_end_scan: MarkerScanState },
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum HyV3Event {
|
||||
enum HyEvent {
|
||||
Text {
|
||||
len: usize,
|
||||
},
|
||||
@@ -84,7 +108,7 @@ enum HyV3Event {
|
||||
IgnoredRest,
|
||||
}
|
||||
|
||||
/// Tool parser for HY3 XML-style tool calls.
|
||||
/// Tool parser for HY XML-style tool calls.
|
||||
///
|
||||
/// Example tool call content:
|
||||
///
|
||||
@@ -97,43 +121,45 @@ enum HyV3Event {
|
||||
/// ```
|
||||
///
|
||||
/// Arguments are emitted only after a full `<tool_call>` block is parsed.
|
||||
/// HY3 marker tokens are added-vocabulary tokens rather than tokenizer special
|
||||
/// HY marker tokens are added-vocabulary tokens rather than tokenizer special
|
||||
/// tokens, so the default `preserve_special_tokens() == false` is sufficient.
|
||||
pub(crate) struct HyV3ToolParser {
|
||||
pub(crate) struct HyToolParser {
|
||||
buffer: String,
|
||||
mode: HyV3Mode,
|
||||
mode: HyMode,
|
||||
emitted_tool_count: usize,
|
||||
tool_parameters: ToolSchemas,
|
||||
markers: Arc<HyV3ToolMarkers>,
|
||||
structural_tag_builder: HyV3StructuralTagBuilder,
|
||||
dialect: HyDialect,
|
||||
markers: Arc<HyToolMarkers>,
|
||||
structural_tag_builder: HyStructuralTagBuilder,
|
||||
}
|
||||
|
||||
impl HyV3ToolParser {
|
||||
/// Create a HY3 tool parser.
|
||||
pub(crate) fn new(tools: &[Tool], suffix: &str) -> Self {
|
||||
let markers = Arc::new(HyV3ToolMarkers::new(suffix));
|
||||
impl HyToolParser {
|
||||
/// Create a HY tool parser for one wire dialect.
|
||||
pub(crate) fn new(tools: &[Tool], suffix: &str, dialect: HyDialect) -> Self {
|
||||
let markers = Arc::new(HyToolMarkers::new(suffix, dialect));
|
||||
Self {
|
||||
buffer: String::new(),
|
||||
mode: HyV3Mode::Text,
|
||||
mode: HyMode::Text,
|
||||
emitted_tool_count: 0,
|
||||
tool_parameters: ToolSchemas::from_tools(tools),
|
||||
dialect,
|
||||
markers: Arc::clone(&markers),
|
||||
structural_tag_builder: HyV3StructuralTagBuilder::new(markers),
|
||||
structural_tag_builder: HyStructuralTagBuilder::new(markers, dialect),
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply one parsed HY3 event to parser state and output.
|
||||
fn apply_event(&mut self, event: HyV3Event, output: &mut ToolParserOutput) -> Result<()> {
|
||||
/// Apply one parsed HY event to parser state and output.
|
||||
fn apply_event(&mut self, event: HyEvent, output: &mut ToolParserOutput) -> Result<()> {
|
||||
match event {
|
||||
HyV3Event::Text { len: consumed_len } => {
|
||||
HyEvent::Text { len: consumed_len } => {
|
||||
output.push_text(&self.buffer[..consumed_len]);
|
||||
}
|
||||
HyV3Event::ToolBlockStart => {
|
||||
self.mode = HyV3Mode::ToolBlock {
|
||||
HyEvent::ToolBlockStart => {
|
||||
self.mode = HyMode::ToolBlock {
|
||||
tool_call_end_scan: MarkerScanState::default(),
|
||||
};
|
||||
}
|
||||
HyV3Event::ToolCall { name, raw_params } => {
|
||||
HyEvent::ToolCall { name, raw_params } => {
|
||||
let arguments = self.tool_parameters.convert_params_with_schema(&name, raw_params);
|
||||
let arguments = serde_json::to_string(&arguments)
|
||||
.map_err(|error| parsing_failed!("failed to serialize arguments: {}", error))?;
|
||||
@@ -145,22 +171,22 @@ impl HyV3ToolParser {
|
||||
});
|
||||
self.emitted_tool_count += 1;
|
||||
}
|
||||
HyV3Event::ToolBlockEnd => self.mode = HyV3Mode::Done,
|
||||
HyV3Event::IgnoredRest => {}
|
||||
HyEvent::ToolBlockEnd => self.mode = HyMode::Done,
|
||||
HyEvent::IgnoredRest => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToolParser for HyV3ToolParser {
|
||||
// Suffix discovery belongs to `HyV3UnifiedParser` so its reasoning and
|
||||
impl ToolParser for HyToolParser {
|
||||
// Suffix discovery belongs to the unified HY parser so its reasoning and
|
||||
// tool delimiters always use the same tokenizer-derived value.
|
||||
fn create(_tools: &[Tool]) -> Result<Box<dyn ToolParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Err(ToolParserError::DummyUnifiedParser {
|
||||
name: "hy_v3".to_string(),
|
||||
name: "hy".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -172,7 +198,7 @@ impl ToolParser for HyV3ToolParser {
|
||||
self.buffer.push_str(chunk);
|
||||
|
||||
while let Some((event, consumed_len)) = parse_buffered_event(&self.buffer, |input| {
|
||||
parse_next_hy_v3_event(input, &mut self.mode, &self.markers)
|
||||
parse_next_hy_event(input, &mut self.mode, &self.markers, self.dialect)
|
||||
})? {
|
||||
self.apply_event(event, output)?;
|
||||
self.buffer.drain(..consumed_len);
|
||||
@@ -184,113 +210,145 @@ impl ToolParser for HyV3ToolParser {
|
||||
fn finish(&mut self) -> Result<ToolParserOutput> {
|
||||
let mut output = ToolParserOutput::default();
|
||||
match self.mode {
|
||||
HyV3Mode::Text => output.push_text(&self.buffer),
|
||||
HyV3Mode::ToolBlock { .. } => return Err(parsing_failed!("incomplete HY3 tool call")),
|
||||
HyV3Mode::Done => {}
|
||||
HyMode::Text => output.push_text(&self.buffer),
|
||||
HyMode::ToolBlock { .. } => {
|
||||
return Err(parsing_failed!(
|
||||
"incomplete {} tool call",
|
||||
self.dialect.label()
|
||||
));
|
||||
}
|
||||
HyMode::Done => {}
|
||||
}
|
||||
let _ = self.reset();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.mode = HyV3Mode::Text;
|
||||
self.mode = HyMode::Text;
|
||||
self.emitted_tool_count = 0;
|
||||
std::mem::take(&mut self.buffer)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a HY3 event for the current parser mode.
|
||||
fn parse_next_hy_v3_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
mode: &mut HyV3Mode,
|
||||
markers: &HyV3ToolMarkers,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
/// Parse a HY event for the current parser mode.
|
||||
fn parse_next_hy_event(
|
||||
input: &mut HyInput<'_>,
|
||||
mode: &mut HyMode,
|
||||
markers: &HyToolMarkers,
|
||||
dialect: HyDialect,
|
||||
) -> ModalResult<HyEvent> {
|
||||
match mode {
|
||||
HyV3Mode::Text => parse_text_event(input, markers),
|
||||
HyV3Mode::ToolBlock { tool_call_end_scan } => {
|
||||
parse_tool_block_event(input, tool_call_end_scan, markers)
|
||||
HyMode::Text => parse_text_event(input, markers),
|
||||
HyMode::ToolBlock { tool_call_end_scan } => {
|
||||
parse_tool_block_event(input, tool_call_end_scan, markers, dialect)
|
||||
}
|
||||
HyV3Mode::Done => ignored_rest_event(input),
|
||||
HyMode::Done => ignored_rest_event(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a text-mode HY3 event.
|
||||
fn parse_text_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
markers: &HyV3ToolMarkers,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
/// Parse a text-mode HY event.
|
||||
fn parse_text_event(input: &mut HyInput<'_>, markers: &HyToolMarkers) -> ModalResult<HyEvent> {
|
||||
alt((
|
||||
|input: &mut HyV3Input<'_>| tool_block_start_event(input, markers),
|
||||
|input: &mut HyV3Input<'_>| safe_text_event(input, markers),
|
||||
|input: &mut HyInput<'_>| tool_block_start_event(input, markers),
|
||||
|input: &mut HyInput<'_>| safe_text_event(input, markers),
|
||||
))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a HY3 tool-block start marker.
|
||||
/// Parse a HY tool-block start marker.
|
||||
fn tool_block_start_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
markers: &HyV3ToolMarkers,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
input: &mut HyInput<'_>,
|
||||
markers: &HyToolMarkers,
|
||||
) -> ModalResult<HyEvent> {
|
||||
literal(markers.tool_calls_start.as_str())
|
||||
.value(HyV3Event::ToolBlockStart)
|
||||
.value(HyEvent::ToolBlockStart)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a safe text run before the next HY3 marker.
|
||||
fn safe_text_event(input: &mut HyV3Input<'_>, markers: &HyV3ToolMarkers) -> ModalResult<HyV3Event> {
|
||||
safe_text_len(input, &markers.tool_calls_start).map(|len| HyV3Event::Text { len })
|
||||
/// Parse a safe text run before the next HY marker.
|
||||
fn safe_text_event(input: &mut HyInput<'_>, markers: &HyToolMarkers) -> ModalResult<HyEvent> {
|
||||
safe_text_len(input, &markers.tool_calls_start).map(|len| HyEvent::Text { len })
|
||||
}
|
||||
|
||||
/// Parse one event inside a HY3 tool block.
|
||||
/// Parse one event inside a HY tool block.
|
||||
fn parse_tool_block_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
input: &mut HyInput<'_>,
|
||||
tool_call_end_scan: &mut MarkerScanState,
|
||||
markers: &HyV3ToolMarkers,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
markers: &HyToolMarkers,
|
||||
dialect: HyDialect,
|
||||
) -> ModalResult<HyEvent> {
|
||||
alt((
|
||||
|input: &mut HyV3Input<'_>| tool_block_end_event(input, markers),
|
||||
|input: &mut HyV3Input<'_>| tool_call_event(input, tool_call_end_scan, markers),
|
||||
|input: &mut HyInput<'_>| tool_block_end_event(input, markers),
|
||||
|input: &mut HyInput<'_>| tool_call_event(input, tool_call_end_scan, markers, dialect),
|
||||
))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a HY3 tool-block end marker.
|
||||
fn tool_block_end_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
markers: &HyV3ToolMarkers,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
/// Parse a HY tool-block end marker.
|
||||
fn tool_block_end_event(input: &mut HyInput<'_>, markers: &HyToolMarkers) -> ModalResult<HyEvent> {
|
||||
(ws0, literal(markers.tool_calls_end.as_str()))
|
||||
.value(HyV3Event::ToolBlockEnd)
|
||||
.value(HyEvent::ToolBlockEnd)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
/// Parse a complete HY3 tool-call block.
|
||||
/// Parse a complete HY tool-call block.
|
||||
fn tool_call_event(
|
||||
input: &mut HyV3Input<'_>,
|
||||
input: &mut HyInput<'_>,
|
||||
tool_call_end_scan: &mut MarkerScanState,
|
||||
markers: &HyV3ToolMarkers,
|
||||
) -> ModalResult<HyV3Event> {
|
||||
let (name, body) = seq!(
|
||||
markers: &HyToolMarkers,
|
||||
dialect: HyDialect,
|
||||
) -> ModalResult<HyEvent> {
|
||||
let (body,) = seq!(
|
||||
_: ws0,
|
||||
_: literal(markers.tool_call_start.as_str()),
|
||||
take_until(0.., markers.tool_sep.as_str()),
|
||||
_: literal(markers.tool_sep.as_str()),
|
||||
take_until_marker(markers.tool_call_end.as_str(), tool_call_end_scan),
|
||||
_: literal(markers.tool_call_end.as_str()),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
let raw_params = parse_tool_call_params(body, markers)?;
|
||||
let mut body_input = body;
|
||||
let (name, params) = parse_tool_call_body(&mut body_input, markers, dialect)?;
|
||||
let raw_params = parse_tool_call_params(params, markers)?;
|
||||
|
||||
Ok(HyV3Event::ToolCall {
|
||||
Ok(HyEvent::ToolCall {
|
||||
name: name.trim().to_string(),
|
||||
raw_params,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse all parameter blocks inside a complete HY3 tool call.
|
||||
/// Parse a complete HY tool-call body according to its dialect.
|
||||
fn parse_tool_call_body<'i>(
|
||||
input: &mut &'i str,
|
||||
markers: &HyToolMarkers,
|
||||
dialect: HyDialect,
|
||||
) -> ModalResult<(&'i str, &'i str)> {
|
||||
match dialect {
|
||||
HyDialect::V3 => {
|
||||
let tool_sep = markers.tool_sep.as_deref().expect("HY3 has a tool separator");
|
||||
terminated(
|
||||
seq!(
|
||||
take_until(0.., tool_sep),
|
||||
_: literal(tool_sep),
|
||||
rest,
|
||||
),
|
||||
eof,
|
||||
)
|
||||
.parse_next(input)
|
||||
}
|
||||
HyDialect::V4 => terminated(
|
||||
alt((
|
||||
seq!(take_until(0.., markers.arg_key_start.as_str()), rest,),
|
||||
rest.map(|name| (name, "")),
|
||||
)),
|
||||
eof,
|
||||
)
|
||||
.parse_next(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse all parameter blocks inside a complete HY tool call.
|
||||
fn parse_tool_call_params(
|
||||
tool_call_body: &str,
|
||||
markers: &HyV3ToolMarkers,
|
||||
markers: &HyToolMarkers,
|
||||
) -> ModalResult<Vec<(String, String)>> {
|
||||
let mut input = tool_call_body;
|
||||
delimited(
|
||||
@@ -304,8 +362,8 @@ fn parse_tool_call_params(
|
||||
.parse_next(&mut input)
|
||||
}
|
||||
|
||||
/// Parse a HY3 argument key/value block.
|
||||
fn parameter(input: &mut &str, markers: &HyV3ToolMarkers) -> ModalResult<(String, String)> {
|
||||
/// Parse a HY argument key/value block.
|
||||
fn parameter(input: &mut &str, markers: &HyToolMarkers) -> ModalResult<(String, String)> {
|
||||
let (name, value) = seq!(
|
||||
_: literal(markers.arg_key_start.as_str()),
|
||||
take_until(0.., markers.arg_key_end.as_str()),
|
||||
@@ -320,9 +378,9 @@ fn parameter(input: &mut &str, markers: &HyV3ToolMarkers) -> ModalResult<(String
|
||||
Ok((name.trim().to_string(), value.to_string()))
|
||||
}
|
||||
|
||||
/// Parse ignored rest after the HY3 tool block ends.
|
||||
fn ignored_rest_event(input: &mut HyV3Input<'_>) -> ModalResult<HyV3Event> {
|
||||
rest.value(HyV3Event::IgnoredRest).parse_next(input)
|
||||
/// Parse ignored rest after the HY tool block ends.
|
||||
fn ignored_rest_event(input: &mut HyInput<'_>) -> ModalResult<HyEvent> {
|
||||
rest.value(HyEvent::IgnoredRest).parse_next(input)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -331,7 +389,7 @@ mod tests {
|
||||
use serde_json::{Value, json};
|
||||
use thiserror_ext::AsReport;
|
||||
|
||||
use super::{HyV3ToolParser, ToolParser};
|
||||
use super::{HyDialect, HyToolParser, ToolParser};
|
||||
use crate::tool::test_utils::{collect_stream, split_by_chars, test_tools};
|
||||
use crate::tool::{ToolParserOutput, ToolParserTestExt as _};
|
||||
|
||||
@@ -354,14 +412,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_does_not_preserve_special_tokens() {
|
||||
let parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
|
||||
assert!(!parser.preserve_special_tokens());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_without_tool_call_keeps_text() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let output = parser.parse_complete("This is a plain response.").unwrap();
|
||||
|
||||
assert_eq!(output.normal_text(), "This is a plain response.");
|
||||
@@ -370,7 +428,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_extracts_zero_arg_inline_tool_call() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
"<tool_calls><tool_call>get_current_date<tool_sep></tool_call></tool_calls>",
|
||||
@@ -385,7 +443,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_extracts_zero_arg_newline_tool_call() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
"<tool_calls>\n<tool_call>get_current_date<tool_sep>\n</tool_call>\n</tool_calls>",
|
||||
@@ -398,7 +456,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_extracts_arguments_on_same_line() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
"<tool_calls><tool_call>get_weather<tool_sep><arg_key>city</arg_key><arg_value>Beijing</arg_value><arg_key>date</arg_key><arg_value>2026-03-30</arg_value></tool_call></tool_calls>",
|
||||
@@ -413,7 +471,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_extracts_arguments_with_newlines() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_calls(&[build_tool_call(
|
||||
"get_weather",
|
||||
@@ -429,7 +487,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_preserves_prefix_and_ignores_trailing_text() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let output = parser
|
||||
.parse_complete(&format!(
|
||||
"Checking.{} trailing text",
|
||||
@@ -443,7 +501,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_extracts_multiple_tool_calls_in_one_block() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_calls(&[
|
||||
build_tool_call(
|
||||
@@ -486,7 +544,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_parse_complete_converts_schema_types() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let output = parser
|
||||
.parse_complete(&build_tool_calls(&[build_tool_call(
|
||||
"convert",
|
||||
@@ -512,7 +570,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_without_tool_call_emits_text_incrementally() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let mut output = ToolParserOutput::default();
|
||||
|
||||
output.append(parser.parse_chunk("This is ").unwrap());
|
||||
@@ -526,7 +584,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_extracts_zero_arg_tool_call() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let chunks = [
|
||||
"<tool_calls>",
|
||||
"\n<tool_call>",
|
||||
@@ -545,7 +603,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_extracts_arguments() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let chunks = [
|
||||
"<tool_calls>",
|
||||
"\n<tool_call>",
|
||||
@@ -571,7 +629,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_preserves_prefix_text() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let chunks = [
|
||||
"Checking.",
|
||||
"<tool_calls>",
|
||||
@@ -601,7 +659,7 @@ mod tests {
|
||||
),
|
||||
]);
|
||||
let chunks = split_by_chars(&input, 9);
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
@@ -617,7 +675,7 @@ mod tests {
|
||||
build_tool_calls(&[build_tool_call("get_weather", &[("city", "Beijing")])])
|
||||
);
|
||||
let chunks = split_by_chars(&input, 5);
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
@@ -628,7 +686,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_streaming_does_not_emit_incomplete_tool_call() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let mut output = ToolParserOutput::default();
|
||||
|
||||
parser
|
||||
@@ -644,7 +702,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_finish_fails_incomplete_tool_call() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
parser.parse_chunk("<tool_calls><tool_call>get_weather<tool_sep>").unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
@@ -655,7 +713,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hy_v3_malformed_tool_call_fails_fast() {
|
||||
let mut parser = HyV3ToolParser::new(&test_tools(), "");
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V3);
|
||||
let error = parser
|
||||
.parse_complete(
|
||||
"<tool_calls><tool_call>get_weather<tool_sep><arg_key>city</arg_key><arg_value>Beijing</tool_call></tool_calls>",
|
||||
@@ -664,4 +722,78 @@ mod tests {
|
||||
|
||||
assert!(error.to_report_string().starts_with("tool parser parsing failed:"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v4_parse_complete_extracts_compact_calls_with_and_without_arguments() {
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V4);
|
||||
let output = parser
|
||||
.parse_complete(
|
||||
"<tool_calls><tool_call>get_weather<arg_key>city</arg_key><arg_value>Beijing</arg_value><arg_key>date</arg_key><arg_value>2026-03-30</arg_value></tool_call><tool_call>get_current_date</tool_call></tool_calls>",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
expect![[r#"
|
||||
ToolParserOutput {
|
||||
events: [
|
||||
ToolCall(
|
||||
ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some(
|
||||
"get_weather",
|
||||
),
|
||||
arguments: "{\"city\":\"Beijing\",\"date\":\"2026-03-30\"}",
|
||||
},
|
||||
),
|
||||
ToolCall(
|
||||
ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some(
|
||||
"get_current_date",
|
||||
),
|
||||
arguments: "{}",
|
||||
},
|
||||
),
|
||||
],
|
||||
}
|
||||
"#]]
|
||||
.assert_debug_eq(&output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v4_streaming_handles_markers_split_across_chunks() {
|
||||
let input = "prefix<tool_calls><tool_call>get_weather<arg_key>city</arg_key><arg_value>上海</arg_value></tool_call><tool_call>get_current_date</tool_call></tool_calls>";
|
||||
let chunks = split_by_chars(input, 7);
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V4);
|
||||
|
||||
let output = collect_stream(&mut parser, &chunks);
|
||||
|
||||
assert_eq!(output.normal_text(), "prefix");
|
||||
assert_eq!(output.calls().len(), 2);
|
||||
assert_eq!(parsed_arguments(&output, 0), json!({ "city": "上海" }));
|
||||
assert_eq!(output.calls()[1].name.as_deref(), Some("get_current_date"));
|
||||
assert_eq!(parsed_arguments(&output, 1), json!({}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v4_finish_reports_its_dialect_for_incomplete_calls() {
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V4);
|
||||
parser.parse_chunk("<tool_calls><tool_call>get_weather").unwrap();
|
||||
|
||||
let error = parser.finish().unwrap_err();
|
||||
|
||||
expect!["tool parser parsing failed: incomplete HY4 tool call"]
|
||||
.assert_eq(&error.to_report_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v4_malformed_argument_pair_fails_fast() {
|
||||
let mut parser = HyToolParser::new(&test_tools(), "", HyDialect::V4);
|
||||
let error = parser
|
||||
.parse_complete(
|
||||
"<tool_calls><tool_call>get_weather<arg_key>city</arg_key>Beijing</tool_call></tool_calls>",
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
assert!(error.to_report_string().starts_with("tool parser parsing failed:"));
|
||||
}
|
||||
}
|
||||
+86
-23
@@ -1,7 +1,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
//! Structural-tag grammar for HY3 XML-style tool calls.
|
||||
//! Structural-tag grammar for HY XML-style tool calls.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
@@ -12,31 +12,40 @@ use xgrammar_structural_tag::builders::{StructuralTagBuilder, StructuralTagConte
|
||||
use xgrammar_structural_tag::format::{Format, StructuralTag, TagFormat};
|
||||
use xgrammar_structural_tag::tool::{BuilderToolChoice, FunctionToolParam};
|
||||
|
||||
use super::HyV3ToolMarkers;
|
||||
use super::{HyDialect, HyToolMarkers};
|
||||
|
||||
/// HY3 structural-tag builder using tokenizer-specific structural markers.
|
||||
/// HY structural-tag builder using tokenizer-specific structural markers.
|
||||
#[derive(Debug, Clone)]
|
||||
pub(super) struct HyV3StructuralTagBuilder {
|
||||
markers: Arc<HyV3ToolMarkers>,
|
||||
pub(super) struct HyStructuralTagBuilder {
|
||||
markers: Arc<HyToolMarkers>,
|
||||
dialect: HyDialect,
|
||||
}
|
||||
|
||||
impl HyV3StructuralTagBuilder {
|
||||
pub(super) fn new(markers: Arc<HyV3ToolMarkers>) -> Self {
|
||||
Self { markers }
|
||||
impl HyStructuralTagBuilder {
|
||||
pub(super) fn new(markers: Arc<HyToolMarkers>, dialect: HyDialect) -> Self {
|
||||
Self { markers, dialect }
|
||||
}
|
||||
|
||||
fn argument_pair(&self, key: &str) -> Format {
|
||||
let excludes = self.markers.iter().collect::<Vec<_>>();
|
||||
Format::sequence(vec![
|
||||
let separator = self.dialect.separator();
|
||||
let mut elements = vec![
|
||||
Format::const_string(&self.markers.arg_key_start),
|
||||
Format::const_string(key),
|
||||
Format::const_string(&self.markers.arg_key_end),
|
||||
Format::const_string("\n"),
|
||||
];
|
||||
if !separator.is_empty() {
|
||||
elements.push(Format::const_string(separator));
|
||||
}
|
||||
elements.extend([
|
||||
Format::const_string(&self.markers.arg_value_start),
|
||||
Format::any_text_excluding(&excludes),
|
||||
Format::const_string(&self.markers.arg_value_end),
|
||||
Format::const_string("\n"),
|
||||
])
|
||||
]);
|
||||
if !separator.is_empty() {
|
||||
elements.push(Format::const_string(separator));
|
||||
}
|
||||
Format::sequence(elements)
|
||||
}
|
||||
|
||||
fn tool_call(&self, tool: &FunctionToolParam) -> TagFormat {
|
||||
@@ -56,14 +65,21 @@ impl HyV3StructuralTagBuilder {
|
||||
}
|
||||
|
||||
let content = if elements.is_empty() {
|
||||
Format::any_text()
|
||||
match self.dialect {
|
||||
HyDialect::V3 => Format::any_text(),
|
||||
HyDialect::V4 => Format::const_string(""),
|
||||
}
|
||||
} else {
|
||||
Format::sequence(elements)
|
||||
};
|
||||
let tool_sep = self.markers.tool_sep.as_deref().unwrap_or_default();
|
||||
TagFormat::new(
|
||||
format!(
|
||||
"{}{}{}\n",
|
||||
self.markers.tool_call_start, tool.function.name, self.markers.tool_sep
|
||||
"{}{}{}{}",
|
||||
self.markers.tool_call_start,
|
||||
tool.function.name,
|
||||
tool_sep,
|
||||
self.dialect.separator(),
|
||||
),
|
||||
content,
|
||||
self.markers.tool_call_end.clone(),
|
||||
@@ -72,15 +88,16 @@ impl HyV3StructuralTagBuilder {
|
||||
|
||||
fn tool_calls(&self, tools: &[FunctionToolParam], choice: BuilderToolChoice) -> Format {
|
||||
let mut calls = tools.iter().map(|tool| self.tool_call(tool)).collect::<Vec<_>>();
|
||||
let begin = format!("{}\n", self.markers.tool_calls_start);
|
||||
let end = format!("\n{}", self.markers.tool_calls_end);
|
||||
let separator = self.dialect.separator();
|
||||
let begin = format!("{}{separator}", self.markers.tool_calls_start);
|
||||
let end = format!("{separator}{}", self.markers.tool_calls_end);
|
||||
|
||||
match choice {
|
||||
BuilderToolChoice::Auto if calls.is_empty() => Format::any_text(),
|
||||
BuilderToolChoice::Auto => {
|
||||
let outer = TagFormat::new(
|
||||
begin,
|
||||
Format::tags_with_separator(calls, "\n", true, false),
|
||||
Format::tags_with_separator(calls, separator, true, false),
|
||||
end,
|
||||
);
|
||||
Format::triggered_tags(&[&self.markers.tool_calls_start], vec![outer])
|
||||
@@ -92,14 +109,14 @@ impl HyV3StructuralTagBuilder {
|
||||
]),
|
||||
BuilderToolChoice::Required => Format::sequence(vec![
|
||||
Format::const_string(begin),
|
||||
Format::tags_with_separator(calls, "\n", true, false),
|
||||
Format::tags_with_separator(calls, separator, true, false),
|
||||
Format::const_string(end),
|
||||
]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StructuralTagBuilder for HyV3StructuralTagBuilder {
|
||||
impl StructuralTagBuilder for HyStructuralTagBuilder {
|
||||
fn build(&self, ctx: StructuralTagContext<'_>) -> Result<StructuralTag> {
|
||||
Ok(StructuralTag::new(
|
||||
self.tool_calls(ctx.function_tools, ctx.tool_choice),
|
||||
@@ -152,8 +169,8 @@ mod tests {
|
||||
FunctionDefinition, FunctionToolParam, ToolChoice, ToolParam, build_structural_tag,
|
||||
};
|
||||
|
||||
use super::HyV3StructuralTagBuilder;
|
||||
use crate::tool::HyV3ToolMarkers;
|
||||
use super::HyStructuralTagBuilder;
|
||||
use crate::tool::{HyDialect, HyToolMarkers};
|
||||
|
||||
fn tool(name: &str, parameters: Value) -> ToolParam {
|
||||
ToolParam::Function(FunctionToolParam::new(
|
||||
@@ -165,9 +182,18 @@ mod tests {
|
||||
suffix: &str,
|
||||
tools: &[ToolParam],
|
||||
choice: ToolChoice,
|
||||
) -> xgrammar_structural_tag::format::StructuralTag {
|
||||
build_for_dialect(HyDialect::V3, suffix, tools, choice)
|
||||
}
|
||||
|
||||
fn build_for_dialect(
|
||||
dialect: HyDialect,
|
||||
suffix: &str,
|
||||
tools: &[ToolParam],
|
||||
choice: ToolChoice,
|
||||
) -> xgrammar_structural_tag::format::StructuralTag {
|
||||
build_structural_tag(
|
||||
HyV3StructuralTagBuilder::new(Arc::new(HyV3ToolMarkers::new(suffix))),
|
||||
HyStructuralTagBuilder::new(Arc::new(HyToolMarkers::new(suffix, dialect)), dialect),
|
||||
tools,
|
||||
choice,
|
||||
StructuralTagOptions::default().with_reasoning(false),
|
||||
@@ -196,6 +222,25 @@ mod tests {
|
||||
expect![[r#"{"type":"structural_tag","format":{"type":"sequence","elements":[{"type":"const_string","value":"<tool_calls:opensource>\n"},{"type":"tags_with_separator","tags":[{"begin":"<tool_call:opensource>get_weather<tool_sep:opensource>\n","content":{"type":"sequence","elements":[{"type":"sequence","elements":[{"type":"const_string","value":"<arg_key:opensource>"},{"type":"const_string","value":"city"},{"type":"const_string","value":"</arg_key:opensource>"},{"type":"const_string","value":"\n"},{"type":"const_string","value":"<arg_value:opensource>"},{"type":"any_text","excludes":["<tool_calls:opensource>","</tool_calls:opensource>","<tool_call:opensource>","</tool_call:opensource>","<tool_sep:opensource>","<arg_key:opensource>","</arg_key:opensource>","<arg_value:opensource>","</arg_value:opensource>"]},{"type":"const_string","value":"</arg_value:opensource>"},{"type":"const_string","value":"\n"}]},{"type":"star","content":{"type":"sequence","elements":[{"type":"const_string","value":"<arg_key:opensource>"},{"type":"const_string","value":"days"},{"type":"const_string","value":"</arg_key:opensource>"},{"type":"const_string","value":"\n"},{"type":"const_string","value":"<arg_value:opensource>"},{"type":"any_text","excludes":["<tool_calls:opensource>","</tool_calls:opensource>","<tool_call:opensource>","</tool_call:opensource>","<tool_sep:opensource>","<arg_key:opensource>","</arg_key:opensource>","<arg_value:opensource>","</arg_value:opensource>"]},{"type":"const_string","value":"</arg_value:opensource>"},{"type":"const_string","value":"\n"}]}}]},"end":"</tool_call:opensource>"}],"separator":"\n","at_least_one":true,"stop_after_first":false},{"type":"const_string","value":"\n</tool_calls:opensource>"}]}}"#]].assert_eq(&tag.to_json_string().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn required_uses_suffixed_compact_hy4_skeleton_and_bounded_values() {
|
||||
let tag = build_for_dialect(
|
||||
HyDialect::V4,
|
||||
":opensource",
|
||||
&[tool(
|
||||
"get_weather",
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": { "city": { "type": "string" } },
|
||||
"required": ["city"]
|
||||
}),
|
||||
)],
|
||||
ToolChoice::required(),
|
||||
);
|
||||
|
||||
expect![[r#"{"type":"structural_tag","format":{"type":"sequence","elements":[{"type":"const_string","value":"<tool_calls:opensource>"},{"type":"tags_with_separator","tags":[{"begin":"<tool_call:opensource>get_weather","content":{"type":"sequence","elements":[{"type":"sequence","elements":[{"type":"const_string","value":"<arg_key:opensource>"},{"type":"const_string","value":"city"},{"type":"const_string","value":"</arg_key:opensource>"},{"type":"const_string","value":"<arg_value:opensource>"},{"type":"any_text","excludes":["<tool_calls:opensource>","</tool_calls:opensource>","<tool_call:opensource>","</tool_call:opensource>","<arg_key:opensource>","</arg_key:opensource>","<arg_value:opensource>","</arg_value:opensource>"]},{"type":"const_string","value":"</arg_value:opensource>"}]}]},"end":"</tool_call:opensource>"}],"separator":"","at_least_one":true,"stop_after_first":false},{"type":"const_string","value":"</tool_calls:opensource>"}]}}"#]].assert_eq(&tag.to_json_string().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn optional_only_schema_keeps_declared_key_alternatives() {
|
||||
let tag = build(
|
||||
@@ -235,6 +280,24 @@ mod tests {
|
||||
expect![[r#"{"type":"structural_tag","format":{"type":"sequence","elements":[{"type":"const_string","value":"<tool_calls>\n"},{"type":"tag","begin":"<tool_call>lookup<tool_sep>\n","content":{"type":"any_text","excludes":[]},"end":"</tool_call>"},{"type":"const_string","value":"\n</tool_calls>"}]}}"#]].assert_eq(&forced);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hy_v4_zero_argument_tool_has_empty_compact_body() {
|
||||
let tag = build_for_dialect(
|
||||
HyDialect::V4,
|
||||
"",
|
||||
&[tool(
|
||||
"get_current_date",
|
||||
json!({ "type": "object", "properties": {} }),
|
||||
)],
|
||||
ToolChoice::function("get_current_date"),
|
||||
);
|
||||
let value = serde_json::to_value(tag).unwrap();
|
||||
|
||||
expect![[r#"{"type":"tag","begin":"<tool_call>get_current_date","content":{"type":"const_string","value":""},"end":"</tool_call>"}"#]].assert_eq(
|
||||
&serde_json::to_string(&value["format"]["elements"][1]).unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_required_property_is_still_emitted() {
|
||||
let tag = build(
|
||||
@@ -8,7 +8,7 @@ pub(crate) mod error;
|
||||
mod deepseek_dsml;
|
||||
pub(crate) mod deepseek_json;
|
||||
mod glm_xml;
|
||||
mod hy_v3;
|
||||
mod hy;
|
||||
pub(crate) mod json;
|
||||
mod kimi_k2;
|
||||
mod minimax_m2;
|
||||
@@ -24,7 +24,7 @@ pub use deepseek_dsml::{DeepSeekV4ToolParser, DeepSeekV32ToolParser};
|
||||
pub use deepseek_json::{DeepSeekV3ToolParser, DeepSeekV31ToolParser};
|
||||
pub use error::{Result, ToolParserError};
|
||||
pub use glm_xml::{Glm45MoeToolParser, Glm47MoeToolParser};
|
||||
pub(crate) use hy_v3::{HyV3ToolMarkers, HyV3ToolParser};
|
||||
pub(crate) use hy::{HyDialect, HyToolMarkers, HyToolParser};
|
||||
pub use json::{
|
||||
Granite4ToolParser, HermesToolParser, Internlm2ToolParser, Llama3JsonToolParser,
|
||||
MistralToolParser, Phi4MiniJsonToolParser, Qwen3XmlToolParser,
|
||||
|
||||
@@ -1,20 +1,12 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
use vllm_tokenizer::{DynTokenizer, Tokenizer};
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use super::{CombinedParser, Result, UnifiedParser, UnifiedParserOutput, token_id};
|
||||
use crate::reasoning::HyV3ReasoningParser;
|
||||
use crate::tool::{HyV3ToolMarkers, HyV3ToolParser, StructuralTagBuilder, Tool};
|
||||
|
||||
const HY_V3_MARKER_STEMS: &[&str] = &[
|
||||
"think",
|
||||
"tool_calls",
|
||||
"tool_call",
|
||||
"tool_sep",
|
||||
"arg_key",
|
||||
"arg_value",
|
||||
];
|
||||
use super::detect_hy_token_suffix;
|
||||
use crate::reasoning::HyReasoningParser;
|
||||
use crate::tool::{HyDialect, HyToolMarkers, HyToolParser, StructuralTagBuilder, Tool};
|
||||
use crate::unified::{CombinedParser, Result, UnifiedParser, UnifiedParserOutput, token_id};
|
||||
|
||||
/// Unified reasoning and tool parser for HY3 output.
|
||||
pub struct HyV3UnifiedParser {
|
||||
@@ -24,14 +16,14 @@ pub struct HyV3UnifiedParser {
|
||||
impl HyV3UnifiedParser {
|
||||
/// Create a HY3 parser using the suffix encoded in tokenizer added tokens.
|
||||
pub fn new(tools: &[Tool], tokenizer: DynTokenizer) -> Result<Self> {
|
||||
let suffix = detect_token_suffix(tokenizer.as_ref());
|
||||
let markers = HyV3ToolMarkers::new(&suffix);
|
||||
let suffix = detect_hy_token_suffix(tokenizer.as_ref());
|
||||
let markers = HyToolMarkers::new(&suffix, HyDialect::V3);
|
||||
for marker in markers.iter() {
|
||||
token_id(tokenizer.as_ref(), marker)?;
|
||||
}
|
||||
|
||||
let reasoning = HyV3ReasoningParser::new(tokenizer, &suffix)?;
|
||||
let tool = HyV3ToolParser::new(tools, &suffix);
|
||||
let reasoning = HyReasoningParser::new(tokenizer, &suffix)?;
|
||||
let tool = HyToolParser::new(tools, &suffix, HyDialect::V3);
|
||||
Ok(Self {
|
||||
inner: CombinedParser::new(Some(Box::new(reasoning)), Some(Box::new(tool))),
|
||||
})
|
||||
@@ -75,28 +67,6 @@ impl UnifiedParser for HyV3UnifiedParser {
|
||||
}
|
||||
}
|
||||
|
||||
/// Detect the HY3 structural-token suffix from tokenizer added vocabulary.
|
||||
fn detect_token_suffix(tokenizer: &dyn Tokenizer) -> String {
|
||||
tokenizer
|
||||
.added_vocab()
|
||||
.iter()
|
||||
.filter_map(|(token, id)| marker_suffix(token).map(|suffix| (*id, suffix)))
|
||||
.min_by_key(|(id, _)| *id)
|
||||
.map(|(_, suffix)| suffix.to_string())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Extract the suffix from one opening or closing HY3 structural token.
|
||||
fn marker_suffix(token: &str) -> Option<&str> {
|
||||
let body = token.strip_prefix('<')?.strip_suffix('>')?;
|
||||
let body = body.strip_prefix('/').unwrap_or(body);
|
||||
|
||||
HY_V3_MARKER_STEMS.iter().find_map(|stem| {
|
||||
let suffix = body.strip_prefix(stem)?;
|
||||
(suffix.is_empty() || suffix.starts_with(':')).then_some(suffix)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
@@ -0,0 +1,194 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
use vllm_tokenizer::DynTokenizer;
|
||||
|
||||
use super::detect_hy_token_suffix;
|
||||
use crate::reasoning::HyReasoningParser;
|
||||
use crate::tool::{HyDialect, HyToolMarkers, HyToolParser, StructuralTagBuilder, Tool};
|
||||
use crate::unified::{CombinedParser, Result, UnifiedParser, UnifiedParserOutput, token_id};
|
||||
|
||||
/// Unified reasoning and tool parser for HY4 output.
|
||||
pub struct HyV4UnifiedParser {
|
||||
inner: CombinedParser,
|
||||
}
|
||||
|
||||
impl HyV4UnifiedParser {
|
||||
/// Create a HY4 parser using the suffix encoded in tokenizer added tokens.
|
||||
pub fn new(tools: &[Tool], tokenizer: DynTokenizer) -> Result<Self> {
|
||||
let suffix = detect_hy_token_suffix(tokenizer.as_ref());
|
||||
let markers = HyToolMarkers::new(&suffix, HyDialect::V4);
|
||||
for marker in markers.iter() {
|
||||
token_id(tokenizer.as_ref(), marker)?;
|
||||
}
|
||||
|
||||
let reasoning = HyReasoningParser::new(tokenizer, &suffix)?;
|
||||
let tool = HyToolParser::new(tools, &suffix, HyDialect::V4);
|
||||
Ok(Self {
|
||||
inner: CombinedParser::new(Some(Box::new(reasoning)), Some(Box::new(tool))),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl UnifiedParser for HyV4UnifiedParser {
|
||||
fn create(tools: &[Tool], tokenizer: DynTokenizer) -> Result<Box<dyn UnifiedParser>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
Self::new(tools, tokenizer).map(|parser| Box::new(parser) as Box<dyn UnifiedParser>)
|
||||
}
|
||||
|
||||
fn initialize(&mut self, prompt_token_ids: &[u32]) -> Result<()> {
|
||||
self.inner.initialize(prompt_token_ids)
|
||||
}
|
||||
|
||||
fn preserve_special_tokens(&self) -> bool {
|
||||
self.inner.preserve_special_tokens()
|
||||
}
|
||||
|
||||
fn structural_tag_builder(&self) -> Option<&dyn StructuralTagBuilder> {
|
||||
self.inner.structural_tag_builder()
|
||||
}
|
||||
|
||||
fn tool_call_id(&self, tool_index: usize) -> Option<&str> {
|
||||
self.inner.tool_call_id(tool_index)
|
||||
}
|
||||
|
||||
fn parse_into(&mut self, delta: &str, output: &mut UnifiedParserOutput) -> Result<()> {
|
||||
self.inner.parse_into(delta, output)
|
||||
}
|
||||
|
||||
fn finish(&mut self) -> Result<UnifiedParserOutput> {
|
||||
self.inner.finish()
|
||||
}
|
||||
|
||||
fn reset(&mut self) -> String {
|
||||
self.inner.reset()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::json;
|
||||
use vllm_tokenizer::{Tokenizer, test_utils::TestTokenizer};
|
||||
use xgrammar_structural_tag::builders::StructuralTagOptions;
|
||||
use xgrammar_structural_tag::{
|
||||
FunctionDefinition, FunctionToolParam, ToolChoice, ToolParam, build_structural_tag,
|
||||
};
|
||||
|
||||
use super::{HyV4UnifiedParser, UnifiedParser};
|
||||
use crate::tool::Tool;
|
||||
use crate::unified::{UnifiedParserEvent, UnifiedParserOutput};
|
||||
|
||||
fn tokenizer() -> TestTokenizer {
|
||||
[
|
||||
"<think:opensource>",
|
||||
"</think:opensource>",
|
||||
"<tool_calls:opensource>",
|
||||
"</tool_calls:opensource>",
|
||||
"<tool_call:opensource>",
|
||||
"</tool_call:opensource>",
|
||||
"<arg_key:opensource>",
|
||||
"</arg_key:opensource>",
|
||||
"<arg_value:opensource>",
|
||||
"</arg_value:opensource>",
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.fold(TestTokenizer::new(), |tokenizer, (index, token)| {
|
||||
tokenizer.with_regular_token(token, 1000 + index as u32)
|
||||
})
|
||||
}
|
||||
|
||||
fn tools() -> Vec<Tool> {
|
||||
vec![
|
||||
Tool {
|
||||
name: "get_weather".to_string(),
|
||||
description: None,
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": { "city": { "type": "string" } },
|
||||
}),
|
||||
strict: None,
|
||||
},
|
||||
Tool {
|
||||
name: "get_current_date".to_string(),
|
||||
description: None,
|
||||
parameters: json!({ "type": "object", "properties": {} }),
|
||||
strict: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_suffixed_reasoning_and_compact_tool_calls_as_one_stream() {
|
||||
let tokenizer = Arc::new(tokenizer());
|
||||
let think_start_id = tokenizer.token_to_id("<think:opensource>").unwrap();
|
||||
let mut parser = HyV4UnifiedParser::new(&tools(), tokenizer).unwrap();
|
||||
parser.initialize(&[think_start_id]).unwrap();
|
||||
|
||||
let chunks = [
|
||||
"reasoning</think:open",
|
||||
"source>answer<tool_calls:opensource><tool_call:opensource>get_weather",
|
||||
"<arg_key:opensource>city</arg_key:opensource>",
|
||||
"<arg_value:opensource>Beijing</arg_value:opensource>",
|
||||
"</tool_call:opensource><tool_call:opensource>get_current_date",
|
||||
"</tool_call:opensource></tool_calls:opensource>",
|
||||
];
|
||||
let mut output = UnifiedParserOutput::default();
|
||||
for chunk in chunks {
|
||||
parser.parse_into(chunk, &mut output).unwrap();
|
||||
}
|
||||
output.append(parser.finish().unwrap());
|
||||
|
||||
assert_eq!(
|
||||
output.events,
|
||||
vec![
|
||||
UnifiedParserEvent::Reasoning("reasoning".to_string()),
|
||||
UnifiedParserEvent::Text("answer".to_string()),
|
||||
UnifiedParserEvent::ToolCall(crate::tool::ToolCallDelta {
|
||||
tool_index: 0,
|
||||
name: Some("get_weather".to_string()),
|
||||
arguments: r#"{"city":"Beijing"}"#.to_string(),
|
||||
}),
|
||||
UnifiedParserEvent::ToolCall(crate::tool::ToolCallDelta {
|
||||
tool_index: 1,
|
||||
name: Some("get_current_date".to_string()),
|
||||
arguments: "{}".to_string(),
|
||||
}),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structural_tag_uses_compact_hy4_skeleton() {
|
||||
let parser = HyV4UnifiedParser::new(&tools(), Arc::new(tokenizer())).unwrap();
|
||||
let structural_tools = [ToolParam::Function(FunctionToolParam::new(
|
||||
FunctionDefinition::new("get_weather").with_parameters(json!({
|
||||
"type": "object",
|
||||
"properties": { "city": { "type": "string" } },
|
||||
"required": ["city"]
|
||||
})),
|
||||
))];
|
||||
|
||||
let tag = build_structural_tag(
|
||||
parser.structural_tag_builder().unwrap(),
|
||||
&structural_tools,
|
||||
ToolChoice::required(),
|
||||
StructuralTagOptions::default().with_reasoning(false),
|
||||
)
|
||||
.unwrap()
|
||||
.to_json_string()
|
||||
.unwrap();
|
||||
|
||||
assert!(tag.contains("<tool_calls:opensource>"));
|
||||
assert!(tag.contains("<tool_call:opensource>get_weather"));
|
||||
assert!(tag.contains("<arg_key:opensource>"));
|
||||
assert!(tag.contains("</tool_call:opensource>"));
|
||||
assert!(tag.contains("</tool_calls:opensource>"));
|
||||
assert!(!tag.contains("tool_sep"));
|
||||
assert!(!tag.contains(r#"\n"#));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
mod hy_v3;
|
||||
mod hy_v4;
|
||||
|
||||
pub use hy_v3::HyV3UnifiedParser;
|
||||
pub use hy_v4::HyV4UnifiedParser;
|
||||
|
||||
const HY_MARKER_STEMS: &[&str] = &[
|
||||
"think",
|
||||
"tool_calls",
|
||||
"tool_call",
|
||||
"tool_sep",
|
||||
"arg_key",
|
||||
"arg_value",
|
||||
];
|
||||
|
||||
/// Detect the HY structural-token suffix from tokenizer added vocabulary.
|
||||
fn detect_hy_token_suffix(tokenizer: &dyn vllm_tokenizer::Tokenizer) -> String {
|
||||
tokenizer
|
||||
.added_vocab()
|
||||
.iter()
|
||||
.filter_map(|(token, id)| hy_marker_suffix(token).map(|suffix| (*id, suffix)))
|
||||
.min_by_key(|(id, _)| *id)
|
||||
.map(|(_, suffix)| suffix.to_string())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Extract the suffix from one opening or closing HY structural token.
|
||||
fn hy_marker_suffix(token: &str) -> Option<&str> {
|
||||
let body = token.strip_prefix('<')?.strip_suffix('>')?;
|
||||
let body = body.strip_prefix('/').unwrap_or(body);
|
||||
|
||||
HY_MARKER_STEMS.iter().find_map(|stem| {
|
||||
let suffix = body.strip_prefix(stem)?;
|
||||
(suffix.is_empty() || suffix.starts_with(':')).then_some(suffix)
|
||||
})
|
||||
}
|
||||
@@ -5,13 +5,13 @@
|
||||
|
||||
mod combined;
|
||||
mod gemma4;
|
||||
mod hy_v3;
|
||||
mod hy;
|
||||
mod inkling;
|
||||
mod kimi_k3;
|
||||
|
||||
pub use combined::CombinedParser;
|
||||
pub use gemma4::Gemma4UnifiedParser;
|
||||
pub use hy_v3::HyV3UnifiedParser;
|
||||
pub use hy::{HyV3UnifiedParser, HyV4UnifiedParser};
|
||||
pub use inkling::InklingUnifiedParser;
|
||||
pub use kimi_k3::{KimiK3StructuralTagBuilder, KimiK3UnifiedParser};
|
||||
use thiserror::Error;
|
||||
|
||||
Reference in New Issue
Block a user