HappenLee commented on code in PR #51725:
URL: https://github.com/apache/doris/pull/51725#discussion_r2224877435


##########
be/src/vec/functions/llm/llm_functions.cpp:
##########
@@ -0,0 +1,386 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "vec/columns/column_array.h"
+#include "vec/functions/llm/llm_classify.h"
+#include "vec/functions/llm/llm_extract.h"
+#include "vec/functions/llm/llm_fix_grammar.h"
+#include "vec/functions/llm/llm_generate.h"
+#include "vec/functions/llm/llm_mask.h"
+#include "vec/functions/llm/llm_sentiment.h"
+#include "vec/functions/llm/llm_summarize.h"
+#include "vec/functions/llm/llm_translate.h"
+#include "vec/functions/simple_function_factory.h"
+
+namespace doris::vectorized {
+Status FunctionLLMClassify::build_prompt(const Block& block, const 
ColumnNumbers& arguments,
+                                         size_t row_num, std::string& prompt) 
const {
+    // Get the text column
+    const ColumnWithTypeAndName& text_column = 
block.get_by_position(arguments[1]);
+    std::string text_str;
+    if (const auto* col_const_text = 
check_and_get_column<ColumnConst>(text_column.column.get())) {
+        StringRef text_ref = col_const_text->get_data_at(0);
+        text_str = std::string(text_ref.data, text_ref.size);
+    } else {
+        const auto* col_text = assert_cast<const 
ColumnString*>(text_column.column.get());
+        StringRef text = col_text->get_data_at(row_num);
+        text_str = std::string(text.data, text.size);
+    }
+
+    // Get the labels array column
+    const ColumnWithTypeAndName& labels_column = 
block.get_by_position(arguments[2]);
+    std::vector<std::string> label_values;
+    if (const auto* col_const_labels =
+                check_and_get_column<ColumnConst>(labels_column.column.get())) 
{
+        const auto* const nested_column = &col_const_labels->get_data_column();
+        const auto* col_array = assert_cast<const ColumnArray*>(nested_column);
+
+        const auto& data = col_array->get_data();
+        const auto& offsets = col_array->get_offsets();
+
+        size_t start = 0;
+        size_t end = offsets[0];
+
+        for (size_t i = start; i < end; ++i) {
+            Field field;
+            data.get(i, field);
+            label_values.emplace_back(field.get<String>());
+        }
+    } else {
+        const auto* col_array = assert_cast<const 
ColumnArray*>(labels_column.column.get());
+        const auto& data = col_array->get_data();
+        const auto& offsets = col_array->get_offsets();
+
+        size_t start = row_num > 0 ? offsets[row_num - 1] : 0;
+        size_t end = offsets[row_num];
+
+        for (size_t i = start; i < end; ++i) {
+            Field field;
+            data.get(i, field);
+            label_values.emplace_back(field.get<String>());
+        }
+    }
+
+    std::string labels_str = "[";
+    for (size_t i = 0; i < label_values.size(); ++i) {
+        if (i > 0) {
+            labels_str += ", ";
+        }
+        labels_str += "\"" + label_values[i] + "\"";
+    }
+    labels_str += "]";
+
+    prompt =
+            "Classify the text below into one of the following JSON encoded 
labels: " + labels_str +
+            "\n"
+            "Output only the label without any quotation marks or additional 
text.\n"
+            "Text: " +
+            text_str;
+
+    return Status::OK();
+}
+
+Status FunctionLLMExtract::build_prompt(const Block& block, const 
ColumnNumbers& arguments,
+                                        size_t row_num, std::string& prompt) 
const {
+    // Get the text column
+    const ColumnWithTypeAndName& text_column = 
block.get_by_position(arguments[1]);
+    std::string text_str;
+    if (const auto* col_const_text = 
check_and_get_column<ColumnConst>(text_column.column.get())) {
+        StringRef text_ref = col_const_text->get_data_at(0);
+        text_str = std::string(text_ref.data, text_ref.size);
+    } else {
+        const auto* col_text = assert_cast<const 
ColumnString*>(text_column.column.get());
+        StringRef text = col_text->get_data_at(row_num);
+        text_str = std::string(text.data, text.size);
+    }
+
+    // Get the labels array column
+    const ColumnWithTypeAndName& labels_column = 
block.get_by_position(arguments[2]);
+    std::vector<std::string> label_values;
+    if (const auto* col_const_labels =

Review Comment:
   not need so complex。 only call col_text->get_data_at(row_num);



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