当前位置: 首页 > 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.xdnf.cn/news/1311193.html

相关文章:

  • RH134 访问网络附加存储知识点
  • 【图论】分层图 / 拆点
  • 计算机存储器分类和层次结构详解
  • PyTorch生成式人工智能——使用MusicGen生成音乐
  • 探索粒子世界:从基础理论到前沿应用与未来展望
  • Python-深度学习(一)
  • flash通信
  • 机器学习核心概念精要:从定义到评估
  • C++STL标准模板库详解
  • sql链接的url中serverTimezone的作用
  • MQ迁移方案
  • Unity 游戏提升 Android TargetVersion 相关记录
  • 深入了解 swap:作用、局限与分区建立
  • (第十七期)HTML图像标签详解:从入门到精通
  • 解决html-to-image在 ios 上dom里面的图片不显示出来
  • [Linux] Linux交换空间管理 Linux系统启动原理
  • 8.16 pq
  • 从 Windows 到 Linux 服务器的全自动部署教程(免密登录 + 压缩 + 上传 + 启动)
  • 嵌入式硬件篇---运算放大器
  • 要想在Trae运行Java程序,该怎样配置Java环境?
  • TOGAF八步一法笔记2
  • TexStudio中的Latex,PDFLatex,XeLatex和LuaLatex的区别
  • RocketMq面试集合
  • 暴雨服务器:以定制化满足算力需求多样化
  • 小白挑战一周上架元服务——元服务开发06
  • 肖臻《区块链技术与应用》第20-22讲 - 以太坊难度调整、权益证明和智能合约
  • 415. 字符串相加
  • Java设计模式之《工厂模式》
  • 【Java web】HTTP 协议详解
  • HTTP 1.0, 2.0 和 3.0 有什么区别?