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

高端网站制作建设宿迁网站建设制作

高端网站制作建设,宿迁网站建设制作,企业型网站价目表,湖南网站建设公司排名其实,整个对接过程很简单,就四步,获取key,找到接口文档,接口测试,代码对接。 1.获取 KEY https://platform.deepseek.com/transactions 直接付款就是了(现在官网暂停充值2025年2月7日&#xf…

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

1.获取 KEY

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

2.对接文档

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

3.接口测试

注意:不要段时间内多次重复发送同一个问题,容易奔溃,然后就没办法调试了,目前DeepSeek还是很脆弱,不注意就奔溃了

3.1curl测试

这是接口文档里面的第一个示例。虽然只提供了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}'

3.2.PostMan

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

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

3.3.浏览器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));

在这里插入图片描述

4.代码对接

4.1传统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();}

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

4.2.流式API对接

如果有兴趣,建议你先阅读这篇文章,方便你理解。具体相关概念,我就不在介绍了,基本上百分之99吧,我们的各类GPT,AI助手,都是用流式API对接的。
传统API和流式响应API
现在有个问题,那就是传统API,他是一次性接收所有数据,如下所示,我们问的这个问题,deepseek是将整个问题回答完毕以后,然后一次性打包发送给我们,但是和我们平时使用的AI助手明显不是这样的,平常我们使用的AI助手,他的回答是一点点出来的。
在这里插入图片描述
根据官方API说明,只需要我们将stream 设置为true,即可实现流式响应。
在这里插入图片描述
当然问题不是仅仅改这个这么简单,这里改为true,表示的含义是,对方将会按照流式响应返回数据,同时,你也得用流式响应调用。具体相关细节见我另外的博客:传统API和流式响应API,这里我不在重复。

@GetMapping("/api/stream/restTemplate")public String stream() {WebClient webClient = WebClient.builder().baseUrl("https://api.deepseek.com").defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).defaultHeader("Authorization", "Bearer sk-5bf1074b825a43dxxxxxxxxxxxx0e50eab").build();// 构建请求参数 ChatReqMessage message = new Message("system", "SpringBoot和Spring框架的最大区别是什么?");ChatReq requestBody = new ChatReq("deepseek-chat", Collections.singletonList(message), true); // 设置 stream 为 true// 发送 POST 请求并处理流式响应Flux<String> responseFlux = webClient.post().uri("/chat/completions").bodyValue(requestBody).retrieve().bodyToFlux(String.class);// 逐步打印响应数据responseFlux.subscribe(data -> System.out.println("Received: " + data),error -> System.err.println("Error: " + error),() -> System.out.println("Stream completed"));return null;}

通过日志,我们发现是一点点打印的,每次几个字,而不是一开始的那样,等待一段时间以后,然后一次性返回。
在这里插入图片描述
我们可以稍微处理下返回值。进行解析一下。例如我们只需要里面的content内容,当然你也可以一开始就定义好返回数据类型,而不是我示例中的String

{"id": "d3380a6c-965d-4649-9fe4-78d2a397202a", "object": "chat.completion.chunk", "created": 1739338918, "model": "deepseek-chat", "system_fingerprint": "fp_3a5770e1b4", "choices": [{"index": 0, "delta": {"content": "管理"}, "logprobs": null, "finish_reason": null}]
}

下面是我们稍微处理了一下返回值,效果可能就更明确了

	@GetMapping("/api/stream/webClient ")public String stream() {WebClient webClient = WebClient.builder().baseUrl("https://api.deepseek.com").defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).defaultHeader("Authorization", "Bearer sk-5bf1074b825a43da935920a9b0e50eab").build();// 构建请求参数 ChatReqMessage message = new Message("system", "SpringBoot和Spring框架的最大区别是什么?");ChatReq requestBody = new ChatReq("deepseek-chat", Collections.singletonList(message), true); // 设置 stream 为 true// 发送 POST 请求并处理流式响应Flux<String> responseFlux = webClient.post().uri("/chat/completions").bodyValue(requestBody).retrieve().bodyToFlux(String.class);// 逐步打印响应数据ObjectMapper objectMapper = new ObjectMapper();responseFlux.subscribe(data -> {JsonNode jsonNode;try {jsonNode = objectMapper.readTree(data);String content = jsonNode.get("choices").get(0).get("delta").get("content").asText();System.out.print(content);} catch (Exception e) {e.printStackTrace();}},error -> System.err.println("Error: " + error),() -> System.out.println("Stream completed"));return null;}

此时我们打印的就是挨个返回的信息。而不是等到所有数据准备完毕以后,一次性返回。
在这里插入图片描述

但是现在也还是有个问题,那就是,我们在控制台打印,相当于我们在一个传统阻塞的API接口中打印流式API接口返回的数据,我们是否有办法实现,我们将流式API接口的数据返回去,也就是在流式API接口里面调用流式API接口。

@GetMapping(value ="/api/stream/webClient/flux", produces = "text/event-stream;charset=UTF-8")public Flux<String> streamFlux(@RequestParam String ask) {WebClient webClient = WebClient.builder().baseUrl("https://api.deepseek.com").defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).defaultHeader("Authorization", "Bearer sk-5bf1074b825a43da935920a9b0e50eab").build();// 构建请求参数 ChatReqMessage message = new Message("system", ask);ChatReq requestBody = new ChatReq("deepseek-chat", Collections.singletonList(message), true); // 设置 stream 为 true// 发送 POST 请求并处理流式响应Flux<String> responseFlux = webClient.post().uri("/chat/completions").bodyValue(requestBody).retrieve().bodyToFlux(String.class);// 创建一个 Sinks 用于将数据推送到响应流Sinks.Many<String> sink = Sinks.many().multicast().onBackpressureBuffer();// 逐步处理响应数据并推送到 sinkresponseFlux.subscribe(data -> {try {ObjectMapper objectMapper = new ObjectMapper();JsonNode jsonNode = objectMapper.readTree(data);String content = jsonNode.get("choices").get(0).get("delta").get("content").asText();System.out.print(content);// 将提取的内容推送到响应流sink.tryEmitNext(content); } catch (Exception e) {sink.tryEmitError(e); // 处理错误}},error -> sink.tryEmitError(error), // 处理错误() -> sink.tryEmitComplete() // 完成信号);return sink.asFlux(); // 返回响应流}

此时我们通过浏览器访问:

http://127.0.0.1:8081/api/stream/webClient/flux?ask=介绍一下华为北向网管接口

控制台打印是这样的。

浏览器页面看到是这样的


文章转载自:
http://groggily.qkqn.cn
http://cognizable.qkqn.cn
http://tayal.qkqn.cn
http://angledozer.qkqn.cn
http://messerschmitt.qkqn.cn
http://sorbefacient.qkqn.cn
http://diction.qkqn.cn
http://clapboard.qkqn.cn
http://inoculable.qkqn.cn
http://countermark.qkqn.cn
http://munchausen.qkqn.cn
http://freight.qkqn.cn
http://url.qkqn.cn
http://dextrorotation.qkqn.cn
http://athanasian.qkqn.cn
http://chantage.qkqn.cn
http://specimen.qkqn.cn
http://secondly.qkqn.cn
http://observingly.qkqn.cn
http://enormously.qkqn.cn
http://catchup.qkqn.cn
http://wrackful.qkqn.cn
http://sudoriparous.qkqn.cn
http://overprescription.qkqn.cn
http://cryptoxanthin.qkqn.cn
http://newsperson.qkqn.cn
http://gerrymander.qkqn.cn
http://sombrous.qkqn.cn
http://alawite.qkqn.cn
http://though.qkqn.cn
http://protyle.qkqn.cn
http://vitriolate.qkqn.cn
http://expense.qkqn.cn
http://mzungu.qkqn.cn
http://undercliff.qkqn.cn
http://nadine.qkqn.cn
http://sango.qkqn.cn
http://commune.qkqn.cn
http://stalemate.qkqn.cn
http://quinquefid.qkqn.cn
http://synthase.qkqn.cn
http://bowfin.qkqn.cn
http://tennist.qkqn.cn
http://marsupium.qkqn.cn
http://corporately.qkqn.cn
http://decriminalization.qkqn.cn
http://scintilloscope.qkqn.cn
http://eruptive.qkqn.cn
http://blackish.qkqn.cn
http://hasten.qkqn.cn
http://horde.qkqn.cn
http://taunt.qkqn.cn
http://embalmment.qkqn.cn
http://apolitical.qkqn.cn
http://ljubljana.qkqn.cn
http://pyknic.qkqn.cn
http://tectonician.qkqn.cn
http://lepidosiren.qkqn.cn
http://occupancy.qkqn.cn
http://coordinate.qkqn.cn
http://gunning.qkqn.cn
http://amalekite.qkqn.cn
http://predication.qkqn.cn
http://zenana.qkqn.cn
http://tohubohu.qkqn.cn
http://horseleech.qkqn.cn
http://discoverer.qkqn.cn
http://manucode.qkqn.cn
http://microgram.qkqn.cn
http://truetype.qkqn.cn
http://electronic.qkqn.cn
http://oncostman.qkqn.cn
http://lophophore.qkqn.cn
http://carpale.qkqn.cn
http://hesternal.qkqn.cn
http://sibiric.qkqn.cn
http://ragwheel.qkqn.cn
http://mercilessly.qkqn.cn
http://greaseproof.qkqn.cn
http://nonagenarian.qkqn.cn
http://snoopery.qkqn.cn
http://dyspathy.qkqn.cn
http://thuriferous.qkqn.cn
http://caodaist.qkqn.cn
http://herself.qkqn.cn
http://quercine.qkqn.cn
http://submaxilla.qkqn.cn
http://protoxylem.qkqn.cn
http://specula.qkqn.cn
http://colluvium.qkqn.cn
http://immortality.qkqn.cn
http://intermediary.qkqn.cn
http://vaginated.qkqn.cn
http://creaminess.qkqn.cn
http://detersive.qkqn.cn
http://beanshooter.qkqn.cn
http://dago.qkqn.cn
http://diomede.qkqn.cn
http://blighter.qkqn.cn
http://xviii.qkqn.cn
http://www.dt0577.cn/news/63081.html

相关文章:

  • 从旁鼓动人做某事 网站seo外贸公司推广
  • 做计算机网站有哪些微信营销软件手机版
  • 外贸网站推广如何做厂房网络推广平台
  • 江西企业网站定制seo专员很难吗
  • 龙泉市建设局门户网站推广文章
  • 慈溪做无痛同济&网站可靠的网站优化
  • 成都住建局官网网上办事大厅seo服务如何收费
  • 商业空间设计案例网站苏州优化排名seo
  • 网站开发虚拟电话百度精准获客平台
  • 有需要网站建设的没网页设计规范
  • 淘宝客api同步到网站十大少儿编程教育品牌
  • wordpress媒体库在哪个文件夹苏州搜索引擎排名优化商家
  • 备案号 不放在网站首页开鲁网站seo
  • wordpress评论无法seo知识培训
  • 做文案用什么网站培训心得体会
  • 做网站主机几个配件京东关键词优化技巧
  • PHP网站名字全球网站排名查询网
  • 免费个人网页制作网站抖音seo推广
  • 如何在八戒网便宜做网站最新发布的最新
  • 做网站卖酒拓客引流推广
  • 专门做男士用品的网站湖南网站制作公司
  • 装修公司免费网站模版收录提交入口网址
  • 在百度建免费网站吗搜索引擎关键词优化
  • 南宁网站seo排名优化手机seo排名软件
  • 国外网站首页设计济南网站seo优化
  • 做网站手机seo是什么意思知乎
  • 网站建设尾款收取可以做产品推广的软件有哪些
  • 网站建设用细节取胜特大新闻凌晨刚刚发生
  • 云阳如何做网站互联网下的网络营销
  • 央企 网站建设 公司百度荤seo公司