github-actions[bot] commented on code in PR #62494:
URL: https://github.com/apache/doris/pull/62494#discussion_r3090536957


##########
be/src/exprs/function/ai/ai_adapter.h:
##########
@@ -137,26 +139,70 @@ class AIAdapter {
 
     virtual Status build_embedding_request(const std::vector<std::string>& 
inputs,
                                            std::string& request_body) const {
-        return Status::NotSupported("{} does not support the Embed feature.",
-                                    _config.provider_type);
+        return embed_not_supported_status();
     }
 
-    virtual Status build_multimodal_embedding_request(MultimodalType 
/*media_type*/,
-                                                      const std::string& 
/*media_url*/,
-                                                      std::string& 
/*request_body*/) const {
+    virtual Status build_multimodal_embedding_request(
+            const std::vector<MultimodalType>& /*media_types*/,
+            const std::vector<std::string>& /*media_urls*/, std::string& 
/*request_body*/) const {
         return Status::NotSupported("{} does not support multimodal Embed 
feature.",
                                     _config.provider_type);
     }
 
     virtual Status parse_embedding_response(const std::string& response_body,
                                             std::vector<std::vector<float>>& 
results) const {
-        return Status::NotSupported("{} does not support the Embed feature.",
-                                    _config.provider_type);
+        return embed_not_supported_status();
     }
 
 protected:
     TAIResource _config;
 
+    Status embed_not_supported_status() const {
+        return Status::NotSupported(
+                "{} does not support the Embed feature. Currently supported 
providers are "
+                "OpenAI, Gemini, Voyage, Jina, Qwen, and Minimax.",
+                _config.provider_type);
+    }
+
+    // Appends one provider-parsed text result to `results`.
+    // The adapter has already parsed the provider's outer response envelope 
before calling here.
+    // Example:
+    // provider response -> choices[0].message.content = "[\"1\",\"0\",\"1\"]"
+    // this helper       -> appends "1", "0", "1" into `results`
+    static Status append_parsed_text_result(std::string_view text,
+                                            std::vector<std::string>& results) 
{
+        size_t begin = 0;
+        size_t end = text.size();
+        while (begin < end && std::isspace(static_cast<unsigned 
char>(text[begin]))) {
+            ++begin;
+        }
+        while (begin < end && std::isspace(static_cast<unsigned char>(text[end 
- 1]))) {
+            --end;
+        }
+
+        if (begin < end && text[begin] == '[' && text[end - 1] == ']') {

Review Comment:
   This broad `[`...`]` detection changes the semantics of every adapter parse 
path, not just the new batched string functions. A valid scalar model output 
like `[MSKED]` (which `ai_mask` explicitly asks for) now goes through JSON 
parsing and fails with `Invalid batch result format` instead of being returned 
as plain text. The same regression also reaches existing non-batched callers 
such as `ai_agg`, because they still call `adapter->parse_response()` and now 
inherit this helper. We need a stricter batch marker here instead of assuming 
any bracketed scalar text is a JSON string array.



##########
be/src/exprs/function/ai/ai_functions.h:
##########
@@ -89,80 +91,63 @@ class AIFunction : public IFunction {
 
 protected:
     // Derived classes can override this method for non-text/default behavior.
-    // The base implementation keeps previous text-oriented processing 
unchanged.
+    // The base implementation handles all string-input/string-output 
batchable functions.
     Status execute_with_adapter(FunctionContext* context, Block& block,
                                 const ColumnNumbers& arguments, uint32_t 
result,
                                 size_t input_rows_count, const TAIResource& 
config,
                                 std::shared_ptr<AIAdapter>& adapter) const {
-        DataTypePtr return_type_impl =
-                assert_cast<const 
Derived&>(*this).get_return_type_impl(DataTypes());
-        if (return_type_impl->get_primitive_type() != 
PrimitiveType::TYPE_STRING) {
-            return Status::InternalError("{} must override execute for 
non-string return type",
-                                         get_name());
-        }
-        MutableColumnPtr col_result = ColumnString::create();
+        auto col_result = assert_cast<const 
Derived&>(*this).create_result_column();
+        RETURN_IF_ERROR(execute_batched_prompts(context, block, arguments, 
input_rows_count, config,
+                                                adapter, *col_result));
 
-        for (size_t i = 0; i < input_rows_count; ++i) {
-            // Build AI prompt text
-            std::string prompt;
-            RETURN_IF_ERROR(
-                    assert_cast<const Derived&>(*this).build_prompt(block, 
arguments, i, prompt));
+        block.replace_by_position(result, std::move(col_result));
+        return Status::OK();
+    }
 
-            std::string string_result;
-            RETURN_IF_ERROR(
-                    execute_single_request(prompt, string_result, config, 
adapter, context));
-            assert_cast<ColumnString&>(*col_result)
-                    .insert_data(string_result.data(), string_result.size());
-        }
+    MutableColumnPtr create_result_column() const { return 
ColumnString::create(); }
 
-        block.replace_by_position(result, std::move(col_result));
+    // Provider-reusable hook for AI functions(string) -> string.
+    Status append_batch_results(const std::vector<std::string>& batch_results,
+                                IColumn& col_result) const {
+        auto& string_col = assert_cast<ColumnString&>(col_result);
+        for (const auto& batch_result : batch_results) {
+            string_col.insert_data(batch_result.data(), batch_result.size());
+        }
         return Status::OK();
     }
 
-    // The endpoint `v1/completions` does not support `system_prompt`.
-    // To ensure a clear structure and stable AI results.
-    // Convert from `v1/completions` to `v1/chat/completions`
     static void normalize_endpoint(TAIResource& config) {
+        // If users configure only the version root like `.../v1` or 
`.../v1beta`, append
+        // `models/<model>:batchEmbedContents` for `embed`, and 
`models/<model>:generateContent`
+        // for other AI scalar functions. If the endpoint is already a full 
method path, keep it.
+        if (iequal(config.provider_type, "GEMINI")) {

Review Comment:
   This only rewrites bare Gemini version roots. After this PR, 
`GeminiAdapter::build_embedding_request()` always emits a `batchEmbedContents` 
payload (`requests` array), but users can still configure an explicit endpoint 
such as the documented `.../models/gemini-embedding-001:embedContent`. For 
those existing resources, `normalize_endpoint()` is now a no-op, so BE will 
POST the new batch body to the old single-embed method and fail. That is a 
compatibility regression introduced by the batching change. Please either 
rewrite explicit Gemini embed endpoints too, or keep the single-request payload 
shape when the configured endpoint is still `:embedContent`.



-- 
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