Spring AI來了,Java生態(tài)接入LLM大模型變得更加簡單!
SpringAI
今天官宣Spring AI已經(jīng)上架到Spring Initializr 上,它提供了一種更簡潔的方式和AI交互,減輕Java業(yè)務(wù)中接入LLM模型應(yīng)用的學(xué)習(xí)成本,目前在 https://start.spring.io/ 上可以使用并構(gòu)建。
Spring AI 是一個人工智能工程的應(yīng)用框架。其目標(biāo)是將 Spring 生態(tài)系統(tǒng)設(shè)計原則(例如可移植性和模塊化設(shè)計)應(yīng)用于 AI 領(lǐng)域,并推廣使用 POJO 作為 AI 領(lǐng)域應(yīng)用程序的構(gòu)建塊。
Features
跨 AI 提供商的便攜式 API 支持聊天、文本到圖像和嵌入模型。支持同步和流 API 選項。還支持配置參數(shù)訪問特定Model。
支持的聊天模型
- OpenAI
- Azure Open AI
- Amazon Bedrock
- Anthropic’s Claude
- Cohere’s Command
- AI21 Labs’ Jurassic-2
- Meta’s LLama 2
- Amazon’s Titan
- Google Vertex AI
- HuggingFace - HuggingFace上的大量模型,例如Llama2
- Ollama - 支持本地?zé)oGPU情況下運行AI模型
支持的文生圖模型
- OpenAI with DALL-E
- StabilityAI
支持的向量模型
- OpenAI
- Azure OpenAI
- Ollama
- ONNX
- PostgresML
- Bedrock Cohere
- Bedrock Titan
- Google VertexAI
官方文檔:https://spring.io/projects/spring-ai#overview
快速開始
使用IDEA快速新建項目,選擇要使用的AI模型依賴
這里我以ollama模型為例
Ollama
Ollama幫助我們在本地的電腦上無需GPU(顯卡)資源,也能一鍵構(gòu)建大模型,并且提供控制臺、RestfulAPI方式快速測試和接入Ollama上的大模型。
Ollama支持哪些模型?
Ollama官網(wǎng):https://ollama.com/library
Tips:
- 其中g(shù)emma就是谷歌Meta近期新發(fā)布的模型
- llama2模型基本不支持中文語言,gemma模型對中文支持比較友好
引入依賴
**Tips:**Spring AI的相關(guān)依賴并沒有開放在Meven中央倉庫,因此需要配置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>
啟動Ollama模型
在本地電腦控制臺運行ollama run gemma:2b
(這里使用gemma模型)
第一次運行會先下載模型文件(大概3G,會比較耗時)
下載完模型資源后會自動啟動模型,如上,可以在控制臺測試和模型交互。
配置Ollama模型
修改此項目的application.yml
配置文件,增加如下:
spring:
ai:
ollama:
## 默認(rèn)地址無需配置
base-url: http://localhost:11434
chat:
model: gemma:2b
測試
@SpringBootTest
class SpringAiApplicationTests {
@Autowired
private OllamaChatClient chatClient;
@Test
void contextLoads() {
String message = """
魯迅和周樹人是什么關(guān)系?
""";
System.out.println(chatClient.call(message));
}
}
流式訪問
@Test
void streamChat() throws ExecutionException, InterruptedException {
// 構(gòu)建一個異步函數(shù),實現(xiàn)手動關(guān)閉測試函數(shù)
CompletableFuture<Void> future = new CompletableFuture<>();
String message = """
年終總結(jié)
""";
PromptTemplate promptTemplate = new PromptTemplate("""
你是一個Java開發(fā)工程師,你擅長于寫公司年底的工作總結(jié)報告,
根據(jù):{message} 場景寫100字的總結(jié)報告
""");
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();
}
示例代碼: https://github.com/TyCoding/spring-ai文章來源:http://www.zghlxwxcb.cn/news/detail-842089.html
更多的應(yīng)用示例關(guān)注后續(xù)文章哦!文章來源地址http://www.zghlxwxcb.cn/news/detail-842089.html
推薦項目
- https://github.com/TyCoding/lang-sora React NextJS全??焖贅?gòu)建Sora AI Video演示項目
合作和聯(lián)系
- 個人博客:http://tycoding.cn
- GitHub:https://github.com/tycoding
- 微信公眾號:程序員涂陌
- 微信交流群:公眾號后臺回復(fù):
微信群
到了這里,關(guān)于Spring AI來了,Java開發(fā)者福音的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!