当前位置: 首页 > news >正文

java(spring boot)实现向deepseek/GPT等模型的api发送请求/多轮对话(附源码)

我们再启动应用并获取api密钥后就可以对它发送请求了,但是官方文档对于如何进行多轮对话以及怎么自定义参数并没有说的很清楚,给的模板也没有java的,因此我们需要自己实现。


import org.json.JSONArray;
import org.json.JSONObject;import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;public class DeepSeekUtil {private static final String API_URL = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"; //  API 地址private static final String API_KEY = ""; // 请替换为你的 API 密钥// 与模型进行交互public static String chat(String userMessage, JSONArray messages) {// 如果没有传入消息历史,初始化一个空的 JSONArrayif (messages == null) {messages = new JSONArray();}// 添加用户的消息到对话历史messages.put(new JSONObject().put("role", "user").put("content", userMessage));JSONObject requestBody = new JSONObject();requestBody.put("model", "deepseek-v3-241226"); // 使用正确的模型名称requestBody.put("messages", messages); // 将历史对话传递给 APIrequestBody.put("temperature", 0.7); // 控制生成文本的创意性//requestBody.put("max_tokens", 1024); // 最大生成 token 数量,避免生成过长的回答HttpRequest request = HttpRequest.newBuilder().uri(URI.create(API_URL)).header("Content-Type", "application/json").header("Authorization", "Bearer " + API_KEY).POST(HttpRequest.BodyPublishers.ofString(requestBody.toString())).build();HttpClient client = HttpClient.newHttpClient();try {// 发送请求并获取响应HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());// 检查响应状态if (response.statusCode() != 200) {System.out.println("API Response Error: " + response.body());return "Error: API responded with status code " + response.statusCode();}// 从响应中获取 API 返回的内容String responseBody = response.body();System.out.println("API Response: " + responseBody);// 解析 API 响应JSONObject jsonResponse = new JSONObject(responseBody);JSONArray choices = jsonResponse.getJSONArray("choices");// 获取第一个 choice 中的 message 内容JSONObject firstChoice = choices.getJSONObject(0);JSONObject message = firstChoice.getJSONObject("message");String apiReply = message.getString("content");// 添加模型回复到对话历史messages.put(new JSONObject().put("role", "assistant").put("content", apiReply));// 返回 API 的回复return apiReply;} catch (Exception e) {// 出现错误时返回错误消息e.printStackTrace(); // 打印详细的错误信息return "Error: " + e.getMessage();}}
}

我们再编写测试类

 @Testvoid testChat(){JSONArray array=new JSONArray();String response=DeepSeekUtil.chat("你好",array);System.out.println(response);String response1=DeepSeekUtil.chat("帮我设计一个演示自由落体的网页",array);System.out.println(response1);}
http://www.lryc.cn/news/540904.html

相关文章:

  • module ‘cv2.dnn‘ has no attribute ‘DictValue‘解决办法
  • 将RocketMQ集成到了Spring Boot项目中,实现站内信功能
  • Deepseek 怼CHATGPT实况
  • 基础篇11-图像分割(上)--阈值的方法
  • [特殊字符] LeetCode 62. 不同路径 | 动态规划+递归优化详解
  • 常用的 JVM 参数:配置与优化指南
  • 【JavaWeb学习Day17】
  • DeepSeek 提示词:定义、作用、分类与设计原则
  • 前端大文件上传
  • JDK源码系列(一)Object
  • 【Python 打造高效文件分类工具】
  • 大数据组件(四)快速入门实时数据湖存储系统Apache Paimon(1)
  • 边缘安全加速(Edge Security Acceleration)
  • C/C++高性能Web开发框架全解析:2025技术选型指南
  • fedora 安装 ffmpeg 过程记录
  • 【GPU驱动】OpenGLES图形管线渲染机制
  • Spring Boot项目@Cacheable注解的使用
  • mac开发环境配置笔记
  • 重装CentOS YUM
  • 对免认证服务提供apikey验证
  • 数据库驱动免费下载(Oracle、Mysql、达梦、Postgresql)
  • OceanBase 初探学习历程之——安装部署
  • Windows 下免费开源的多格式文件差异对比工具
  • Vue3+element UI:使用el-dialog时,对话框不出现解决方案
  • postman调用ollama的api
  • PyTorch的dataloader制作自定义数据集
  • 如何调用 DeepSeek API:详细教程与示例
  • Hadoop-HA集群部署
  • 三、linux字符驱动详解
  • 【Research Proposal】基于提示词方法的智能体工具调用研究——研究问题