• AI编程新手快速体验SpringCloud Alibaba 集成AI功能


    上周六写了一篇文章  震撼发布!Spring AI 框架重磅上线,Java 集成 AI 轻松搞定!    部分同学可能没有科学上网的条件,本地ollama 集成又比较笨重。趁着周六,写一篇基于SpringCloud Alibaba 集成AI的文章。

    先简单介绍下 Spring Cloud Alibaba AI。

    Spring Cloud Alibaba AI 基于 Spring AI 0.8.1 版本完成通义系列大模型的接入。DashScope灵积模型服务建立在 模型即服务(Model-as-a-Service,MaaS)的理念基础之上,围绕AI各领域模型,通过标准化的API提供包括模型推理、模型微调训练在内的多种模型服务。目前支持的模型主要有:对话、文生图、文生语音,更多功能特性正在适配中。

    实践前, Spring AI 0.8.1  最低需要JDK17版本,  公司项目还是JDK8, 先去下载JDK17。下载地址:

     https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html

    1. springcloud、springboot对应小常识
    2. 2023.x 分支对应的是 Spring Cloud 2023 与 Spring Boot 3.2.x,最低支持 JDK 17
    3. 2022.x 分支对应的是 Spring Cloud 2022 与 Spring Boot 3.0.x,最低支持 JDK 17
    4. 2021.x 分支对应的是 Spring Cloud 2021 与 Spring Boot 2.6.x,最低支持 JDK 1.8
    5. 2020.0 分支对应的是 Spring Cloud 2020 与 Spring Boot 2.4.x,最低支持 JDK 1.8
    6. 2.2.x 分支对应的是 Spring Cloud Hoxton 与 Spring Boot 2.2.x,最低支持 JDK 1.8
    7. greenwich 分支对应的是 Spring Cloud Greenwich 与 Spring Boot 2.1.x,最低支持 JDK 1.8
    8. finchley 分支对应的是 Spring Cloud Finchley 与 Spring Boot 2.0.x,最低支持 JDK 1.8
    9. 1.x 分支对应的是 Spring Cloud Edgware 与 Spring Boot 1.x,最低支持 JDK 1.7



    如果你不想放弃JDK8,可以直接使用dashscope-sdk-java。 看个人选择了

     https://help.aliyun.com/zh/dashscope/developer-reference/install-dashscope-sdk?spm=a2c4g.11186623.0.i7#c2bdbca0cfc8y

    这里SpringCloud Alibaba AI快速学习, 直接去github下载 官方示例:

     https://github.com/alibaba/spring-cloud-alibaba/tree/2023.x/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example

    先去阿里云官网https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-keyapi-key, 就能快速体验基本的 对话、画图、语音功能了。 新开通有1个月的免费额度,真香。

    具体实操步骤,我直接复制官方的操作说明

    1. 接入 spring-cloud-starter-alibaba-ai
    2. 在项目 pom.xml 中加入以下依赖:
    3. <dependency>
    4.     <groupId>com.alibaba.cloud</groupId>
    5.     <artifactId>spring-cloud-starter-alibaba-ai</artifactId>
    6. </dependency>
    7. 在 application.yml 配置文件中加入以下配置:
    8. Note: 推荐使用环境变量的方式设置 api-key,避免 api-key 泄露。
    9. export SPRING_CLOUD_AI_TONGYI_API_KEY=sk-a3d73b1709bf4a178c28ed7c8b3b5a45
    10. spring:
    11.   cloud:
    12.     ai:
    13.       tongyi:
    14.         # apiKey is invalid.
    15.         api-key: sk-a3d73b1709bf4a178c28ed7c8b3b5a45
    16. 添加如下代码:
    17. controller:
    18. @Autowired
    19. @Qualifier("tongYiSimpleServiceImpl")
    20. private TongYiService tongYiSimpleService;
    21. @GetMapping("/example")
    22. public String completion(
    23.     @RequestParam(value = "message", defaultValue = "Tell me a joke")
    24.     String message
    25. ) {
    26.     return tongYiSimpleService.completion(message);
    27. }
    28. service:
    29. private final ChatClient chatClient;
    30. @Autowired
    31. public TongYiSimpleServiceImpl(ChatClient chatClient, StreamingChatClient streamingChatClient) {
    32.     this.chatClient = chatClient;
    33.     this.streamingChatClient = streamingChatClient;
    34. }
    35. @Override
    36. public String completion(String message) {
    37.     Prompt prompt = new Prompt(new UserMessage(message));
    38.     return chatClient.call(prompt).getResult().getOutput().getContent();
    39. }
    40. 至此,便完成了最简单的模型接入!和本 example 中的代码略有不同,但 example 中的代码无需修改。可完成对应功能。
    41. 启动应用
    42. 本 Example 项目支持如下两种启动方式:
    43. IDE 直接启动:找到主类 TongYiApplication,执行 main 方法启动应用。
    44. 打包编译后启动:首先执行 mvn clean package 将工程编译打包,进入 target 文件夹执行 java -jar spring-cloud-ai-example.jar 启动应用。

    先简单聊个天,    http://localhost:8080/


    再画个图测试,直接浏览器访问 http://localhost:8080/ai/img?prompt="美女"

    再来个语音测试,http://localhost:8080/ai/audio?prompt="你好,我是子晓",出现本地的音频文件路径。播放效果挺不错。

    springCloud Alibaba集成AI功能是不是很简单, 我们可以根据自己的实际需要对接到业务系统中。虽然我之前就知道springCloud Alibaba AI功能, 前不久国内几大大模型厂商价格战,我才试用了下。真香,程序员的福音,比自己折腾部署大模型方便多了。

    【人工智能】阿里、百度等大模型搞价格战了,开发者爽了,盘点下国内大模型API价格

    原文链接:【JAVA技术】AI编程新手快速体验SpringCloud Alibaba 集成AI功能

  • 相关阅读:
    《逆向工程核心原理》学习笔记(四):API钩取
    几分钟就搞定网站速度慢、网站卡等问题
    xxl-job中多节点分片的时候如何在linux服务器开启多个执行器实例?
    面试:java中的各种锁
    Kafka 开启SASL/SCRAM认证 及 ACL授权(三)验证
    QT信号槽实现分析
    Linux操作系统网络模块
    初入数据库
    WPF绘图(基础图形:直线、矩形和椭圆)
    LeetCode //C - 918. Maximum Sum Circular Subarray
  • 原文地址:https://blog.csdn.net/xiaoliouc/article/details/139364928