当前位置:   article > 正文

超级详细Spring AI运用Ollama大模型_spring-ai-ollama-spring-boot-starter

spring-ai-ollama-spring-boot-starter

大模型工具Ollama

官网:https://ollama.com/
Ollama是一个用于部署和运行各种开源大模型的工具;
它能够帮助用户快速在本地运行各种大模型,极大地简化了大模型在本地运行的过程。用户通过执行几条命令就能在本地运行开源大模型,如Lama 2等;
综上,Ollama是一个大模型部署运行工具,在该工具里面可以部署运行各种大模型,方便开发者在本地搭建一套大模型运行环境;

下载:https://ollama.com/download

下载Ollama
说明:Ollama的运行会受到所使用模型大小的影响;
1、例如,运行一个7B(70亿参数)的模型至少需要8GB的可用内存(RAM),而运行一个13B(130亿参数)的模型需要16GB的内存,33B(330亿参数)的型需要32GB的内存;
2、需要考虑有足够的磁盘空间,大模型的文件大小可能比较大,建议至少为Ollama和其模型预留50GB的磁盘空间3、性能较高的CPU可以提供更好的运算速度和效率,多核处理器能够更好地处理并行任务,选择具有足够核心数的CPU:
4、显卡(GPU):Ollama支持纯CPU运行,但如果电脑配备了NVIDIA GPU,可以利用GPU进行加速,提高模型的运行速度和性能;

命令行使用ollama 打开终端,输入 ollama -h,查看到所有的命令

service ollama start启动allama

输入ollama -v查看当前版本,能输出版本则安装成功

运行模型单行对话

拉取并运行llama2模型
ollama run llama2
直接输入该命令会检查目录下是否有该模型,没有会自动下载,下载好后自动运行该模型
其他模型见library (ollama.com)

  1. # 查看 Ollama 版本
  2. ollama -v
  3. # 查看已安装的模型
  4. ollama list
  5. # 删除指定模型
  6. ollama rm [modelname]
  7. # 模型存储路径
  8. # C:\Users\<username>\.ollama\models

ollama run qwen:0.5b

默认Ollama api会监听11434端口,可以使用命令进行查看netstat -ano |findstr 114341

  1. //加依赖
  2. <dependency>
  3. <groupld>org.springframework,ai</groupld>
  4. <artifactld>spring-ai-ollama-spring-boot-starter</artifactld>
  5. </dependency>
  6. //写代码
  7. 注入OllamaChatClient
  8. @Resource
  9. private OllamaChatClient ollamaChatClient,
  10. //调用call方法
  11. ollamaChatClient.call(msg);

完整pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.3.0</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.zzq</groupId>
  12. <artifactId>spring-ai-ollama</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>spring-ai-ollama</name>
  15. <description>spring-ai-ollama</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. <!-- 快照版本-->
  19. <spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.ai</groupId>
  28. <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-devtools</artifactId>
  33. <scope>runtime</scope>
  34. <optional>true</optional>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.projectlombok</groupId>
  38. <artifactId>lombok</artifactId>
  39. <optional>true</optional>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-test</artifactId>
  44. <scope>test</scope>
  45. </dependency>
  46. </dependencies>
  47. <dependencyManagement>
  48. <dependencies>
  49. <dependency>
  50. <groupId>org.springframework.ai</groupId>
  51. <artifactId>spring-ai-bom</artifactId>
  52. <version>${spring-ai.version}</version>
  53. <type>pom</type>
  54. <scope>import</scope>
  55. </dependency>
  56. </dependencies>
  57. </dependencyManagement>
  58. <build>
  59. <plugins>
  60. <plugin>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-maven-plugin</artifactId>
  63. <configuration>
  64. <excludes>
  65. <exclude>
  66. <groupId>org.projectlombok</groupId>
  67. <artifactId>lombok</artifactId>
  68. </exclude>
  69. </excludes>
  70. </configuration>
  71. </plugin>
  72. </plugins>
  73. </build>
  74. <!-- 快照版本-->
  75. <repositories>
  76. <repository>
  77. <id>spring-snapshot</id>
  78. <name>Spring Snapshots</name>
  79. <url>https://repo.spring.io/snapshot</url>
  80. <releases>
  81. <enabled>false</enabled>
  82. </releases>
  83. </repository>
  84. </repositories>
  85. </project>

application文件内容

  1. spring:
  2. application:
  3. name:spring-ai-05-ollama
  4. ai:
  5. ollama:
  6. base-url: http://localhost:11434
  7. chat:
  8. options:
  9. model: qwen:0.5b

controller

  1. package com.zzq.controller;
  2. import jakarta.annotation.Resource;
  3. import org.springframework.ai.ollama.OllamaChatModel;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class OllamaController {
  9. @Resource
  10. private OllamaChatModel ollamaChatModel;
  11. @RequestMapping(value = "/ai/ollama")
  12. public Object ollama(@RequestParam(value = "msg")String msg){
  13. String called=ollamaChatModel.call(msg);
  14. System.out.println(called);
  15. return called;
  16. }
  17. }

  1. package com.zzq.controller;
  2. import jakarta.annotation.Resource;
  3. import org.springframework.ai.chat.model.ChatResponse;
  4. import org.springframework.ai.chat.prompt.Prompt;
  5. import org.springframework.ai.ollama.OllamaChatModel;
  6. import org.springframework.ai.ollama.api.OllamaOptions;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.RestController;
  10. @RestController
  11. public class OllamaController {
  12. @Resource
  13. private OllamaChatModel ollamaChatModel;
  14. @RequestMapping(value = "/ai/ollama")
  15. public Object ollama(@RequestParam(value = "msg")String msg){
  16. String called=ollamaChatModel.call(msg);
  17. System.out.println(called);
  18. return called;
  19. }
  20. @RequestMapping(value = "/ai/ollama2")
  21. public Object ollama2(@RequestParam(value = "msg")String msg){
  22. ChatResponse chatResponse=ollamaChatModel.call(new Prompt(msg, OllamaOptions.create()
  23. .withModel("qwen:0.5b")//使用哪个大模型
  24. .withTemperature(0.4F)));//温度,温度值越高,准确率下降,温度值越低,准确率上升
  25. System.out.println(chatResponse.getResult().getOutput().getContent());
  26. return chatResponse.getResult().getOutput().getContent();
  27. }
  28. }

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

闽ICP备14008679号