一、项目接入线上deepseek

第一步:获取接入deepseek需要的key值

进入官网DeepSeek | 深度求索

第二步:项目接入deepseek

引入依赖

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.9.0</version>
        </dependency>

下面的方法是利用sse实现消息的流式传输

package com.dylan.thesis.controller;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;


@RestController
@RequestMapping("/api/v1")
public class ChatController {

    @GetMapping(value = "/send")
    public SseEmitter createConnect(@RequestParam(name = "message", required = true) String message) {
        SseEmitter sseEmitter = new SseEmitter(-0L); //创建sse对象 -0表示永不超时
        sendMessage(sseEmitter,"你好", "deepseek-reasoner", "https://api.deepseek.com/v1/chat/completions", "sk-d6fb2***********************ba7b");
        return sseEmitter;
    }

    /**

     * @param emitter  sse对象
     * @param message  提问的问题
     * @param mode     模型  //DeepSeek-V3:deepseek-chat  DeepSeek-R1:deepseek-reasoner
     * @param apiHost  接入的api的url
     * @param apiKey   接入的key
     */
    public void sendMessage(SseEmitter emitter,String message, String mode, String apiHost, String apiKey) {
        new Thread(()->{
            if (emitter == null) {
                throw new RuntimeException("聊天消息推送失败uid:[{}],没有创建连接,请重试。~");
            }
            //update-begin---author:chenrui ---date:20240625  for:[TV360X-1570]给于更友好的提示,提示未配置ai------------
            try {
                OkHttpClient client = new OkHttpClient();
                //创建问题对象
                Map<String, String> messages = new HashMap<>();
                messages.put("role", "user");
                messages.put("content", message);
                //创建请求参数体
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("model", mode); //模型
                requestMap.put("messages", Collections.singletonList(messages)); //问题
                requestMap.put("stream", true); //流式返回
                // 1. 构建 Multipart 请求体
                MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                String s = JSONObject.toJSONString(requestMap);
                RequestBody body = RequestBody.create(s, JSON);
                // 2. 构建请求
                Request request = new Request.Builder()
                        .url(apiHost)
                        .addHeader("Content-Type", "application/json")
                        .post(body)
                        .header("Authorization", "Bearer " + apiKey)
                        .build();
                Response response = client.newCall(request).execute();
                if (!response.isSuccessful()) {
                    emitter.completeWithError(new RuntimeException("Unexpected code " + response));
                    return;
                }
                ResponseBody responseBody = response.body();
                if (responseBody != null) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody.byteStream()));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        // 处理每一行数据并发送给客户端
                        SseEmitter.SseEventBuilder event = SseEmitter.event()
                                .data(line)
                                .id(String.valueOf(System.currentTimeMillis()))
                                .name("message");
                        emitter.send(event);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                emitter.complete();
            }

        }).start();
    }


}

下面是返回结果红色框起来的就是问题回答的内容

二、项目接入本地部署的deepseek

第一步:进入ollama官网 Ollama

下载好ollama之后一直下一步就安装好了

第二步:ollama下载

进入控制台运行(下载的1.5b ,第二个图为所有版的deepseek,7b的下载命令为ollama run deepseek-r1:7b,其他的一次类推)

ollama run deepseek-r1:1.5b

下载好了之后

看到这个你就本地部署好了,但是本地访问想要ip远程访问需要配置系统环境变量

OLLAMA_HOST  0.0.0.0:11434

这样就可以远程访问了

第二步:项目接入deepseek

 引入依赖

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.9.0</version>
        </dependency>

下面的方法是利用sse实现消息的流式传输

package com.dylan.thesis.controller;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;


@RestController
@RequestMapping("/api/v1")
public class ChatController {

    @GetMapping(value = "/send")
    public SseEmitter createConnect(@RequestParam(name = "message", required = true) String message) {
        SseEmitter sseEmitter = new SseEmitter(-0L); //创建sse对象 -0表示永不超时
       sendMessage(sseEmitter,"你好", "deepseek-r1:1.5b", "http://localhost:11434/v1/chat/completions", "ollama", "123456789"); //本地接入 key:ollama固定 mode:deepseek-r1:1.5b固定为你的版本    
        return sseEmitter;
    }

    /**

     * @param emitter  sse对象
     * @param message  提问的问题
     * @param mode     模型  //DeepSeek-V3:deepseek-chat  DeepSeek-R1:deepseek-reasoner
     * @param apiHost  接入的api的url
     * @param apiKey   接入的key
     */
    public void sendMessage(SseEmitter emitter,String message, String mode, String apiHost, String apiKey) {
        new Thread(()->{
            if (emitter == null) {
                throw new RuntimeException("聊天消息推送失败uid:[{}],没有创建连接,请重试。~");
            }
            //update-begin---author:chenrui ---date:20240625  for:[TV360X-1570]给于更友好的提示,提示未配置ai------------
            try {
                OkHttpClient client = new OkHttpClient();
                //创建问题对象
                Map<String, String> messages = new HashMap<>();
                messages.put("role", "user");
                messages.put("content", message);
                //创建请求参数体
                Map<String, Object> requestMap = new HashMap<>();
                requestMap.put("model", mode); //模型
                requestMap.put("messages", Collections.singletonList(messages)); //问题
                requestMap.put("stream", true); //流式返回
                // 1. 构建 Multipart 请求体
                MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                String s = JSONObject.toJSONString(requestMap);
                RequestBody body = RequestBody.create(s, JSON);
                // 2. 构建请求
                Request request = new Request.Builder()
                        .url(apiHost)
                        .addHeader("Content-Type", "application/json")
                        .post(body)
                        .header("Authorization", "Bearer " + apiKey)
                        .build();
                Response response = client.newCall(request).execute();
                if (!response.isSuccessful()) {
                    emitter.completeWithError(new RuntimeException("Unexpected code " + response));
                    return;
                }
                ResponseBody responseBody = response.body();
                if (responseBody != null) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody.byteStream()));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        // 处理每一行数据并发送给客户端
                        SseEmitter.SseEventBuilder event = SseEmitter.event()
                                .data(line)
                                .id(String.valueOf(System.currentTimeMillis()))
                                .name("message");
                        emitter.send(event);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                emitter.complete();
            }

        }).start();
    }


}

下面是返回结果红色框起来的就是问题回答的内容

至此结束

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐