使用Spring AI--以应用Ollama为例

部署运行你感兴趣的模型镜像

前言

Spring AI截止25.4.20,仍然没有发行版本

今天拉以前项目,导入pom.xml内容时报错,后来发现是版本更改了

Spring AI中文网站:https://www.spring-doc.cn/projects/spring-ai

官网显示支持Springboot版本在 3.2.x、3.3.x

但我在使用Ollama时,需要3.4.x --> 我项目使用的是3.4.4稳定版

使用步骤

添加远程仓库

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>

版本管理

<dependencyManagement>
    
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>1.0.0-M2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>

</dependencyManagement>

本人当时报错就在这里的

当时我用的时1.0.0-SNAPSHOT,在之前的项目是成功的,但是此次运行报错:

Could not find artifact org.springframework.ai:spring-ai-ollama-spring-boot-starter:pom:unknown in spring-milestones (https://repo.spring.io/milestone)

我一查https://repo.spring.io/ui/native/milestone网站:

没有找到我原来的1.0.0-SNAPSHOT版本的,可能那个快照版本没了

​ 然后改为1.0.0-M2,解决了…
在这里插入图片描述

引入Ollama依赖

<dependencies>

    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
    </dependency>

</dependencies>

测试

前件条件

注意,请提前在本机安装ollama及其模型!

在配置文件配置ollama配置:

spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.model=llama3.2:3b

util测试代码

package com.example.demo.util;

import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class ChatUtils {

    private final OllamaChatModel chatModel;

    @Autowired
    public ChatUtils(OllamaChatModel chatModel) {
        this.chatModel = chatModel;
    }

    public String test(String input) {
        String message = input; // 这里message应该是你要喂给 AI 的内容

        String result = this.chatModel.call(message);

        return result;
    }
}

controller代码

package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.util.ChatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class test {

    @Autowired
    private ChatUtils chatUtils;

    @GetMapping("/a")
    public String test(@RequestBody JSONObject input) {
        System.out.println(input);
        return chatUtils.test(input.getString("message"));
    }
}

接口测试

在这里插入图片描述

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

要在本地环境中将 Ollama 和 DeepSeek 集成到基于 Spring 的项目中(假设使用 spring-ai-alibaba),需要完成以下几个关键步骤。以下是详细的介绍: --- ### **一、背景知识** Ollama 和 DeepSeek 均为大模型推理引擎,提供强大的文本生成能力。而 Spring-AI-Alibaba 是阿里巴巴提供的一个 AI 开发套件,可以帮助开发者更轻松地整合第三方 AI 能力。 在此场景下,我们需要搭建一个本地服务架构,使得用户可以通过 Spring 应用程序调用 Ollama 和 DeepSeek 提供的功能。 --- ### **二、准备工作** 1. **安装 Ollama** 官方文档:https://ollama.ai/ 下载对应平台的二进制文件并启动服务,默认监听地址为 `http://localhost:11434`。 2. **下载 DeepSeek 模型** 如果你需要自托管 DeepSeek 模型,可以从 Hugging Face 获取对应的权重文件,并将其部署到你的系统上。 3. **创建 Spring Boot 工程** 创建一个新的 Spring Boot 项目(推荐使用 https://start.spring.io 初始化)。确保加入以下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-ai-alibaba-starter</artifactId> <version>x.x.x</version> </dependency> ``` 4. **配置 HTTP 请求工具** 可以选择 RestTemplate 或 WebClient 来发送请求给 Ollama 和 DeepSeek API。 --- ### **三、核心代码实现** #### **1. 配置 Ollama 和 DeepSeek 地址** 在 `application.yml` 中定义两者的访问路径: ```yaml ollama: url: http://localhost:11434/generate deepseek: url: http://localhost:7860/api/inference # 替换为你实际的服务端口 ``` #### **2. 封装请求方法** 编写统一的接口用于向两个外部服务发起调用: ```java import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class AiService { private final String ollamaUrl; private final String deepseekUrl; private final RestTemplate restTemplate = new RestTemplate(); public AiService() { this.ollamaUrl = "http://localhost:11434/generate"; this.deepseekUrl = "http://localhost:7860/api/inference"; // 示URL } /** * 发送请求到 Ollama */ public String callOllama(String prompt) { HttpHeaders headers = new HttpHeaders(); headers.set("Content-Type", "application/json"); Map<String, Object> body = Map.of( "prompt", prompt, "mo
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值