Java对接腾讯混元大模型AI
腾讯混元api地址:https://cloud.tencent.com/document/product/1729/97732。哈哈以上代码,只是一个单纯的对接Demo写的很随意,见谅哈!生活不易犒劳一下自己,哈哈哈。设置到的jar包导入。
·
腾讯混元api地址:https://cloud.tencent.com/document/product/1729/97732
设置到的jar包导入
代码如下:
`package com.linkwechat.ai;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import java.util.*;
public class Hunyuan {
private static String secretId="AKIDAFD3jEaCkASlBSXPgnjT2Sly0pSCo";
private static String url="https://hunyuan.cloud.tencent.com/hyllm/v1/chat/completions";
private static String secret="eGo5EBziRSKRPDuTr20oRt9C6uXy";
public static void main(String[] args) {
HashMap<String, Object> msgMap = new HashMap<>();
msgMap.put("role","user");
msgMap.put("content","LinkWechat是企业微信开源Scrm");
HashMap<String, Object> map = new HashMap<>();
map.put("app_id",1318664983);
map.put("secret_id",secretId);
map.put("timestamp", System.currentTimeMillis()/1000);
map.put("expired", System.currentTimeMillis()/1000+24*60*60);
map.put("query_id",UUID.randomUUID().toString());
map.put("temperature",1);
map.put("top_p",0.8);
map.put("stream",0);
map.put("messages",JSONUtil.toJsonStr(Arrays.asList(msgMap)));
String signature=Base64.encode(new HMac(HmacAlgorithm.HmacSHA1, secret.getBytes()).digest(formatUrlMap(map)));
map.remove("messages");
map.put("messages",Arrays.asList(msgMap));
String result = HttpRequest.post(url)
.header("Content-Type", "application/json")
.header("Authorization", signature)
.body(JSONUtil.toJsonStr(map))
.execute().body();
Result result1 = JSONUtil
.toBean(result.replace("data:", ""), Result.class);
StringBuilder sb=new StringBuilder();
if(null != result1){
List<Choices> choices = result1.getChoices();
if(CollectionUtil.isNotEmpty(choices)){
choices.stream().forEach(choice -> {
sb.append(choice.getMessages().getContent());
});
}
}
System.out.println(sb.toString());
}
public static String formatUrlMap(Map<String, Object> paraMap) {
String buff;
try {
List<Map.Entry<String, Object>> infoIds = new ArrayList<>(paraMap.entrySet());
// 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序)
Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() {
@Override
public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
return (o1.getKey()).compareTo(o2.getKey());
}
});
// 构造URL 键值对的格式
StringBuilder buf = new StringBuilder();
for (Map.Entry<String, Object> item : infoIds) {
if (!"".equals(item.getKey())) {
String key = item.getKey();
Object val = item.getValue();
buf.append(key).append("=").append(val);
buf.append("&");
}
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
buff = buf.toString();
} catch (Exception e) {
return null;
}
return "hunyuan.cloud.tencent.com/hyllm/v1/chat/completions?"+buff;
}
public static class Result{
private String req_id;
private String note;
private List<Choices> choices;
public String getReq_id() {
return req_id;
}
public void setReq_id(String req_id) {
this.req_id = req_id;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public List<Choices> getChoices() {
return choices;
}
public void setChoices(List<Choices> choices) {
this.choices = choices;
}
}
public static class Choices{
private String finish_reason;
private Delta messages;
public String getFinish_reason() {
return finish_reason;
}
public void setFinish_reason(String finish_reason) {
this.finish_reason = finish_reason;
}
public Delta getMessages() {
return messages;
}
public void setMessages(Delta messages) {
this.messages = messages;
}
}
public static class Delta{
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
}
`
哈哈以上代码,只是一个单纯的对接Demo写的很随意,见谅哈!!
更多推荐
已为社区贡献1条内容
所有评论(0)