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

2023-12-29 09:34:56


前言

本篇文章基于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
在这里插入图片描述
就会有个Accesskeyid和Secret值(这俩值要记住真正的开发中有用,本文章没用到)的弹窗,然后点击已创建的AIP-KEY查看并复制,这个会用到。

在这里插入图片描述

在这里插入图片描述

三、📝基于java实现调用

📣打开IDEA,开始操作了

1.设置API-KEY

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

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

2.体验大语言模型

多轮对话演示

创建一个测试类:
在这里插入图片描述
代码如下:

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import com.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 com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.ResultCallback;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import io.reactivex.Flowable;
import com.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 onComplete(){
        System.out.println("Completed");
        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 com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.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-cn-beijing.aliyuncs.com/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 com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import io.reactivex.Flowable;
import com.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 com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.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-cn-beijing.aliyuncs.com/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 com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import io.reactivex.Flowable;
import com.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-cn-beijing.aliyuncs.com/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🌤?)。

文章来源:https://blog.csdn.net/weixin_62951900/article/details/135277506
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。