赞
踩
首先附上Spring Ai的官方文档,以便后面去深入学习
官方文档:Prompts :: Spring AI Reference
SpringAI的认识
Spring AI项目旨在简化包含人工智能功能的应用程序的开发,而不会产生不必要的复杂性,核心是提供抽象,作为开发AI应用程序的基础。 这些抽象有多个实现,可以用最少的代码更改轻松地进行组件交换。
Spring Ai 怎么对接Open AI
准备:1.OpenAI 的Api Key
2.JDK 17(这个可用可不用)
会用到的依赖
- <dependencies>
-
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- <version>4.5.13</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.ai</groupId>
- <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- <optional>true</optional>
- </dependency>
-
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.ai</groupId>
- <artifactId>spring-ai-bom</artifactId>
- <version>${spring-ai.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <repositories>
- <!--快照版本的仓库-->
- <repository>
- <id>spring-snapshot</id>
- <name>Spring Snapshots</name>
- <url>https://repo.spring.io/snapshot</url>
- <releases>
- <enabled>false</enabled>
- </releases>
- </repository>
- </repositories>

编写Controller接口
- @RestController
- public class ChatGPTController{
-
- @Autowired
- private OpenAiChatClient openAiChatClient;
-
- @GetMapping("/chatGPT")
- public Object chatGPT(@RequestParam("message") String message){
- String result = openAiChatClient.call(message);
- return result;
- }
-
- }
Config配置文件格式
- server:
- port: 8088
-
- spring:
- application:
- name: Firstcht
-
- ai:
- openai:
- api-key: xxx
- base-url: openai的地址
以上只是简单的使用,更多深入可以通过官方文档学习
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。