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

SpringAI 调用本地ollama大模型

pom文件

<?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.bjpowernode</groupId>
    <artifactId>spring-ai-ollama</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>spring-ai-ollama</name>
    <description>spring-ai-ollama</description>

    <properties>
        <java.version>17</java.version>
        <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>

        <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>

    <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>
            <id>spring-snapshot</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

</project>

配置文件:application.yml

spring:
  application:
    name: spring-ai-ollama

  ai:
    ollama:
      base-url: http://localhost:11434
      chat:
        options:
          model: qwen2.5-coder:7b

Demo

package com.bjpowernode.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.ChatModel;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OllamaController {

    @Resource
    private OllamaChatModel ollamaChatClient;


    @Resource
    private ChatModel chatClient;

    @RequestMapping(value = "/ai/ollama")
    public Object ollama(@RequestParam(value = "msg") String msg) {
        String called = ollamaChatClient.call(msg);
        System.out.println(called);

        return called;
    }

    @RequestMapping(value = "/ai/ollama2")
    public Object ollama2(@RequestParam(value = "msg") String msg) {



        ChatResponse chatResponse = ollamaChatClient.call(new Prompt(msg, OllamaOptions.builder()
                .model("qwen2.5-coder:7b") //使用哪个大模型
                .temperature(0.4).build())); //温度,温度值越高,准确率下降,温度值越低,准确率会提高
        System.out.println(chatResponse.getResult().getOutput().getText());
        return chatResponse.getResult().getOutput().getText();
    }

}

运行项目 测试demo
在这里插入图片描述
在这里插入图片描述

我这里ollama部署的是 qwen2.5-coder:7b 你们可以根据自己部署的模型更改代码中的配置进行访问

前文: Windows部署千问大模型
需要调用其他模型的可以根据下面地址查看API
Spring :官网
接口文档查看:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
AI Models就是可以调用的所有模型
Chat Models就是对话模型
在这里插入图片描述

SpringAI可以调用的对话模型:https://docs.spring.io/spring-ai/reference/api/chat/qianfan-chat.html
在这里插入图片描述
Spring AI支持:
1、Chat Models 聊天模型
2、Text-to-image Models 文本到图像模型:
3、Transcription (audio to text) Models 转录(音频到文本)模型
4、Embedding Models 嵌入模型
5、矢量数据库
6、用于AI模型和矢量存储的Spring Boot自动配置和启动器;
7、函数调用
8、用于数据工程的ETL框架
9、广泛的参考文档、示例应用程序和研讨会/课程材料

相关文章:

  • C++:四大强制类型转换
  • Redis7——进阶篇(二)
  • VirtualBox虚拟机转VM虚拟机
  • AIGC(生成式AI)试用 25 -- 跟着清华教程学习 - DeepSeek+DeepResearch让科研像聊天一样简单
  • 2025-03-01 学习记录--C/C++-C语言 使用欧几里得算法(辗转相除法)计算两个整数的最大公约数
  • 【2025年15期免费获取股票数据API接口】实例演示五种主流语言获取股票行情api接口之沪深A股解禁限售数据获取实例演示及接口API说明文档
  • 基于eRDMA实测DeepSeek开源的3FS
  • 【实战 ES】实战 Elasticsearch:快速上手与深度实践-2.2.3案例:电商订单日志每秒10万条写入优化
  • C# OnnxRuntime部署DAMO-YOLO人头检测
  • Spring Boot全局异常处理:“危机公关”团队
  • 【星云 Orbit • STM32F4】04.一触即发:GPIO 外部中断
  • 560.和为 K 的子数组
  • spring boot + vue 搭建环境
  • 【C++】类与对象:深入理解默认成员函数
  • Qt信号和槽
  • uniapp 系统学习,从入门到实战(八)—— Vuex 的使用
  • Oracle删除重复数据保留其中一条
  • C++:volatile、const、mutable关键字
  • SQL-labs less9-12 闯关记录
  • 2继续NTS库学习(读取shapefile)
  • 郑州做网站网站建设费用/seo百度快速排名软件
  • 泰州市住房和城乡建设局官方网站/查询网站收录
  • 动态网站开发是什么/青岛seo推广专员
  • 申请做网站编辑组长的工作设想/南昌seo招聘信息
  • 手机网站的文本排版是怎么做的/竞价是什么意思
  • 淘宝店铺装修免费全套模板/什么是seo和sem