Spring AI
什么是Spring AI
Spring AI是一款用于AI工程
的应用框架,就是把现在开发中的三层架构或MVC中的那一套搬到AI里面去。
Spring AI目前支持的功能包括下面一些。
支持主流的模型提供商,例如,DALL·E、Stable Diffusion和HuggingFace等。
支持聊天、文生图、文生语音、音频转录等。
至于其他的功能基本上可以无视,因为它本质上就是用Spring WebFlux把常见大模型提供商的接口封装了一遍,然后改个名字叫Spring AI。
有两种方式创建Spring AI项目。
通过添加特定组件的依赖项从头搭建应用。
通过示例项目直接在现成的基础上来改。
从头搭建
通过Spring Initializr创建一个Spring AI项目。
首先单击ADD DEPENDENCIES...⌘ + B
,然后拖到最下面,选择相关的AI依赖。

这里选择了经典的OpenAI和Stable Diffusion。

然后单击GENERATE ⌘ + ⏎
创建项目。
将创建的项目导入到IDEA就行了。
修改示例
官方提供的示例项目也就三个,可以先从第一个OpenAI-Helloworld开始。
整个项目也就三个文件(不包括配置文件,因为没有):Application
、AIController
和Config
。
从名字就能看出来都是干嘛的。
因为没有配置文件,所以就自己写一个(pom.xml
中的内容保持不变)。
spring:
ai:
openai:
api-key: sk-XXXXX......
chat:
options:
# 这里如果不写,那默认就是gpt-4o
model: gpt-3.5-turbo
启动项目,通过Postman访问http://localhost:8080/ai,会碰到最常见的三个错误。
Error while extracting response
......
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.web.client.RestClientException: Error while extracting response for type [org.springframework.ai.openai.api.OpenAiApi$ChatCompletion] and content type [application/json;charset=utf-8]] with root cause
......
这大概率是因为没有在ChatGPT中设置secret key
。
可以在https://platform.openai.com/settings/profile?tab=api-keys处设置。
billing details
......
org.springframework.ai.retry.NonTransientAiException: 429 - {
"error": {
"message": "You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.",
"type": "insufficient_quota",
"param": null,
"code": "insufficient_quota"
}
}
......
这是因为账户没钱了(OpenAI的免费额度极少)。
“gpt-4o” does not exist
......
org.springframework.ai.retry.NonTransientAiException: 404 - {
"error": {
"message": "The model `gpt-4o` does not exist or you do not have access to it.",
"type": "invalid_request_error",
"param": null,
"code": "model_not_found"
}
}
......
在配置文件或代码中改成gpt-3.5-turbo
就好了。
代码中这样改。
package org.springframework.ai.openai.samples.helloworld;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
class AIController {
private final ChatClient chatClient;
public AIController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@GetMapping("/ai")
Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
// Map<String, String> map = new HashMap<>();
// map.put("Authorization", "Bearer sk-XXX...");
return Map.of(
"completion",
chatClient.prompt(message)
// 官方实例代码中没有,可以自己增加构建选项
.options(OpenAiChatOptions.builder()
// 指定gpt版本
.withModel(OpenAiApi.ChatModel.GPT_3_5_TURBO)
// 温度越高越发散,越低越准确
.withTemperature(0.5D)
// 显示最大token
.withMaxTokens(4096)
// // 指定种子值,以后每次生成会以这个种子值为参照生成相似的内容(一般都不需要)
// .withSeed(1)
// // 增加http请求头
// .withHttpHeaders(map)
// 更多的选项值可以看源码,里面都解释的很清楚
.build())
.user("lixingyun")
.call()
.content());
}
}
感谢支持
更多内容,请移步《超级个体》。