diff options
author | kaixiliu <liukaixi@chinamobile.com> | 2024-04-10 14:53:56 +0800 |
---|---|---|
committer | kaixiliu <liukaixi@chinamobile.com> | 2024-04-10 15:00:22 +0800 |
commit | 826f025a08a831a53c5e290fedb0911931c1b680 (patch) | |
tree | 674eff5517842a526eebb8c3f0be8ee44260a389 | |
parent | 59cfdfafd9b8a838de45fba8006d8a50b38c686f (diff) |
UUI introduces large models
Issue-ID: USECASEUI-834
Change-Id: I5be69a26191952cf883a14b16db0e9e98665dd0c
Signed-off-by: kaixiliu <liukaixi@chinamobile.com>
7 files changed, 316 insertions, 0 deletions
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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.usecase-ui.llm-adaptation</groupId> + <artifactId>usecase-ui-llm-adaptation-parent</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <groupId>org.onap.usecase-ui.llm-adaptation</groupId> + <artifactId>usecase-ui-llm-adaptation</artifactId> + <version>1.0-SNAPSHOT</version> + + <properties> + <maven.compiler.source>17</maven.compiler.source> + <maven.compiler.target>17</maven.compiler.target> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-framework-bom</artifactId> + <version>6.0.12</version> + <type>pom</type> + <scope>import</scope> + </dependency> + <dependency> + <!-- Import dependency management from Spring Boot --> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-dependencies</artifactId> + <version>3.0.11</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> + <dependency> + <groupId>com.nimbusds</groupId> + <artifactId>nimbus-jose-jwt</artifactId> + <version>8.16</version> + </dependency> + <dependency> + <groupId>com.squareup.retrofit2</groupId> + <artifactId>converter-jackson</artifactId> + <version>2.9.0</version> + </dependency> + <dependency> + <groupId>com.alibaba</groupId> + <artifactId>fastjson</artifactId> + <version>2.0.6</version> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + </dependency> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + </dependency> + </dependencies> + +</project>
\ 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<String> 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<String> getHistory() { + return history; + } + + public void setHistory(List<String> 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 @@ -29,6 +29,9 @@ <artifactId>usecase-ui-llm-adaptation-parent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> + <modules> + <module>llm-adaptation</module> + </modules> <properties> <maven.compiler.source>17</maven.compiler.source> |