SpringAI 集成本地Ollama大模型

06-01 1330阅读

随着人工智能技术的快速演进,DeepSeek 作为国内领先的大模型研发团队,其开源的 R1 系列模型凭借 "低成本、高性能" 的特性,在数学推理、代码生成等领域展现出与国际顶尖模型相媲美的能力。与此同时,Spring AI 作为 Spring 生态体系中专门针对 AI 工程化的框架,通过模块化设计和多模型适配能力,为 Java 开发者提供了便捷的 AI 集成方案。二者的结合,为企业级智能应用开发带来了新的技术路径。

关于如何在本地搭建deepSeek,可参考文献:基于DeepSeek R1 微调自己的大模型&Ollama本地部署_如何使用魔搭微调deepseek,并将大模型部署在本地-CSDN博客

SpringAi工程搭建

在idea中创建一个普通的Maven项目

SpringAI 集成本地Ollama大模型

添加项目依赖:

    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        3.2.4
         
    
    springAi
    1.0.0
    jar
    
        22
        22
        UTF-8
        1.0.0-M5
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.ai
            spring-ai-ollama-spring-boot-starter
            ${spring-ai.version}
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                3.2.3
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    
    
    
        
            spring-milestones
            https://repo.spring.io/milestone
        
    

这里注意,一定要指定milestones的仓库,因为在正式的仓库中还没有spring-ai的package。

SpringAI 集成本地Ollama大模型 

属性文件application.properties配置

属性文件的配置,一定要根据引入jar合适使用。网上有很多

SpringAI 集成本地Ollama大模型

SpringAI 集成本地Ollama大模型

SpringAI 集成本地Ollama大模型

SpringAI 集成本地Ollama大模型

上述做法都是错误的,依赖的jar没有相关的视线类,或者类根本不存在。

翻看OllamaChatModel的源码,可以找到实现类:

@AutoConfiguration(after = RestClientAutoConfiguration.class)
@ConditionalOnClass(OllamaApi.class)
@EnableConfigurationProperties({ OllamaChatProperties.class, OllamaEmbeddingProperties.class,
		OllamaConnectionProperties.class, OllamaInitializationProperties.class })
@ImportAutoConfiguration(classes = { RestClientAutoConfiguration.class, WebClientAutoConfiguration.class })
public class OllamaAutoConfiguration {
...
}

找到属性配置类:

SpringAI 集成本地Ollama大模型

SpringAI 集成本地Ollama大模型

SpringAI 集成本地Ollama大模型

 配置属性文件application.properties

server.port=8080
spring.application.name=SpringAi
# local ollama chat model
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.model=divine:latest
# self define model parameters
spring.ai.ollama.parameters.temperature=0.7
spring.ai.ollama.parameters.max_tokens=2048
spring.ai.ollama.parameters.streaming=true

配置类:

package com.test.ai;
import org.springframework.ai.autoconfigure.ollama.OllamaConnectionDetails;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;
import java.net.http.HttpClient;
import java.time.Duration;
@Configuration
public class OllamaConfig {
    // 配置restClient超时时间
    @Bean
    @Qualifier("OllamaRestClientBuilder")
    public RestClient.Builder ollamaRestClientBuilder() {
        JdkClientHttpRequestFactory requestFactory = new JdkClientHttpRequestFactory(
                HttpClient.newHttpClient());
        requestFactory.setReadTimeout(Duration.ofMinutes(3));
        return RestClient.builder().requestFactory(requestFactory);
    }
    // 配置WebClient超时时间
    @Bean
    @Qualifier("OllamaWebClientBuilder")
    public WebClient.Builder ollamaWebClientBuilder() {
        return WebClient.builder()
                .defaultHeader("Content-Type", "application/json")
                ;
    }
    @Bean
    public OllamaApi ollamaApi(OllamaConnectionDetails connectionDetails,
                               @Qualifier("OllamaRestClientBuilder") RestClient.Builder restClientBuilder,
                               @Qualifier("OllamaWebClientBuilder") WebClient.Builder ollamaWebClientBuilder) {
        return new OllamaApi(connectionDetails.getBaseUrl(), restClientBuilder, ollamaWebClientBuilder);
    }
}

RestController类:

package com.test.ai;
import jakarta.annotation.Resource;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ChatController {
    @Resource
    private OllamaChatModel chatModel;
    @GetMapping("/chat")
    public String generate(@RequestParam String prompt) {
        return chatModel.call(prompt);
    }
}

启动配置类:

package com.test.ai;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringAiApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringAiApplication.class, args);
    }
}

 

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码