Bootstrap

JAVA对接语言大模型,百度云(文心一言)、阿里(通义千问)、科大讯飞(星火认知大模型)

科大讯飞
有免费使用次数
官网:授权接口文档
获取必要参数:API_KEY、API_SECRET
在这里插入图片描述

public static final String URL = "https://spark-api.xf-yun.com/v3.1/chat";
public static final String API_SECRET = "**********";
public static final String API_KEY = "**********";

public String getAuthorization() throws Exception {
        URL url = new URL(URL);
        SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        String date = format.format(new Date());
        String preStr = "host: " + url.getHost() + "\n" +
                "date: " + date + "\n" +
                "GET " + url.getPath() + " HTTP/1.1";
        Mac mac = Mac.getInstance("hmacsha256");
        SecretKeySpec spec = new SecretKeySpec(API_SECRET.getBytes(StandardCharsets.UTF_8), "hmacsha256");
        mac.init(spec);
        byte[] hexDigits = mac.doFinal(preStr.getBytes(StandardCharsets.UTF_8));
        String sha = Base64.getEncoder().encodeToString(hexDigits);
        String authorization = String.format("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", API_KEY, "hmac-sha256", "host date request-line", sha);
        HttpUrl httpUrl = Objects.requireNonNull(HttpUrl.parse("https://" + url.getHost() + url.getPath())).newBuilder().
                addQueryParameter("authorization", Base64.getEncoder().encodeToString(authorization.getBytes(StandardCharsets.UTF_8))).
                addQueryParameter("date", date).
                addQueryParameter("host", url.getHost()).
                build();
        return httpUrl.toString().replaceAll("http://", "ws://").replaceAll("https://","wss://");
    }

页面下载地址:UNI-APP
使用的那个版本要改为一致,不然会出现错误

百度大模型
无免费使用
获取必要参数连接:获取权限参数地址
在这里插入图片描述

 public static final String API_KEY = "*********";
    public static final String SECRET_KEY = "*********";

    @GetMapping("/send")
    public String send(String messages) {
        String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" + getAccessToken();
        HashMap<String, String> msg = new HashMap<>();
        msg.put("role", "user");
        msg.put("content", messages);
        ArrayList<HashMap> messagesList = new ArrayList<>();
        messagesList.add(msg);
        HashMap<String, Object> requestBody = new HashMap<>();
        requestBody.put("messages", messagesList);
        String parame = JSON.toJSONString(requestBody);
        Map map = JSONObject.parseObject(HttpUtil.post(url, parame), Map.class);
        return map.get("result").toString();
    }
	
    public String getAccessToken() {
        String url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + API_KEY + "&client_secret=" + SECRET_KEY;
        Map map = JSONObject.parseObject(HttpUtil.get(url), Map.class);
        return map.get("access_token").toString();
    }

阿里大模型
提供免费使用(限时)
获取必要参数:Authorization获取
在这里插入图片描述

在这里插入图片描述

	@GetMapping("/send")
    public Object callWithMessage(String messages) {
        String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
        Map<String, Object> map = new HashMap<String, Object>();
        // qwen-max 是免费版本
        map.put("model", "qwen-max");
        Map<String, String> input = new HashMap<String, String>();
        input.put("prompt", messages);
        map.put("input", input);
        String parame = JSON.toJSONString(map);
        HttpResponse response = HttpRequest.post(url)
                .header("Authorization", "******************")
                .body(parame)
                .execute();
        Map body = JSONObject.parseObject(response.body(), Map.class);
        Map res = JSONObject.parseObject(body.get("output").toString(), Map.class);
        return res.get("text");
    }

总结:
响应速度:讯飞(即时通讯所以比较快) > 阿里 > 百度 (经供参考,只是我的测试速度)。推荐科大讯飞,速度较快免费送的也够用来测试。感觉安全性也好一点。

;