AIGC盛行,带你轻松调用开发


前言

本篇文章基于java和阿里云的通义千问大模型手把手带你使用AIGC开发,实现文本对话和图像分析。


一、📖AIGC简介

你知道什么是AIGC吗?不知道?没关系,我来告诉你:

  • AIGC就是由AI自动创作生成的内容(AI Generated Content),比如图片、视频、音乐、文字等。AIGC就像一支神奇的画笔,拥有无限的创造力。这支画笔的特别之处在于,它是由AI打造的。AI利用它的理解力、想象力和创作力,根据指定的需求和风格,创作出各种内容:文章、短篇小说、报告、音乐、图像,甚至是视频。AIGC的出现,打开了一个全新的创作世界,为人们提供了无数的可能性
  • AI,就是人工智能。它的目标是让机器能够像人一样有智能,能够看、听、说、想、做。要实现这个目标,AI需要用到重要的技术:深度学习NLP(自然语言处理神经网络生成式对抗网络(GAN)

🌤️通过上面的讲诉,无论你曾经不知道还是模棱两可,现在应该大概了解了,下面带你走进AIGC,用程序员的身份打开AIGC。

二、📣开通体验

开通模型获取API-KEY

大家不要害怕开通不会产生收费,本文讲述的完全是免费就可使用的方法。

登录阿里云进入控制台后,鼠标移到下图位置

然后出现如下界面,找到人工智能与机器学习下的模型服务灵积,点击

参照开发文档,开通DashScope并创建API-KEY

总结文档上的开通步骤如下:

在控制台中点击总览,去开通


立开通(大家不要怕,这是不会产生扣费的)

开通后,点击 管理中心 的 API-KEY 菜单项,然后创建新的API-KEY

就会有个A***esskeyid和Secret值(这俩值要记住真正的开发中有用,本文章没用到)的弹窗,然后点击已创建的AIP-KEY查看并复制,这个会用到。

三、📝基于java实现调用

📣打开IDEA,开始操作了

1.设置API-KEY

先了解以下如何设置API-KEY值的:

import ***.alibaba.dashscope.utils.Constants;
Constants.apiKey="这就是你的API-KEY的值(上面复制的)";

2.体验大语言模型

多轮对话演示

创建一个测试类:

代码如下:

import ***.alibaba.dashscope.aigc.generation.Generation;
import ***.alibaba.dashscope.aigc.generation.GenerationResult;
import ***.alibaba.dashscope.aigc.generation.models.QwenParam;
import ***.alibaba.dashscope.***mon.Message;
import ***.alibaba.dashscope.***mon.MessageManager;
import ***.alibaba.dashscope.***mon.Role;
import ***.alibaba.dashscope.exception.ApiException;
import ***.alibaba.dashscope.exception.InputRequiredException;
import ***.alibaba.dashscope.exception.NoApiKeyException;
import ***.alibaba.dashscope.utils.Constants;
import ***.alibaba.dashscope.utils.JsonUtils;
import org.mockito.internal.matchers.Contains;

import java.awt.*;


class test {
    public static void callWithMessage()
            throws NoApiKeyException, ApiException, InputRequiredException {
        // 设置API-KEY值
        Constants.apiKey="你的API-KEY值";
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        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();//对话问题
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
                QwenParam.builder().model(Generation.Models.QWEN_PLUS).messages(msgManager.get())
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .topP(0.8)
                        .enableSearch(true)
                        .build();
        GenerationResult result = gen.call(param);
        System.out.println(result);//输出回复
        msgManager.add(result);
        System.out.println(JsonUtils.toJson(result));//JSON格式输出回复
        param.setPrompt("找个近点的");//继续问题
        param.setMessages(msgManager.get());
        result = gen.call(param);
        System.out.println(result);
        System.out.println(JsonUtils.toJson(result));
    }


    public static void main(String[] args){
        try {
            callWithMessage();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}

上诉代码就是向模型提问周末去哪玩,模型给出回复后,又继续说让他说个近点的地方


补充流式输出

import java.util.Arrays;
import java.util.concurrent.Semaphore;
import ***.alibaba.dashscope.aigc.generation.Generation;
import ***.alibaba.dashscope.aigc.generation.GenerationResult;
import ***.alibaba.dashscope.aigc.generation.models.QwenParam;
import ***.alibaba.dashscope.***mon.Message;
import ***.alibaba.dashscope.***mon.ResultCallback;
import ***.alibaba.dashscope.***mon.Role;
import ***.alibaba.dashscope.exception.ApiException;
import ***.alibaba.dashscope.exception.InputRequiredException;
import ***.alibaba.dashscope.exception.NoApiKeyException;
import ***.alibaba.dashscope.utils.JsonUtils;
import io.reactivex.Flowable;
import ***.alibaba.dashscope.utils.Constants;

public class Main {
  public static void streamCallWithMessage()
      throws NoApiKeyException, ApiException, InputRequiredException {
     Constants.apiKey="你的API-KEY值";
    Generation gen = new Generation();
    Message userMsg = Message
    .builder()
    .role(Role.USER.getValue())
    .content("如何做西红柿炖牛腩?")
    .build();
    QwenParam param =
        QwenParam.builder().model(Generation.Models.QWEN_PLUS).messages(Arrays.asList(userMsg))
            .resultFormat(QwenParam.ResultFormat.MESSAGE)
            .topP(0.8)
            .enableSearch(true)
            .incrementalOutput(true) // get streaming output incrementally
            .build();
    Flowable<GenerationResult> result = gen.streamCall(param);
    StringBuilder fullContent = new StringBuilder();
    result.blockingForEach(message -> {
      fullContent.append(message.getOutput().getChoices().get(0).getMessage().getContent());
      System.out.println(JsonUtils.toJson(message));
    });
    System.out.println("Full content: \n" + fullContent.toString());
  }

  public static void streamCallWithCallback()
      throws NoApiKeyException, ApiException, InputRequiredException,InterruptedException {
    Generation gen = new Generation();
    Message userMsg = Message
    .builder()
    .role(Role.USER.getValue())
    .content("如何做西红柿炖牛腩?")
    .build();
    QwenParam param = QwenParam
    .builder()
    .model(Generation.Models.QWEN_PLUS)
    .resultFormat(QwenParam.ResultFormat.MESSAGE)
    .messages(Arrays.asList(userMsg))
    .topP(0.8)
    .incrementalOutput(true) // get streaming output incrementally
    .build();
    Semaphore semaphore = new Semaphore(0);
    StringBuilder fullContent = new StringBuilder();
    gen.streamCall(param, new ResultCallback<GenerationResult>() {

      @Override
      public void onEvent(GenerationResult message) {
        fullContent.append(message.getOutput().getChoices().get(0).getMessage().getContent());
        System.out.println(message);
      }
      @Override
      public void onError(Exception err){
        System.out.println(String.format("Exception: %s", err.getMessage()));
        semaphore.release();
      }

      @Override
      public void on***plete(){
        System.out.println("***pleted");
        semaphore.release();
      }
      
    });
    semaphore.acquire();
    System.out.println("Full content: \n" + fullContent.toString());
  }
  public static void main(String[] args) {
    try {
      streamCallWithMessage();
    } catch (ApiException | NoApiKeyException | InputRequiredException e) {
      System.out.println(e.getMessage());
    }
    try {
      streamCallWithCallback();
    } catch (ApiException | NoApiKeyException | InputRequiredException | InterruptedException e) {
      System.out.println(e.getMessage());
    }
    System.exit(0);
  }
}

3.体验通义千问VL

下面使用接口实现图片解析

使用官方提供照片

首先大家看一下官方提供的图片:

/**
 * @Version: 1.0.0
 * @Author: Dragon_王
 * @ClassName: test1
 * @Description: TODO描述
 * @Date: 2023/12/28 17:41
 */

import java.util.Arrays;
import java.util.Collections;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import ***.alibaba.dashscope.***mon.MultiModalMessage;
import ***.alibaba.dashscope.***mon.Role;
import ***.alibaba.dashscope.exception.ApiException;
import ***.alibaba.dashscope.exception.NoApiKeyException;
import ***.alibaba.dashscope.exception.UploadFileException;
import ***.alibaba.dashscope.utils.Constants;

class test1 {
    public static void simpleMultiModalConversationCall()
            throws ApiException, NoApiKeyException, UploadFileException {
        Constants.apiKey="你的api-key";
        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
                .content(Arrays.asList(Collections.singletonMap("image", "https://dashscope.oss-***-beijing.aliyuncs.***/images/dog_and_girl.jpeg"),
                        Collections.singletonMap("text", "这是什么?"))).build();// 对话的问题
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                .model(MultiModalConversation.Models.QWEN_VL_PLUS)
                .message(userMessage)
                .build();
        MultiModalConversationResult result = conv.call(param);
        System.out.println(result);
    }

    public static void main(String[] args) {
        try {
            simpleMultiModalConversationCall();
        } catch (ApiException | NoApiKeyException | UploadFileException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}

本地文件

import java.util.Arrays;
import java.util.HashMap;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import ***.alibaba.dashscope.***mon.MultiModalMessage;
import ***.alibaba.dashscope.***mon.Role;
import ***.alibaba.dashscope.exception.ApiException;
import ***.alibaba.dashscope.exception.NoApiKeyException;
import ***.alibaba.dashscope.exception.UploadFileException;
import io.reactivex.Flowable;
import ***.alibaba.dashscope.utils.Constants;

public class MultiModalConversationLocalFile2 {
    /**
     * sample of use local file
     * Windows file format: file:///D:/test/images/test.png
     * Linux & Mac format: file://The_absolute_local_path
     * 
     */
    public static void callWithLocalFile()
            throws ApiException, NoApiKeyException, UploadFileException {
        Constants.apiKey="你的api-key";
        String localFilePath = "file://The_file_absolute_path"; 
        MultiModalConversation conv = new MultiModalConversation();
        // must create mutable map.
        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
        .content(Arrays.asList(new HashMap<String, Object>(){{put("image", localFilePath);}}, 
        new HashMap<String, Object>(){{put("text", "这是什么?");}})).build();// 对话问题
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                .model(MultiModalConversation.Models.QWEN_VL_PLUS)
                .message(userMessage)
                .build();
        Flowable<MultiModalConversationResult> result = conv.streamCall(param);
        result.blockingForEach(item -> {
            System.out.println(item);
        });
    }

    public static void main(String[] args) {
        try {
            callWithLocalFile();
        } catch (ApiException | NoApiKeyException | UploadFileException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}


多轮对话

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import ***.alibaba.dashscope.***mon.MultiModalMessage;
import ***.alibaba.dashscope.***mon.Role;
import ***.alibaba.dashscope.exception.ApiException;
import ***.alibaba.dashscope.exception.NoApiKeyException;
import ***.alibaba.dashscope.exception.UploadFileException;
import ***.alibaba.dashscope.utils.Constants;

public class Main {
    private static final String modelName = "qwen-vl-plus";
    public static void MultiRoundConversationCall() throws ApiException, NoApiKeyException, UploadFileException {
    	Constants.apiKey="你的api-key";
        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessage systemMessage = MultiModalMessage.builder().role(Role.SYSTEM.getValue())
        .content(Arrays.asList(Collections.singletonMap("text", "You are a helpful assistant."))).build();

        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
        .content(Arrays.asList(Collections.singletonMap("image", "https://dashscope.oss-***-beijing.aliyuncs.***/images/dog_and_girl.jpeg"), 
        Collections.singletonMap("text", "图片里有什么动物?"))).build();// 第一个对话问题
        List<MultiModalMessage> messages = new ArrayList<>();
        messages.add(systemMessage);
        messages.add(userMessage);
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                .model(MultiModalConversationUsage2.modelName)
                .messages(messages)
                .build();
        MultiModalConversationResult result = conv.call(param);
        System.out.println(result);
        // add the result to conversation
        messages.add(result.getOutput().getChoices().get(0).getMessage());
        MultiModalMessage msg = MultiModalMessage.builder().role(Role.USER.getValue())
        .content(Arrays.asList(Collections.singletonMap("text", "图片动物是什么?"))).build();// 第二个对话问题
        messages.add(msg);
        // new messages
        param.setMessages((List)messages);
        result = conv.call(param);
        System.out.print(result);
    }

    public static void main(String[] args) {
        try {
            MultiRoundConversationCall();
        } catch (ApiException | NoApiKeyException | UploadFileException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    

流式输出

只是输出采用的方式不一样

import java.util.Arrays;
import java.util.HashMap;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import ***.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import ***.alibaba.dashscope.***mon.MultiModalMessage;
import ***.alibaba.dashscope.***mon.Role;
import ***.alibaba.dashscope.exception.ApiException;
import ***.alibaba.dashscope.exception.NoApiKeyException;
import ***.alibaba.dashscope.exception.UploadFileException;
import io.reactivex.Flowable;
import ***.alibaba.dashscope.utils.Constants;

public class MultiModalConversationStream {
    public static void streamCall()
            throws ApiException, NoApiKeyException, UploadFileException {
        Constants.apiKey="你的api-key";
        MultiModalConversation conv = new MultiModalConversation();
        // must create mutable map.
        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
        .content(Arrays.asList(new HashMap<String, Object>(){{put("image", "https://dashscope.oss-***-beijing.aliyuncs.***/images/dog_and_girl.jpeg");}}, 
        new HashMap<String, Object>(){{put("text", "基于这幅图,讲一个故事");}})).build();
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                .model(MultiModalConversation.Models.QWEN_VL_PLUS)
                .message(userMessage)
                .build();
        Flowable<MultiModalConversationResult> result = conv.streamCall(param);
        result.blockingForEach(item -> {
            System.out.println(item);
        });
    }

    public static void main(String[] args) {
        try {
            streamCall();
        } catch (ApiException | NoApiKeyException | UploadFileException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}

总结

上面就是使用java调用通义千问大模型的案例,上诉代码都可以运行,如果想改变问题,就将对话问题修改就行。如果再自己写一个UI界面的话,你就拥有了自己的AIGC了(虽然是调用的API🌤️)。

转载请说明出处内容投诉
CSS教程_站长资源网 » AIGC盛行,带你轻松调用开发

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买