当前位置:   article > 正文

SpringBoot3.x 整合 Spring AI_spring boot pgvector

spring boot pgvector

Spring AI 已经发布了一段时间,虽然推出的时候就被人说只是一个套了 API 的壳,但是作为 Spring 生态的一个开源项目,用它来结合到现有业务系统中还是一个比较好的方案,毕竟像笔者当初为了接入 OpenAI 的 API,还专门学了一些 Python 代码,还使用了 fastapi 框架搭建了一套接口

创建项目

填写项目名等信息,JDK版本选择使用 21

可以看到依赖库中,最下面有一个AI模块

点开后可以看到有很多不同的AI接口模块可以选择,像比较热门的 OpenAI、谷歌的Gemini等

除此以外,还可以到官方的仓库中查看最新的 starters

spring-ai/spring-ai-spring-boot-starters at main · spring-projects/spring-ai · GitHubAn Application Framework for AI Engineering. Contribute to spring-projects/spring-ai development by creating an account on GitHub.icon-default.png?t=N7T8https://github.com/spring-projects/spring-ai/tree/main/spring-ai-spring-boot-starters像目前比较火热的 ollama、还有transformers框架也是有的

调整 pom 文件

从 idea 生成的版本是比较老的,我们需要更新一下组件库的版本,升级为 1.0.3 版本

  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>3.2.5</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>org.liurb.springboot3.crud</groupId>
  12. <artifactId>springboot3-ai</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot3-ai</name>
  15. <description>springboot3-ai</description>
  16. <properties>
  17. <java.version>21</java.version>
  18. <spring-ai.version>1.0.3</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>io.springboot.ai</groupId>
  27. <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. </dependencies>
  35. <dependencyManagement>
  36. <dependencies>
  37. <dependency>
  38. <groupId>io.springboot.ai</groupId>
  39. <artifactId>spring-ai-bom</artifactId>
  40. <version>${spring-ai.version}</version>
  41. <type>pom</type>
  42. <scope>import</scope>
  43. </dependency>
  44. </dependencies>
  45. </dependencyManagement>
  46. <build>
  47. <plugins>
  48. <plugin>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-maven-plugin</artifactId>
  51. </plugin>
  52. </plugins>
  53. </build>
  54. </project>

要注意需要删除原本 pom 文件中的 repositories 标签内容,貌似从 1.x 版本后连包名都改了,像引入的两个 starter 的 groupId 也改为了 io.springboot.ai

新版的包路径

项目资源下载​​​​​​​

编写测试用例

加入 openai 配置

在 application.properties 配置文件中加入 OpenAI 的 api key,如果需要设置 base url 也可以加上

  1. # openai 配置
  2. spring.ai.openai.base-url=your base url
  3. spring.ai.openai.api-key=your api key

测试用例代码

我们只要简单的设置一个使用模型和提问,就可以实现与 openai 的接口调用

  1. @SpringBootTest
  2. class Springboot3AiApplicationTests {
  3. @Resource
  4. OpenAiChatClient chatClient;
  5. @Test
  6. void geminiContextLoads() {
  7. ChatResponse response = chatClient.call(
  8. new Prompt(
  9. "Generate the names of 5 famous pirates.",
  10. OpenAiChatOptions.builder()
  11. .withModel("gpt-3.5-turbo")
  12. .withTemperature(0.4f)
  13. .build()
  14. ));
  15. System.out.println(response);
  16. }
  17. }

控制器用例

  1. @RestController
  2. public class ChatController {
  3. private final OpenAiChatClient chatClient;
  4. @Autowired
  5. public ChatController(OpenAiChatClient chatClient) {
  6. this.chatClient = chatClient;
  7. }
  8. @GetMapping("/ai/generate")
  9. public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
  10. return Map.of("generation", chatClient.call(message));
  11. }
  12. // 流式
  13. @GetMapping("/ai/generateStream")
  14. public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
  15. Prompt prompt = new Prompt(new UserMessage(message));
  16. return chatClient.stream(prompt);
  17. }
  18. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号