1、注册登录阿里云百炼
通义大模型_AI大模型_一站式大模型推理和部署服务-阿里云
2、创建永久密钥
大模型服务平台百炼控制台
3、调用示例
public static GenerationResult callWithMessage(String apiKey, List<Message> messages, String modalName,
Boolean enableSearch, Boolean enableThinking,
Integer maxTokens, Float temperature, Double topP, String stopStrings
) throws ApiException, NoApiKeyException, InputRequiredException {
Generation gen = new Generation();
GenerationParam param = GenerationParam.builder()
.apiKey(apiKey)
.model(modalName)
.messages(messages)
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.enableSearch(enableSearch)
.enableThinking(enableThinking)
.maxTokens(maxTokens)
.temperature(temperature)
.topP(topP)
.stopStrings(Arrays.asList(stopStrings.split(",")))
.build();
return gen.call(param);
}
public static Message buildMessage(String msg,String role){
return Message.builder()
.role(role)
.content(msg)
.build();
}
public static void main(String[] args) {
long time = System.currentTimeMillis();
try {
String apiKey = "";
String aiDesc = "你是一个智能助手";
String modalName = "qwen-plus";
List<Message> messages = new ArrayList<>();
Message message = buildMessage(aiDesc,Role.SYSTEM.getValue());
messages.add(message);
message = buildMessage("1+1=几",Role.USER.getValue());
messages.add(message);
GenerationResult result = callWithMessage(apiKey,messages,modalName,
false,true,500,0.7F,0.9,"共产党");
log.info("AI-Answer:{}",result.getOutput().getChoices().get(0).getMessage().getContent());
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
log.error("错误信息:{}", e.getMessage());
log.error("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}
log.info("耗时【{}】ms",System.currentTimeMillis()-time);
System.exit(0);
}