我们来看看普通调用的展示
发布时间:2025-06-24 18:53:48 作者:北方职教升学中心 阅读量:590
我们来看看普通调用的展示。
yaml配置。<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>3.2.5</version> </dependency> <dependency> <groupId>io.springboot.ai</groupId> <artifactId>spring-ai-ollama</artifactId> <version>1.0.3</version> </dependency> </dependencies>
2,构建简单的springboot框架。此外,
然后我们需要在这个AI中下载一个现有的模型供我们自己使用。
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}。在pom文件中准备相关依赖。import com.rojer.delegete.OllamaDelegete;import org.springframework.ai.chat.ChatResponse;import org.springframework.ai.chat.prompt.Prompt;import org.springframework.ai.ollama.OllamaChatClient;import org.springframework.ai.ollama.api.OllamaApi;import org.springframework.ai.ollama.api.OllamaOptions;import org.springframework.stereotype.Service;import reactor.core.publisher.Flux;import java.util.List;import java.util.stream.Collectors;@Servicepublic class OllamaImpl implements OllamaDelegete { OllamaApi ollamaApi; OllamaChatClient chatClient; { // ollama实例化 ollamaApi = new OllamaApi(); OllamaOptions options = new OllamaOptions(); options.setModel("lllama3"#;); chatClient = new OllamaChatClient(ollamaApi).withDefaultOptions(options); } /** * 调用普通文本 * * @param msg * @return */ @Override public Object getOllame(String msg) { Prompt prompt = new Prompt(msg); ChatResponse call = chatClient.call(prompt); return call.getResult().getOutput().getContent(); } /** * 流式调用 * * @param msg * @return */ @Override public Object getOllameByStream(String msg) { Prompt prompt = new Prompt(msg); Flux<ChatResponse> stream = chatClient.stream(prompt); List<String> result = stream.toStream().map(a -> { return a.getResult().getOutput().getContent(); }).collect(Collectors.toList()); return result; }}。1,首先需要在ollama官网下载开源ai到本地。有机会在后期制定个人模型训练方法。
实现具体代码--controller。我们还可以访问spring官方网站,目前支持的ai模型有哪些?c;我在这里简单截图希望各位看官老爷点赞#xff0c;加个关注,谢谢!Spring AI。
spring: ai: ollama: base-url: localhost:11434。import com.rojer.delegete.OllamaDelegete;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class OllamaController { @Autowired(required=true) OllamaDelegete ollamaDelegete; @GetMapping("/ai/getMsg") public Object getOllame(@RequestParam(value = "msg") String msg) { return ollamaDelegete.getOllame(msg); } @GetMapping("/ai/stream/getMsg") public Object getOllameByStream(@RequestParam(value = "msg") String msg) { return ollamaDelegete.getOllameByStream(msg); }。
1,首先需要在ollama官网下载开源ai到本地。有机会在后期制定个人模型训练方法。
实现具体代码--controller。我们还可以访问spring官方网站,目前支持的ai模型有哪些?c;我在这里简单截图希望各位看官老爷点赞#xff0c;加个关注,谢谢!Spring AI。
spring: ai: ollama: base-url: localhost:11434。import com.rojer.delegete.OllamaDelegete;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class OllamaController { @Autowired(required=true) OllamaDelegete ollamaDelegete; @GetMapping("/ai/getMsg") public Object getOllame(@RequestParam(value = "msg") String msg) { return ollamaDelegete.getOllame(msg); } @GetMapping("/ai/stream/getMsg") public Object getOllameByStream(@RequestParam(value = "msg") String msg) { return ollamaDelegete.getOllameByStream(msg); }。
直接下载到本地然后启动ollama。
实现具体代码--impl。1,首先,
网站:Ollama。
一,提前准备本地AI模型。启动类。
2, 下载ai moudle。
启动类。

三,调用展示。
启动完成后在cmd中执行ollama可以看到相关的命令。

显示流式调用(我们和ai聊天答案不是一下子出来的,这就是流式调用所显示的)这里没有具体的代码深挖,只使用基本基础。三,调用展示。

可以下载命令操作。
二, 准备本地代码。