-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
server : add Hermes-3 tool call support (WIP) #9254
Draft
ngxson
wants to merge
3
commits into
ggerganov:master
Choose a base branch
from
ngxson:xsn/tool_call
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+281
−63
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#pragma once | ||
|
||
#include "llama.h" | ||
#include "common.h" | ||
#include "utils.hpp" | ||
|
||
// Change JSON_ASSERT from assert() to GGML_ASSERT: | ||
#define JSON_ASSERT GGML_ASSERT | ||
#include "json.hpp" | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <sstream> | ||
|
||
using json = nlohmann::ordered_json; | ||
|
||
enum llama_tool_format { | ||
LLAMA_TOOL_FORMAT_NOT_SUPPORTED, | ||
LLAMA_TOOL_FORMAT_HERMES_3, | ||
}; | ||
|
||
enum llama_response_state { | ||
LLAMA_RESPONSE_STATE_UNKNOWN, | ||
LLAMA_RESPONSE_STATE_TEXT, | ||
LLAMA_RESPONSE_STATE_TOOL_CALL, | ||
}; | ||
|
||
// get the tool call format for the loaded model | ||
// this function does linear search, so do not call it repeatedly | ||
inline enum llama_tool_format get_tool_format(const struct llama_context * ctx) { | ||
auto model = llama_get_model(ctx); | ||
auto has_token = [&](std::string piece) { | ||
for (int i = 0; i < llama_n_vocab(model); i++) { | ||
const std::string token_str = llama_token_to_piece(ctx, i, true); | ||
if (token_str == piece) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
if (has_token("<|im_start|>") && has_token("<tool_call>")) { | ||
return LLAMA_TOOL_FORMAT_HERMES_3; | ||
} | ||
return LLAMA_TOOL_FORMAT_NOT_SUPPORTED; | ||
} | ||
|
||
inline std::string format_chat_with_tool(enum llama_tool_format format, const std::vector<json> & messages, json tools) { | ||
if (!tools.is_array()) { | ||
throw std::runtime_error("tools must be an array"); | ||
} | ||
std::stringstream ss; | ||
auto chat = parse_chat_messages(messages); | ||
if (format == LLAMA_TOOL_FORMAT_HERMES_3) { | ||
ss << "<|im_start|>system\n\n"; | ||
ss << "You are a function calling AI model. You are provided with function signatures within <tools></tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. Here are the available tools: <tools>\n\n"; | ||
for (auto tool : tools) { | ||
ss << tool.dump(1, '\t') << "\n\n"; | ||
} | ||
ss << "</tools> Use the following pydantic model json schema for each tool call you will make: {\"properties\": {\"arguments\": {\"title\": \"Arguments\", \"type\": \"object\"}, \"name\": {\"title\": \"Name\", \"type\": \"string\"}}, \"required\": [\"arguments\", \"name\"], \"title\": \"FunctionCall\", \"type\": \"object\"} For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:\n"; | ||
ss << "<tool_call>\n"; | ||
ss << "{\"arguments\": <args-dict>, \"name\": <function-name>}\n"; | ||
ss << "</tool_call><|im_end|>\n"; | ||
for (auto & message : chat) { | ||
std::string role(message.role); | ||
if (role == "system") { | ||
continue; // for optimal performance, we skip user-defined system message | ||
} | ||
ss << "<|im_start|>" << role << "\n\n"; | ||
if (role == "tool") { | ||
ss << "<tool_response>\n" << string_strip(message.content) << "\n</tool_response>\n"; | ||
} else { | ||
ss << string_strip(message.content) << "<|im_end|>\n"; | ||
} | ||
} | ||
ss << "<|im_start|>assistant\n\n"; | ||
} else { | ||
throw std::runtime_error("tool_call is not supported by this model"); | ||
} | ||
LOG_VERBOSE("format_chat_with_tool", {{"text", ss.str()}}); | ||
return ss.str(); | ||
} | ||
|
||
// check if the response is text or tool_call | ||
// if it is tool_call, we may have to disable streaming, because we must parse the whole JSON response | ||
inline enum llama_response_state check_response_state(enum llama_tool_format format, const std::string & generated_text) { | ||
if (format == LLAMA_TOOL_FORMAT_NOT_SUPPORTED) { | ||
return LLAMA_RESPONSE_STATE_TEXT; | ||
} else if (format == LLAMA_TOOL_FORMAT_HERMES_3 && generated_text.rfind("<tool_call>", 0) == 0) { | ||
return LLAMA_RESPONSE_STATE_TOOL_CALL; | ||
} | ||
return LLAMA_RESPONSE_STATE_TEXT; | ||
} | ||
|
||
// convert model's response to OAI format | ||
inline json parse_tool_response(enum llama_tool_format format, const std::string & generated_text) { | ||
if (format == LLAMA_TOOL_FORMAT_NOT_SUPPORTED) { | ||
return json{}; | ||
} else if (format == LLAMA_TOOL_FORMAT_HERMES_3) { | ||
std::string tmp(generated_text); | ||
string_replace_all(tmp, "<tool_call>", ""); | ||
string_replace_all(tmp, "</tool_call>", ""); | ||
json tool = json::parse(tmp); | ||
std::vector<json> tool_calls = {json{ | ||
{"id", tool.at("name")}, | ||
{"type", "function"}, | ||
{"function", { | ||
{"name", tool.at("name")}, | ||
{"arguments", tool.at("arguments").dump()}, // OAI requires this to be JSON-stringified | ||
}}, | ||
}}; | ||
return tool_calls; | ||
} | ||
return generated_text; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the tabulations? They are increasing the number of tokens, but I think they do not provide useful information.