Java调用DeepSeek实现多轮对话功能
作者:渡劫的咸鱼
对于广大Java开发者而言,DeepSeek这一先进的人工智能技术,犹如一把钥匙,为我们打开了提升开发效率与创新能力的新大门,本文给大家介绍了Java调用DeepSeek实现多轮对话功能,需要的朋友可以参考下
背景
近期deepSeek比较火,很多小伙伴尝试用各种语言调用deepSeek,最近刚好也有些兴趣,写了一版关于适用java调用deepSeek的代码供小白使用。
效果
1.创建API keys
登陆deepSeek开放平台DeepSeek开放平台创建api key
2.参考官网调用文档
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对话,请输入内容(输入 '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; // 退出循环 } 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) { // 将响应体转换为字符串 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; } }
到此这篇关于Java调用DeepSeek实现多轮对话功能的文章就介绍到这了,更多相关Java DeepSeek多轮对话内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!