前言:
最近發(fā)現(xiàn)SprinAI更新了1.0.0-M1預(yù)覽版腺办,即意味著SpringAI即將正式推出,本地跑一個(gè)玩玩糟描,說不定以后有在項(xiàng)目中應(yīng)用的場景怀喉。
環(huán)境:
Gradle - Kotlin 8.7
SpringBoot 3.2.6
Java17
SpringAI-1.0.0-M1
Ollama v0.1.42
phi3-3B
......
開始:
1、模型工具下載
https://ollama.com/在這個(gè)地址下載ollama船响,或者去GitHub找也行
本文使用的是辦公電腦躬拢,16G內(nèi)存完全可以運(yùn)行
2、本地啟動(dòng)模型
本地安裝一個(gè)小一點(diǎn)的模型见间,本文選用phi3-3B
ollama run phi3
執(zhí)行這條命令后聊闯,就會(huì)開始下載并運(yùn)行大模型
3、代碼相關(guān):
在https://start.spring.io/勾選對應(yīng)所需的依賴米诉,打包下載后本地打開
測試代碼如下:
spring:
application:
name: practice
ai:
ollama:
chat:
options:
model: "phi3"
package com.example.practice;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@Resource
private OllamaChatModel ollamaChatModel;
@GetMapping("/chat/{msg}")
public String chat(@PathVariable String msg) {
ChatResponse phi3 = ollamaChatModel.call(new Prompt(msg, OllamaOptions.create().withModel("phi3")));
return phi3.getResult().getOutput().getContent();
}
}
之前舊版本也玩過菱蔬,跟之前大有不同,因?yàn)榕f版本號甚至沒有貼在官網(wǎng)史侣,舊代碼就不貼出來了
4拴泌、運(yùn)行效果
PixPin_2024-06-17_17-59-57.png