@@ -17,46 +17,74 @@ limitations under the License.
1717
1818#include < iostream>
1919#include < stdexcept>
20+ #include < unordered_map>
2021
2122#include " core/util/uuid.h"
2223#include " deepseekv3_detector.h"
2324#include " glm45_detector.h"
2425#include " glm47_detector.h"
2526#include " kimik2_detector.h"
2627#include " qwen25_detector.h"
28+
2729namespace xllm {
2830namespace function_call {
2931
30- const std::unordered_map<std::string, std::string>
31- FunctionCallParser::kToolCallParserMap = {
32- {" qwen25" , " qwen25" },
33- {" qwen3" , " qwen25" },
34- {" kimi_k2" , " kimi_k2" },
35- {" deepseekv3" , " deepseekv3" },
36- {" glm45" , " glm45" },
37- {" glm47" , " glm47" },
38- // TODO
39- // {"llama3", "llama3"},
40- // {"mistral", "mistral"},
41- // {"pythonic", "pythonic"},
42- // {"qwen3_coder", "qwen3_coder"},
43- // {"step3", "step3"},
32+ namespace {
33+
34+ const std::unordered_map<std::string, std::vector<std::string>>
35+ AutoToolCallParserMap = {
36+ {" qwen25" , {" qwen2" , " qwen3" }},
37+ {" kimi_k2" , {" kimi_k2" }},
38+ {" deepseekv3" , {" deepseek_v3" }},
39+ {" glm45" , {" glm4_moe" }},
40+ {" glm47" , {" glm4_moe" }},
4441};
4542
43+ } // namespace
44+
45+ std::string FunctionCallParser::get_parser_auto (const std::string& parser,
46+ const std::string& model_type) {
47+ if (parser.empty ()) {
48+ return " " ;
49+ }
50+ if (parser == " auto" ) {
51+ // find the tool call parser that supports the model type
52+ for (const auto & [key, value] : AutoToolCallParserMap) {
53+ if (std::find (value.begin (), value.end (), model_type) != value.end ()) {
54+ LOG (INFO) << " Using tool call parser: " << key
55+ << " for model type: " << model_type;
56+ return key;
57+ }
58+ }
59+ LOG (FATAL) << " Unsupported model type for auto tool call parser: "
60+ << model_type;
61+ return " " ;
62+ } else {
63+ // check if the tool call parser is supported
64+ if (parser == " qwen2" || parser == " qwen3" ) {
65+ return " qwen25" ;
66+ }
67+ if (AutoToolCallParserMap.find (parser) != AutoToolCallParserMap.end ()) {
68+ return parser;
69+ }
70+ LOG (FATAL) << " Unsupported tool call parser: " << parser
71+ << " . Supported parsers are: " << []() {
72+ std::string supported = " qwen2, qwen3" ;
73+ for (const auto & [key, value] : AutoToolCallParserMap) {
74+ supported += " , " + key;
75+ }
76+ return supported;
77+ }();
78+ return " " ;
79+ }
80+ }
81+
4682FunctionCallParser::FunctionCallParser (const std::vector<JsonTool>& tools,
4783 const std::string& tool_call_parser)
4884 : tools_(tools) {
4985 detector_ = create_detector (tool_call_parser);
5086 CHECK (detector_ != nullptr )
51- << " Unsupported tool_call_parser: " << tool_call_parser
52- << " . Supported parsers are: " << [this ]() {
53- std::string supported;
54- for (const auto & [key, value] : kToolCallParserMap ) {
55- if (!supported.empty ()) supported += " , " ;
56- supported += key;
57- }
58- return supported;
59- }();
87+ << " Unsupported tool_call_parser: " << tool_call_parser;
6088}
6189
6290bool FunctionCallParser::has_tool_call (const std::string& text) const {
@@ -82,38 +110,26 @@ StreamingParseResult FunctionCallParser::parse_streaming_increment(
82110
83111std::unique_ptr<BaseFormatDetector> FunctionCallParser::create_detector (
84112 const std::string& tool_call_parser) {
85- auto it = kToolCallParserMap .find (tool_call_parser);
86- if (it == kToolCallParserMap .end ()) {
113+ if (tool_call_parser.empty ()) {
87114 return nullptr ;
88115 }
89116
90- if (it-> second == " qwen25" ) {
117+ if (tool_call_parser == " qwen25" ) {
91118 return std::make_unique<Qwen25Detector>();
92119 }
93-
94- if (it->second == " kimi_k2" ) {
120+ if (tool_call_parser == " kimi_k2" ) {
95121 return std::make_unique<KimiK2Detector>();
96122 }
97-
98- if (it->second == " deepseekv3" ) {
123+ if (tool_call_parser == " deepseekv3" ) {
99124 return std::make_unique<DeepSeekV3Detector>();
100125 }
101-
102- if (it->second == " glm45" ) {
126+ if (tool_call_parser == " glm45" ) {
103127 return std::make_unique<Glm45Detector>();
104128 }
105-
106- if (it->second == " glm47" ) {
129+ if (tool_call_parser == " glm47" ) {
107130 return std::make_unique<Glm47Detector>();
108131 }
109-
110- // if (tool_call_parser == "llama3") {
111- // return std::make_unique<Llama32Detector>();
112- // }
113- // if (tool_call_parser == "mistral") {
114- // return std::make_unique<MistralDetector>();
115- // }
116-
132+ LOG (ERROR) << " Unsupported tool call parser: " << tool_call_parser;
117133 return nullptr ;
118134}
119135
0 commit comments