Java調(diào)用DeepSeek實(shí)現(xiàn)多輪對(duì)話功能
背景
近期deepSeek比較火,很多小伙伴嘗試用各種語(yǔ)言調(diào)用deepSeek,最近剛好也有些興趣,寫了一版關(guān)于適用java調(diào)用deepSeek的代碼供小白使用。
效果
1.創(chuàng)建API keys
登陸deepSeek開(kāi)放平臺(tái)DeepSeek開(kāi)放平臺(tái)創(chuàng)建api key
2.參考官網(wǎng)調(diào)用文檔
地址對(duì)話補(bǔ)全 | DeepSeek API Docs
3.多輪對(duì)話
代碼
最后附上代碼,感興趣的小伙伴可以拿去玩玩
package com.ssc.gdh.phbc.data.serve.strategy.layer; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson2.JSON; import okhttp3.*; import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.util.Scanner; import java.util.concurrent.TimeUnit; public class DeepSeek { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); JSONArray chat = null; while (true){ System.out.print("與deepSeek對(duì)話,請(qǐng)輸入內(nèi)容(輸入 'exit' 退出):"); String msg = scanner.nextLine(); // 讀取一行輸入 OkHttpClient client = new OkHttpClient().newBuilder() .connectTimeout(10, TimeUnit.SECONDS) // 連接超時(shí)時(shí)間 .readTimeout(300, TimeUnit.SECONDS) // 讀取超時(shí)時(shí)間 .writeTimeout(300, TimeUnit.SECONDS) // 寫入超時(shí)時(shí)間 .build(); // 檢查是否退出 if ("exit".equalsIgnoreCase(msg)) { System.out.println("程序已退出。"); break; // 退出循環(huán) } chat = sendMessage(client, msg, chat); } } private static JSONArray sendMessage(OkHttpClient client,String question,JSONArray chat) { MediaType mediaType = MediaType.parse("application/json"); JSONObject param = new JSONObject(); param.put("model","deepseek-chat"); param.put("frequency_penalty",0); param.put("presence_penalty",0); JSONArray messages = chat; if (messages == null){ messages = new JSONArray(); JSONObject o1 = new JSONObject(); o1.put("role","user"); o1.put("content",question); messages.add(o1); }else { JSONObject o1 = new JSONObject(); o1.put("role","user"); o1.put("content",question); messages.add(o1); } param.put("messages",messages); // RequestBody body = RequestBody.create(mediaType, String.format("{\n \"messages\": [\n {\n \"content\": \"%s\"," + // "\n \"role\": \"user\"\n },\n {\n \"content\": \"%s\",\n \"role\": \"user\"\n }\n ],\n \"model\": \"deepseek-chat\",\n \"frequency_penalty\": 0,\n \"max_tokens\": 2048," + // "\n \"presence_penalty\": 0,\n \"response_format\": {\n \"type\": \"text\"\n },\n \"stop\": null,\n \"stream\": false," + // "\n \"stream_options\": null,\n \"temperature\": 1,\n \"top_p\": 1,\n \"tools\": null,\n \"tool_choice\": \"none\",\n \"logprobs\": false,\n \"top_logprobs\": null\n}",question,question1)); RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(param)); Request request = new Request.Builder() .url("https://api.deepseek.com/chat/completions") .method("POST", body) .addHeader("Content-Type", "application/json") .addHeader("Accept", "application/json") .addHeader("Authorization", "Bearer 你的api_keys")//你申請(qǐng)的api_keys .build(); try (Response response = client.newCall(request).execute()) { if (response.isSuccessful()) { ResponseBody responseBody = response.body(); if (responseBody != null) { // 將響應(yīng)體轉(zhuǎn)換為字符串 String responseString = responseBody.string(); JSONObject jsonObject = JSONObject.parseObject(responseString); JSONArray choices = JSONArray.parseArray(jsonObject.getString("choices")); String content = JSONObject.parseObject(JSONObject.parseObject(choices.get(0).toString()).get("message").toString()).get("content").toString(); if (StringUtils.isNotBlank(content)){ JSONObject o1 = new JSONObject(); o1.put("role","assistant"); o1.put("content",content); messages.add(o1); } System.out.println(content); return messages; } } else { System.out.println("Request failed with code: " + response.code()); } } catch (IOException e) { e.printStackTrace(); } return null; } }
到此這篇關(guān)于Java調(diào)用DeepSeek實(shí)現(xiàn)多輪對(duì)話功能的文章就介紹到這了,更多相關(guān)Java DeepSeek多輪對(duì)話內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java默認(rèn)傳入時(shí)間段時(shí)間的實(shí)例
下面小編就為大家?guī)?lái)一篇Java默認(rèn)傳入時(shí)間段時(shí)間的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10SpringBoot中項(xiàng)目如何讀取外置logback配置文件
這篇文章主要介紹了SpringBoot中項(xiàng)目如何讀取外置logback配置文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11java list,set,map,數(shù)組間的相互轉(zhuǎn)換詳解
這篇文章主要介紹了java list,set,map,數(shù)組間的相互轉(zhuǎn)換詳解的相關(guān)資料,這里附有實(shí)例代碼,具有參考價(jià)值,需要的朋友可以參考下2017-01-01Spring Boot中自動(dòng)化配置的利弊以及解決方法
這篇文章主要給大家介紹了關(guān)于Spring Boot中自動(dòng)化配置的利弊以及解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-08-08mybatis?@InsertProvider報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了mybatis?@InsertProvider報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07spring?boot集成jasypt?并實(shí)現(xiàn)自定義加解密的詳細(xì)步驟
由于項(xiàng)目中的配置文件?配置的地方過(guò)多,現(xiàn)將配置文件統(tǒng)一放到nacos上集中管理?且密碼使用加密的方式放在配置文件中,配置文件的加密使用加密庫(kù)jasypt,本文給大家介紹spring boot集成jasypt并實(shí)現(xiàn)自定義加解密,感興趣的朋友一起看看吧2023-08-08