linrrzqqq commented on code in PR #62494:
URL: https://github.com/apache/doris/pull/62494#discussion_r3091979697


##########
be/src/exprs/function/ai/ai_functions.h:
##########
@@ -211,77 +213,182 @@ class AIFunction : public IFunction {
                                               });
     }
 
-    // Wrapper for executing a single LLM request
-    Status execute_single_request(const std::string& input, std::string& 
result,
-                                  const TAIResource& config, 
std::shared_ptr<AIAdapter>& adapter,
-                                  FunctionContext* context) const {
-        std::vector<std::string> inputs = {input};
-        std::vector<std::string> results;
+    // Provider-reusable helper for string-returning functions.
+    // Estimates one batch entry size using the raw prompt length plus the 
fixed JSON wrapper cost.
+    size_t estimate_batch_entry_size(size_t idx, const std::string& prompt) 
const {
+        static constexpr size_t json_wrapper_size = 20;
+        return prompt.size() + std::to_string(idx).size() + json_wrapper_size;
+    }
+
+    // Provider-reusable helper for string-returning functions.
+    // Executes one batch request and parses the provider result into one 
string per input row.
+    Status execute_batch_request(const std::vector<std::string>& batch_prompts,
+                                 std::vector<std::string>& results, const 
TAIResource& config,
+                                 std::shared_ptr<AIAdapter>& adapter,
+                                 FunctionContext* context) const {
+#ifdef BE_TEST
+        const char* test_result = std::getenv("AI_TEST_RESULT");
+        if (test_result != nullptr) {
+            std::vector<std::string> parsed_test_response;
+            RETURN_IF_ERROR(
+                    adapter->parse_response(std::string(test_result), 
parsed_test_response));
+            if (parsed_test_response.empty()) {
+                return Status::InternalError("AI returned empty result");
+            }
+            if (parsed_test_response.size() != batch_prompts.size()) {
+                return Status::RuntimeError(
+                        "Failed to parse {} batch result, expected {} items 
but got {}", get_name(),
+                        batch_prompts.size(), parsed_test_response.size());
+            }
+            results = std::move(parsed_test_response);
+            return Status::OK();
+        }
+        if (config.provider_type == "MOCK") {
+            results.clear();
+            results.reserve(batch_prompts.size());
+            for (const auto& prompt : batch_prompts) {
+                results.emplace_back("this is a mock response. " + prompt);
+            }
+            return Status::OK();
+        }
+#endif
+
+        std::string batch_prompt;
+        RETURN_IF_ERROR(build_batch_prompt(batch_prompts, batch_prompt));
+
+        std::vector<std::string> inputs = {batch_prompt};
+        std::vector<std::string> parsed_response;
 
         std::string request_body;
         RETURN_IF_ERROR(adapter->build_request_payload(
                 inputs, assert_cast<const Derived&>(*this).system_prompt, 
request_body));
 
         std::string response;
-        if (config.provider_type == "MOCK") {
-            // Mock path for UT
-            response = "this is a mock response. " + input;
-        } else {
-            RETURN_IF_ERROR(send_request_to_llm(request_body, response, 
config, adapter, context));
-        }
-
-        RETURN_IF_ERROR(adapter->parse_response(response, results));
-        if (results.empty()) {
+        RETURN_IF_ERROR(send_request_to_llm(request_body, response, config, 
adapter, context));
+        RETURN_IF_ERROR(adapter->parse_response(response, parsed_response));
+        if (parsed_response.empty()) {
             return Status::InternalError("AI returned empty result");
         }
-
-        result = std::move(results[0]);
+        if (parsed_response.size() != batch_prompts.size()) {
+            return Status::RuntimeError(
+                    "Failed to parse {} batch result, expected {} items but 
got {}", get_name(),
+                    batch_prompts.size(), parsed_response.size());
+        }
+        results = std::move(parsed_response);
         return Status::OK();
     }
 
-    Status execute_single_request(const std::string& input, 
std::vector<float>& result,
-                                  const TAIResource& config, 
std::shared_ptr<AIAdapter>& adapter,
-                                  FunctionContext* context) const {
-        std::vector<std::string> inputs = {input};
-        std::vector<std::vector<float>> results;
+    // Provider-reusable helper for string-returning functions.
+    // Runs the common batch execution flow; derived classes only need to 
define how one batch of
+    // string results is inserted into the final output column.
+    Status execute_batched_prompts(FunctionContext* context, Block& block,
+                                   const ColumnNumbers& arguments, size_t 
input_rows_count,
+                                   const TAIResource& config, 
std::shared_ptr<AIAdapter>& adapter,
+                                   IColumn& col_result) const {
+        std::vector<std::string> batch_prompts;
+        size_t current_batch_size = 2; // []
+        const size_t max_batch_prompt_size =
+                static_cast<size_t>(get_ai_context_window_size(context));
 
-        std::string request_body;
-        RETURN_IF_ERROR(adapter->build_embedding_request(inputs, 
request_body));
+        for (size_t i = 0; i < input_rows_count; ++i) {
+            std::string prompt;
+            RETURN_IF_ERROR(
+                    assert_cast<const Derived&>(*this).build_prompt(block, 
arguments, i, prompt));
 
-        std::string response;
-        if (config.provider_type == "MOCK") {
-            // Mock path for UT
-            response = "{\"embedding\": [0, 1, 2, 3, 4]}";
-        } else {
-            RETURN_IF_ERROR(send_request_to_llm(request_body, response, 
config, adapter, context));
+            size_t entry_size = 
estimate_batch_entry_size(batch_prompts.size(), prompt);
+            if (entry_size > max_batch_prompt_size) {
+                if (!batch_prompts.empty()) {
+                    std::vector<std::string> batch_results;
+                    RETURN_IF_ERROR(this->execute_batch_request(batch_prompts, 
batch_results,
+                                                                config, 
adapter, context));
+                    RETURN_IF_ERROR(assert_cast<const 
Derived&>(*this).append_batch_results(
+                            batch_results, col_result));
+                    batch_prompts.clear();
+                    current_batch_size = 2;
+                }
+
+                std::vector<std::string> single_prompts;
+                single_prompts.emplace_back(std::move(prompt));
+                std::vector<std::string> single_results;
+                RETURN_IF_ERROR(this->execute_batch_request(single_prompts, 
single_results, config,
+                                                            adapter, context));
+                RETURN_IF_ERROR(assert_cast<const 
Derived&>(*this).append_batch_results(
+                        single_results, col_result));
+                continue;
+            }
+
+            size_t additional_size = entry_size + (batch_prompts.empty() ? 0 : 
1);
+            if (!batch_prompts.empty() &&
+                current_batch_size + additional_size > max_batch_prompt_size) {
+                std::vector<std::string> batch_results;
+                RETURN_IF_ERROR(this->execute_batch_request(batch_prompts, 
batch_results, config,
+                                                            adapter, context));
+                RETURN_IF_ERROR(assert_cast<const 
Derived&>(*this).append_batch_results(
+                        batch_results, col_result));
+                batch_prompts.clear();
+                current_batch_size = 2;
+                additional_size = entry_size;
+            }
+
+            batch_prompts.emplace_back(std::move(prompt));
+            current_batch_size += additional_size;
         }
 
-        RETURN_IF_ERROR(adapter->parse_embedding_response(response, results));
-        if (results.empty()) {
-            return Status::InternalError("AI returned empty result");
+        if (!batch_prompts.empty()) {
+            std::vector<std::string> batch_results;
+            RETURN_IF_ERROR(this->execute_batch_request(batch_prompts, 
batch_results, config,
+                                                        adapter, context));
+            RETURN_IF_ERROR(assert_cast<const 
Derived&>(*this).append_batch_results(batch_results,
+                                                                               
     col_result));
         }
+        return Status::OK();
+    }
+
+private:
+    // The ai resource must be literal
+    Status _init_from_resource(FunctionContext* context, const Block& block,
+                               const ColumnNumbers& arguments, TAIResource& 
config,
+                               std::shared_ptr<AIAdapter>& adapter) const {
+        const ColumnWithTypeAndName& resource_column = 
block.get_by_position(arguments[0]);
+        StringRef resource_name_ref = resource_column.column->get_data_at(0);
+        std::string resource_name = std::string(resource_name_ref.data, 
resource_name_ref.size);

Review Comment:
   FE will check it first, if BE cannot find ai_resource, it indicates there is 
a wrong/missing transmission



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to