珊珊老师 的笔记

做最负责任的教育~我是执行者珊珊

2025-03-28 16:03

Java项目引入DeepSeek搭建私有AI

珊珊老师

Java后端

(12)

(0)

收藏

一、环境准备

Java环境:确保安装了JDK 17+。

构建工具:使用Maven作为项目管理工具。

DeepSeek API Key:访问DeepSeek API开放平台,按照页面提示申请API Key。

Spring Boot:使用Spring Boot 3.2+版本。

二、项目创建

1. 创建Spring Boot项目

使用Spring Initializr快速创建一个Spring Boot项目,添加以下依赖:

Spring Web

Spring AI(如果使用Spring AI集成)

2. 配置文件

在application.properties中添加DeepSeek的API配置:

spring.ai.openai.base-url=https://api.deepseek.com/
spring.ai.openai.api-key=你的API密钥
spring.ai.openai.chat.options.model=deepseek-r1:8b
logging.level.org.springframework.ai=DEBUG

3. 添加Maven依赖

pom.xml中添加必要的依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
</dependencies>

三、集成DeepSeek API

1. 创建AI服务客户端

创建一个DeepSeekClient类,用于封装与DeepSeek API的交互:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
public class DeepSeekClient {
    private static final String API_URL = "http://localhost:11434/api/generate"; // 私有化部署后的API地址
 
    public String generateText(String prompt) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost post = new HttpPost(API_URL);
            String json = "{"
                    + "\"model\": \"deepseek-r1:8b\","
                    + "\"prompt\": \"" + prompt + "\","
                    + "\"max_tokens\": 50"
                    + "}";
            post.setEntity(new StringEntity(json));
            post.setHeader("Content-Type", "application/json");
 
            HttpResponse response = httpClient.execute(post);
            String result = EntityUtils.toString(response.getEntity());
            System.out.println("API Response:\n" + result);
 
            JSONObject jsonObject = JSON.parseObject(result);
            String responseStr = jsonObject.getString("response");
            String cleanedResponse = responseStr.replaceAll("\\u003c/?.*?\\u003e", "").trim();
            return cleanedResponse;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

2. 调用AI服务

Main类中调用DeepSeek的AI服务:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
 
        DeepSeekClient client = new DeepSeekClient();
        String prompt = "Write a short story about a futuristic world.";
        String generatedText = client.generateText(prompt);
        System.out.println("Generated Text: " + generatedText);
    }
}

四、私有化部署DeepSeek

1. 部署DeepSeek

根据DeepSeek的私有化部署指南,在Linux服务器上部署DeepSeek服务。以下是部署步骤的简要概述:

  1. 安装Docker:确保服务器上安装了Docker。

  2. 拉取DeepSeek镜像:

docker pull deepseek/ollama:latest

  3. 运行DeepSeek容器:

docker run -d -p 11434:11434 deepseek/ollama:latest

2. 配置项目

将项目中的API地址指向私有化部署的DeepSeek服务地址。

五、运行与测试

启动Spring Boot项目,运行Main类中的代码,即可调用DeepSeek的AI服务,生成文本内容。


0条评论

点击登录参与评论