Spring AI简介
- Spring AI 是 AI 工程的应用框架。其目标是将 Spring 生态系统设计原则(如可移植性和模块化设计)应用于 AI,并推广使用 POJO 作为 AI 领域应用程序的构建块。
- 特征:跨AI 提供商的可移植 API 支持,适用于聊天、文本到图像和嵌入模型。支持同步和流 API 选项。还支持下拉以访问特定于模型的功能。
- 聊天模型:OpenAI、Azure Open AI、Amazon Bedrock、Cohere’s Command(AI21 Labs’ Jurassic-2、Meta’s LLama 2、Amazon’s Titan)、Google Vertex AI Palm、Google Gemini、HuggingFace (access thousands of models, including those from Meta such as Llama2)、Ollama(run AI models on your local machine)、MistralAI
- …
本地环境准备
-
硬件条件:最好具有N卡,ollama可以在轻量运行大模型提供支持。内存8G以上,cpu好一点。
-
软件环境:windows系统安装ollama
-
ollama:配置开发测试模型:
qwen:7b
(运行 7B 型号至少需要 8 GB 可用 RAM (内存),运行 13B 型号至少需要16 GB可用 RAM (内存) ,运行 33B 型号至少需要32 GB 可用 RAM (内存))# 下载千问 7b模型 ollama run qwen:7b
创建Spring AI项目
- 打开IDEA创建一个新的spring boot项目,填写项目名称和位置,类型选择maven,组、工件、软件包名称可以自定义,JDK选择17+即可,java语言标准和JDK相同即可
- 配置Spring Boot版本和开发所需的依赖,主要如下图所示
- Spring Boot版本可以选择3.2.5或者更高的版本(作者使用3.2.5和3.2.6(SNAPSHOT)可以正常开发)
- Spring Boot DevTools:spring项目热部署工具,修改完代码(不含application和pom配置文件)即刻热部署项目
- Lombok:通过配置快速配置对象的get、set、toString
- Spring AI:Spring AI是一个用于AI工程的应用框架
- 创建完成后,项目结构大体如下(这里删除了无用的maven文件内容、修改application的文件格式为yaml)
配置项目pom和application文件
- 注意:修改pom文件,重新下载spring ai依赖需要科学上网,请确保网络连接没有问题
- 打开项目的pom文件,修改spring ai的版本(项目默认使用稳定版0.8.1)
- 主要注意默认的spring ai版本和配置依赖jar包仓库(maven仓库中还没有spring ai的依赖)
<properties>
<java.version>21</java.version>
<spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>
</properties>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
- 配置application文件(api-key的获取参考Spring AI开发前期开发指导)
spring:
application:
name: ChatOllama
ai:
openai:
base-url: http://localhost:11434
server:
port: 8085
controller接口开发
- 图片资源
- controller内容
import jakarta.annotation.Resource;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.messages.Media;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.io.IOException;
import java.util.List;
@RestController
public class OllamaController {
@Resource
private OllamaChatClient ollamaChatClient;
@RequestMapping("/ai/ollama")
public Object chat(@RequestParam(value = "msg") String msg) {
ChatResponse response = ollamaChatClient.call(
new Prompt(msg,
OllamaOptions.create()
.withModel("qwen:7b")
.withTemperature(0.8f)
)
);
System.out.print(response.getResult().getOutput());
return response.getResult().getOutput();
}
@GetMapping("/ai/generateStream")
public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
Prompt prompt = new Prompt(new UserMessage(message),
OllamaOptions.create().withModel("qwen:7b"));
return ollamaChatClient.stream(prompt);
}
/**
* 本次使用llava:7b 硬件有限,怕跑的很慢 所以不返回结果,直接打印
*/
@GetMapping("/ai/test")
public void test() throws IOException {
byte[] imageData = new ClassPathResource("/multimodal.test.png").getContentAsByteArray();
var userMessage = new UserMessage("描述一下图片内容?",
List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageData)));
ChatResponse response = ollamaChatClient.call(
new Prompt(List.of(userMessage),
OllamaOptions.create().withModel("llava:7b").withMainGPU(1)
)
);
System.out.println(response.getResult().getOutput().getContent());
}
//更多的图片相关的接口开发可以参考MultiModel项目的内容
}
运行测试
#获取ollama已经下载的模型
http://localhost:11434/api/tags
http://localhost:8085/ai/ollama?msg=hello
http://localhost:8085/ai/generateStream=hello
http://localhost:8085/ai/test