当前位置:   article > 正文

ollama,springAi实现自然语言处理_spring-ai-ollama-spring-boot-starter必须用java17么

spring-ai-ollama-spring-boot-starter必须用java17么

ollama安装使用:

https://ollama.com/

下载速度比较慢的可以直接使用以下版本0.1.41

        https://pan.baidu.com/s/1hCCkYvFjWqxvPyYA2-YElA?pwd=otap 

直接管理员身份双击安装,安装成功后会在任务栏里出现这个小图标:

打开cmd,输入ollama --version能够显示ollama版本则证明已经安装完成

输入ollama命令,能够列出帮助命令:

其中常用的有

  • serve表示后台启动model

  • create表示从本地medelFile中创建model

  • run表示启动model,并打开对话框

  • list表示列出当前所有的model

  • ps表示列出当前运行的model

  • rm表示删除model

down Model

在ollama官网中有model仓,我们可以挑选自己需要的model进行下载使用

以阿里通义千问模型为例,我们搜索到qwen2进入到该模型的仓库中。

上面的tag表示不同参数的千问大模型,7b,72b表示大模型版本,后面4.4GB,41GB表示模型大小,即运行时需要占用的GPU/CPU的大小。

右侧ollama run qwen2表示启动命令,如果我们需要拉取并使用7b模型,则只需要在cmd中执行ollama run qwen2:7b 命令,等待下载安装即可(使用的模型越大表示训练的参数越大,精准度越高。同时需要的GPU/CPU就越大,运行反应速度会越慢,需要根据自身需求选择合适的模型进行使用)

使用SpringAi对接ollamahttps://spring.io/projects/spring-ai

首先需要强调的时SpringAi依赖的jdk>=17,springboot版本>=3.0

接下来介绍构建简单的springboot项目,使用springAi调用ollama进行对话

构建一个maven项目,修改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.1</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <url/>
  17. <properties>
  18. <java.version>17</java.version>
  19. <spring-ai.version>1.0.0-M1</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-starter-test</artifactId>
  33. <scope>test</scope>
  34. </dependency>
  35. </dependencies>
  36. <dependencyManagement>
  37. <dependencies>
  38. <dependency>
  39. <groupId>org.springframework.ai</groupId>
  40. <artifactId>spring-ai-bom</artifactId>
  41. <version>${spring-ai.version}</version>
  42. <type>pom</type>
  43. <scope>import</scope>
  44. </dependency>
  45. </dependencies>
  46. </dependencyManagement>
  47. <build>
  48. <plugins>
  49. <plugin>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-maven-plugin</artifactId>
  52. </plugin>
  53. </plugins>
  54. </build>
  55. <repositories>
  56. <repository>
  57. <id>spring-milestones</id>
  58. <name>Spring Milestones</name>
  59. <url>https://repo.spring.io/milestone</url>
  60. <snapshots>
  61. <enabled>false</enabled>
  62. </snapshots>
  63. </repository>
  64. </repositories>
  65. </project>

application.yml文件中填入:

  1. spring:
  2. ai:
  3. ollama:
  4. base-url: http://localhost:11434
  5. chat:
  6. enabled: true
  7. options:
  8. model: qwen2:7b

创建controller,对接用户输入问题并返回结果

  1. @RestController
  2. @RequestMapping("/")
  3. public class AiController {
  4. @Resource
  5. private OllamaChatModel ollamaChatModel;
  6. @PostMapping("/chat")
  7. public String chat(@RequestBody String message) {
  8. return ollamaChatModel.call(message);
  9. }
  10. }

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

闽ICP备14008679号