Java調(diào)用DeepSeek實(shí)現(xiàn)多輪對話功能
背景
近期deepSeek比較火,很多小伙伴嘗試用各種語言調(diào)用deepSeek,最近剛好也有些興趣,寫了一版關(guān)于適用java調(diào)用deepSeek的代碼供小白使用。
效果

1.創(chuàng)建API keys
登陸deepSeek開放平臺DeepSeek開放平臺創(chuàng)建api key

2.參考官網(wǎng)調(diào)用文檔
地址對話補(bǔ)全 | DeepSeek API Docs

3.多輪對話

代碼
最后附上代碼,感興趣的小伙伴可以拿去玩玩
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對話,請輸入內(nèi)容(輸入 'exit' 退出):");
String msg = scanner.nextLine(); // 讀取一行輸入
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(10, TimeUnit.SECONDS) // 連接超時時間
.readTimeout(300, TimeUnit.SECONDS) // 讀取超時時間
.writeTimeout(300, TimeUnit.SECONDS) // 寫入超時時間
.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")//你申請的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)多輪對話功能的文章就介紹到這了,更多相關(guān)Java DeepSeek多輪對話內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中項(xiàng)目如何讀取外置logback配置文件
這篇文章主要介紹了SpringBoot中項(xiàng)目如何讀取外置logback配置文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
java list,set,map,數(shù)組間的相互轉(zhuǎn)換詳解
這篇文章主要介紹了java list,set,map,數(shù)組間的相互轉(zhuǎn)換詳解的相關(guān)資料,這里附有實(shí)例代碼,具有參考價值,需要的朋友可以參考下2017-01-01
mybatis?@InsertProvider報錯問題及解決
這篇文章主要介紹了mybatis?@InsertProvider報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
spring?boot集成jasypt?并實(shí)現(xiàn)自定義加解密的詳細(xì)步驟
由于項(xiàng)目中的配置文件?配置的地方過多,現(xiàn)將配置文件統(tǒng)一放到nacos上集中管理?且密碼使用加密的方式放在配置文件中,配置文件的加密使用加密庫jasypt,本文給大家介紹spring boot集成jasypt并實(shí)現(xiàn)自定義加解密,感興趣的朋友一起看看吧2023-08-08

