springboot接入deepseek深度求索代碼示例(java版)
以下是在springboot中接入ai deepseek的過程。官網(wǎng)并沒有java的示例。
1. 創(chuàng)建 API key
點擊創(chuàng)建API key,把創(chuàng)建的key值復制下來,以后就不能再查看了,只能重新創(chuàng)建。
2. 封裝詢問deepseek的工具方法
添加key值和詢問路徑。API_KEY為你創(chuàng)建的key值。
private static final String API_URL = "https://api.deepseek.com/chat/completions"; private static final String API_KEY = "11111111"; // 替換為你的 API Key
傳入的question就是要詢問的問題
public String askDeepSeek(String question) throws IOException { CloseableHttpClient client = HttpClients.createDefault(); // 創(chuàng)建 HTTP POST 請求 HttpPost request = new HttpPost(API_URL); request.setHeader("Content-Type", "application/json"); request.setHeader("Authorization", "Bearer " + API_KEY); // 動態(tài)構建請求體 String requestBody = String.format( "{\"model\": \"deepseek-chat\", \"messages\": [{\"role\": \"user\", \"content\": \"%s\"}], \"stream\": false}", question ); request.setEntity(new StringEntity(requestBody)); // 發(fā)送請求并獲取響應 try (CloseableHttpResponse response = client.execute(request)) { // 返回響應內容 return EntityUtils.toString(response.getEntity()); } }
3. 調用該詢問ai的方法
String question1= "今天是星期幾。 " ; String answer = askDeepSeek(question); System.out.println("answer = " + answer);
4. 成功返回示例
answer = {"id":"88dbce49-2841-448d-a74f-a2d3180c5672","object":"chat.completion","created":1734525002,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"當然,我很高興!謝謝你的關心。??"},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":12,"completion_tokens":11,"total_tokens":23,"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":12},"system_fingerprint":"fp_1bcb2de9ac"}
不過我接入之后,他只能回答一些很簡單的問題,有沒有大佬會用啊~
總結
到此這篇關于springboot接入deepseek深度求索的文章就介紹到這了,更多相關springboot接入deepseek深度求索內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java的Swing編程中使用SwingWorker線程模式及頂層容器
這篇文章主要介紹了在Java的Swing編程中使用SwingWorker線程模式及頂層容器的方法,適用于客戶端圖形化界面軟件的開發(fā),需要的朋友可以參考下2016-01-01解決mybatis竟然報Invalid value for getInt()的問題
使用mybatis遇到一個非常奇葩的問題,總是報Invalid value for getInt()的問題,怎么解決呢?下面小編通過場景分析給大家代來了mybatis報Invalid value for getInt()的解決方法,感興趣的朋友參考下吧2021-10-10SpringBoot應用程序啟動監(jiān)聽功能的常見方法
應用啟動監(jiān)聽在?Spring?Boot?和其他框架中扮演著重要的角色,它們的主要作用是在應用啟動或關閉時觸發(fā)特定的操作或任務,本文給大家介紹了SpringBoot應用程序啟動監(jiān)聽功能的常見方法,需要的朋友可以參考下2024-05-05mybatis-plus與mybatis共存的實現(xiàn)
本文主要介紹了mybatis-plus與mybatis共存的實現(xiàn),文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03解決新版 Idea 中 SpringBoot 熱部署不生效的問題
這篇文章主要介紹了解決新版 Idea 中 SpringBoot 熱部署不生效的問題,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08