Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cb8f25f
chore: change update to patch
vansangpfiev Oct 7, 2024
7f0349c
fix: swagger
vansangpfiev Oct 7, 2024
8d8846d
fix: pull api
vansangpfiev Oct 7, 2024
1a97362
Merge branch 'dev' into fix/pull-api
vansangpfiev Oct 7, 2024
dda27e1
Merge branch 'dev' of github.com:janhq/cortex.cpp into dev
nguyenhoangthuan99 Oct 14, 2024
d626c52
chore: refactor server controller
vansangpfiev Oct 14, 2024
48eb3bd
Merge branch 'dev' of github.com:janhq/nitro into chore/refactor-server
vansangpfiev Oct 14, 2024
472f716
fix: update status
vansangpfiev Oct 14, 2024
b32fac7
Merge branch 'dev' of github.com:janhq/cortex.cpp into dev
nguyenhoangthuan99 Oct 14, 2024
609d54a
Merge branch 'chore/refactor-server' of github.com:janhq/cortex.cpp i…
nguyenhoangthuan99 Oct 14, 2024
6fd4696
feat: mimic openai function calling api with llama3.1
nguyenhoangthuan99 Oct 14, 2024
35104fb
fix: conflict
nguyenhoangthuan99 Oct 14, 2024
7e39c78
chore: remove unnecessary cout
nguyenhoangthuan99 Oct 14, 2024
f2655cb
feat: add tool choice option to api
nguyenhoangthuan99 Oct 15, 2024
0eb1369
feat: add unitest
nguyenhoangthuan99 Oct 15, 2024
a353e4c
Merge branch 'dev' into feat/function-calling
nguyenhoangthuan99 Oct 15, 2024
97b8e5d
chore: format code
nguyenhoangthuan99 Oct 15, 2024
0d3ce8a
Merge branch 'feat/function-calling' of github.com:janhq/cortex.cpp i…
nguyenhoangthuan99 Oct 15, 2024
cea10a6
Merge branch 'dev' of github.com:janhq/cortex.cpp into feat/function-…
nguyenhoangthuan99 Oct 16, 2024
7c78344
feat: function calling in user message
nguyenhoangthuan99 Oct 16, 2024
e18d29c
Merge branch 'dev' into feat/function-calling
nguyenhoangthuan99 Oct 16, 2024
c11aad2
Update inference_service.cc
nguyenhoangthuan99 Oct 16, 2024
812fad1
Merge branch 'dev' into feat/function-calling
nguyenhoangthuan99 Oct 16, 2024
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
11 changes: 11 additions & 0 deletions engine/controllers/swagger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,21 @@ Json::Value SwaggerController::generateOpenAPISpec() {
"#/components/schemas/ChatMessage";
schemas["ChatCompletionRequest"]["properties"]["stream"]["type"] = "boolean";
schemas["ChatCompletionRequest"]["properties"]["engine"]["type"] = "string";
schemas["ChatCompletionRequest"]["properties"]["tools"]["type"] = "array";
schemas["ChatCompletionRequest"]["properties"]["tools"]["items"]["$ref"] =
"#/components/schemas/ToolsCall";
schemas["ChatCompletionRequest"]["properties"]["tools_call_in_user_message"]
["type"] = "boolean";
schemas["ChatCompletionRequest"]["properties"]["tools_call_in_user_message"]
["default"] = false;
schemas["ToolsCall"]["type"] = "object";

schemas["ChatMessage"]["type"] = "object";
schemas["ChatMessage"]["properties"]["role"]["type"] = "string";
schemas["ChatMessage"]["properties"]["content"]["type"] = "string";
schemas["ChatMessage"]["properties"]["tools"]["type"] = "array";
schemas["ChatMessage"]["properties"]["tools"]["items"]["$ref"] =
"#/components/schemas/ToolsCall";

schemas["ChatCompletionResponse"]["type"] = "object";
// Add properties based on your implementation
Expand Down
2 changes: 1 addition & 1 deletion engine/services/inference_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,4 @@ bool InferenceService::HasFieldInReq(std::shared_ptr<Json::Value> json_body,
}
return true;
}
} // namespace services
} // namespace services
48 changes: 33 additions & 15 deletions engine/utils/function_calling/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ inline std::string ReplaceCustomFunctions(const std::string& original,
}

inline bool HasTools(const std::shared_ptr<Json::Value>& request) {
return request->isMember("tools") && (*request)["tools"].isArray() &&
(*request)["tools"].size() > 0;
return (request->isMember("tools") && (*request)["tools"].isArray() &&
(*request)["tools"].size() > 0) ||
request->get("tools_call_in_user_message", false).asBool();
}

inline std::string ProcessTools(const std::shared_ptr<Json::Value>& request) {
Expand Down Expand Up @@ -149,7 +150,7 @@ inline void UpdateMessages(std::string& system_prompt,
Json::Value tool_choice = request->get("tool_choice", "auto");
if (tool_choice.isString() && tool_choice.asString() == "required") {
system_prompt +=
"\n\nYou must use a function to answer the user's question.";
"\n\nYou must call a function to answer the user's question.";
} else if (!tool_choice.isString()) {

system_prompt +=
Expand All @@ -158,10 +159,14 @@ inline void UpdateMessages(std::string& system_prompt,
"' to answer the user's question.";
}

bool tools_call_in_user_message =
request->get("tools_call_in_user_message", false).asBool();

bool original_stream_config = (*request).get("stream", false).asBool();
// (*request)["grammar"] = function_calling_utils::gamma_json;
(*request)["stream"] =
false; //when using function calling, disable stream automatically because we need to parse the response to get function name and params

if (!request->isMember("messages") || !(*request)["messages"].isArray() ||
(*request)["messages"].empty()) {
// If no messages, add the system prompt as the first message
Expand All @@ -170,21 +175,34 @@ inline void UpdateMessages(std::string& system_prompt,
systemMessage["content"] = system_prompt;
(*request)["messages"].append(systemMessage);
} else {
Json::Value& firstMessage = (*request)["messages"][0];
if (firstMessage["role"] == "system") {
bool addCustomPrompt =
request->get("add_custom_system_prompt", true).asBool();
if (addCustomPrompt) {
firstMessage["content"] =
system_prompt + "\n" + firstMessage["content"].asString();

if (tools_call_in_user_message) {
for (Json::Value& message : (*request)["messages"]) {
if (message["role"] == "user" && message.isMember("tools") &&
message["tools"].isArray() && message["tools"].size() > 0) {
message["content"] = system_prompt + "\n User question: " +
message["content"].asString();
}
}
} else {
// If the first message is not a system message, prepend the system prompt
Json::Value systemMessage;
systemMessage["role"] = "system";
systemMessage["content"] = system_prompt;
(*request)["messages"].insert(0, systemMessage);
Json::Value& firstMessage = (*request)["messages"][0];
if (firstMessage["role"] == "system") {
bool addCustomPrompt =
request->get("add_custom_system_prompt", true).asBool();
if (addCustomPrompt) {
firstMessage["content"] =
system_prompt + "\n" + firstMessage["content"].asString();
}
} else {
// If the first message is not a system message, prepend the system prompt
Json::Value systemMessage;
systemMessage["role"] = "system";
systemMessage["content"] = system_prompt;
(*request)["messages"].insert(0, systemMessage);
}
}

// transform last message role to tool if it is a function call
Json::Value& lastMessage =
(*request)["messages"][(*request)["messages"].size() - 1];
if (lastMessage.get("role", "") == "tool") {
Expand Down
Loading