From 826f025a08a831a53c5e290fedb0911931c1b680 Mon Sep 17 00:00:00 2001 From: kaixiliu Date: Wed, 10 Apr 2024 14:53:56 +0800 Subject: UUI introduces large models Issue-ID: USECASEUI-834 Change-Id: I5be69a26191952cf883a14b16db0e9e98665dd0c Signed-off-by: kaixiliu --- llm-adaptation/pom.xml | 72 ++++++++++++++++ .../llmadaptation/LlmAdaptationApplication.java | 28 +++++++ .../llmadaptation/bean/LargeModelRequestParam.java | 71 ++++++++++++++++ .../llmadaptation/controller/LlmController.java | 96 ++++++++++++++++++++++ .../usecaseui/llmadaptation/util/TokenUtil.java | 42 ++++++++++ llm-adaptation/src/main/resources/application.yaml | 4 + pom.xml | 3 + 7 files changed, 316 insertions(+) create mode 100644 llm-adaptation/pom.xml create mode 100644 llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/LlmAdaptationApplication.java create mode 100644 llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/bean/LargeModelRequestParam.java create mode 100644 llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/controller/LlmController.java create mode 100644 llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/util/TokenUtil.java create mode 100644 llm-adaptation/src/main/resources/application.yaml diff --git a/llm-adaptation/pom.xml b/llm-adaptation/pom.xml new file mode 100644 index 0000000..db03490 --- /dev/null +++ b/llm-adaptation/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + + org.onap.usecase-ui.llm-adaptation + usecase-ui-llm-adaptation-parent + 1.0-SNAPSHOT + + + org.onap.usecase-ui.llm-adaptation + usecase-ui-llm-adaptation + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + + org.springframework + spring-framework-bom + 6.0.12 + pom + import + + + + org.springframework.boot + spring-boot-dependencies + 3.0.11 + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + com.nimbusds + nimbus-jose-jwt + 8.16 + + + com.squareup.retrofit2 + converter-jackson + 2.9.0 + + + com.alibaba + fastjson + 2.0.6 + + + org.apache.httpcomponents + httpclient + + + org.projectlombok + lombok + + + + \ No newline at end of file diff --git a/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/LlmAdaptationApplication.java b/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/LlmAdaptationApplication.java new file mode 100644 index 0000000..2968f06 --- /dev/null +++ b/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/LlmAdaptationApplication.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2024 CMCC, Inc. and others. All rights reserved. + * + * Licensed 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. + */ + +package org.onap.usecaseui.llmadaptation; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class LlmAdaptationApplication { + + public static void main(String[] args) { + SpringApplication.run(LlmAdaptationApplication.class, args); + } +} \ No newline at end of file diff --git a/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/bean/LargeModelRequestParam.java b/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/bean/LargeModelRequestParam.java new file mode 100644 index 0000000..904038b --- /dev/null +++ b/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/bean/LargeModelRequestParam.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2024 CMCC, Inc. and others. All rights reserved. + * + * Licensed 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. + */ + +package org.onap.usecaseui.llmadaptation.bean; + +import java.util.List; + +public class LargeModelRequestParam { + private String prompt; + + private List history; + + private boolean stream; + + private boolean reference; + + private double temperature; + + public String getPrompt() { + return prompt; + } + + public void setPrompt(String prompt) { + this.prompt = prompt; + } + + public List getHistory() { + return history; + } + + public void setHistory(List history) { + this.history = history; + } + + public boolean isStream() { + return stream; + } + + public void setStream(boolean stream) { + this.stream = stream; + } + + public boolean isReference() { + return reference; + } + + public void setReference(boolean reference) { + this.reference = reference; + } + + public double getTemperature() { + return temperature; + } + + public void setTemperature(double temperature) { + this.temperature = temperature; + } +} diff --git a/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/controller/LlmController.java b/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/controller/LlmController.java new file mode 100644 index 0000000..120110f --- /dev/null +++ b/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/controller/LlmController.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2024 CMCC, Inc. and others. All rights reserved. + * + * Licensed 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. + */ + +package org.onap.usecaseui.llmadaptation.controller; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.nimbusds.jose.JOSEException; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.onap.usecaseui.llmadaptation.bean.LargeModelRequestParam; +import org.onap.usecaseui.llmadaptation.util.TokenUtil; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.ArrayList; + +@Slf4j +@RestController +public class LlmController { + + @PostMapping(value = "/getHelper") + public String getHelp(@RequestBody String question) { + String result = ""; + String url = "http://jiutian.hq.cmcc/largemodel/api/v1/completions?klAssisId=65e6c42ba8a3d22f0366c84d"; + String apiKey = "65e82b2fa8a3d22f03679898.kTywdU/witQJlHdwgWAI+1thI2UUWfHN"; + String token; + try { + token = TokenUtil.generateToken(apiKey, 200000); + } catch (JOSEException e) { + log.error("error is {}", e.getMessage()); + return result; + } + String authorization = "Bearer " + token; + + LargeModelRequestParam helpRequest = new LargeModelRequestParam(); + helpRequest.setPrompt(question); + helpRequest.setReference(false); + helpRequest.setStream(false); + helpRequest.setHistory(new ArrayList<>()); + helpRequest.setTemperature(0.01); + + RequestConfig defaultRequestConfig = RequestConfig.custom() + .setConnectTimeout(10000) + .setConnectionRequestTimeout(10000) + .setSocketTimeout(10000) + .build(); + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpPost httpPost = new HttpPost(url); + + httpPost.setHeader("Content-Type", "application/json"); + httpPost.setHeader("Authorization", authorization); + httpPost.setConfig(defaultRequestConfig); + + StringEntity requestEntity = new StringEntity(JSON.toJSONString(helpRequest), ContentType.APPLICATION_JSON); + httpPost.setEntity(requestEntity); + + HttpResponse response = httpClient.execute(httpPost); + + HttpEntity responseEntity = response.getEntity(); + + if (responseEntity != null) { + String responseString = EntityUtils.toString(responseEntity, "utf-8"); + String json = responseString.replaceAll("^data:", ""); + JSONObject jsonObject = JSON.parseObject(json); + result = jsonObject.getString("response"); + return result; + } + } catch (Exception e) { + log.error("error is {}", e.getMessage()); + } + return result; + } +} diff --git a/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/util/TokenUtil.java b/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/util/TokenUtil.java new file mode 100644 index 0000000..13c877b --- /dev/null +++ b/llm-adaptation/src/main/java/org/onap/usecaseui/llmadaptation/util/TokenUtil.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2024 CMCC, Inc. and others. All rights reserved. + * + * Licensed 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. + */ + +package org.onap.usecaseui.llmadaptation.util; + +import com.alibaba.fastjson.JSONObject; +import com.nimbusds.jose.*; +import com.nimbusds.jose.crypto.MACSigner; + +import java.util.Date; + +public class TokenUtil { + public static String generateToken(String apikey,long expSeconds) throws JOSEException { + String[] apikeyArr=apikey.split("\\.",2);//kid.secret + Date now=new Date(); + + JSONObject payload=new JSONObject(); + payload.put("api_key",apikeyArr[0]); + payload.put("exp",now.getTime()/1000+expSeconds); + payload.put("timestamp",now.getTime()/1000); + + //create JWS object + JWSObject jwsObject = new JWSObject( + new JWSHeader.Builder(JWSAlgorithm.HS256).type(JOSEObjectType.JWT).build(), + new Payload(payload.toJSONString())); + jwsObject.sign(new MACSigner(apikeyArr[1])); + return jwsObject.serialize(); + } +} diff --git a/llm-adaptation/src/main/resources/application.yaml b/llm-adaptation/src/main/resources/application.yaml new file mode 100644 index 0000000..e8e1727 --- /dev/null +++ b/llm-adaptation/src/main/resources/application.yaml @@ -0,0 +1,4 @@ +server: + port: 8084 + servlet: + context-path: /api/usecaseui-llm-adaptation/v1 diff --git a/pom.xml b/pom.xml index 536985d..5d7fd53 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,9 @@ usecase-ui-llm-adaptation-parent 1.0-SNAPSHOT pom + + llm-adaptation + 17 -- cgit 1.2.3-korg