Skip to content

Commit 63b4179

Browse files
committed
feat: add mistral and phi4 tool parser
1 parent 125be8c commit 63b4179

File tree

2 files changed

+356
-8
lines changed

2 files changed

+356
-8
lines changed

lib/llm/src/postprocessor/tool_calling/json_parser.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,21 @@ fn handle_single_token_tool_calls(input: &str, start_token: &str) -> String {
7474
continue;
7575
}
7676
// Only consider segments that start like JSON
77-
if s.starts_with('{') || s.starts_with('[') {
77+
if s.starts_with('{') {
7878
// Trim trailing non-JSON by cutting at the last closing brace/bracket
79-
if let Some(pos) = s.rfind(['}', ']']) {
80-
let candidate = &s[..=pos];
79+
if let Some(pos) = s.rfind('}') {
80+
let candidate = &s[..=pos].trim();
8181
// Keep only valid JSON candidates
8282
if serde_json::from_str::<serde_json::Value>(candidate).is_ok() {
8383
items.push(candidate.to_string());
8484
}
8585
}
8686
}
8787
}
88-
8988
if items.is_empty() {
90-
return input.to_string();
89+
// Try removing the start token and see if it's a valid JSON in case it is already in an array format
90+
let stripped_content = input.strip_prefix(start_token).unwrap_or(input);
91+
return stripped_content.to_string();
9192
}
9293
format!("[{}]", items.join(","))
9394
}
@@ -169,6 +170,7 @@ pub fn try_tool_call_parse_json(
169170

170171
// Convert json to &str if it's a String, otherwise keep as &str
171172
let json = json.as_str();
173+
println!("JSON: {:?}", json);
172174

173175
// Anonymous function to attempt deserialization into a known representation
174176
let parse = |name: String, args: HashMap<String, Value>| -> anyhow::Result<_> {

0 commit comments

Comments
 (0)