提交修改
This commit is contained in:
parent
c1f3de06bf
commit
f5f891b41a
88
api-third/src/main/java/com/heyu/api/alibaba/LLMUtils.java
Normal file
88
api-third/src/main/java/com/heyu/api/alibaba/LLMUtils.java
Normal file
@ -0,0 +1,88 @@
|
||||
package com.heyu.api.alibaba;
|
||||
|
||||
import com.alibaba.dashscope.aigc.generation.Generation;
|
||||
import com.alibaba.dashscope.aigc.generation.GenerationParam;
|
||||
import com.alibaba.dashscope.aigc.generation.GenerationResult;
|
||||
import com.alibaba.dashscope.aigc.generation.GenerationUsage;
|
||||
import com.alibaba.dashscope.common.Message;
|
||||
import com.alibaba.dashscope.common.Role;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.heyu.api.alibaba.resp.ModelResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class LLMUtils {
|
||||
|
||||
public final static String apiKey = "sk-ef6213245c3648ea81f2e4a8ccd34d75";
|
||||
|
||||
public final static String
|
||||
prompt = "# 角色\n"
|
||||
+"你是一个语言翻译专家,能将用户输入的内容进行翻译\n" +
|
||||
"# 任务说明\n" +
|
||||
"翻译成 " ;
|
||||
;
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
ModelResult modelResult = callBaiLian("开通阿里云百炼:使用阿里云主账号前往阿里云百炼控制台,如果页面顶部显示以下消息,您需要开通阿里云百炼的模型服务,以获得免费额度。如果未显示该消息,则表示您已经开通。",prompt);
|
||||
|
||||
System.out.println(JSON.toJSON(modelResult));
|
||||
|
||||
}
|
||||
|
||||
public static ModelResult callBaiLian(String content, String prompt ){
|
||||
ModelResult modelResult = new ModelResult();
|
||||
try {
|
||||
Date startDate = new Date();
|
||||
Generation gen = new Generation();
|
||||
Message systemMsg = Message.builder()
|
||||
.role(Role.SYSTEM.getValue())
|
||||
.content(prompt)
|
||||
.build();
|
||||
Message userMsg = Message.builder()
|
||||
.role(Role.USER.getValue())
|
||||
.content(content)
|
||||
.build();
|
||||
GenerationParam param = GenerationParam.builder()
|
||||
// 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
|
||||
.apiKey("sk-ef6213245c3648ea81f2e4a8ccd34d75")
|
||||
// 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
|
||||
.model("qwen-plus")
|
||||
.messages(Arrays.asList(systemMsg, userMsg))
|
||||
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
|
||||
.build();
|
||||
|
||||
GenerationResult generationResult = gen.call(param);
|
||||
String resp= generationResult.getOutput()
|
||||
.getChoices().get(0).
|
||||
getMessage().getContent() ;
|
||||
|
||||
|
||||
modelResult.setResult(resp);
|
||||
|
||||
Date endDate = new Date();
|
||||
|
||||
GenerationUsage generationUsage = generationResult.getUsage();
|
||||
|
||||
modelResult.setTokens(generationUsage.getTotalTokens());
|
||||
modelResult.setStartTime(startDate);
|
||||
modelResult.setEndTime( endDate);
|
||||
|
||||
modelResult.setExet(endDate .getTime()- startDate.getTime());
|
||||
return modelResult;
|
||||
}catch (Exception e ){
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
log.info("callBaiLian content :{}, callBaiLian modelResult:{},prompt:{}",content, JSON.toJSONString(modelResult),prompt);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package com.heyu.api.alibaba.request.common.text;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class TranslationRequest implements Serializable {
|
||||
|
||||
|
||||
/***
|
||||
* 用户输入的内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
|
||||
/***
|
||||
* 要翻译成的目标语言
|
||||
*/
|
||||
private String language;
|
||||
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package com.heyu.api.alibaba.request.common.text;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class TranslationResp implements Serializable {
|
||||
|
||||
|
||||
/***
|
||||
* 用户输入的内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/***
|
||||
* 要翻译成的目标语言
|
||||
*/
|
||||
private String language;
|
||||
|
||||
/***
|
||||
* 目标内容
|
||||
*/
|
||||
private String translationContent;
|
||||
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package com.heyu.api.alibaba.resp;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class ModelResult {
|
||||
|
||||
|
||||
private String result ;
|
||||
|
||||
private Integer tokens ;
|
||||
|
||||
|
||||
|
||||
|
||||
private Date startTime ;
|
||||
|
||||
|
||||
|
||||
private Date endTime ;
|
||||
|
||||
|
||||
private long exet;
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package com.heyu.api.controller.translation;
|
||||
|
||||
import com.heyu.api.alibaba.LLMUtils;
|
||||
import com.heyu.api.alibaba.request.common.text.TranslationRequest;
|
||||
import com.heyu.api.alibaba.request.common.text.TranslationResp;
|
||||
import com.heyu.api.alibaba.resp.ModelResult;
|
||||
import com.heyu.api.data.annotation.EbAuthentication;
|
||||
import com.heyu.api.data.constants.ApiConstants;
|
||||
import com.heyu.api.data.utils.R;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/general/translation")
|
||||
public class TranslationController {
|
||||
|
||||
// http://localhost:8888/general/translation/language?content=我是一只小猫&language=英语
|
||||
@EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
|
||||
@RequestMapping("/language")
|
||||
public R language(@RequestBody TranslationRequest translationRequest) throws Exception {
|
||||
String prompt = LLMUtils.prompt + translationRequest.getLanguage();
|
||||
|
||||
ModelResult modelResult = LLMUtils.callBaiLian(translationRequest.getContent(), prompt);
|
||||
|
||||
TranslationResp resp = new TranslationResp();
|
||||
|
||||
resp.setLanguage(translationRequest.getLanguage());
|
||||
resp.setContent(modelResult.getResult());
|
||||
|
||||
if(modelResult!=null){
|
||||
resp.setTranslationContent(modelResult.getResult());
|
||||
}
|
||||
return R.ok().setData(resp);
|
||||
}
|
||||
|
||||
}
|
||||
44
api-web/api-interface/src/test/java/com/api/test/Main.java
Normal file
44
api-web/api-interface/src/test/java/com/api/test/Main.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.api.test;
|
||||
|
||||
import com.alibaba.dashscope.aigc.generation.Generation;
|
||||
import com.alibaba.dashscope.aigc.generation.GenerationParam;
|
||||
import com.alibaba.dashscope.aigc.generation.GenerationResult;
|
||||
import com.alibaba.dashscope.common.Message;
|
||||
import com.alibaba.dashscope.common.Role;
|
||||
import com.alibaba.dashscope.exception.ApiException;
|
||||
import com.alibaba.dashscope.exception.InputRequiredException;
|
||||
import com.alibaba.dashscope.exception.NoApiKeyException;
|
||||
|
||||
import java.util.Arrays;
|
||||
public class Main {
|
||||
public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
|
||||
Generation gen = new Generation();
|
||||
Message systemMsg = Message.builder()
|
||||
.role(Role.SYSTEM.getValue())
|
||||
.content("You are a helpful assistant.")
|
||||
.build();
|
||||
Message userMsg = Message.builder()
|
||||
.role(Role.USER.getValue())
|
||||
.content("你是谁?")
|
||||
.build();
|
||||
GenerationParam param = GenerationParam.builder()
|
||||
// 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
|
||||
.apiKey("sk-ef6213245c3648ea81f2e4a8ccd34d75")
|
||||
// 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
|
||||
.model("qwen-plus")
|
||||
.messages(Arrays.asList(systemMsg, userMsg))
|
||||
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
|
||||
.build();
|
||||
return gen.call(param);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
GenerationResult result = callWithMessage();
|
||||
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
|
||||
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
|
||||
System.err.println("错误信息:"+e.getMessage());
|
||||
System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
14
pom.xml
14
pom.xml
@ -343,6 +343,16 @@
|
||||
|
||||
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>dashscope-sdk-java</artifactId>
|
||||
<version>2.20.5</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>credentials-java</artifactId>
|
||||
@ -393,8 +403,8 @@
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<source>11</source>
|
||||
<target>11</target>
|
||||
<debug>true</debug>
|
||||
<debuglevel>lines,vars,source</debuglevel>
|
||||
<compilerArgs>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user