Java 使用 Spring Boot 本地集成 DeepSeek

        了解如何在本地运行 DeepSeek 的开源模型并将其与 Spring Boot 集成,以构建强大的 AI 应用程序,而无需依赖付费 API

Springboot + Deepseek

 源码示例:https://download.youkuaiyun.com/download/hefeng_aspnet/90460307 

        人工智能正在以闪电般的速度发展,但运行大型语言模型 (LLM) 往往伴随着对云的依赖、高成本和 API 限制。如果您可以完全在本地机器上运行强大的人工智能模型,而且是免费的,那会怎样? 

        在本文中,将指导您如何将DeepSeek LLM 与 Spring Boot集成,从而实现实时 AI 响应,而无需依赖昂贵的基于云的 API。这种设置不仅经济高效,而且速度极快,使其成为开发人员进行实验、构建和亲身学习 AI 的完美场所。

Spring AI 来了

        Spring 还凭借其新集成Spring AI进入了 AI 领域,这使得将 AI 模型集成到 Spring Boot 应用程序中变得非常容易。借助对 LLM、向量存储和 AI 驱动的工作流的内置支持,开发人员现在可以轻松构建智能应用程序,同时保持可扩展性和灵活性。 

先决条件

  1. Java 17+
  2. Spring Boot 3.2+
  3. Maven/Gradle
  4. IDE(IntelliJ/Eclipse)
  5. DeepSeek API / 本地 DeepSeek

在本地设置 DeepSeek

步骤 1:安装 Ollama

从官方网站下载并安装Ollama : https://ollama.com(您将收到需要运行以设置 Ollama 的安装程序)

步骤 2:安装 DeepSeek LLM

您可以根据您的选择从https://ollama.com/library/deepseek-r1下载 DeepSeek LLM 模型

运行命令ollama run deepseek-r1:8b(这里我选择了 8b 型号(4.9GB))

Ollama DeepSeek-r1 模型 

安装成功后,您将看到以下输出。

将 DeepSeek 与 SpringBoot 应用程序集成

步骤 1:在 Pom.xml 中添加依赖项

<!-- pom.xml -->
<dependencies>

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

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
    </dependency>

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

</dependencies>

步骤2:设置应用程序Yaml

<!-- For connecting with Deepseek API -->

deepseek.api.key = your_api_key_here
deepseek.api.url = https://api.deepseek.com/v1/chat/completions

<!-- ------------------------------ --> 

<!-- For connecting with Deepseek Running on Local -->

spring.ai.ollama.chat.options.model=deepseek-r1:8b

<!-- ------------------------------ -->

步骤 3:设置 Rest Controller 来接收请求

@RestController
@RequestMapping("/ai-chat")
public class AIChatController {

    private final AIChatService aiChatService;

    public AIChatController(AIChatService aiChatService) {
        this.aiChatService = aiChatService;
    }


    @GetMapping
    public Flux<String> getAIChatResponse(@RequestParam String message) {
        return aiChatService.getAIResponse(message);
    }


}

步骤 4:构建服务层以集成 AI 聊天

@Service
@Slf4j
public class AIChatService {

    private final ChatClient chatClient;

    AIChatService(ChatClient.Builder builder){
        this.chatClient = builder
                .defaultSystem("You are an advanced AI assistant integrated into a Spring Boot application.Ensure clarity and accuracy in responses.")
                .build();
    }

    public Flux<String> getAIResponse(String message) {
        try {
            return chatClient.prompt(message)
                    .stream()
                    .content();
        } catch (Exception e) {
            throw new RuntimeException("Error occurred while fetching AI response, ex = " + e.getMessage());
        }
    }
}

步骤 5:测试

→ 样品请求

curl --location 'http://localhost:8080/ai-chat?message=What%20is%20Spring%20AI'

→ 示例回复

**Spring AI Overview**

Spring AI is a Java-based, open-source framework that provides a unified platform for building intelligent applications using machine learning (ML) and artificial intelligence (AI). It is built on top of the popular Spring Framework, which makes it easy to integrate with existing Spring applications.

**Key Features of Spring AI**

1. **Machine Learning**: Spring AI includes a range of ML algorithms, including supervised and unsupervised learning, regression, classification, clustering, and more.
2. **Deep Learning**: The framework supports popular deep learning frameworks like TensorFlow, PyTorch, and Keras, allowing developers to build complex neural networks.
3. **Model Serving**: Spring AI provides a model serving component that enables the deployment of trained models in production environments.
4. **Data Preprocessing**: The framework offers data preprocessing tools, including data ingestion, cleaning, transformation, and feature engineering.
5. **Integration with Other Frameworks**: Spring AI integrates seamlessly with other popular frameworks like Spring Boot, Apache Kafka, and Apache Spark.

**Benefits of Using Spring AI**
1. **Simplified ML Development**: Spring AI simplifies the process of building intelligent applications by providing a unified platform for ML development.
2. **Reduced Integration Effort**: By leveraging existing Spring infrastructure, developers can reduce integration effort and focus on building ML models.
3. **Improved Collaboration**: Spring AI enables teams to collaborate more effectively by providing a common framework for ML development.

源码示例:https://download.youkuaiyun.com/download/hefeng_aspnet/90460307

结论

总之,将 Free DeepSeek 与 Spring Boot 应用程序本地集成不仅可以增强项目的搜索功能,还可以提供处理复杂查询的高效且可扩展的解决方案。通过遵循分步指南,您可以成功实现本地 AI 聊天机器人,以确保更快、更相关的结果,同时保持对数据的完全控制。

借助 Spring Boot 强大的框架和 Free DeepSeek 的强大功能,您可以为应用程序的性能和用户体验解锁新的可能性。我希望本指南能帮助您了解集成过程,并鼓励您探索进一步的定制以满足您的特定需求。

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值