|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.transform.nlpmodel.llm.remote.microsoft; |
| 19 | + |
| 20 | +import org.apache.seatunnel.shade.com.fasterxml.jackson.core.type.TypeReference; |
| 21 | +import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.JsonNode; |
| 22 | +import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | +import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | + |
| 25 | +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; |
| 26 | +import org.apache.seatunnel.api.table.type.SqlType; |
| 27 | +import org.apache.seatunnel.transform.nlpmodel.CustomConfigPlaceholder; |
| 28 | +import org.apache.seatunnel.transform.nlpmodel.llm.remote.AbstractModel; |
| 29 | + |
| 30 | +import org.apache.http.client.config.RequestConfig; |
| 31 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 32 | +import org.apache.http.client.methods.HttpPost; |
| 33 | +import org.apache.http.entity.StringEntity; |
| 34 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 35 | +import org.apache.http.impl.client.HttpClients; |
| 36 | +import org.apache.http.util.EntityUtils; |
| 37 | + |
| 38 | +import com.google.common.annotations.VisibleForTesting; |
| 39 | + |
| 40 | +import java.io.IOException; |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +public class MicrosoftModel extends AbstractModel { |
| 44 | + |
| 45 | + private final CloseableHttpClient client; |
| 46 | + private final String apiKey; |
| 47 | + private final String model; |
| 48 | + private final String apiPath; |
| 49 | + |
| 50 | + public MicrosoftModel( |
| 51 | + SeaTunnelRowType rowType, |
| 52 | + SqlType outputType, |
| 53 | + List<String> projectionColumns, |
| 54 | + String prompt, |
| 55 | + String model, |
| 56 | + String apiKey, |
| 57 | + String apiPath) { |
| 58 | + super(rowType, outputType, projectionColumns, prompt); |
| 59 | + this.model = model; |
| 60 | + this.apiKey = apiKey; |
| 61 | + this.apiPath = |
| 62 | + CustomConfigPlaceholder.replacePlaceholders( |
| 63 | + apiPath, CustomConfigPlaceholder.REPLACE_PLACEHOLDER_MODEL, model, null); |
| 64 | + this.client = HttpClients.createDefault(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected List<String> chatWithModel(String prompt, String data) throws IOException { |
| 69 | + HttpPost post = new HttpPost(apiPath); |
| 70 | + post.setHeader("Authorization", "Bearer " + apiKey); |
| 71 | + post.setHeader("Content-Type", "application/json"); |
| 72 | + ObjectNode objectNode = createJsonNodeFromData(prompt, data); |
| 73 | + post.setEntity(new StringEntity(OBJECT_MAPPER.writeValueAsString(objectNode), "UTF-8")); |
| 74 | + post.setConfig( |
| 75 | + RequestConfig.custom().setConnectTimeout(20000).setSocketTimeout(20000).build()); |
| 76 | + CloseableHttpResponse response = client.execute(post); |
| 77 | + String responseStr = EntityUtils.toString(response.getEntity()); |
| 78 | + if (response.getStatusLine().getStatusCode() != 200) { |
| 79 | + throw new IOException("Failed to chat with model, response: " + responseStr); |
| 80 | + } |
| 81 | + |
| 82 | + JsonNode result = OBJECT_MAPPER.readTree(responseStr); |
| 83 | + String resultData = result.get("choices").get(0).get("message").get("content").asText(); |
| 84 | + return OBJECT_MAPPER.readValue( |
| 85 | + convertData(resultData), new TypeReference<List<String>>() {}); |
| 86 | + } |
| 87 | + |
| 88 | + @VisibleForTesting |
| 89 | + public ObjectNode createJsonNodeFromData(String prompt, String data) { |
| 90 | + ObjectNode objectNode = OBJECT_MAPPER.createObjectNode(); |
| 91 | + ArrayNode messages = objectNode.putArray("messages"); |
| 92 | + messages.addObject().put("role", "system").put("content", prompt); |
| 93 | + messages.addObject().put("role", "user").put("content", data); |
| 94 | + return objectNode; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void close() throws IOException { |
| 99 | + if (client != null) { |
| 100 | + client.close(); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments