This is an automated email from the ASF dual-hosted git repository.

arawat pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git

commit 28686165d089bef85c76fce31ac1135e508b1cb2
Author: Abhishek Rawat <[email protected]>
AuthorDate: Sun Mar 2 21:23:51 2025 -0800

    IMPALA-13817: Impala fails to start if 'ai_endpoint' and
    'ai_additional_platforms' are not set in the right order
    
    The 'ai_endpoint' flag has a validator which depends on
    'ai_additional_platforms'. This could result in failure if the
    'ai_additional_platforms' is not set before 'ai_endpoint' in the
    flagfile.
    
    This patch simplifies the validator for 'ai_endpoint' flag and moves
    the supported platforms check to ExecEnv::Init.
    
    Testing:
    - Added unit tests for 'is_api_endpoint_supported' function.
    
    Change-Id: I16480564ce434513ab73b531c03e1d229dac2dd6
    Reviewed-on: http://gerrit.cloudera.org:8080/22567
    Reviewed-by: Yida Wu <[email protected]>
    Tested-by: Impala Public Jenkins <[email protected]>
---
 be/src/exprs/ai-functions-ir.cc |  4 ++--
 be/src/exprs/ai-functions.cc    |  3 +--
 be/src/exprs/ai-functions.h     |  2 ++
 be/src/exprs/expr-test.cc       | 15 +++++++++++++++
 be/src/runtime/exec-env.cc      | 13 +++++++++++++
 5 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/be/src/exprs/ai-functions-ir.cc b/be/src/exprs/ai-functions-ir.cc
index a3a40459d..2b36e268b 100644
--- a/be/src/exprs/ai-functions-ir.cc
+++ b/be/src/exprs/ai-functions-ir.cc
@@ -62,8 +62,8 @@ const char* AiFunctions::AZURE_OPEN_AI_REQUEST_AUTH_HEADER =
 // other constants
 static const StringVal NULL_STRINGVAL = StringVal::null();
 static const char* AI_API_ENDPOINT_PREFIX = "https://";;
-static const char* OPEN_AI_AZURE_ENDPOINT = "openai.azure.com";
-static const char* OPEN_AI_PUBLIC_ENDPOINT = "api.openai.com";
+const char* AiFunctions::OPEN_AI_AZURE_ENDPOINT = "openai.azure.com";
+const char* AiFunctions::OPEN_AI_PUBLIC_ENDPOINT = "api.openai.com";
 // OPEN AI specific constants
 static const char* OPEN_AI_RESPONSE_FIELD_CHOICES = "choices";
 static const char* OPEN_AI_RESPONSE_FIELD_MESSAGE = "message";
diff --git a/be/src/exprs/ai-functions.cc b/be/src/exprs/ai-functions.cc
index a9dadf844..2e7dd6617 100644
--- a/be/src/exprs/ai-functions.cc
+++ b/be/src/exprs/ai-functions.cc
@@ -37,8 +37,7 @@ using namespace impala_udf;
 DEFINE_string(ai_endpoint, "https://api.openai.com/v1/chat/completions";,
     "The default API endpoint for an external AI engine.");
 DEFINE_validator(ai_endpoint, [](const char* name, const string& endpoint) {
-  return (impala::AiFunctions::is_api_endpoint_valid(endpoint) &&
-      impala::AiFunctions::is_api_endpoint_supported(endpoint));
+  return impala::AiFunctions::is_api_endpoint_valid(endpoint);
 });
 
 DEFINE_string(ai_model, "gpt-4", "The default AI model used by an external AI 
engine.");
diff --git a/be/src/exprs/ai-functions.h b/be/src/exprs/ai-functions.h
index d8480a93b..d72c3b54c 100644
--- a/be/src/exprs/ai-functions.h
+++ b/be/src/exprs/ai-functions.h
@@ -39,6 +39,8 @@ class AiFunctions {
   static const char* OPEN_AI_REQUEST_FIELD_CONTENT_TYPE_HEADER;
   static const char* OPEN_AI_REQUEST_AUTH_HEADER;
   static const char* AZURE_OPEN_AI_REQUEST_AUTH_HEADER;
+  static const char* OPEN_AI_AZURE_ENDPOINT;
+  static const char* OPEN_AI_PUBLIC_ENDPOINT;
   enum class AI_PLATFORM {
     /// Unsupported platform
     UNSUPPORTED,
diff --git a/be/src/exprs/expr-test.cc b/be/src/exprs/expr-test.cc
index 21be6c6a5..0662354c3 100644
--- a/be/src/exprs/expr-test.cc
+++ b/be/src/exprs/expr-test.cc
@@ -11598,6 +11598,21 @@ TEST_P(ExprTest, AiFunctionsTestAdditionalSites) {
   EXPECT_EQ(
       AiFunctions::GetAiPlatformFromEndpoint("https://AI-API.COM/v1/generate";, 
true),
       AiFunctions::AI_PLATFORM::GENERAL);
+
+  // Test for supported/unsupported endpoints.
+  EXPECT_EQ(
+      
AiFunctions::is_api_endpoint_supported("https://ai-api.com/v1/generate";), true);
+  EXPECT_EQ(
+      
AiFunctions::is_api_endpoint_supported("https://another-ai.org/completions";), 
true);
+  EXPECT_EQ(
+      AiFunctions::is_api_endpoint_supported(
+          "https://api.openai.com/v1/chat/completions";), true);
+  EXPECT_EQ(
+      AiFunctions::is_api_endpoint_supported(
+          "https://openai.azure.com/openai/deployments/";), true);
+  EXPECT_EQ(
+      
AiFunctions::is_api_endpoint_supported("https://random-ai.com/generate";), 
false);
+
 }
 
 } // namespace impala
diff --git a/be/src/runtime/exec-env.cc b/be/src/runtime/exec-env.cc
index eb614421e..bf45e42bb 100644
--- a/be/src/runtime/exec-env.cc
+++ b/be/src/runtime/exec-env.cc
@@ -157,6 +157,8 @@ DECLARE_string(debug_actions);
 DECLARE_string(ssl_client_ca_certificate);
 
 DECLARE_string(ai_api_key_jceks_secret);
+DECLARE_string(ai_endpoint);
+DECLARE_string(ai_additional_platforms);
 
 DEFINE_int32(backend_client_connection_num_retries, 3, "Retry backend 
connections.");
 // When network is unstable, TCP will retry and sending could take longer time.
@@ -535,6 +537,17 @@ Status ExecEnv::Init() {
         frontend_->GetSecretFromKeyStore(FLAGS_ai_api_key_jceks_secret, 
&api_key));
     AiFunctions::set_api_key(api_key);
   }
+  // Validate default ai_endpoint.
+  if (FLAGS_ai_endpoint != "" &&
+      !AiFunctions::is_api_endpoint_supported(FLAGS_ai_endpoint)) {
+    string supported_platforms = Substitute("$0, $1",
+        AiFunctions::OPEN_AI_AZURE_ENDPOINT, 
AiFunctions::OPEN_AI_PUBLIC_ENDPOINT);
+    if (!FLAGS_ai_additional_platforms.empty()) {
+      supported_platforms += ", " + FLAGS_ai_additional_platforms;
+    }
+    return Status(Substitute("Unsupported --ai_endpoint=$0, supported 
platforms are: $1",
+        FLAGS_ai_endpoint, supported_platforms));
+  }
 
   jwt_helper_ = new JWTHelper();
   oauth_helper_ = new JWTHelper();

Reply via email to