Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,17 +838,30 @@ impl Render<Message> for HarmonyEncoding {
}
};

// next render the header recipient, if there is one
if let Some(recipient) = &message.recipient {
if recipient != "all" {
self.render_text_into(format!(" to={recipient}"), into)?;
// Header channel vs recipient order: commentary uses <|channel|>… before to=…;
// other channels keep to=… before <|channel|>… (legacy order).
let commentary_channel_first = message.channel.as_deref() == Some("commentary");

if commentary_channel_first {
if let Some(channel) = &message.channel {
self.render_formatting_token_into(FormattingToken::Channel, into)?;
self.render_text_into(channel, into)?;
}
if let Some(recipient) = &message.recipient {
if recipient != "all" {
self.render_text_into(format!(" to={recipient}"), into)?;
}
}
} else {
if let Some(recipient) = &message.recipient {
if recipient != "all" {
self.render_text_into(format!(" to={recipient}"), into)?;
}
}
if let Some(channel) = &message.channel {
self.render_formatting_token_into(FormattingToken::Channel, into)?;
self.render_text_into(channel, into)?;
}
}

// next header channel
if let Some(channel) = &message.channel {
self.render_formatting_token_into(FormattingToken::Channel, into)?;
self.render_text_into(channel, into)?;
}

// finally content type
Expand All @@ -865,7 +878,9 @@ impl Render<Message> for HarmonyEncoding {
self.render_text_into(rest, into)?;
}
} else {
self.render_text_into(format!(" {content_type}"), into)?;
self.render_text_into(" ", into)?;
self.render_formatting_token_into(FormattingToken::ConstrainedFormat, into)?;
self.render_text_into(content_type, into)?;
}
} else {
self.render_text_into(format!(" {content_type}"), into)?;
Expand Down
2 changes: 1 addition & 1 deletion test-data/test_does_not_drop_if_ongoing_analysis.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<|start|>user<|message|>What is the weather in SF?<|end|><|start|>assistant<|channel|>analysis<|message|>User asks: “What is the weather in SF?” We need to use lookup_weather tool.<|end|><|start|>assistant to=functions.lookup_weather<|channel|>commentary <|constrain|>json<|message|>{"location": "San Francisco"}<|call|><|start|>functions.lookup_weather<|message|>{"temperature": 20, "description": "sunny"}<|end|><|start|>assistant
<|start|>user<|message|>What is the weather in SF?<|end|><|start|>assistant<|channel|>analysis<|message|>User asks: “What is the weather in SF?” We need to use lookup_weather tool.<|end|><|start|>assistant<|channel|>commentary to=functions.lookup_weather <|constrain|>json<|message|>{"location": "San Francisco"}<|call|><|start|>functions.lookup_weather<|message|>{"temperature": 20, "description": "sunny"}<|end|><|start|>assistant
2 changes: 1 addition & 1 deletion test-data/test_tool_response_parsing.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<|start|>browser.search to=assistant<|channel|>commentary<|message|>{"result": "https://openai.com/"}
<|start|>browser.search<|channel|>commentary to=assistant<|message|>{"result": "https://openai.com/"}
2 changes: 1 addition & 1 deletion tests/test_harmony.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def test_tool_call_with_constrain_tokenized_correctly(encoding_name):
"""
encoding = load_harmony_encoding(encoding_name)
text = (
"<|start|>assistant to=functions.get_weather<|channel|>commentary"
"<|start|>assistant<|channel|>commentary to=functions.get_weather"
' <|constrain|>json<|message|>{"location": "Tokyo"}<|call|>'
)
tokens = encoding.encode(text, allowed_special="all")
Expand Down
Loading