springboot整合阿里云百煉DeepSeek實(shí)現(xiàn)sse流式打印的操作方法
1.開通阿里云百煉,獲取到key
官方文檔地址
https://bailian.console.aliyun.com/?tab=api#/api/?type=model&url=https%3A%2F%2Fhelp.aliyun.com%2Fdocument_detail%2F2868565.html
2.新建SpringBoot項(xiàng)目
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>devicewx-ai</groupId> <artifactId>devicewx-ai</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.15</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--阿里云deepseek--> <!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dashscope-sdk-java</artifactId> <version>2.18.3</version> </dependency> <!-- 集成redis依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency> </dependencies> </project>
3.工具類
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; /** * redis工具類 * @Author: albc * @Date: 2024/07/17/15:31 * @Description: good good study,day day up */ @Component public class RedisUtil { @Resource private RedisTemplate<String, Object> redisTemplate; // =============================common============================ /** * 指定緩存失效時(shí)間 * * @param key 鍵 * @param time 時(shí)間(秒) */ public boolean expire(String key, long time) { try { if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 根據(jù)key 獲取過期時(shí)間 * * @param key 鍵 不能為null * @return 時(shí)間(秒) 返回0代表為永久有效 */ public long getExpire(String key) { return redisTemplate.getExpire(key, TimeUnit.SECONDS); } /** * 判斷key是否存在 * * @param key 鍵 * @return true 存在 false不存在 */ public boolean hasKey(String key) { try { return redisTemplate.hasKey(key); } catch (Exception e) { e.printStackTrace(); return false; } } /** * 刪除緩存 * * @param key 可以傳一個(gè)值 或多個(gè) */ @SuppressWarnings("unchecked") public void del(String... key) { if (key != null && key.length > 0) { if (key.length == 1) { redisTemplate.delete(key[0]); } else { //redisTemplate.delete(CollectionUtils.arrayToList(key)); } } } // ============================String============================= /** * 普通緩存獲取 * * @param key 鍵 * @return 值 */ public Object get(String key) { return key == null ? null : redisTemplate.opsForValue().get(key); } /** * 普通緩存放入 * * @param key 鍵 * @param value 值 * @return true成功 false失敗 */ public boolean set(String key, Object value) { try { redisTemplate.opsForValue().set(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 普通緩存放入并設(shè)置時(shí)間 * * @param key 鍵 * @param value 值 * @param time 時(shí)間(秒) time要大于0 如果time小于等于0 將設(shè)置無限期 * @return true成功 false 失敗 */ public boolean set(String key, Object value, long time) { try { if (time > 0) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } else { set(key, value); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 遞增 * * @param key 鍵 * @param delta 要增加幾(大于0) */ public long incr(String key, long delta) { if (delta < 0) { throw new RuntimeException("遞增因子必須大于0"); } return redisTemplate.opsForValue().increment(key, delta); } /** * 遞減 * * @param key 鍵 * @param delta 要減少幾(小于0) */ public long decr(String key, long delta) { if (delta < 0) { throw new RuntimeException("遞減因子必須大于0"); } return redisTemplate.opsForValue().increment(key, -delta); } // ================================Map================================= /** * HashGet * * @param key 鍵 不能為null * @param item 項(xiàng) 不能為null */ public Object hget(String key, String item) { return redisTemplate.opsForHash().get(key, item); } /** * 獲取hashKey對應(yīng)的所有鍵值 * * @param key 鍵 * @return 對應(yīng)的多個(gè)鍵值 */ public Map<Object, Object> hmget(String key) { return redisTemplate.opsForHash().entries(key); } /** * HashSet * * @param key 鍵 * @param map 對應(yīng)多個(gè)鍵值 */ public boolean hmset(String key, Map<String, Object> map) { try { redisTemplate.opsForHash().putAll(key, map); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * HashSet 并設(shè)置時(shí)間 * * @param key 鍵 * @param map 對應(yīng)多個(gè)鍵值 * @param time 時(shí)間(秒) * @return true成功 false失敗 */ public boolean hmset(String key, Map<String, Object> map, long time) { try { redisTemplate.opsForHash().putAll(key, map); if (time > 0) { expire(key, time); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建 * * @param key 鍵 * @param item 項(xiàng) * @param value 值 * @return true 成功 false失敗 */ public boolean hset(String key, String item, Object value) { try { redisTemplate.opsForHash().put(key, item, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建 * * @param key 鍵 * @param item 項(xiàng) * @param value 值 * @param time 時(shí)間(秒) 注意:如果已存在的hash表有時(shí)間,這里將會(huì)替換原有的時(shí)間 * @return true 成功 false失敗 */ public boolean hset(String key, String item, Object value, long time) { try { redisTemplate.opsForHash().put(key, item, value); if (time > 0) { expire(key, time); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 刪除hash表中的值 * * @param key 鍵 不能為null * @param item 項(xiàng) 可以使多個(gè) 不能為null */ public void hdel(String key, Object... item) { redisTemplate.opsForHash().delete(key, item); } /** * 判斷hash表中是否有該項(xiàng)的值 * * @param key 鍵 不能為null * @param item 項(xiàng) 不能為null * @return true 存在 false不存在 */ public boolean hHasKey(String key, String item) { return redisTemplate.opsForHash().hasKey(key, item); } /** * hash遞增 如果不存在,就會(huì)創(chuàng)建一個(gè) 并把新增后的值返回 * * @param key 鍵 * @param item 項(xiàng) * @param by 要增加幾(大于0) */ public double hincr(String key, String item, double by) { return redisTemplate.opsForHash().increment(key, item, by); } /** * hash遞減 * * @param key 鍵 * @param item 項(xiàng) * @param by 要減少記(小于0) */ public double hdecr(String key, String item, double by) { return redisTemplate.opsForHash().increment(key, item, -by); } // ============================set============================= /** * 根據(jù)key獲取Set中的所有值 * * @param key 鍵 */ public Set<Object> sGet(String key) { try { return redisTemplate.opsForSet().members(key); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 根據(jù)value從一個(gè)set中查詢,是否存在 * * @param key 鍵 * @param value 值 * @return true 存在 false不存在 */ public boolean sHasKey(String key, Object value) { try { return redisTemplate.opsForSet().isMember(key, value); } catch (Exception e) { e.printStackTrace(); return false; } } /** * 將數(shù)據(jù)放入set緩存 * * @param key 鍵 * @param values 值 可以是多個(gè) * @return 成功個(gè)數(shù) */ public long sSet(String key, Object... values) { try { return redisTemplate.opsForSet().add(key, values); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 將set數(shù)據(jù)放入緩存 * * @param key 鍵 * @param time 時(shí)間(秒) * @param values 值 可以是多個(gè) * @return 成功個(gè)數(shù) */ public long sSetAndTime(String key, long time, Object... values) { try { Long count = redisTemplate.opsForSet().add(key, values); if (time > 0) { expire(key, time); } return count; } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 獲取set緩存的長度 * * @param key 鍵 */ public long sGetSetSize(String key) { try { return redisTemplate.opsForSet().size(key); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 移除值為value的 * * @param key 鍵 * @param values 值 可以是多個(gè) * @return 移除的個(gè)數(shù) */ public long setRemove(String key, Object... values) { try { Long count = redisTemplate.opsForSet().remove(key, values); return count; } catch (Exception e) { e.printStackTrace(); return 0; } } // ===============================list================================= /** * 獲取list緩存的內(nèi)容 * * @param key 鍵 * @param start 開始 * @param end 結(jié)束 0 到 -1代表所有值 */ public List<Object> lGet(String key, long start, long end) { try { return redisTemplate.opsForList().range(key, start, end); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 獲取第一個(gè)元素并彈出 * * @param key key * @return 第一個(gè)元素 */ public Object lPop(String key) { try { return redisTemplate.opsForList().leftPop(key); } catch (Exception e) { return null; } } /** * 阻塞時(shí)間獲取 * @param key * @param timeout 秒 * @return */ public Object lPop(String key,long timeout) { try { return redisTemplate.opsForList().leftPop(key,timeout,TimeUnit.SECONDS); } catch (Exception e) { return null; } } /** * 獲取list緩存的長度 * * @param key 鍵 */ public long lGetListSize(String key) { try { return redisTemplate.opsForList().size(key); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 通過索引 獲取list中的值 * * @param key 鍵 * @param index 索引 index>=0時(shí), 0 表頭,1 第二個(gè)元素,依次類推;index<0時(shí),-1,表尾,-2倒數(shù)第二個(gè)元素,依次類推 */ public Object lGetIndex(String key, long index) { try { return redisTemplate.opsForList().index(key, index); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 將list放入緩存 * * @param key 鍵 * @param value 值 */ public boolean lSet(String key, Object value) { try { redisTemplate.opsForList().rightPush(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 將list放入緩存 * * @param key 鍵 * @param value 值 * @param time 時(shí)間(秒) */ public boolean lSet(String key, Object value, long time) { try { redisTemplate.opsForList().rightPush(key, value); if (time > 0) { expire(key, time); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 將list放入緩存 * * @param key 鍵 * @param value 值 * @return */ public boolean lSet(String key, List<Object> value) { try { redisTemplate.opsForList().rightPushAll(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 將list放入緩存 * * @param key 鍵 * @param value 值 * @param time 時(shí)間(秒) * @return */ public boolean lSet(String key, List<Object> value, long time) { try { redisTemplate.opsForList().rightPushAll(key, value); if (time > 0) { expire(key, time); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 根據(jù)索引修改list中的某條數(shù)據(jù) * * @param key 鍵 * @param index 索引 * @param value 值 * @return */ public boolean lUpdateIndex(String key, long index, Object value) { try { redisTemplate.opsForList().set(key, index, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 移除N個(gè)值為value * * @param key 鍵 * @param count 移除多少個(gè) * @param value 值 * @return 移除的個(gè)數(shù) */ public long lRemove(String key, long count, Object value) { try { Long remove = redisTemplate.opsForList().remove(key, count, value); return remove; } catch (Exception e) { e.printStackTrace(); return 0; } } }
4.啟動(dòng)類
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @Author: albc * @Date: 2025/04/17/10:03 * @Description: good good study,day day up */ @SpringBootApplication public class AiApplication { public static void main(String[] args) { SpringApplication.run(AiApplication.class,args); System.out.println("啟動(dòng)成功"); } }
5.測試類
import lombok.Data; /** * @Author: albc * @Date: 2025/04/17/10:12 * @Description: good good study,day day up */ @Data public class TestDto { private String content; }
6.測試方法
import com.alibaba.dashscope.aigc.generation.Generation; import com.alibaba.dashscope.aigc.generation.GenerationParam; import com.alibaba.dashscope.aigc.generation.GenerationResult; import com.alibaba.dashscope.common.Message; import com.alibaba.dashscope.common.Role; import com.alibaba.dashscope.exception.ApiException; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import com.atkj.dto.TestDto; import com.atkj.util.RedisUtil; import io.reactivex.Flowable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; /** * @Author: albc * @Date: 2025/04/17/10:02 * @Description: good good study,day day up */ @RequestMapping("/test") @RestController public class TestController { /** * 問題分析 */ private static StringBuilder reasoningContent = new StringBuilder(); /** * 完整回復(fù) */ private static StringBuilder finalContent = new StringBuilder(); private static boolean isFirstPrint = true; @Autowired private RedisUtil redisUtil; @PostMapping("/testRdisListSet") public String testRdisList(){ return "Ai測試方法"; } @PostMapping("/testAi") @ResponseBody public void testAi(HttpServletResponse httpServletResponse,@RequestBody TestDto testDto){ //正式版本自定義線程池,禁止創(chuàng)建線程 new Thread(()->{try { String content = testDto.getContent(); Generation gen = new Generation(); Message userMsg = Message.builder().role(Role.USER.getValue()).content(content).build(); streamCallWithMessage(gen, userMsg); // 打印最終結(jié)果 // if (reasoningContent.length() > 0) { // System.out.println("\n====================完整回復(fù)===================="); // System.out.println(finalContent.toString()); // } } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.println("An exception occurred:"+e.getMessage()); }}).start(); httpServletResponse.setContentType("text/event-stream"); httpServletResponse.setCharacterEncoding("utf-8"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String s = ""; while (true) { Object deep = redisUtil.lPop("deep",5); if (deep == null){ break; } s = "data: " + deep + "\n\n"; try { PrintWriter pw = httpServletResponse.getWriter(); Thread.sleep(10L); pw.write(s); pw.flush(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } private void handleGenerationResult(GenerationResult message) { String reasoning = message.getOutput().getChoices().get(0).getMessage().getReasoningContent(); String content = message.getOutput().getChoices().get(0).getMessage().getContent(); if (!reasoning.isEmpty()) { // reasoningContent.append(reasoning); if (isFirstPrint) { System.out.println("====================思考過程===================="); isFirstPrint = false; } redisUtil.lSet("deep", reasoning); System.out.print(reasoning); } if (!content.isEmpty()) { //finalContent.append(content); if (!isFirstPrint) { System.out.println("\n====================完整回復(fù)===================="); isFirstPrint = true; } redisUtil.lSet("deep", content); System.out.print(content); } } private static GenerationParam buildGenerationParam(Message userMsg) { return GenerationParam.builder() // 若沒有配置環(huán)境變量,請用百煉API Key將下行替換為:.apiKey("sk-xxx") //qwen-plus,deepseek-r1 .apiKey("sk-123123123123123123123123123123123") .model("deepseek-r1") .messages(Arrays.asList(userMsg)) // 不可以設(shè)置為"text" .resultFormat(GenerationParam.ResultFormat.MESSAGE) .incrementalOutput(true) .build(); } public void streamCallWithMessage(Generation gen, Message userMsg) throws NoApiKeyException, ApiException, InputRequiredException { GenerationParam param = buildGenerationParam(userMsg); Flowable<GenerationResult> result = gen.streamCall(param); result.blockingForEach(message -> handleGenerationResult(message)); } }
7.配置文件
server: port: 18868 spring: redis: host: 192.168.11.198 port: 7366 password: 123321 database: 0
8.測試結(jié)果
http://127.0.0.1:18868/test/testAi
到此這篇關(guān)于springboot整合阿里云百煉DeepSeek實(shí)現(xiàn)sse流式打印的操作方法的文章就介紹到這了,更多相關(guān)springboot整合阿里云百煉DeepSeek內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Java操作MySQL實(shí)現(xiàn)數(shù)據(jù)交互的方法
JDBC是Java中用于操作數(shù)據(jù)庫的API,可以為多種關(guān)系數(shù)據(jù)庫提供統(tǒng)一訪問,它通過JDK自帶的JDBC API和數(shù)據(jù)庫驅(qū)動(dòng)包進(jìn)行操作,實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查,本文給大家介紹使用Java操作MySQL實(shí)現(xiàn)數(shù)據(jù)交互的方法,感興趣的朋友跟隨小編一起看看吧2025-01-01Java基礎(chǔ)之位運(yùn)算知識(shí)總結(jié)
最近接觸到了java位運(yùn)算,之前對位運(yùn)算的了解僅僅停留在表現(xiàn)結(jié)果上,乘2除以2,對背后的原理并不了解,現(xiàn)在學(xué)習(xí)記錄一下,需要的朋友可以參考下2021-05-05Java?使用geotools讀取tiff數(shù)據(jù)的示例代碼
這篇文章主要介紹了Java?通過geotools讀取tiff,一般對于tiff數(shù)據(jù)的讀取,都會(huì)借助于gdal,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04Java線程啟動(dòng)為什么要用start()而不是run()?
這篇文章主要介紹了線程啟動(dòng)為什么要用start()而不是run()?下面文章圍繞start()與run()的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考價(jià)值,西藥的小火熬版可以參考一下,希望對你有所幫助2021-12-12