当前位置:   article > 正文

SpringAI集成本地AI大模型ollama(调用篇)非常简单!!_springboot ollama

springboot ollama

一,前提准备本地ai模型

1,首先需要去ollama官网下载开源ai到本地

网址:Ollama

直接下载到本地,然后启动ollama

启动完成后,我们可以在cmd中执行ollama可以看到相关命令行

2, 下载ai moudle

然后我们需要在这个ai中给它下载好一个已有模型给我们自己使用

将命令行运行即可下载。

二, 准备本地代码

1,首先准备pom文件中的相关依赖
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. <version>3.2.5</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>io.springboot.ai</groupId>
  9. <artifactId>spring-ai-ollama</artifactId>
  10. <version>1.0.3</version>
  11. </dependency>
  12. </dependencies>
2,搭建一个简单的springboot框架

启动类

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. @SpringBootApplication
  4. public class Application {
  5. public static void main(String[] args) {
  6. SpringApplication.run(Application.class, args);
  7. }
  8. }
yaml配置
  1. spring:
  2. ai:
  3. ollama:
  4. base-url: localhost:11434
具体代码实现--controller
  1. import com.rojer.delegete.OllamaDelegete;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class OllamaController {
  8. @Autowired(required=true)
  9. OllamaDelegete ollamaDelegete;
  10. @GetMapping("/ai/getMsg")
  11. public Object getOllame(@RequestParam(value = "msg") String msg) {
  12. return ollamaDelegete.getOllame(msg);
  13. }
  14. @GetMapping("/ai/stream/getMsg")
  15. public Object getOllameByStream(@RequestParam(value = "msg") String msg) {
  16. return ollamaDelegete.getOllameByStream(msg);
  17. }
具体代码实现--impl
  1. import com.rojer.delegete.OllamaDelegete;
  2. import org.springframework.ai.chat.ChatResponse;
  3. import org.springframework.ai.chat.prompt.Prompt;
  4. import org.springframework.ai.ollama.OllamaChatClient;
  5. import org.springframework.ai.ollama.api.OllamaApi;
  6. import org.springframework.ai.ollama.api.OllamaOptions;
  7. import org.springframework.stereotype.Service;
  8. import reactor.core.publisher.Flux;
  9. import java.util.List;
  10. import java.util.stream.Collectors;
  11. @Service
  12. public class OllamaImpl implements OllamaDelegete {
  13. OllamaApi ollamaApi;
  14. OllamaChatClient chatClient;
  15. {
  16. // 实例化ollama
  17. ollamaApi = new OllamaApi();
  18. OllamaOptions options = new OllamaOptions();
  19. options.setModel("llama3");
  20. chatClient = new OllamaChatClient(ollamaApi).withDefaultOptions(options);
  21. }
  22. /**
  23. * 普通文本调用
  24. *
  25. * @param msg
  26. * @return
  27. */
  28. @Override
  29. public Object getOllame(String msg) {
  30. Prompt prompt = new Prompt(msg);
  31. ChatResponse call = chatClient.call(prompt);
  32. return call.getResult().getOutput().getContent();
  33. }
  34. /**
  35. * 流式调用
  36. *
  37. * @param msg
  38. * @return
  39. */
  40. @Override
  41. public Object getOllameByStream(String msg) {
  42. Prompt prompt = new Prompt(msg);
  43. Flux<ChatResponse> stream = chatClient.stream(prompt);
  44. List<String> result = stream.toStream().map(a -> {
  45. return a.getResult().getOutput().getContent();
  46. }).collect(Collectors.toList());
  47. return result;
  48. }
  49. }

三,调用展示

我们看看普通调用的展示

流式调用的展示(我们跟ai聊天,回答不是一下子就出来的,就是这种流式调用所展示的这般)

这里不做具体的代码深挖,只做基本基础的运用。后期有机会会出个人模型训练方法。另外我们可以去spring官网查看,目前支持的ai模型有哪些,我这里简单截个图,希望各位看官老爷点个赞,加个关注,谢谢!Spring AI

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/794501
推荐阅读
相关标签
  

闽ICP备14008679号