Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 965a0c8

Browse files
committed
fix: api key template on engine level
1 parent ea81b18 commit 965a0c8

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

engine/cortex-common/remote_enginei.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ class RemoteEngineI {
3434

3535
// Get available remote models
3636
virtual Json::Value GetRemoteModels(const std::string& url,
37-
const std::string& api_key) = 0;
37+
const std::string& api_key,
38+
const std::string& api_key_template) = 0;
3839
};

engine/extensions/remote-engine/remote_engine.cc

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ CurlResponse RemoteEngine::MakeStreamingChatCompletionRequest(
107107

108108
struct curl_slist* headers = nullptr;
109109
if (!config.api_key.empty()) {
110-
headers = curl_slist_append(headers, api_key_template_.c_str());
110+
headers = curl_slist_append(headers, api_key_header_.c_str());
111111
}
112112

113113
if (is_anthropic(config.model)) {
@@ -199,8 +199,9 @@ RemoteEngine::ModelConfig* RemoteEngine::GetModelConfig(
199199
return nullptr;
200200
}
201201

202-
CurlResponse RemoteEngine::MakeGetModelsRequest(const std::string& url,
203-
const std::string& api_key) {
202+
CurlResponse RemoteEngine::MakeGetModelsRequest(
203+
const std::string& url, const std::string& api_key,
204+
const std::string& api_key_template) {
204205
CURL* curl = curl_easy_init();
205206
CurlResponse response;
206207

@@ -210,7 +211,9 @@ CurlResponse RemoteEngine::MakeGetModelsRequest(const std::string& url,
210211
return response;
211212
}
212213

213-
std::string api_key_header = "Authorization: Bearer " + api_key;
214+
std::string api_key_header =
215+
ReplaceApiKeyPlaceholder(api_key_template, api_key);
216+
214217
struct curl_slist* headers = nullptr;
215218
headers = curl_slist_append(headers, api_key_header.c_str());
216219
headers = curl_slist_append(headers, "Content-Type: application/json");
@@ -251,7 +254,7 @@ CurlResponse RemoteEngine::MakeChatCompletionRequest(
251254

252255
struct curl_slist* headers = nullptr;
253256
if (!config.api_key.empty()) {
254-
headers = curl_slist_append(headers, api_key_template_.c_str());
257+
headers = curl_slist_append(headers, api_key_header_.c_str());
255258
}
256259

257260
if (is_anthropic(config.model)) {
@@ -310,7 +313,7 @@ bool RemoteEngine::LoadModelConfig(const std::string& model,
310313
// model_config.url = ;
311314
// Optional fields
312315
if (config["api_key_template"]) {
313-
api_key_template_ = ReplaceApiKeyPlaceholder(
316+
api_key_header_ = ReplaceApiKeyPlaceholder(
314317
config["api_key_template"].as<std::string>(), api_key);
315318
}
316319
if (config["transform_req"]) {
@@ -393,17 +396,6 @@ void RemoteEngine::LoadModel(
393396
const std::string& model_path = (*json_body)["model_path"].asString();
394397
const std::string& api_key = (*json_body)["api_key"].asString();
395398

396-
if (!LoadModelConfig(model, model_path, api_key)) {
397-
Json::Value error;
398-
error["error"] = "Failed to load model configuration";
399-
Json::Value status;
400-
status["is_done"] = true;
401-
status["has_error"] = true;
402-
status["is_stream"] = false;
403-
status["status_code"] = k500InternalServerError;
404-
callback(std::move(status), std::move(error));
405-
return;
406-
}
407399
if (json_body->isMember("metadata")) {
408400
metadata_ = (*json_body)["metadata"];
409401
if (!metadata_["transform_req"].isNull() &&
@@ -424,6 +416,25 @@ void RemoteEngine::LoadModel(
424416
}
425417
}
426418

419+
if (!LoadModelConfig(model, model_path, api_key)) {
420+
Json::Value error;
421+
error["error"] = "Failed to load model configuration";
422+
Json::Value status;
423+
status["is_done"] = true;
424+
status["has_error"] = true;
425+
status["is_stream"] = false;
426+
status["status_code"] = k500InternalServerError;
427+
callback(std::move(status), std::move(error));
428+
return;
429+
}
430+
431+
if (json_body->isMember("metadata")) {
432+
if (!metadata_["api_key_template"].isNull()) {
433+
api_key_header_ = ReplaceApiKeyPlaceholder(
434+
metadata_["api_key_template"].asString(), api_key);
435+
}
436+
}
437+
427438
Json::Value response;
428439
response["status"] = "Model loaded successfully";
429440
Json::Value status;
@@ -688,7 +699,8 @@ void RemoteEngine::HandleEmbedding(
688699
}
689700

690701
Json::Value RemoteEngine::GetRemoteModels(const std::string& url,
691-
const std::string& api_key) {
702+
const std::string& api_key,
703+
const std::string& api_key_template) {
692704
if (url.empty()) {
693705
if (engine_name_ == kAnthropicEngine) {
694706
Json::Value json_resp;
@@ -710,7 +722,7 @@ Json::Value RemoteEngine::GetRemoteModels(const std::string& url,
710722
return Json::Value();
711723
}
712724
} else {
713-
auto response = MakeGetModelsRequest(url, api_key);
725+
auto response = MakeGetModelsRequest(url, api_key, api_key_template);
714726
if (response.error) {
715727
Json::Value error;
716728
error["error"] = response.error_message;

engine/extensions/remote-engine/remote_engine.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RemoteEngine : public RemoteEngineI {
5050
Json::Value metadata_;
5151
std::string chat_req_template_;
5252
std::string chat_res_template_;
53-
std::string api_key_template_;
53+
std::string api_key_header_;
5454
std::string engine_name_;
5555

5656
// Helper functions
@@ -61,7 +61,8 @@ class RemoteEngine : public RemoteEngineI {
6161
const ModelConfig& config, const std::string& body,
6262
const std::function<void(Json::Value&&, Json::Value&&)>& callback);
6363
CurlResponse MakeGetModelsRequest(const std::string& url,
64-
const std::string& api_key);
64+
const std::string& api_key,
65+
const std::string& api_key_template);
6566

6667
// Internal model management
6768
bool LoadModelConfig(const std::string& model, const std::string& yaml_path,
@@ -99,7 +100,8 @@ class RemoteEngine : public RemoteEngineI {
99100
std::function<void(Json::Value&&, Json::Value&&)>&& callback) override;
100101

101102
Json::Value GetRemoteModels(const std::string& url,
102-
const std::string& api_key) override;
103+
const std::string& api_key,
104+
const std::string& api_key_template) override;
103105
};
104106

105107
} // namespace remote_engine

engine/services/engine_service.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,9 @@ cpp::result<Json::Value, std::string> EngineService::GetRemoteModels(
10771077
auto& e = std::get<RemoteEngineI*>(engines_[engine_name].engine);
10781078
auto url = remote_engine_json["metadata"]["get_models_url"].asString();
10791079
auto api_key = remote_engine_json["api_key"].asString();
1080-
auto res = e->GetRemoteModels(url, api_key);
1080+
auto api_key_template =
1081+
remote_engine_json["metadata"]["api_key_template"].asString();
1082+
auto res = e->GetRemoteModels(url, api_key, api_key_template);
10811083
if (!res["error"].isNull()) {
10821084
return cpp::fail(res["error"].asString());
10831085
} else {
@@ -1094,6 +1096,7 @@ bool EngineService::IsRemoteEngine(const std::string& engine_name) {
10941096
}
10951097
return true;
10961098
}
1099+
10971100
cpp::result<bool, std::string> EngineService::GenerateRemoteModel(
10981101
const std::string& engine_name) {
10991102
namespace fmu = file_manager_utils;
@@ -1112,7 +1115,9 @@ cpp::result<bool, std::string> EngineService::GenerateRemoteModel(
11121115
auto& e = std::get<RemoteEngineI*>(engines_[engine_name].engine);
11131116
auto url = remote_engine_json["metadata"]["get_models_url"].asString();
11141117
auto api_key = remote_engine_json["api_key"].asString();
1115-
auto res = e->GetRemoteModels(url, api_key);
1118+
auto api_key_template =
1119+
remote_engine_json["metadata"]["api_key_template"].asString();
1120+
auto res = e->GetRemoteModels(url, api_key, api_key_template);
11161121
if (!res["error"].isNull()) {
11171122
return cpp::fail(res["error"].asString());
11181123
} else {

0 commit comments

Comments
 (0)