Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fb7d01f
Implement function calling / tools for ik_llama.cpp for Kimi K2
iSevenDays Jul 17, 2025
7f54f55
Implement basic tool choice
iSevenDays Jul 17, 2025
e9e7fe6
Backport llama.cpp tool calls support
iSevenDays Jul 20, 2025
2d46776
Enhance function calls with improved chat parser and string utilities
iSevenDays Jul 21, 2025
b88626d
Enhance function calling with unified streaming and parser improvements
iSevenDays Jul 22, 2025
3513db9
Replace hardcoded values in kimi_k2_parser.hpp with named constants
iSevenDays Jul 22, 2025
d230096
Fix duplicate common_chat_parse definition
iSevenDays Jul 22, 2025
3eff579
Fix JSON assertion failure in function call parsing
iSevenDays Jul 22, 2025
cd0392f
Merge branch 'ikawrakow:main' into function_calling
iSevenDays Jul 23, 2025
3fd9758
Add comprehensive Qwen3 XML tool calling support with unit tests
iSevenDays Jul 23, 2025
de31581
Add DeepSeek R1 function calling support with comprehensive unit tests
iSevenDays Jul 23, 2025
0272064
Add partial parsing support for JSON and regex
iSevenDays Jul 23, 2025
f38a524
Add format_chat integration tests for Qwen3 tool injection
iSevenDays Jul 23, 2025
ff6be37
Fix Qwen3 tool call parsing - pass model name to parser
iSevenDays Jul 23, 2025
8726ae5
Fix non-streaming path to use model-specific parsing
iSevenDays Jul 23, 2025
aff9de3
Update Qwen3 function call handling in server and tests
iSevenDays Jul 24, 2025
501bbe9
Merge origin/main into qwen3-function-calls branch
iSevenDays Jul 24, 2025
d42de28
Add DeepSeek-R1 function call parsing support
iSevenDays Jul 25, 2025
a493e82
Update function_calls.md documentation for DeepSeek-R1 Format 4
iSevenDays Jul 26, 2025
a80811d
Merge origin/main with DeepSeek-R1 implementation
iSevenDays Jul 26, 2025
343304a
Fix merge conflict in test-function-calls.cpp
iSevenDays Jul 26, 2025
e807b4c
Fix Qwen3 content extraction breaking code formatting
iSevenDays Jul 29, 2025
d90ac64
Merge tool-calls-bug-fix: Fix Qwen3 whitespace preservation
iSevenDays Jul 29, 2025
3d3b335
Merge deepseek-r1-parsing: Add DeepSeek R1 parsing functionality
iSevenDays Jul 29, 2025
dda98ed
Fix OpenAI content field handling to match API specification
iSevenDays Jul 29, 2025
a6222a4
Fix empty tool call IDs in OpenAI API responses
iSevenDays Jul 30, 2025
f0cdad9
Fix Kimi-K2 tool injection when first message is not system
iSevenDays Jul 31, 2025
bbfa962
Add support for <function=Name:id{...} tool call format
iSevenDays Jul 31, 2025
41be260
Add OpenAI API conversation validation and cleanup debug logging
iSevenDays Aug 2, 2025
cf25a04
Remove debug logging for tool injection checks
iSevenDays Aug 2, 2025
cd2484d
Add debug logging for AnythingLLM tool call parsing
iSevenDays Aug 7, 2025
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
85 changes: 3 additions & 82 deletions common/chat-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,90 +208,11 @@ void common_chat_msg_parser::parse_generic_format() {
}

void common_chat_msg_parser::parse_deepseek_r1_format() {
// DeepSeek R1 format supports <think> tags for reasoning content
try_parse_reasoning("<think>", "</think>");

if (!syntax_.enable_tool_calls) {
add_content(consume_rest());
return;
}

// DeepSeek R1 tool call patterns from original llama.cpp
static const common_regex tool_calls_begin("(?:<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>|<|tool\\\\_calls\\\\_begin|>|<|tool▁calls|>)");
static const common_regex tool_calls_end("<|tool▁calls▁end|>");
static const common_regex function_regex("(?:<|tool▁call▁begin|>)?function<|tool▁sep|>([^\n]+)\n```json\n");
static const common_regex close_regex("```[\\s\\r\\n]*<|tool▁call▁end|>");

parse_deepseek_r1_tool_calls(tool_calls_begin, function_regex, close_regex, tool_calls_end);
// Delegate to the main chat.cpp function which has the corrected implementation
// This follows the original llama.cpp pattern where chat-parser delegates to chat.cpp
common_chat_parse_deepseek_r1(*this);
}

void common_chat_msg_parser::parse_deepseek_r1_tool_calls(
const common_regex & tool_calls_begin,
const common_regex & function_regex,
const common_regex & close_regex,
const common_regex & tool_calls_end) {

// Helper function to wrap code as JSON arguments (ported from original llama.cpp)
auto wrap_code_as_arguments = [this](const std::string & code) -> std::string {
std::string arguments;
if (is_partial_) {
arguments = (json {{"code", code + healing_marker_}}).dump();
auto idx = arguments.find(healing_marker_);
if (idx != std::string::npos) {
arguments.resize(idx);
}
} else {
arguments = (json {{"code", code}}).dump();
}
return arguments;
};

auto parse_tool_calls = [&]() {
size_t from = std::string::npos;
while (true) {
auto res = try_find_regex(function_regex, from);
if (res) {
// Extract function name from regex group 1
std::string name = str(res->groups[1]);
from = std::string::npos;

if (name.empty()) {
from = res->groups[0].begin + 1;
continue;
}

auto maybe_raw_python = name == "python";
if (input_[pos_] == '{' || !maybe_raw_python) {
if (auto arguments = try_consume_json_with_dumped_args({{}})) {
if (!add_tool_call(name, "", arguments->value) || arguments->is_partial) {
throw common_chat_msg_partial_exception("incomplete tool call");
}
try_consume_regex(close_regex);
}
continue;
}
if (maybe_raw_python) {
auto arguments = wrap_code_as_arguments(consume_rest());
if (!add_tool_call(name, "", arguments)) {
throw common_chat_msg_partial_exception("incomplete tool call");
}
return;
}
throw common_chat_msg_partial_exception("incomplete tool call");
}
break;
}
try_consume_regex(tool_calls_end);
consume_spaces();
add_content(consume_rest());
};

if (auto res = try_find_regex(tool_calls_begin)) {
parse_tool_calls();
} else {
add_content(consume_rest());
}
}

void common_chat_msg_parser::finish() {
// Any final processing can go here
Expand Down
7 changes: 0 additions & 7 deletions common/chat-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,6 @@ class common_chat_msg_parser {
void parse_deepseek_r1_format();
void parse_generic_format();

// DeepSeek R1 specific tool call parsing
void parse_deepseek_r1_tool_calls(
const common_regex & tool_calls_begin,
const common_regex & function_regex,
const common_regex & close_regex,
const common_regex & tool_calls_end);


// JSON parsing utilities (enhanced streaming support)
struct json_parse_result {
Expand Down
Loading