Repository navigation
feat(symphony): keyed arguments, the third tagged assembler, with Hy4, Ling and IQuest #2842
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a20f0a0
feat(symphony): the keyed assembler, for a call written as its name a…
slin1237 50d9cb8
feat(symphony): the Hy4, Ling and IQuest tables
slin1237 ee91d71
test(symphony): Hy4, Ling and IQuest in the parity table, the prompt …
slin1237 5c0dfda
fix(symphony): the keyed assembler ends a started call when the strea…
slin1237 a5c845a
fix(symphony): the keyed tables name their turn openers, and the prob…
slin1237 855bc1f
fix(symphony): a keyed block that names no call opens no key, held by…
slin1237 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| //! Hy4: reasoning between `<think:opensource>` and `</think:opensource>`, the turn's calls between | ||
| //! `<tool_calls:opensource>` and `</tool_calls:opensource>`, each call between | ||
| //! `<tool_call:opensource>` and `</tool_call:opensource>` as its name and keyed arguments in Hy4's | ||
| //! spelling of the tags ([`keyed::Tags::HY4`]), typed by the request's tools. Everything else is | ||
| //! content. The prompt opens the thought, and an empty thought writes `</think:opensource>` at | ||
| //! once. A turn opens with `<|hy_start:opensource|>assistant<|hy_middle:opensource|>`, so the | ||
| //! prompt's replay starts there. Recorded as `hy4-preview` (tencent/Hy4-preview). | ||
|
|
||
| use crate::{ | ||
| format::{CallSyntax, Emits, Format}, | ||
| tagged::keyed, | ||
| }; | ||
|
|
||
| /// The Hy4 table. | ||
| pub fn hy4() -> Format { | ||
| Format::new("hy4") | ||
| .terminal("think_open", "<think:opensource>") | ||
| .terminal("think_close", "</think:opensource>") | ||
| .terminal("calls_open", "<tool_calls:opensource>") | ||
| .terminal("calls_close", "</tool_calls:opensource>") | ||
| .terminal("call_open", "<tool_call:opensource>") | ||
| .terminal("call_close", "</tool_call:opensource>") | ||
| .state("content", Emits::Content) | ||
| .state("reasoning", Emits::Reasoning) | ||
| .state("calls", Emits::Wrapper) | ||
| .state("call", Emits::Arguments) | ||
| .transition("content", "think_open", "reasoning") | ||
| .transition("reasoning", "think_close", "content") | ||
| .transition("content", "calls_open", "calls") | ||
| .transition("calls", "call_open", "call") | ||
| .transition("call", "call_close", "calls") | ||
| .transition("calls", "calls_close", "content") | ||
| .calls(CallSyntax::Keyed(keyed::Tags::HY4)) | ||
| .opens_turn("<|hy_start:opensource|>assistant<|hy_middle:opensource|>") | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use openai_protocol::common::{Function, Tool}; | ||
| use serde_json::json as value; | ||
|
|
||
| use super::*; | ||
| use crate::{ | ||
| engine::Engine, | ||
| event::{Event, Events}, | ||
| input::{EngineFinish, Input}, | ||
| parser::Parser, | ||
| tagged::Declared, | ||
| }; | ||
|
|
||
| fn declared() -> Declared { | ||
| Declared::of(&[Tool { | ||
| tool_type: "function".to_string(), | ||
| function: Function { | ||
| name: "get_weather".to_string(), | ||
| description: None, | ||
| parameters: value!({"type": "object", "properties": { | ||
| "city": {"type": "string"}, | ||
| "days": {"type": "integer"}, | ||
| }}), | ||
| strict: None, | ||
| }, | ||
| }]) | ||
| } | ||
|
|
||
| const PROMPT: &str = "<think:opensource>\n"; | ||
| const OUTPUT: &str = concat!( | ||
| "The user asks.</think:opensource><tool_calls:opensource>", | ||
| "<tool_call:opensource>get_weather<arg_key:opensource>city</arg_key:opensource>", | ||
| "<arg_value:opensource>Paris</arg_value:opensource><arg_key:opensource>days", | ||
| "</arg_key:opensource><arg_value:opensource>3</arg_value:opensource>", | ||
| "</tool_call:opensource>", | ||
| "<tool_call:opensource>get_weather</tool_call:opensource></tool_calls:opensource>" | ||
| ); | ||
|
|
||
| fn run(prompt: &str, output: &str) -> Vec<Event> { | ||
| let mut parser = Engine::new(hy4(), declared()); | ||
| let mut out = Events::new(); | ||
| parser | ||
| .feed( | ||
| Input::Prompt { | ||
| token_ids: &[], | ||
| text: prompt, | ||
| }, | ||
| &mut out, | ||
| ) | ||
| .expect("prompt"); | ||
| parser | ||
| .feed( | ||
| Input::Delta { | ||
| token_ids: &[], | ||
| text: output, | ||
| spans: &[], | ||
| }, | ||
| &mut out, | ||
| ) | ||
| .expect("delta"); | ||
| parser | ||
| .feed( | ||
| Input::End { | ||
| finish: EngineFinish::Stop, | ||
| }, | ||
| &mut out, | ||
| ) | ||
| .expect("end"); | ||
| out.drain() | ||
| } | ||
|
|
||
| fn bytes(events: &[Event]) -> String { | ||
| events | ||
| .iter() | ||
| .map(|e| match e { | ||
| Event::Content(t) | Event::Reasoning(t) => t.text.as_str(), | ||
| Event::Dropped { text, .. } | Event::Malformed { text, .. } => text.text.as_str(), | ||
| Event::ToolCallStart { source, .. } | ||
| | Event::ToolCallArguments { source, .. } | ||
| | Event::ToolCallEnd { source, .. } => source.text.as_str(), | ||
| Event::ReasoningStart | Event::ReasoningEnd | Event::Finish { .. } => "", | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| fn arguments(events: &[Event]) -> Vec<(String, String)> { | ||
| let mut calls: Vec<(String, String)> = Vec::new(); | ||
| for event in events { | ||
| match event { | ||
| Event::ToolCallStart { name, .. } => calls.push((name.clone(), String::new())), | ||
| Event::ToolCallArguments { json, .. } => { | ||
| if let Some(last) = calls.last_mut() { | ||
| last.1.push_str(json); | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| calls | ||
| } | ||
|
|
||
| #[test] | ||
| fn the_prompt_is_replayed_from_hy4s_own_turn_opener() { | ||
|
slin1237 marked this conversation as resolved.
|
||
| // A call marker quoted in the user's turn, then the generation prompt opening the thought: | ||
| // the replay starts at Hy4's own turn opener, so the output is the thought and then | ||
| // content (smg #2842, Alex's probe with the rendered prompt). | ||
| let prompt = "<|hy_start:opensource|>user<|hy_middle:opensource|>Why did you print \ | ||
| <tool_calls:opensource> there?<|hy_start:opensource|>assistant\ | ||
| <|hy_middle:opensource|><think:opensource>\n"; | ||
| let output = "The user asks about the tag.</think:opensource>It opens a call."; | ||
| let events = run(prompt, output); | ||
| assert_eq!(events[0], Event::ReasoningStart); | ||
| assert_eq!(bytes(&events), output); | ||
| assert!(arguments(&events).is_empty()); | ||
| let reasoning: String = events | ||
| .iter() | ||
| .filter_map(|event| match event { | ||
| Event::Reasoning(text) => Some(text.text.as_str()), | ||
| _ => None, | ||
| }) | ||
| .collect(); | ||
| let content: String = events | ||
| .iter() | ||
| .filter_map(|event| match event { | ||
| Event::Content(text) => Some(text.text.as_str()), | ||
| _ => None, | ||
| }) | ||
| .collect(); | ||
| assert_eq!(reasoning, "The user asks about the tag."); | ||
| assert_eq!(content, "It opens a call."); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_recorded_output_gives_its_calls_typed_by_the_tools_and_every_byte() { | ||
| let events = run(PROMPT, OUTPUT); | ||
| assert_eq!(bytes(&events), OUTPUT); | ||
| assert_eq!( | ||
| arguments(&events), | ||
| [ | ||
| ( | ||
| "get_weather".to_string(), | ||
| r#"{"city": "Paris", "days": 3}"#.to_string() | ||
| ), | ||
| ("get_weather".to_string(), "{}".to_string()), | ||
| ] | ||
| ); | ||
| assert!(matches!( | ||
| events.last(), | ||
| Some(Event::Finish { tool_calls: 2, .. }) | ||
| )); | ||
| let reasoning: String = events | ||
| .iter() | ||
| .filter_map(|event| match event { | ||
| Event::Reasoning(text) => Some(text.text.as_str()), | ||
| _ => None, | ||
| }) | ||
| .collect(); | ||
| assert_eq!(reasoning, "The user asks."); | ||
| } | ||
|
|
||
| #[test] | ||
| fn every_chunking_says_the_same_and_accounts_for_every_byte() { | ||
| let whole = arguments(&run(PROMPT, OUTPUT)); | ||
| for cut in 1..OUTPUT.len() { | ||
| if !OUTPUT.is_char_boundary(cut) { | ||
| continue; | ||
| } | ||
| let mut parser = Engine::new(hy4(), declared()); | ||
| let mut out = Events::new(); | ||
| parser | ||
| .feed( | ||
| Input::Prompt { | ||
| token_ids: &[], | ||
| text: PROMPT, | ||
| }, | ||
| &mut out, | ||
| ) | ||
| .expect("prompt"); | ||
| for piece in [&OUTPUT[..cut], &OUTPUT[cut..]] { | ||
| parser | ||
| .feed( | ||
| Input::Delta { | ||
| token_ids: &[], | ||
| text: piece, | ||
| spans: &[], | ||
| }, | ||
| &mut out, | ||
| ) | ||
| .expect("delta"); | ||
| } | ||
| parser | ||
| .feed( | ||
| Input::End { | ||
| finish: EngineFinish::Stop, | ||
| }, | ||
| &mut out, | ||
| ) | ||
| .expect("end"); | ||
| let events = out.drain(); | ||
| assert_eq!(bytes(&events), OUTPUT, "cut at {cut}"); | ||
| assert_eq!(arguments(&events), whole, "cut at {cut}"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Nit: The rebase onto the updated #2841 left this arm passing every terminal to the keyed assembler as the call's end. The base fix (c9234c9 / 3c548eb) stopped doing that for DSML, "so no call's end holds the next call's opening and no structural marker is reported as malformed when no call had started".
Ling and IQuest both have
.transition("call", "call_open", "call"). So when a<tool_call>arrives inside an open call, it reaches this arm withterminal = "<tool_call>". Two cases:<tool_call>f\n<arg_key>a</arg_key>\n<arg_value>1</arg_value>\n<tool_call>g\n</tool_call>, call 0 ends withToolCallEnd { index: 0, source: "<tool_call>" }. The next call's opener, and its token count throughtokens.relabel, are attributed to the previous call's end.<tool_call>\n<tool_call>g\n</tool_call>,keyed::Assembler::closeappends the terminal tocarriedand reports it asMalformed(WITHOUT_A_NAME)(keyed.rs:212–214). The chat adapter treatsMalformedtext as content (adapt/chat.rs:45,:150), so the client receives a literal<tool_call>incontentnext to the real call.Byte conservation still holds, so
contract.rsdoesn't catch this. But the doc this push edited above ("the keyed one takes the call's closing marker") and the doc onkeyed::Assembler::close("terminalis the closing marker the engine read") don't hold for these two tables. Hy4 isn't affected, because onlycall_closeleaves itscallstate.One fix, matching DSML: when the terminal is the next call's opening, close the call with no bytes and return
false, so the engine drops the terminal asWrapper.terminal()already knowsnext, andself.format.emits(next) == Emits::Argumentsmeans the terminal opens another call.close_call/endcould take that as a flag:A Ling contract case such as
<tool_call>f\n<arg_key>city</arg_key>\n<arg_value>a</arg_value>\n<tool_call>\n<tool_call>get_weather\n</tool_call>would cover both shapes.