阿里云百炼文档地址: 百炼控制台 

设置账号

首先跟着文档设置账号,新建一个api key

文档地址: 百炼控制台

 对接会话API

你可以使用sdk来对接,但没有必要,因为所有接口对接都是http形式的,直接使用http库来对接就行了,使用http库还有一个好处是所有大模型的调用方式是相通的,你想换一个模型调用会非常方便,这里我使用okhttp库,下面的okhttp-sse库是非必须的,只有使用流式传输的时候需要

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp-sse</artifactId>
    <version>4.10.0</version>
</dependency>

我们来对接一个最简单的聊天接口,只要按照下图所示发送http请求就行了,其中Authorization放入刚申请的api key

public void testChat() {
    String url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
    String appKey = "你自己的key";
    JSONArray messages = new JSONArray()
            .set(new JSONObject()
                    .set("role", "system")
                    .set("content", "You are a helpful assistant.")
            )
            .set(new JSONObject()
                    .set("role", "user")
                    .set("content", "你是谁?")
            );
    try {
        String requestBody = new JSONObject()
                .putOpt("model", "qwen-plus")
                .putOpt("messages", messages)
                .toString();
        Request okhttpRequest = new Request.Builder()
                .url(url)
                .post(RequestBody.create(requestBody, MediaType.get(ContentType.JSON.getValue())))
                .addHeader("Authorization", "Bearer " + appKey)
                .build();
        Call call = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                .build()
                .newCall(okhttpRequest);
        Response okhttpResponse = call.execute();

        JSONObject data = JSONUtil.parseObj(IoUtil.read(okhttpResponse.body().charStream()));
        System.out.println(data.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getStr("content"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 输出:

我是通义千问,阿里巴巴集团旗下的超大规模语言模型。我能够回答问题、创作文字,如写故事、公文、邮件、剧本等,还能进行逻辑推理、编程,甚至表达观点和玩游戏。我在多国语言上都有很好的掌握,能为你提供多样化的帮助。有什么我可以帮到你的吗?

视觉理解

文档:百炼控制台

有以下图片,我们让AI来理解下图片的内容

public void testImage() {
    String url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
    String appKey = "你的key";
    JSONArray finalJsonArray = new JSONArray()
            .set(new JSONObject()
                    .set("role", "system")
                    .set("content", new JSONArray()
                            .set(new JSONObject()
                                    .set("type", "text")
                                    .set("text", "You are a helpful assistant.")
                            )
                    )
            )
            .set(new JSONObject()
                    .set("role", "user")
                    .set("content", new JSONArray()
                            .set(new JSONObject()
                                    .set("type", "image_url")
                                    .set("image_url", new JSONObject()
                                            .set("url", "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg")
                                    )
                            )
                            .set(new JSONObject()
                                    .set("type", "text")
                                    .set("text", "图中描绘的是什么景象?")
                            )
                    )
            );
    try {
        String requestBody = new JSONObject()
                .putOpt("model", "qwen-vl-max")
                .putOpt("messages", finalJsonArray)
                .toString();
        Request okhttpRequest = new Request.Builder()
                .url(url)
                .post(RequestBody.create(requestBody, MediaType.get(ContentType.JSON.getValue())))
                .addHeader("Authorization", "Bearer " + appKey)
                .build();
        Call call = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                .build()
                .newCall(okhttpRequest);
        Response okhttpResponse = call.execute();

        JSONObject data = JSONUtil.parseObj(IoUtil.read(okhttpResponse.body().charStream()));
        System.out.println(data.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getStr("content"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 输出:

图中描绘的是一个人和一只狗在海滩上互动的景象。这个人穿着格子衬衫,坐在沙滩上,与一只戴着项圈的黄色拉布拉多犬握手。背景是海浪和天空,阳光洒在他们身上,营造出温暖和谐的氛围。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐