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


##########
be/src/vec/functions/llm/llm_functions.cpp:
##########
@@ -0,0 +1,284 @@
+// 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]);
+    StringRef text = text_column.column->get_data_at(row_num);
+    std::string text_str = std::string(text.data, text.size);
+
+    // Get the labels array column
+    const ColumnWithTypeAndName& labels_column = 
block.get_by_position(arguments[2]);
+    const auto& [array_column, array_row_num] =
+            check_column_const_set_readability(*labels_column.column, row_num);
+    const auto* col_array = check_and_get_column<ColumnArray>(*array_column);
+    if (col_array == nullptr) {
+        return Status::InternalError(
+                "labels argument for {} must be Array(String) or 
Array(Varchar)", name);
+    }
+
+    std::vector<std::string> label_values;
+    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]);
+    StringRef text = text_column.column->get_data_at(row_num);
+    std::string text_str = std::string(text.data, text.size);
+
+    // Get the labels array column
+    const ColumnWithTypeAndName& labels_column = 
block.get_by_position(arguments[2]);
+    const auto& [array_column, array_row_num] =
+            check_column_const_set_readability(*labels_column.column, row_num);
+    const auto* col_array = check_and_get_column<ColumnArray>(*array_column);
+    if (col_array == nullptr) {
+        return Status::InternalError(
+                "labels argument for {} must be Array(String) or 
Array(Varchar)", name);
+    }
+
+    std::vector<std::string> label_values;
+    const auto& offsets = col_array->get_offsets();
+    const auto& data = col_array->get_data();
+    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 = "Extract a value for each of the JSON encoded labels from the 
text below.\n"
+             "For each label, only extract a single value.\n"
+             "Labels: " +
+             labels_str +
+             "\n"
+             "Output the extracted values as a JSON object.\n"
+             "Answer type like `label_1=info1, label2=info2, ...`"
+             "Output only the answer.\n"
+             "Text: " +
+             text_str;
+
+    return Status::OK();
+}
+
+Status FunctionLLMFixGrammar::build_prompt(const Block& block, const 
ColumnNumbers& arguments,
+                                           size_t row_num, std::string& 
prompt) const {
+    const ColumnWithTypeAndName& text_column = 
block.get_by_position(arguments[1]);
+    StringRef text_ref = text_column.column->get_data_at(row_num);
+    std::string text = std::string(text_ref.data, text_ref.size);
+
+    prompt = "Fix the grammar in the text below.\n"
+             "Output only the corrected text.\n"
+             "Text: " +
+             text;
+
+    return Status::OK();
+}
+
+Status FunctionLLMGenerate::build_prompt(const Block& block, const 
ColumnNumbers& arguments,
+                                         size_t row_num, std::string& prompt) 
const {
+    const ColumnWithTypeAndName& text_column = 
block.get_by_position(arguments[1]);
+    StringRef text_ref = text_column.column->get_data_at(row_num);
+    std::string text = std::string(text_ref.data, text_ref.size);
+
+    prompt = "Generate a response based on the following input.\n"
+             "Output only the generated text.\n"
+             "Text: " +
+             text;
+
+    return Status::OK();
+}
+
+Status FunctionLLMMask::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]);
+    StringRef text = text_column.column->get_data_at(row_num);
+    std::string text_str = std::string(text.data, text.size);
+
+    // Get the labels array column
+    const ColumnWithTypeAndName& labels_column = 
block.get_by_position(arguments[2]);
+    const auto& [array_column, array_row_num] =
+            check_column_const_set_readability(*labels_column.column, row_num);
+    const auto* col_array = check_and_get_column<ColumnArray>(*array_column);
+    if (col_array == nullptr) {
+        return Status::InternalError(
+                "labels argument for {} must be Array(String) or 
Array(Varchar)", name);
+    }
+
+    std::vector<std::string> label_values;
+    const auto& offsets = col_array->get_offsets();
+    const auto& data = col_array->get_data();
+    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 = "Identify and mask sensitive information in the text below.\n"

Review Comment:
   btw, as far as I know, for most type of LLM, the interface originally 
support treating the input separately as system prompt and input. if so, you 
can use a better way to split them than using labels in my example.



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