Spring AI + 通义千问 入门学习
文章目录
- Spring AI + 通义千问 入门学习
- 一,开发环境配置
- 二,项目搭建
- 2.1 pom文件
- 2.2 配置文件
- 三,AI使用
- 3.1 对话问答
- 3.1.1 普通方式
- 3.1.2 流方式
- 3.2 文字生成图片
最近AI很火,而Spring也出了Spring AI,可以让开发者更容易对接各种大模型。在使用Spring AI之前,我的jdk还是保持8这一久远版本,因此趁此机会也是顺便切换到jdk21,然后也顺便将Spring的各个组件进行升级。
一,开发环境配置
组件 | 版本 |
---|---|
jdk | 21 |
Spring Framework | 6.1.11 |
Spring Boot | 3.3.2 |
Spring Cloud | 2023.0.3 |
Nacos | 2.4 |
Spring Security | 6.3.1 |
Spring AI | 1.0.0 |
spring.ai.alibaba | 2023.0.1.0 |
Maven中pom文件的相关配置如下所示:
<properties><java.version>21</java.version><spring.boot.version>3.3.2</spring.boot.version><spring.cloud.version>2023.0.3</spring.cloud.version><spring.cloud.gateway.version>4.1.5</spring.cloud.gateway.version><spring.cloud.nacas.version>2023.0.1.2</spring.cloud.nacas.version><spring.cloud.bootstrap.version>4.0.0</spring.cloud.bootstrap.version><spring.cloud.starter.loadbalancer.version>4.1.4</spring.cloud.starter.loadbalancer.version><spring.ai.version>1.0.2</spring.ai.version><spring.ai.openai.verision>1.0.0</spring.ai.openai.verision><spring.ai.ollama.verision>1.0.0</spring.ai.ollama.verision><spring.ai.alibaba.verision>2023.0.1.0</spring.ai.alibaba.verision><lombok.version>1.18.34</lombok.version></properties>
二,项目搭建
2.1 pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.lmc</groupId><artifactId>lmc-base-demo</artifactId><version>1.0-SNAPSHOT</version></parent><groupId>com.lmc</groupId><artifactId>lmc-hello-ai</artifactId><properties><maven.compiler.source>21</maven.compiler.source><maven.compiler.target>21</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-undertow</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><!--排除tomcat依赖 --><exclusions><exclusion><artifactId>spring-boot-starter-tomcat</artifactId><groupId>org.springframework.boot</groupId></exclusion></exclusions></dependency><!-- Spring WebFlux --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></dependency><!-- 如果使用Maven --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-ai</artifactId><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId></exclusion></exclusions></dependency><!--两种国外的LLM大模型--><!-- <dependency>--><!-- <groupId>io.springboot.ai</groupId>--><!-- <artifactId>spring-ai-openai-spring-boot-starter</artifactId>--><!-- </dependency>--><!-- <dependency>--><!-- <groupId>io.springboot.ai</groupId>--><!-- <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>--><!-- </dependency>--></dependencies></project>
启动类没有需要做特殊配置,这里不做展示
2.2 配置文件
配置文件主要配置nacos和通义千问的api-key,如下所示:
server:port: 50010servlet:context-path: /hello-aispring:application:name: lmc-hello-aiprofiles:active: devcloud:nacos:server-addr: 127.0.0.1:8848discovery:namespace: ${your-namespace}group: ${spring.profiles.active}config:namespace: ${your-namespace}group: ${spring.profiles.active}prefix: ${spring.application.name}file-extension: yamlimport-check:enabled: falseai:tongyi:api-key: ${your-tyqw-api-key}main:allow-bean-definition-overriding: true
三,AI使用
3.1 对话问答
3.1.1 普通方式
@RestController
@RequestMapping("/chat")
@Slf4j
public class ChatController {@Resourceprivate ChatClient chatClient;/*** 简单调用接口example 对话问答* @param message* @return*/@RequestMapping("/example")public String example(@RequestParam("message") String message) {Prompt prompt = new Prompt(new UserMessage(message));return chatClient.call(prompt).getResult().getOutput().getContent();}}
启动后,只要调用接口:localhost:50010/hello-ai/chat/example?message=如何学习Java
就能得到通义千问对该问题的回答。
3.1.2 流方式
@RestController
@RequestMapping("/chat")
@Slf4j
public class ChatController {@Resourceprivate StreamingChatClient streamingChatClient;/*** 流式响应 对话问答* @param message* @return*/@RequestMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)public Flux<String> stream(@RequestParam(value = "message", defaultValue = "1+1=?") String message) {return streamingChatClient.stream(message);}}
3.2 文字生成图片
@RestController
@RequestMapping("/image")
@Slf4j
public class ImageController {/*** 文本生成图片Client*/@Resourceprivate TongYiImagesClient imagesClient;@RequestMapping("/create")public String image(String message) {// 封装信息ImageMessage imageMessage = new ImageMessage(message);// 创建PromptImagePrompt imagePrompt = new ImagePrompt(Collections.singletonList(imageMessage));// 调用文本生成图片ClientImage output = imagesClient.call(imagePrompt).getResult().getOutput();// 生成图片URLreturn output.getUrl();}}