Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature][Transforms-V2] LLM transforms Support custom field name #7640

Merged
merged 2 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions docs/en/transform-v2/llm.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ more.
## Options

| name | type | required | default value |
| ---------------------- | ------ | -------- | ------------- |
|------------------------| ------ | -------- | ------------- |
| model_provider | enum | yes | |
| output_data_type | enum | no | String |
| output_column_name | string | no | String |
| prompt | string | yes | |
| inference_columns | list | no | |
| inference_columns | list | no | |
| model | string | yes | |
| api_key | string | yes | |
| api_path | string | no | |
Expand All @@ -35,6 +36,10 @@ The data type of the output data. The available options are:
STRING,INT,BIGINT,DOUBLE,BOOLEAN.
Default value is STRING.

### output_column_name

Custom output data field name. A custom field name that is the same as an existing field name is replaced with 'llm_output'.

### prompt

The prompt to send to the LLM. This parameter defines how LLM will process and return data, eg:
Expand Down
11 changes: 8 additions & 3 deletions docs/zh/transform-v2/llm.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

## 属性

| 名称 | 类型 | 是否必须 | 默认值 |
| ---------------------- | ------ | -------- | ------ |
| 名称 | 类型 | 是否必须 | 默认值 |
|------------------------| ------ | -------- |--------|
| model_provider | enum | yes | |
| output_data_type | enum | no | String |
| output_column_name | string | no | |
| prompt | string | yes | |
| inference_columns | list | no | |
| inference_columns | list | no | |
| model | string | yes | |
| api_key | string | yes | |
| api_path | string | no | |
Expand All @@ -33,6 +34,10 @@ OPENAI、DOUBAO、CUSTOM
STRING,INT,BIGINT,DOUBLE,BOOLEAN.
默认值为 STRING。

### output_column_name

自定义输出数据字段名称。自定义字段名称与现有字段名称相同时,将替换为`llm_output`。

### prompt

发送到 LLM 的提示。此参数定义 LLM 将如何处理和返回数据,例如:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void testLLMWithOpenAIBoolean(TestContainer container)
throws IOException, InterruptedException {
Container.ExecResult execResult =
container.executeJob("/llm_openai_transform_boolean.conf");
Assertions.assertEquals(0, execResult.getExitCode());
}

@TestTemplate
Expand All @@ -103,6 +104,14 @@ public void testLLMWithOpenAIColumns(TestContainer container)
Assertions.assertEquals(0, execResult.getExitCode());
}

@TestTemplate
public void testLLMWithOpenAIOutputColumnName(TestContainer container)
throws IOException, InterruptedException {
Container.ExecResult execResult =
container.executeJob("/llm_openai_transform_custom_output_name.conf");
Assertions.assertEquals(0, execResult.getExitCode());
}

@TestTemplate
public void testLLMWithCustomModel(TestContainer container)
throws IOException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# 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.
#
######
###### This config file is a demonstration of streaming processing in seatunnel config
######

env {
job.mode = "BATCH"
}

source {
FakeSource {
row.num = 5
schema = {
fields {
id = "int"
name = "string"
}
}
rows = [
{fields = [1, "Jia Fan"], kind = INSERT}
{fields = [2, "Hailin Wang"], kind = INSERT}
{fields = [3, "Tomas"], kind = INSERT}
{fields = [4, "Eric"], kind = INSERT}
{fields = [5, "Guangdong Liu"], kind = INSERT}
]
result_table_name = "fake"
}
}

transform {
LLM {
source_table_name = "fake"
model_provider = OPENAI
model = gpt-4o-mini
api_key = sk-xxx
output_column_name = "nationality"
prompt = "Determine whether someone is Chinese or American by their name"
openai.api_path = "http://mockserver:1080/v1/chat/completions"
result_table_name = "llm_output"
}
}

sink {
Assert {
source_table_name = "llm_output"
rules =
{
field_rules = [
{
field_name = "nationality"
field_type = string
field_value = [
{
rule_type = NOT_NULL
}
]
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
import org.apache.seatunnel.transform.nlpmodel.llm.remote.custom.CustomModel;
import org.apache.seatunnel.transform.nlpmodel.llm.remote.openai.OpenAIModel;

import org.apache.commons.lang3.StringUtils;

import lombok.NonNull;
import lombok.SneakyThrows;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -139,8 +142,18 @@ protected Object getOutputFieldValue(SeaTunnelRowAccessor inputRow) {

@Override
protected Column getOutputColumn() {
String customFieldName = config.get(LLMTransformConfig.OUTPUT_COLUMN_NAME);
String[] fieldNames = inputCatalogTable.getTableSchema().getFieldNames();
boolean isExist = Arrays.asList(fieldNames).contains(customFieldName);
if (isExist) {
throw new IllegalArgumentException(
String.format("Field name %s already exists", customFieldName));
}
if (StringUtils.isEmpty(customFieldName)) {
customFieldName = "llm_output";
}
return PhysicalColumn.of(
"llm_output", outputDataType, (Long) null, true, null, "Output column of LLM");
customFieldName, outputDataType, (Long) null, true, null, "Output column of LLM");
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class LLMTransformConfig extends ModelTransformConfig {
.noDefaultValue()
.withDescription("The row projection field of each inference");

public static final Option<String> OUTPUT_COLUMN_NAME =
Options.key("output_column_name")
.stringType()
.noDefaultValue()
.withDescription("custom field name for the llm output data");

public static final Option<Integer> INFERENCE_BATCH_SIZE =
Options.key("inference_batch_size")
.intType()
Expand Down
Loading