个人随笔
目录
SpringAI2.0环境搭建
2026-10-29 22:41:02

Spring AI项目旨在简化集成人工智能功能的应用程序开发,避免不必要的复杂性。灵感源自 LangChain 和 LlamaIndex 等知名 Python 项目,但 Spring AI 并非这些项目的直接移植。该项目的初衷在于:下一代生成式 AI 应用将不仅面向 Python 开发者,更将在多种编程语言中实现广泛普及。

一、环境

1.Spring AI 2.0.x 版本支持 Spring Boot 4.0.x 和 4.1.x。
2.jdk需要17及以上
3.IDEA需要2025.3的版本以上

二、代码

1、pom.xml

这里用的是openai,因为我用的模型是阿里百炼的模型,可以兼容openai的api

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>4.1.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.suibibk</groupId>
  12. <artifactId>spring-ai-test</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>spring-ai-test</name>
  15. <description>spring-ai-test</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. <spring-ai.version>2.0.1</spring-ai.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.ai</groupId>
  27. <artifactId>spring-ai-starter-model-openai</artifactId>
  28. </dependency>
  29. </dependencies>
  30. <dependencyManagement>
  31. <dependencies>
  32. <dependency>
  33. <groupId>org.springframework.ai</groupId>
  34. <artifactId>spring-ai-bom</artifactId>
  35. <version>${spring-ai.version}</version>
  36. <type>pom</type>
  37. <scope>import</scope>
  38. </dependency>
  39. </dependencies>
  40. </dependencyManagement>
  41. <build>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-maven-plugin</artifactId>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. </project>

2、application.yaml

这里配置的模型都是阿里百炼我还有免费额度的,api-key放在的是系统的环境变量里面。要注意一点是ai下面的配置不同的springai版本貌似不一致,最准确的还是直接自己去搜配置类吧,比如这里用IDEA的快捷键(Ctrl+N)然后搜索OpenAiChatAutoConfiguration可以找到chat的配置类OpenAiChatProperties.搜OpenAiEmbeddingAutoConfiguration可以找到OpenAiEmbeddingProperties的配置类,从类里面就可以找到配置的具体key。

  1. spring:
  2. application:
  3. name: spring-ai-test
  4. ai:
  5. openai:
  6. api-key: ${AI_BAILIAN_API_KEY}
  7. base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
  8. chat:
  9. model: qwen3.8-flash
  10. temperature: 0.7
  11. embedding:
  12. model: qwen3.7-text-embedding-flash
  13. dimensions: 2048

3、AIConfigration.java

AI的配置类,这里创建chatClient,放在这里的好处是后面对接不同的模型比如Ollama这些本地模型以及deepseek的一些模型都可以自己创建对应的chatClient

  1. @Configuration
  2. public class AIConfigration {
  3. @Bean
  4. public ChatClient openaiChatClient(OpenAiChatModel openAiChatModel){
  5. return ChatClient.
  6. builder(openAiChatModel) //设置模型
  7. .build();//创建对象
  8. }
  9. }

4、TestController.java

  1. @RestController
  2. public class TestController {
  3. @Autowired
  4. private ChatClient openaiChatClient;
  5. @GetMapping("/hello")
  6. public String hello(){
  7. return "hello";
  8. }
  9. /**
  10. * 直接请求
  11. * @param question
  12. * @return
  13. */
  14. @GetMapping("/ai")
  15. public String ai(String question){
  16. String content = openaiChatClient
  17. .prompt()
  18. .user(question)
  19. .call()
  20. .content();
  21. return content;
  22. }
  23. /**
  24. * 流式输出
  25. * @param question
  26. * @return
  27. */
  28. @GetMapping(value = "/aiStream",produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
  29. public Flux<String> aiStream(String question){
  30. Flux<String> content = openaiChatClient
  31. .prompt()
  32. .user(question)
  33. .stream()
  34. .content();
  35. return content;
  36. }
  37. }

三、补充

1、官方文档

https://docs.spring.io/spring-ai/reference/getting-started.html
用夸克直接翻译贼好用,如果英语不好的话

2、配置项

配置如果有问题,建议直接搜源码或者问AI

 3

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2