SpringAI 整合 Ollama 大语言模型实践

一、Ollama 大模型部署


Ollama 简介:


Ollama 是一个开源的大型语言模型服务工具,它帮助用户快速在本地运行大模型。通过简单的安装指令,用户可以执行一条命令就在本地运行开源大型语言模型。极大地简化了在 Docker 容器内部署和管理 LLM 的过程,使得用户能够快速地在本地运行大型语言模型。

Ollama 安装部署:


官方网站:https://ollama.com/

下载地址:https://ollama.com/download,以 Mac OS 为例,下载后解压即用,支持多平台一键安装

Ollama下载

官方模型:https://ollama.com/library,我们以 70 亿参数的大模型部署到本地来进行演示。

安装模型

模型启动:下载完成后打开终端,即可操作 Ollama 相关的命令

  1. 查看当前模型列表
# 查看当前模型列表
ollama list
  1. 拉取模型:
# 拉取模型
ollama pull 

拉取模型

  1. 启动模型:
# 启动模型
ollama run llama3:70b
  1. 其它命令:
Available Commands:
  serve       Start ollama
  create      Create a model from a Modelfile
  show        Show information for a model
  run         Run a model
  pull        Pull a model from a registry
  push        Push a model to a registry
  list        List models
  cp          Copy a model
  rm          Remove a model
  help        Help about any command
  1. 大模型交互演示:

大模型交互演示

  1. ⚠️ 注意:大模型的运行对 GPU 的要求比较高,请根据自己的电脑配置选择合适的模型,以我目前这台 Macbook 30核 GPU 为例,在模型运行期间的 GPU 使用情况如图所示,存在轻微卡顿现象:

    GPU

二、SpringAI 整合大模型实践


SpringAI 简介:


Spring AI 是 AI 工程的应用框架,其目标是将 Spring 生态系统设计原则(如可移植性和模块化设计)应用于 AI 领域,并将 Java 类作为应用程序的构建块推广到 AI 领域。

SpringAI 生态链:


目前 SpringAI 涵盖了广泛的功能,包括聊天机器人、嵌入式、图像生成、矢量数据库、高等数学运算等多领域 API。

SpringAI 模型生态链

SpringBoot 整合 SpringAI:


  1. 创建 SpringBoot 工程,整合 SpringAI:以 SpringBoot 3 与 JDK 17 为例

创建 SpringBoot 工程

  1. 选择 AI 中我们前文部署好的 Ollama 模型:我们熟悉的 OpenAI 也可以通过同样的方式部署与访问,具体请参考官方文档

选择组件

  1. 项目依赖:工程创建完成后会自动引入依赖,pom.xml
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	
	<dependency>
	    <groupId>org.springframework.ai</groupId>
	    <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
	</dependency>
  1. 配置文件:application.yml
spring:
  application:
    name: SpringAI
  ai:
    ollama:
      chat:
        enabled: true
        options:
          model: llama3:70b
  1. 主启动类:
@SpringBootApplication
public class SpringAiApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringAiApplication.class, args);
    }
}

SpringAI 交互 Ollama 大模型:

  1. 官方参考代码:
@RestController
@RequestMapping("/ai")
public class ChatController {

    // 客户端
    private final OllamaChatClient chatClient;

    // 构造器注入
    @Autowired
    public ChatController(OllamaChatClient chatClient) {
        this.chatClient = chatClient;
    }

    /**
     * 批处理
     *
     * @param message 提示信息
     * @return 交互结果
     */
    @GetMapping("/generate")
    public Map<String, Object> generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", chatClient.call(message));
    }

    /**
     * 流处理
     *
     * @param message 提示信息
     * @return 交互结果
     */
    @GetMapping("/stream")
    public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return chatClient.stream(prompt);
    }

}
  1. 交互演示:

页面交互演示

写在最后:


我并不擅长前端,所以并没有专门设计一个交互页面,只是希望大家能够理解 SpringAI 可以实现怎样的功能,通过这个案例大家可以举一反三,实现自己想要的功能,暂不建议应用于生产环境

  • 22
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郁希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值