[Rust Frontend] Align tool rendering for Kimi K3 (#50540)
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
72cd5424da
commit
0b1c151cbc
@@ -291,12 +291,17 @@ fn write_tool_declare(
|
|||||||
let mut specs = Vec::with_capacity(tools.len());
|
let mut specs = Vec::with_capacity(tools.len());
|
||||||
for tool in tools {
|
for tool in tools {
|
||||||
let mut function = Map::new();
|
let mut function = Map::new();
|
||||||
function.insert(
|
if let Some(description) = &tool.description {
|
||||||
"description".to_string(),
|
function.insert(
|
||||||
Value::String(tool.description.clone().unwrap_or_default()),
|
"description".to_string(),
|
||||||
);
|
Value::String(description.clone()),
|
||||||
|
);
|
||||||
|
}
|
||||||
function.insert("name".to_string(), Value::String(tool.name.clone()));
|
function.insert("name".to_string(), Value::String(tool.name.clone()));
|
||||||
function.insert("parameters".to_string(), sort_json(&tool.parameters));
|
function.insert("parameters".to_string(), sort_json(&tool.parameters));
|
||||||
|
if let Some(strict) = tool.strict {
|
||||||
|
function.insert("strict".to_string(), Value::Bool(strict));
|
||||||
|
}
|
||||||
specs.push(json!({
|
specs.push(json!({
|
||||||
"function": Value::Object(function),
|
"function": Value::Object(function),
|
||||||
"type": "function",
|
"type": "function",
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
"function": {
|
"function": {
|
||||||
"name": "get_weather",
|
"name": "get_weather",
|
||||||
"description": "weather",
|
"description": "weather",
|
||||||
|
"strict": true,
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -34,12 +35,16 @@
|
|||||||
"type": "function",
|
"type": "function",
|
||||||
"function": {
|
"function": {
|
||||||
"name": "calc",
|
"name": "calc",
|
||||||
"description": "calculator",
|
"strict": false,
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"x": {
|
"x": {
|
||||||
"type": "number"
|
"type": [
|
||||||
|
"number",
|
||||||
|
"null"
|
||||||
|
],
|
||||||
|
"default": null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": [
|
"required": [
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
Here are the available tools, described in JSONSchema.
|
Here are the available tools, described in JSONSchema.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
[{"function":{"description":"weather","name":"get_weather","parameters":{"properties":{"city":{"type":"string"},"days":{"type":"number"}},"required":["city"],"type":"object"}},"type":"function"}]
|
[{"function":{"description":"weather","name":"get_weather","parameters":{"properties":{"city":{"type":"string"},"days":{"type":"number"}},"required":["city"],"type":"object"},"strict":true},"type":"function"}]
|
||||||
```<|close|>message<|sep|><|end_of_msg|><|open|>message role="system" type="thinking-effort"<|sep|>`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low`, `medium`, `high`, and `max`.
|
```<|close|>message<|sep|><|end_of_msg|><|open|>message role="system" type="thinking-effort"<|sep|>`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low`, `medium`, `high`, and `max`.
|
||||||
Now the system is invoked with `thinking_effort=max`.<|close|>message<|sep|><|end_of_msg|><|open|>message role="user"<|sep|>hi<|close|>message<|sep|><|end_of_msg|><|open|>message role="system" type="tool-declare"<|sep|>## New Tools Available
|
Now the system is invoked with `thinking_effort=max`.<|close|>message<|sep|><|end_of_msg|><|open|>message role="user"<|sep|>hi<|close|>message<|sep|><|end_of_msg|><|open|>message role="system" type="tool-declare"<|sep|>## New Tools Available
|
||||||
The system dynamically extends the toolset via lazy-loading.
|
The system dynamically extends the toolset via lazy-loading.
|
||||||
@@ -10,5 +10,5 @@ You have access to all existing and extended tools.
|
|||||||
Here are the specs for the extended tools.
|
Here are the specs for the extended tools.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
[{"function":{"description":"calculator","name":"calc","parameters":{"properties":{"x":{"type":"number"}},"required":["x"],"type":"object"}},"type":"function"}]
|
[{"function":{"name":"calc","parameters":{"properties":{"x":{"default":null,"type":["number","null"]}},"required":["x"],"type":"object"},"strict":false},"type":"function"}]
|
||||||
```<|close|>message<|sep|><|end_of_msg|><|open|>message role="user"<|sep|>use calc<|close|>message<|sep|><|end_of_msg|><|open|>message role="assistant"<|sep|><|open|>think<|sep|>
|
```<|close|>message<|sep|><|end_of_msg|><|open|>message role="user"<|sep|>use calc<|close|>message<|sep|><|end_of_msg|><|open|>message role="assistant"<|sep|><|open|>think<|sep|>
|
||||||
@@ -17,7 +17,9 @@ use super::KimiK3ChatRenderer;
|
|||||||
use crate::ChatRenderer;
|
use crate::ChatRenderer;
|
||||||
use crate::renderer::kimi_k3::encoding::{CLOSE, END_OF_MSG, IMAGE_PLACEHOLDER, OPEN, SEP};
|
use crate::renderer::kimi_k3::encoding::{CLOSE, END_OF_MSG, IMAGE_PLACEHOLDER, OPEN, SEP};
|
||||||
use crate::renderer::test_utils::{FixtureRequestOptions, fixture_chat_request};
|
use crate::renderer::test_utils::{FixtureRequestOptions, fixture_chat_request};
|
||||||
use crate::request::{ChatContentPart, ChatMessage, GenerationPromptMode, ReasoningEffort};
|
use crate::request::{
|
||||||
|
ChatContentPart, ChatMessage, ChatTool, GenerationPromptMode, ReasoningEffort,
|
||||||
|
};
|
||||||
use crate::{AssistantContentBlock, AssistantToolCall};
|
use crate::{AssistantContentBlock, AssistantToolCall};
|
||||||
|
|
||||||
const OPEN_ID: u32 = 256;
|
const OPEN_ID: u32 = 256;
|
||||||
@@ -90,6 +92,70 @@ fn golden_dynamic_system_tool_declare() {
|
|||||||
assert_golden("dynamic_system_tool_declare");
|
assert_golden("dynamic_system_tool_declare");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tool_declare_omits_absent_fields_and_preserves_parameter_nulls() {
|
||||||
|
let mut request = crate::request::ChatRequest::for_test();
|
||||||
|
request.tools = vec![ChatTool {
|
||||||
|
name: "lookup".to_string(),
|
||||||
|
description: None,
|
||||||
|
parameters: json!({
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"limit": {
|
||||||
|
"type": ["integer", "null"],
|
||||||
|
"default": null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
strict: None,
|
||||||
|
}];
|
||||||
|
|
||||||
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
|
assert!(rendered.contains(
|
||||||
|
r#"[{"function":{"name":"lookup","parameters":{"properties":{"limit":{"default":null,"type":["integer","null"]}},"type":"object"}},"type":"function"}]"#
|
||||||
|
));
|
||||||
|
assert!(!rendered.contains(r#""description":"#));
|
||||||
|
assert!(!rendered.contains(r#""strict":"#));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tool_declare_preserves_present_optional_fields() {
|
||||||
|
let mut request = crate::request::ChatRequest::for_test();
|
||||||
|
request.tools = vec![ChatTool {
|
||||||
|
name: "lookup".to_string(),
|
||||||
|
description: Some("Look up a record".to_string()),
|
||||||
|
parameters: json!({"type": "object"}),
|
||||||
|
strict: Some(false),
|
||||||
|
}];
|
||||||
|
|
||||||
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
|
assert!(rendered.contains(
|
||||||
|
r#"[{"function":{"description":"Look up a record","name":"lookup","parameters":{"type":"object"},"strict":false},"type":"function"}]"#
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn malformed_tool_arguments_render_as_raw_text() {
|
||||||
|
let malformed = " {\"city\":\"Beijing\" ";
|
||||||
|
let mut request = crate::request::ChatRequest::for_test();
|
||||||
|
request.messages = vec![ChatMessage::assistant_blocks(vec![
|
||||||
|
AssistantContentBlock::ToolCall(AssistantToolCall {
|
||||||
|
id: "call-1".to_string(),
|
||||||
|
name: "weather".to_string(),
|
||||||
|
arguments: malformed.to_string(),
|
||||||
|
}),
|
||||||
|
])];
|
||||||
|
request.chat_options.generation_prompt_mode = GenerationPromptMode::NoGenerationPrompt;
|
||||||
|
|
||||||
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
|
assert!(rendered.contains(&format!(
|
||||||
|
"<|open|>json type=\"object\"<|sep|>{malformed}<|close|>json<|sep|>"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn token_writer_protects_literal_control_and_media_markers() {
|
fn token_writer_protects_literal_control_and_media_markers() {
|
||||||
let tokenizer = Arc::new(test_tokenizer());
|
let tokenizer = Arc::new(test_tokenizer());
|
||||||
|
|||||||
Reference in New Issue
Block a user