AI - 人工智能;Java之SpringAI(一)

AI - 人工智能;Java之SpringAI(二)

一、Ollama

官网:https://ollama.com/

Ollama是一个大模型部署运行工具,在该工具里面可以部署运行各种大模型,方便开发者在本地搭建一套大模型运行环境

LLM全称(large language model)也就是大语言模型

Ollama是一个用于部署和运行各种开源大模型的工具; 它能够帮助用户快速在本地运行各种大模型,极大地简化了大模型在本地运行的过程。 用户通过执行几条命令就能在本地运行开源大模型,如Llama 2等

(一)下载

下载:https://ollama.com/download

注:Ollama的运行会受到所使用模型大小的影响;

1、例如,运行一个7B(70亿参数)的模型至少需要8GB的可用内存(RAM),而运行一个13B(130亿参数)的模型需要16GB的内存,33B(330亿参数)的模型需要32GB的内存

2、需要考虑有足够的磁盘空间,大模型的文件大小可能比较大,建议至少为Ollama和其模型预留50GB的磁盘空间

3、性能较高的CPU可以提供更好的运算速度和效率,多核处理器能够更好地处理并行任务,选择具有足够核心数的CPU

4、显卡(GPU):Ollama支持纯CPU运行,但如果电脑配备了NVIDIA GPU,可以利用GPU进行加速,提高模型的运行速度和性能

(二)安装

(三)运行

ollama官网大模型:https://ollama.com/library

点击“Models”

输入框搜索大模型名字

选择相应的数据模型,复制右侧的命令

如,选择qwen2.5:3b(阿里的通义千问)

ollama run qwen2.5:3b

大模型下载完成后会出现“success”的提示,还会给出一个让你“>>>Send a message(/? for help)”的提示框,在此输入需要提问的问题

比如输入“苏州”

(四)Ollama api默认监听11434端口

使用命令进行查看:

netstat -ano | findstr 11434

二、SpringAI集成Ollama

使用SpringAI,需要确保开发环境满足以下2个要求:

1、JDK版本:JDK 17(含)以上‌

2、SpringBoot版本:3.2以上‌

3、IDEA2024版

1、创建SpringAI项目

选择Spring Boot 至少3.2版本;且勾选AI中的OpenAI选项(2024之前的老版本IDEA应该没有这个选项)

2、添加依赖;加入spring-ai-ollama-spring-boot-starter依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.demo</groupId>
    <artifactId>spring-ai-06-ollama</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-ai-06-ollama</name>
    <description>spring-ai-06-ollama</description>

    <properties>
        <java.version>17</java.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- SpringAI当前版本1.0.0-M3;快照版1.0.0-SNAPSHOT -->
        <spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--spring ai的starter依赖,启动依赖-->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--相当于是继承一个父项目:spring-ai-bom父项目-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>${spring-ai.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!--配置本项目的仓库:因为maven中心仓库还没有更新spring ai的jar包-->
    <repositories>
        <repository>
            <!-- SpringAI快照版 -->
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <!-- 当前最新M3版 -->
            <!--            <id>spring-milestones</id>-->
            <!--            <name>Spring Milestones</name>-->
            <!--            <url>https://repo.spring.io/milestone</url>-->
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

ai依赖: 

<properties>
    <java.version>17</java.version>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- SpringAI当前版本1.0.0-M3;快照版1.0.0-SNAPSHOT -->
    <spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>
</properties>


        <!--spring ai的starter依赖,启动依赖-->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
        </dependency>


    <!--配置本项目的仓库:因为maven中心仓库还没有更新spring ai的jar包-->
    <repositories>
        <repository>
            <!-- SpringAI快照版 -->
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <!-- 当前最新M3版 -->
            <!--            <id>spring-milestones</id>-->
            <!--            <name>Spring Milestones</name>-->
            <!--            <url>https://repo.spring.io/milestone</url>-->
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

Maven中心仓库搜不到这个依赖

3、配置yml文件

server:
  port: 8086

spring:
  application:
    name: spring-ai-06-ollama
  ai:
    ollama:
      base-url: http://localhost:11434
      chat:
        # 会被代码配置覆盖
        model: qwen2.5:3b

4、测试类(OllamaChatModel) 

 Ollama中SpringAI官网:Ollama Chat :: Spring AI Reference

package com.demo.springai06ollama.controller;

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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: zm
 */
@RequestMapping("/ai")
@RestController
public class OllamaController {
    @Resource
    private OllamaChatModel ollamaChatModel;

    @GetMapping("/ollama")
    public String ollama(@RequestParam("msg") String message) {
        return ollamaChatModel.call(message);
    }

    @GetMapping("/ollama/prompt")
    public Object ollamaPrompt(@RequestParam("msg") String message) {
        ChatResponse response = ollamaChatModel.call(new Prompt(message,
                        OllamaOptions.builder()
                                .withModel("qwen2.5:3b")//使用哪个大模型(代码会覆盖yml配置中的Model)
                                .withTemperature(0.4)//温度,温度值越高,准确率下降,温度值越低,准确率会提高
                                .build()
                ));
        return response.getResult().getOutput().getContent();
    }
}

运行启动,浏览器输入 http://localhost:8086/ai/ollama?msg=为你写一首情诗

5、如遇如下报错:{"error":"model \"mistral\" not found, try pulling it first"}

java.lang.RuntimeException: [404] Not Found - {"error":"model \"mistral\" not found, try pulling it first"}

(1)在yml配置中添加Model配置

spring:
  application:
    name: spring-ai-06-ollama
  ai:
    ollama:
      base-url: http://localhost:11434
      chat:
        # 会被代码配置覆盖
        model: qwen2.5:3b

(2)去Ollama官网Model中下载mistral大模型

Logo

分享最新的 NVIDIA AI Software 资源以及活动/会议信息,精选收录AI相关技术内容,欢迎大家加入社区并参与讨论。

更多推荐