@@ -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
690701Json::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 ;
0 commit comments