SpringAI 调用本地ollama大模型

pom文件

java 复制代码
<?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

java 复制代码
spring:
  application:
    name: spring-ai-ollama

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

Demo

java 复制代码
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部署千问大模型

相关推荐
丸码1 天前
Java异常体系全解析
java·开发语言
v***88561 天前
Springboot项目:使用MockMvc测试get和post接口(含单个和多个请求参数场景)
java·spring boot·后端
q***49451 天前
Ubuntu介绍、与centos的区别、基于VMware安装Ubuntu Server 22.04、配置远程连接、安装jdk+Tomcat
java·ubuntu·centos
q***72191 天前
PHP使用Redis实战实录2:Redis扩展方法和PHP连接Redis的多种方案
开发语言·redis·php
k***82511 天前
python爬虫——爬取全年天气数据并做可视化分析
开发语言·爬虫·python
IMPYLH1 天前
Lua 的 require 函数
java·开发语言·笔记·后端·junit·lua
曾经的三心草1 天前
基于正倒排索引的Java文档搜索引擎1-实现索引模块-实现Parser类
java·开发语言·搜索引擎
q***01651 天前
Python爬虫完整代码拿走不谢
开发语言·爬虫·python
vx_bscxy3221 天前
告别毕设焦虑!Python 爬虫 + Java 系统 + 数据大屏,含详细开发文档 基于web的图书管理系统74010 (上万套实战教程,赠送源码)
java·前端·课程设计
顺心而行...1 天前
一些问题记录
开发语言