Skip to content

chore: function calling cleanup #2195

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

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
30 changes: 24 additions & 6 deletions docs/docs/guides/function-calling.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ tools = [

completion_payload = {
"messages": [
{"role": "system", "content": "You are a helpful customer support assistant. Use the supplied tools to assist the user."},
{"role": "user", "content": "Hi, can you tell me the delivery date for my order?"},
{
"role": "system",
"content": 'You have access to the following CUSTOM functions:\n\n<CUSTOM_FUNCTIONS>\n\nIf a you choose to call a function ONLY reply in the following format:\n<{start_tag}={function_name}>{parameters}{end_tag}\nwhere\n\nstart_tag => `<function`\nparameters => a JSON dict with the function argument name as key and function argument value as value.\nend_tag => `</function>`\n\nHere is an example,\n<function=example_function_name>{"example_name": "example_value"}</function>\n\nReminder:\n- Function calls MUST follow the specified format\n- Required parameters MUST be specified\n- You can call one or more functions at a time, but remember only chose correct function\n- Put the entire function call reply on one line\n- Always add your sources when using search results to answer the user query\n- If you can not find correct parameters or arguments corresponding to function in the user\'s message, ask user again to provide, do not make assumptions.\n- No explanation are needed when calling a function.\n\nYou are a helpful assistant.',
},
{
"role": "user",
"content": "Hi, can you tell me the delivery date for my order?"
},
]
}

Expand Down Expand Up @@ -126,10 +132,22 @@ Once the user provides their order ID:
```python
completion_payload = {
"messages": [
{"role": "system", "content": "You are a helpful customer support assistant. Use the supplied tools to assist the user."},
{"role": "user", "content": "Hi, can you tell me the delivery date for my order?"},
{"role": "assistant", "content": "Of course! Please provide your order ID so I can look it up."},
{"role": "user", "content": "i think it is order_70705"},
{
"role": "system",
"content": 'You have access to the following CUSTOM functions:\n\n<CUSTOM_FUNCTIONS>\n\nIf a you choose to call a function ONLY reply in the following format:\n<{start_tag}={function_name}>{parameters}{end_tag}\nwhere\n\nstart_tag => `<function`\nparameters => a JSON dict with the function argument name as key and function argument value as value.\nend_tag => `</function>`\n\nHere is an example,\n<function=example_function_name>{"example_name": "example_value"}</function>\n\nReminder:\n- Function calls MUST follow the specified format\n- Required parameters MUST be specified\n- You can call one or more functions at a time, but remember only chose correct function\n- Put the entire function call reply on one line\n- Always add your sources when using search results to answer the user query\n- If you can not find correct parameters or arguments corresponding to function in the user\'s message, ask user again to provide, do not make assumptions.\n- No explanation are needed when calling a function.\n\nYou are a helpful assistant.',
},
{
"role": "user",
"content": "Hi, can you tell me the delivery date for my order?"
},
{
"role": "assistant",
"content": "Of course! Please provide your order ID so I can look it up."
},
{
"role": "user",
"content": "i think it is order_70705"
},
]
}

Expand Down
1 change: 0 additions & 1 deletion engine/controllers/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ void server::ProcessStreamRes(std::function<void(const HttpResponsePtr&)> cb,
void server::ProcessNonStreamRes(std::function<void(const HttpResponsePtr&)> cb,
SyncQueue& q) {
auto [status, res] = q.wait_and_pop();
function_calling_utils::PostProcessResponse(res);
LOG_DEBUG << "response: " << res.toStyledString();
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
resp->setStatusCode(
Expand Down
1 change: 1 addition & 0 deletions engine/extensions/local-engine/local_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ void LocalEngine::LoadModel(std::shared_ptr<Json::Value> json_body,

params.push_back("--pooling");
params.push_back("mean");
params.push_back("--jinja");

std::vector<std::string> v;
v.reserve(params.size() + 1);
Expand Down
2 changes: 0 additions & 2 deletions engine/services/inference_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ cpp::result<void, InferResult> InferenceService::HandleChatCompletion(
engine_type = (*(json_body)).get("engine", kLlamaRepo).asString();
}
CTL_DBG("engine_type: " << engine_type);
function_calling_utils::PreprocessRequest(json_body);
CTL_DBG("engine_type: " << engine_type);
auto tool_choice = json_body->get("tool_choice", Json::Value::null);
auto model_id = json_body->get("model", "").asString();
if (saved_models_.find(model_id) != saved_models_.end()) {
Expand Down
157 changes: 0 additions & 157 deletions engine/test/components/test_function_calling.cc

This file was deleted.

26 changes: 15 additions & 11 deletions engine/utils/cli_selection_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ inline void PrintMenu(

inline std::optional<int> GetNumericValue(const std::string& sval) {
try {
return std::stoi(sval);
return std::stoi(sval);
} catch (const std::invalid_argument&) {
// Not a valid number
return std::nullopt;
// Not a valid number
return std::nullopt;
} catch (const std::out_of_range&) {
// Number out of range
return std::nullopt;
// Number out of range
return std::nullopt;
}
}

Expand Down Expand Up @@ -73,14 +73,16 @@ inline std::optional<std::string> PrintModelSelection(
}

// Validate if the selection consists solely of numeric characters
if(!std::all_of(selection.begin(), selection.end(), ::isdigit)){
if (!std::all_of(selection.begin(), selection.end(), ::isdigit)) {
return std::nullopt;
}

// deal with out of range numeric values
std::optional<int> numeric_value = GetNumericValue(selection);

if (!numeric_value.has_value() || (unsigned) numeric_value.value() > availables.size() || numeric_value.value() < 1) {

if (!numeric_value.has_value() ||
(unsigned)numeric_value.value() > availables.size() ||
numeric_value.value() < 1) {
return std::nullopt;
}

Expand All @@ -101,13 +103,15 @@ inline std::optional<std::string> PrintSelection(
}

// Validate if the selection consists solely of numeric characters
if(!std::all_of(selection.begin(), selection.end(), ::isdigit)){
if (!std::all_of(selection.begin(), selection.end(), ::isdigit)) {
return std::nullopt;
}

// deal with out of range numeric values
std::optional<int> numeric_value = GetNumericValue(selection);
if (!numeric_value.has_value() ||(unsigned) numeric_value.value() > options.size() || numeric_value.value() < 1) {
if (!numeric_value.has_value() ||

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break this down into individiual booleans and then compare them.
Like
const bool has_value = !numeric_value.has_value() etc

(unsigned)numeric_value.value() > options.size() ||
numeric_value.value() < 1) {
return std::nullopt;
}

Expand Down
Loading
Loading