Spring AI,Java工程師也能玩轉(zhuǎn)大模型
在這個(gè)人工智能的春天,我們迎來(lái)了Spring AI。在這篇文章中,將介紹Spring AI以及如何將其與Ollama本地模型集成。
一、Spring AI簡(jiǎn)介
圖片
官方正式宣布,Spring AI已經(jīng)列入Spring Initializr。它提供了一種更簡(jiǎn)潔的方式來(lái)與AI交互,降低了將LLM模型集成到Java操作中的學(xué)習(xí)曲線。它現(xiàn)在可以在start.spring.io上使用和構(gòu)建。
Spring AI是一個(gè)人工智能工程應(yīng)用框架。它的目標(biāo)是將Spring生態(tài)系統(tǒng)的設(shè)計(jì)原則,如可移植性和模塊化設(shè)計(jì),應(yīng)用到AI領(lǐng)域,并推廣使用POJO作為AI應(yīng)用程序的構(gòu)建模塊。
二、特性
可移植的API支持跨AI提供商的交互,包括聊天、文本到圖像和嵌入模型。它支持同步和流API選項(xiàng)。它還支持配置參數(shù)以訪問(wèn)特定模型。
支持的聊天模型:
- 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——支持在沒(méi)有GPU的情況下在本地運(yùn)行AI模型。
支持的文本到圖像模型:
- OpenAI與DALL-E。
- StabilityAI。
支持的向量模型:
- OpenAI。
- Azure OpenAI。
- Ollama。
- ONNX。
- PostgresML。
- Bedrock Cohere。
- Bedrock Titan。
- Google VertexAI。
官方文檔:spring.io/projects/spring-ai
三、快速入門
使用IDEA快速啟動(dòng)一個(gè)新項(xiàng)目,選擇需要的AI模型依賴項(xiàng)。
在這里,以O(shè)llama模型為例:
圖片
3.1 Ollama
Ollama使我們能夠在不需要GPU資源的情況下在本地計(jì)算機(jī)上輕松構(gòu)建大型模型,并提供控制臺(tái)和RestfulAPI,以便在Ollama上快速測(cè)試和集成大型模型。
Ollama支持哪些模型?
圖片
Ollama網(wǎng)站:ollama.com/library
提示:
- Gemma是Google Meta最近發(fā)布的一個(gè)模型。
- llama2模型對(duì)中文支持不太友好,而gemma模型對(duì)中文更加友好。
3.2 引入依賴項(xiàng)
提示:Spring AI相關(guān)的依賴項(xiàng)不在Maven中央資源庫(kù)中,因此需要配置Spring的資源庫(kù)。
<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 啟動(dòng)Ollama模型
在本地計(jì)算機(jī)控制臺(tái)中運(yùn)行ollama run gemma:2b(這里使用gemma模型)。
圖片
第一次運(yùn)行會(huì)下載模型文件(約3GB,可能需要一些時(shí)間)。
下載模型資源后,模型將自動(dòng)啟動(dòng),如上所示,你可以在控制臺(tái)中測(cè)試和與模型交互。
3.4 配置Ollama模型
修改該項(xiàng)目的application.yml配置文件,添加以下內(nèi)容:
spring:
ai:
ollama:
base-url: http://localhost:11434
chat:
model: gemma:2b
3.5 測(cè)試
@Test
void contextLoads() {
String message = """
Who is Donald Trump?
""";
System.out.println(chatClient.call(message));
}
圖片
3.6 流式訪問(wèn)
@Test
void streamChat() throws ExecutionException, InterruptedException {
// 構(gòu)建一個(gè)異步函數(shù)來(lái)手動(dòng)關(guān)閉測(cè)試函數(shù)
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~!");
// 關(guān)閉函數(shù)
future.complete(null);
}
);
future.get();
}