spring-ai简单示例
文章目录
- 一、不使用spring-ai时直接使用RestClient调用接口
- 二、使用spring ai
- 1、新增maven依赖
- 2、application.properties设置
- 3、controller
spring ai主要用于提供一致的编程模型,屏蔽不同 AI 服务提供商(如 OpenAI、Azure AI、Hugging Face 等)的底层差异。
一、不使用spring-ai时直接使用RestClient调用接口
controller:
package com.yogi.controller;import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClient;import java.util.List;@RestController
@RequestMapping("/ai")
public class AITestController {@GetMapping("/debug-response")@Observedpublic String debugResponse() {try {RestClient restClient = RestClient.create();String response = restClient.post().uri("https://maas.xxxxx.com/v1/chat/completions").header("Authorization", "Bearer sk-xxxxxxxxxx").contentType(MediaType.APPLICATION_JSON).body("{\"model\": \"DeepSeek-V3.1\", \"messages\": [{\"role\": \"user\", \"content\": \"Hello\"}]}").retrieve().body(String.class);return "API响应: " + response;} catch (Exception e) {return "错误: " + e.getMessage();}}
}
二、使用spring ai
1、新增maven依赖
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId><version>0.8.1</version>
</dependency>
注意我这里用的Spring AI 0.8.1 需要java17及以上版本 spring boot3.2.0及以上版本
这里我用的3.2.6,完整的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.yogi</groupId><artifactId>SpringAITest</artifactId><version>0.0.1-SNAPSHOT</version><name>SpringAITest</name><description>SpringAITest</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId><version>0.8.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2、application.properties设置
注意这里的baseUrl和直接调用相比少了"/v1/chat/completions",因为spring ai会自动拼接。
spring.ai.openai.api-key=sk-xxxxxxxxxx
spring.ai.openai.base-url=https://maas.xxxxxxx.com
spring.ai.openai.chat.options.model=DeepSeek-V3.1
3、controller
package com.yogi.controller;import org.springframework.ai.chat.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/ai")
public class AITestController {private final ChatClient chatClient;// 直接注入统一的 ChatClient@Autowiredpublic AITestController(ChatClient chatClient) {this.chatClient = chatClient;}@GetMapping("/ask")public String ask(@RequestParam String question) {// 调用 call 方法,直接获取字符串回复return chatClient.call(question);}
}