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

对接DeepSeek

其实,整个对接过程很简单,就四步,获取key,找到接口文档,接口测试,代码对接。

获取 KEY

https://platform.deepseek.com/transactions
直接付款就是了(现在官网暂停充值2025年2月7日),不比以前gpt,花钱都不知道怎么充值。
在这里插入图片描述
输入任意key名称,即可创建key。
在这里插入图片描述

对接文档

在当前页面下面,即可看到接口文档。访问就是了。https://api-docs.deepseek.com/zh-cn/
在这里插入图片描述

接口测试

curl测试

这是接口文档里面的第一个示例。虽然只提供了curl、python、nodejs,对于我们对接来说,完全够了。
在这里插入图片描述
这里为了比较符合我们开发规范。如果你现在有个可以访问互联网的linux那就很简单了,把你的KEY复制出来,把下面的sk-5bf10*******************0eab换成你的实际key,然后在linux上面输入下面这个命令即可。

curl https://api.deepseek.com/chat/completions \-H "Content-Type: application/json" \-H "Authorization: Bearer sk-5bf10***********************0eab" \-d '{"model": "deepseek-chat","messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Hello!"}],"stream": false}'

PostMan

如果你没有一个联网的linux,那就简单,下载一个接口测试工具,例如postman
将curl中的对应信息填入到post中即可。
在这里插入图片描述

如下所示即可掉通。
在这里插入图片描述
例如,我用post提问:给一个SpringBoot入门案例
在这里插入图片描述

浏览器F12

如果你的电脑只有浏览一个,并且你实在不想安装类似postman这样的接口测试工具,那就用浏览器F12自带的开发者模式来调试吧。
随便打开一个页面,例如这里我打开的是百度,在console输入下面的代码。
在这里插入图片描述
注意如果出现这个提示信息。
Don’t paste code into the DevTools Console that you don’t understand or haven’t reviewed yourself. This could allow attackers to steal your identity or take control of your computer. Please type ‘allow pasting’ below and hit Enter to allow pasting.表示此时禁用了粘贴,这时候让你输入:allow pasting然后回车,就可以粘贴了。
在这里插入图片描述
有些网页会禁用鼠标右键粘贴(这种情况用ctrl + v就行了)

注意替换你的key

fetch('https://api.deepseek.com/chat/completions', {method: 'POST',headers: {'Content-Type': 'application/json','Authorization': 'Bearer sk-5bf10***********************0eab'},body: JSON.stringify({model: "deepseek-chat",messages: [{ role: "system", content: "SpringBoot和Spring框架的最大区别是什么?" },],stream: false})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

在这里插入图片描述

代码对接

传统API对接

这里,我以大家最为熟练的RestTemplate来调用,代码如下

	@Autowiredprivate RestTemplate restTemplate;@GetMapping("/api/traditional/restTemplate")public String traditional() {String url = "https://api.deepseek.com/chat/completions";HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);//换成你自己的Keyheaders.set("Authorization", "Bearer sk-5bf1074b825a4xxxxxxxx50eab");//构建请求参数ChatReqMessage message = new Message("system", "SpringBoot和Spring框架的最大区别是什么?");ChatReq requestBody = new ChatReq("deepseek-chat", Collections.singletonList(message), false);HttpEntity<ChatReq> entity = new HttpEntity<>(requestBody, headers);ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);if (response.getStatusCode().is2xxSuccessful()) {System.out.println("Response: " + response.getBody());} else {System.out.println("Error: " + response.getStatusCode());}return response.getBody();}

效果展示
在这里插入图片描述

流式API对接

如果有兴趣,建议你先阅读这篇文章,方便你理解。
传统API和流式响应API


http://www.mrgr.cn/news/90364.html

相关文章:

  • C++引用深度详解
  • DeepSeek+3D视觉机器人应用场景、前景和简单设计思路
  • 利用二分法进行 SQL 时间盲注
  • flutter Selector 使用
  • 第40天:Web开发-JS应用VueJS框架Vite构建启动打包渲染XSS源码泄露代码审计
  • Vue3 特点
  • 尚硅谷的ShardingShphere分库分表课程总结
  • ARM Cortex-M3/M4 权威指南 笔记【一】技术综述
  • 【腾讯地图】录入经纬度功能 - 支持地图选点
  • 3. CSS中@scope
  • 深入解析 STM32 GPIO:结构、配置与应用实践
  • FAST_LIVO2初次安装编译
  • DaDianNao:一种无主存储器的多核加速器
  • 西门子S7-200 PLC串口PPI转以太网通讯的模块链接方式
  • 解决:Cannot find a valid baseurl for repo: base/7/x86_64
  • 一个简单的Windows TCP服务器实现
  • Unity-Mirror网络框架-从入门到精通之MultipleMatches示例
  • Excel大数据量导入导出
  • 排列组合
  • Java 中的 128 陷阱与自动拆装箱深度解析
  • 3.3 学习UVM中的uvm_driver 类分为几步?
  • GMS认证相关问题汇总
  • Docker使用指南与Dockerfile文件详解:从入门到实战
  • ESP32S3基于espidf移植I2C SSD1306/sh1106 WouoUIPage磁贴案例
  • 【Qt之·类QTextCursor】
  • 32单片机学习记录1之GPIO