SpringAI+ollama+phi3大模型

前言:

最近发现SprinAI更新了1.0.0-M1预览版,即意味着SpringAI即将正式推出,本地跑一个玩玩,说不定以后有在项目中应用的场景。

环境:

Gradle - Kotlin 8.7
SpringBoot 3.2.6
Java17
SpringAI-1.0.0-M1
Ollama v0.1.42
phi3-3B
......

开始:

1、模型工具下载

https://ollama.com/在这个地址下载ollama,或者去GitHub找也行
本文使用的是办公电脑,16G内存完全可以运行

2、本地启动模型

本地安装一个小一点的模型,本文选用phi3-3B
ollama run phi3
执行这条命令后,就会开始下载并运行大模型

3、代码相关:

https://start.spring.io/勾选对应所需的依赖,打包下载后本地打开

测试代码如下:

spring:
  application:
    name: practice
  ai:
    ollama:
      chat:
        options:
          model: "phi3"

package com.example.practice;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @Resource
    private OllamaChatModel ollamaChatModel;

    @GetMapping("/chat/{msg}")
    public String chat(@PathVariable String msg) {
        ChatResponse phi3 = ollamaChatModel.call(new Prompt(msg, OllamaOptions.create().withModel("phi3")));
        return phi3.getResult().getOutput().getContent();
    }
}

之前旧版本也玩过,跟之前大有不同,因为旧版本号甚至没有贴在官网,旧代码就不贴出来了

4、运行效果

PixPin_2024-06-17_17-59-57.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容