首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >基于SpringBoot+SpringAI+Ollama的本地大模型问答系统开发指南

基于SpringBoot+SpringAI+Ollama的本地大模型问答系统开发指南

作者头像
用户8589624
发布2025-11-16 09:10:02
发布2025-11-16 09:10:02
1890
举报
文章被收录于专栏:nginxnginx

基于SpringBoot+SpringAI+Ollama的本地大模型问答系统开发指南

引言

在人工智能技术飞速发展的今天,大语言模型(LLM)已成为开发者工具箱中不可或缺的一部分。然而,依赖云端API服务不仅存在数据隐私问题,还可能产生高昂成本。本文将介绍如何利用SpringBoot、SpringAI框架结合Ollama本地大模型服务,搭建一个完全运行在本地Windows环境下的智能问答系统,并集成DeepSeek模型来实现高效的知识问答功能。

技术栈概述

SpringBoot与SpringAI

SpringBoot作为Java生态中最流行的应用框架,提供了快速构建生产级应用的能力。SpringAI是Spring生态系统中的新兴成员,专门为AI集成设计,它简化了与各种大语言模型的交互过程,提供了统一的API接口。

Ollama本地模型服务

Ollama是一个开源项目,允许开发者在本地运行和管理大型语言模型。它支持多种开源模型,包括Llama、Mistral等,并提供了简单的API接口。通过Ollama,我们可以在不依赖互联网连接的情况下使用强大的语言模型能力。

DeepSeek模型

DeepSeek是由深度求索公司开发的高性能大语言模型系列,在中文理解和生成任务上表现出色。我们将使用DeepSeek模型作为本系统的核心引擎。

环境准备

硬件要求
  • Windows 10/11操作系统
  • 至少16GB RAM(推荐32GB或以上)
  • NVIDIA显卡(可选,可加速推理)
软件安装

安装Ollama: 访问Ollama官网(https://ollama.ai)下载Windows版本并安装

下载DeepSeek模型: 在命令行中执行:

代码语言:javascript
复制
ollama pull deepseek

验证Ollama安装

代码语言:javascript
复制
ollama list

应该能看到deepseek模型

项目搭建

创建SpringBoot项目

使用Spring Initializr(https://start.spring.io)创建项目,选择以下依赖:

  • Spring Web
  • Lombok
  • Spring AI (如未列出可手动添加)
配置pom.xml

确保包含SpringAI Ollama依赖:

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
    <version>0.8.1</version>
</dependency>
应用配置

application.yml配置:

代码语言:javascript
复制
spring:
  ai:
    ollama:
      base-url: http://localhost:11434
      chat:
        model: deepseek
        options:
          temperature: 0.7
          top-p: 0.9

核心功能实现

问答服务层

创建QAService类:

代码语言:javascript
复制
@Service
public class QAService {
    
    private final OllamaChatClient chatClient;
    
    public QAService(OllamaChatClient chatClient) {
        this.chatClient = chatClient;
    }
    
    public String generateAnswer(String prompt) {
        return chatClient.call(prompt);
    }
    
    public Flux<String> generateStreamAnswer(String prompt) {
        return chatClient.stream(prompt);
    }
}
控制器实现

QAController.java:

代码语言:javascript
复制
@RestController
@RequestMapping("/api/qa")
public class QAController {
    
    private final QAService qaService;
    
    public QAController(QAService qaService) {
        this.qaService = qaService;
    }
    
    @PostMapping("/ask")
    public ResponseEntity<String> askQuestion(@RequestBody String question) {
        String answer = qaService.generateAnswer(question);
        return ResponseEntity.ok(answer);
    }
    
    @GetMapping(value = "/ask-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> askQuestionStream(@RequestParam String question) {
        return qaService.generateStreamAnswer(question);
    }
}
提示工程优化

为提高回答质量,我们可以实现提示模板:

PromptTemplateService.java:

代码语言:javascript
复制
@Service
public class PromptTemplateService {
    
    private static final String QA_TEMPLATE = """
            你是一个专业的AI助手,请根据以下要求回答问题:
            1. 回答要专业、准确
            2. 如果问题涉及不确定信息,请明确说明
            3. 保持回答简洁明了
            
            问题:{question}
            """;
    
    public String buildPrompt(String question) {
        return QA_TEMPLATE.replace("{question}", question);
    }
}

更新QAService使用提示模板:

代码语言:javascript
复制
public String generateAnswer(String prompt) {
    String formattedPrompt = promptTemplateService.buildPrompt(prompt);
    return chatClient.call(formattedPrompt);
}

高级功能实现

对话历史管理

实现简单的对话记忆功能:

ConversationManager.java:

代码语言:javascript
复制
@Service
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class ConversationManager {
    
    private final List<String> conversationHistory = new ArrayList<>();
    
    public void addExchange(String userInput, String aiResponse) {
        conversationHistory.add("用户: " + userInput);
        conversationHistory.add("AI: " + aiResponse);
    }
    
    public String getConversationContext() {
        return String.join("\n", conversationHistory);
    }
    
    public void clear() {
        conversationHistory.clear();
    }
}

更新提示模板以包含历史:

代码语言:javascript
复制
public String buildPrompt(String question, String history) {
    return QA_TEMPLATE.replace("{history}", history)
                     .replace("{question}", question);
}
文件内容问答

实现基于上传文档的问答功能:

DocumentService.java:

代码语言:javascript
复制
@Service
public class DocumentService {
    
    private final ResourceLoader resourceLoader;
    private final TextSplitter textSplitter;
    
    public DocumentService(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
        this.textSplitter = new TokenTextSplitter();
    }
    
    public List<String> processDocument(MultipartFile file) throws IOException {
        String content = new String(file.getBytes(), StandardCharsets.UTF_8);
        return textSplitter.split(content);
    }
    
    public String extractRelevantParts(List<String> chunks, String question) {
        // 简化的相关性匹配 - 实际项目应使用嵌入向量
        return chunks.stream()
                .filter(chunk -> chunk.toLowerCase().contains(question.toLowerCase()))
                .findFirst()
                .orElse("");
    }
}

添加文档问答端点:

代码语言:javascript
复制
@PostMapping(value = "/ask-with-doc", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> askWithDocument(
        @RequestParam String question,
        @RequestParam MultipartFile document) throws IOException {
    
    List<String> chunks = documentService.processDocument(document);
    String context = documentService.extractRelevantParts(chunks, question);
    
    String prompt = """
            基于以下文档内容回答问题:
            
            文档相关部分:
            {context}
            
            问题:{question}
            """.replace("{context}", context)
              .replace("{question}", question);
    
    String answer = qaService.generateAnswer(prompt);
    return ResponseEntity.ok(answer);
}

前端交互实现

简单HTML界面

resources/static/index.html:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
    <title>本地AI问答系统</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
    <h1>DeepSeek本地问答系统</h1>
    <div>
        <textarea id="question" rows="4" cols="50"></textarea>
    </div>
    <button onclick="askQuestion()">提问</button>
    <div id="answer" style="margin-top: 20px; border: 1px solid #ccc; padding: 10px;"></div>
    
    <script>
        function askQuestion() {
            const question = document.getElementById('question').value;
            document.getElementById('answer').innerText = "思考中...";
            
            axios.post('/api/qa/ask', question, {
                headers: { 'Content-Type': 'text/plain' }
            })
            .then(response => {
                document.getElementById('answer').innerText = response.data;
            })
            .catch(error => {
                document.getElementById('answer').innerText = "出错: " + error.message;
            });
        }
    </script>
</body>
</html>
流式响应界面

添加流式问答HTML:

代码语言:javascript
复制
<div style="margin-top: 30px;">
    <h2>流式问答</h2>
    <textarea id="streamQuestion" rows="4" cols="50"></textarea>
    <button onclick="askStreamQuestion()">流式提问</button>
    <div id="streamAnswer" style="margin-top: 20px; border: 1px solid #ccc; padding: 10px;"></div>
</div>

<script>
    function askStreamQuestion() {
        const question = document.getElementById('streamQuestion').value;
        const answerDiv = document.getElementById('streamAnswer');
        answerDiv.innerText = "";
        
        const eventSource = new EventSource(`/api/qa/ask-stream?question=${encodeURIComponent(question)}`);
        
        eventSource.onmessage = function(event) {
            answerDiv.innerText += event.data;
        };
        
        eventSource.onerror = function() {
            eventSource.close();
        };
    }
</script>

性能优化与调试

模型参数调优

application.yml中调整模型参数:

代码语言:javascript
复制
spring:
  ai:
    ollama:
      chat:
        options:
          temperature: 0.5  # 控制创造性(0-1)
          top-p: 0.9        # 核采样阈值
          num-predict: 512  # 最大token数
日志记录

配置日志以监控AI交互:

代码语言:javascript
复制
@Configuration
public class LoggingConfig {
    
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
    
    @Bean
    public OllamaApi ollamaApi(Client client, ObjectProvider<HttpMessageConverterCustomizer> customizers) {
        return new OllamaApiInterceptor(new OllamaApi(client, customizers));
    }
}

class OllamaApiInterceptor implements OllamaApi {
    
    private static final Logger log = LoggerFactory.getLogger(OllamaApiInterceptor.class);
    private final OllamaApi delegate;
    
    public OllamaApiInterceptor(OllamaApi delegate) {
        this.delegate = delegate;
    }
    
    @Override
    public GenerateResponse generate(GenerateRequest request) {
        log.info("Ollama请求: {}", request);
        GenerateResponse response = delegate.generate(request);
        log.debug("Ollama响应: {}", response);
        return response;
    }
}
超时设置

配置连接超时:

代码语言:javascript
复制
spring:
  ai:
    ollama:
      client:
        connect-timeout: 30s
        read-timeout: 5m

安全加固

API认证

添加简单的API密钥认证:

SecurityConfig.java:

代码语言:javascript
复制
@Configuration
@EnableWebSecurity
public class SecurityConfig {
    
    @Value("${app.api-key}")
    private String apiKey;
    
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/api/**").authenticated()
                .anyRequest().permitAll()
            )
            .addFilterBefore(new ApiKeyFilter(apiKey), UsernamePasswordAuthenticationFilter.class)
            .csrf().disable();
        return http.build();
    }
}

class ApiKeyFilter extends OncePerRequestFilter {
    
    private final String expectedApiKey;
    
    public ApiKeyFilter(String expectedApiKey) {
        this.expectedApiKey = expectedApiKey;
    }
    
    @Override
    protected void doFilterInternal(HttpServletRequest request, 
                                   HttpServletResponse response, 
                                   FilterChain filterChain) throws ServletException, IOException {
        String apiKey = request.getHeader("X-API-KEY");
        
        if (!expectedApiKey.equals(apiKey)) {
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "无效的API密钥");
            return;
        }
        
        filterChain.doFilter(request, response);
    }
}

部署与运行

启动Ollama服务

在Windows命令行中:

代码语言:javascript
复制
ollama serve
运行SpringBoot应用

在IDE中直接运行主类,或使用Maven命令:

代码语言:javascript
复制
mvn spring-boot:run
系统测试

访问 http://localhost:8080 测试问答功能,或使用Postman测试API端点。

扩展思路

向量数据库集成

考虑集成Chroma或Milvus等向量数据库实现更精准的文档检索:

代码语言:javascript
复制
@Configuration
public class VectorStoreConfig {
    
    @Bean
    public VectorStore vectorStore(EmbeddingClient embeddingClient) {
        return new SimpleVectorStore(embeddingClient);
    }
    
    @Bean
    public EmbeddingClient embeddingClient(OllamaApi ollamaApi) {
        return new OllamaEmbeddingClient(ollamaApi);
    }
}
多模型切换

实现动态模型选择:

代码语言:javascript
复制
@Service
public class ModelSelectorService {
    
    private final Map<String, ChatClient> clients;
    
    public ModelSelectorService(
            OllamaChatClient deepseekClient,
            OllamaChatClient llamaClient) {
        this.clients = Map.of(
            "deepseek", deepseekClient,
            "llama", llamaClient
        );
    }
    
    public ChatClient getClient(String modelName) {
        return clients.getOrDefault(modelName, clients.get("deepseek"));
    }
}

总结

本文详细介绍了如何使用SpringBoot、SpringAI和Ollama在本地Windows环境搭建一个功能完整的大模型问答系统。通过这个方案,开发者可以:

  1. 完全在本地运行AI服务,保障数据隐私
  2. 利用Spring生态快速构建生产级应用
  3. 灵活选择不同的开源模型
  4. 实现基础的问答到复杂的文档分析功能

随着本地AI技术的不断进步,这种架构将为更多企业应用提供安全、可控的AI解决方案。读者可以根据实际需求扩展本文示例,如增加更多模型支持、优化提示工程或集成更复杂的业务逻辑。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-11-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基于SpringBoot+SpringAI+Ollama的本地大模型问答系统开发指南
    • 引言
    • 技术栈概述
      • SpringBoot与SpringAI
      • Ollama本地模型服务
      • DeepSeek模型
    • 环境准备
      • 硬件要求
      • 软件安装
    • 项目搭建
      • 创建SpringBoot项目
      • 配置pom.xml
      • 应用配置
    • 核心功能实现
      • 问答服务层
      • 控制器实现
      • 提示工程优化
    • 高级功能实现
      • 对话历史管理
      • 文件内容问答
    • 前端交互实现
      • 简单HTML界面
      • 流式响应界面
    • 性能优化与调试
      • 模型参数调优
      • 日志记录
      • 超时设置
    • 安全加固
      • API认证
    • 部署与运行
      • 启动Ollama服务
      • 运行SpringBoot应用
      • 系统测试
    • 扩展思路
      • 向量数据库集成
      • 多模型切换
    • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档