当前位置: 首页 > news >正文

SpringBoot 集成Ollama 本地大模型

SpringBoot 集成Ollama 本地大模型

本文主要简述springBoot如何结合spring ai集成本地大模型实现智能对话
在这里插入图片描述

1.引入依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--ollama依赖,最新稳定版0.8.0--><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-ollama</artifactId><version>0.8.0</version></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-ollama-spring-boot-starter</artifactId><version>0.8.0</version><exclusions><exclusion><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-function-context</artifactId></exclusion></exclusions></dependency>
</dependencies>
<!--配置spring的仓库-->
<repositories><repository><id>spring-milestones</id><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository>
</repositories>、
<!--spring ai的bom管理-->
<dependencyManagement><dependencies><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-bom</artifactId><version>1.0.0.2</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>    

2.修改配置文件

spring.application.name=OllamaProject
spring.ai.ollama.base-url=http://localhost:11434  #ollama地址
spring.ai.ollama.chat.model=qwen3:4b  #大模型名称

3.编写Contoller

spring ai 中集成了OllamaChatClient,直接使用ollamaChatClient即可

import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/ai")
public class OllamaTestController {@Autowiredprivate OllamaChatClient ollamaChatClient;@GetMapping("/chat")public String generate(@RequestParam String message) {return ollamaChatClient.call(message);}@GetMapping(value = "/chatStream",produces = MediaType.TEXT_MARKDOWN_VALUE + ";charset=UTF-8" )public Flux<String> chatStream(@RequestParam String message) {Flux<ChatResponse> stream = ollamaChatClient.stream(new Prompt(message));return stream.map(chatResponse -> chatResponse.getResult().getOutput().getContent()) ;}public Flux<String> chatStream(@RequestParam String message) {Flux<ChatResponse> stream = ollamaChatClient.stream(new Prompt(message));return stream.map(chatResponse -> chatResponse.getResult().getOutput().getContent()) ;}}

4.修改启动类

package com.example.ollamaproject;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration;
@SpringBootApplication(exclude = {ContextFunctionCatalogAutoConfiguration.class
})
public class OllamaProjectApplication {public static void main(String[] args) {SpringApplication.run(OllamaProjectApplication.class, args);}}

5.启动验证

调用chat接口,一次性返回
在这里插入图片描述
调用chatStream接口,会采用流式输出方式:
在这里插入图片描述

6.多模态大模型

如果要完成多模态的大模型集成,可以拉取
在这里插入图片描述
代码如下:

  @Testpublic void ollamaImageTest(){ClassPathResource resource = new ClassPathResource("/files/fengj.jpg");OllamaApi ollamaApi = new OllamaApi("http://localhost:11434");OllamaOptions ollamaOptions = OllamaOptions.create();ollamaOptions.setModel("gemma3:1b");ChatResponse response = new OllamaChatClient(ollamaApi).withDefaultOptions(ollamaOptions).call(new Prompt(new UserMessage(resource)));System.out.println(response.getResult().getOutput().getContent());}

fengj,jpg是一张风景照片
在这里插入图片描述
返回结果:

在这里插入图片描述

以上就是集成ollama的所有内容,使用时由于版本更新,语法上可能存在差异,仅供参考!

http://www.dtcms.com/a/334267.html

相关文章:

  • mysql的group by函数怎么使用
  • Java内功修炼(1)——时光机中的并发革命:从单任务到Java多线程
  • [Linux] Linux文件系统基本管理
  • 基于STM32的精确按键时长测量系统
  • 一周学会Matplotlib3 Python 数据可视化-绘制自相关图
  • 2020/12 JLPT听力原文 问题二 2番
  • Pycaita二次开发基础代码解析:交互选择、参数化建模与球体创建的工业级实现
  • 415. 字符串相加
  • dify 调用本地的 stable diffusion api生成图片的工作流搭建
  • 分布式存储与存储阵列:从传统到现代的存储革命
  • Windows Manager:全方位优化你的Windows系统
  • PCB高频板与普通电路板的核心差异
  • JavaScript 闭包与递归深度解析:从理论到实战
  • [优选算法专题二滑动窗口——最大连续1的个数 III]
  • 【轨物方案】预防性运维:轨物科技用AI+机器人重塑光伏电站价值链
  • K8S的ingress
  • 石头科技披露半年报:营收79.03亿元,同比大增78.96%
  • vscode中用python调用matlab的函数(环境安装)
  • pdf合并代码
  • Autosar Os新手入门
  • IOMMU多级页表查找的验证
  • 从0到1掌握 Spring Security(第三篇):三种认证方式,按配置一键切换
  • Flink Stream API 源码走读 - print()
  • TDengine IDMP 高级功能(3. 概念解释)
  • 用Pygame开发桌面小游戏:从入门到发布
  • MixOne:Electron Remote模块的现代化继任者
  • AI 云电竞游戏盒子:从“盒子”到“云-端-芯”一体化竞技平台的架构实践
  • 【BFS 重构树】P11907 [NHSPC 2023] F. 恐怖的黑色魔物|省选-
  • AI的下一个竞争焦点——世界模型
  • 笔试——Day40