Spring AI,Java工程師也能玩轉大模型
在這個人工智能的春天,我們迎來了Spring AI。在這篇文章中,將介紹Spring AI以及如何將其與Ollama本地模型集成。
一、Spring AI簡介
圖片
官方正式宣布,Spring AI已經列入Spring Initializr。它提供了一種更簡潔的方式來與AI交互,降低了將LLM模型集成到Java操作中的學習曲線。它現在可以在start.spring.io上使用和構建。
Spring AI是一個人工智能工程應用框架。它的目標是將Spring生態系統的設計原則,如可移植性和模塊化設計,應用到AI領域,并推廣使用POJO作為AI應用程序的構建模塊。
二、特性
可移植的API支持跨AI提供商的交互,包括聊天、文本到圖像和嵌入模型。它支持同步和流API選項。它還支持配置參數以訪問特定模型。
支持的聊天模型:
- OpenAI。
- Azure Open AI。
- Amazon Bedrock。
- Anthropic的Claude。
- Cohere的Command。
- AI21 Labs的Jurassic-2。
- Meta的LLama 2。
- Amazon的Titan。
- Google Vertex AI。
- HuggingFace——HuggingFace上的眾多模型,如Llama2。
- Ollama——支持在沒有GPU的情況下在本地運行AI模型。
支持的文本到圖像模型:
- OpenAI與DALL-E。
- StabilityAI。
支持的向量模型:
- OpenAI。
- Azure OpenAI。
- Ollama。
- ONNX。
- PostgresML。
- Bedrock Cohere。
- Bedrock Titan。
- Google VertexAI。
官方文檔:spring.io/projects/spring-ai
三、快速入門
使用IDEA快速啟動一個新項目,選擇需要的AI模型依賴項。
在這里,以Ollama模型為例:
圖片
3.1 Ollama
Ollama使我們能夠在不需要GPU資源的情況下在本地計算機上輕松構建大型模型,并提供控制臺和RestfulAPI,以便在Ollama上快速測試和集成大型模型。
Ollama支持哪些模型?
圖片
Ollama網站:ollama.com/library
提示:
- Gemma是Google Meta最近發布的一個模型。
- llama2模型對中文支持不太友好,而gemma模型對中文更加友好。
3.2 引入依賴項
提示:Spring AI相關的依賴項不在Maven中央資源庫中,因此需要配置Spring的資源庫。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</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>
3.3 啟動Ollama模型
在本地計算機控制臺中運行ollama run gemma:2b(這里使用gemma模型)。
圖片
第一次運行會下載模型文件(約3GB,可能需要一些時間)。
下載模型資源后,模型將自動啟動,如上所示,你可以在控制臺中測試和與模型交互。
3.4 配置Ollama模型
修改該項目的application.yml配置文件,添加以下內容:
spring:
ai:
ollama:
base-url: http://localhost:11434
chat:
model: gemma:2b
3.5 測試
@Test
void contextLoads() {
String message = """
Who is Donald Trump?
""";
System.out.println(chatClient.call(message));
}
圖片
3.6 流式訪問
@Test
void streamChat() throws ExecutionException, InterruptedException {
// 構建一個異步函數來手動關閉測試函數
CompletableFuture<Void> future = new CompletableFuture<>();
String message = """
year-end work summary report
""";
PromptTemplate promptTemplate = new PromptTemplate("""
You are a Java development engineer, and you are good at writing the company’s year-end work summary report.
Write a 100-word summary report based on: {message} scenario
""");
Prompt prompt = promptTemplate.create(Map.of("message", message));
chatClient.stream(prompt).subscribe(
chatResponse -> {
System.out.println("response: " + chatResponse.getResult().getOutput().getContent());
},
throwable -> {
System.err.println("err: " + throwable.getMessage());
},
() -> {
System.out.println("complete~!");
// 關閉函數
future.complete(null);
}
);
future.get();
}