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

中交建设招标有限公司网站济南标场馆建设有新进展

中交建设招标有限公司网站,济南标场馆建设有新进展,搜索引擎优化大致包含哪些内容或环节,职业培训机构管理系统引言 DeepSeek最近异常火爆,作为深度求索公司提供的大模型,提供了强大的自然语言处理和其他AI功能,通过调用其接口,可以在Spring Boot项目中实现智能对话、内容生成等多种功能。本文将详细介绍如何在Spring Boot中调用DeepSeek接…

引言

DeepSeek最近异常火爆,作为深度求索公司提供的大模型,提供了强大的自然语言处理和其他AI功能,通过调用其接口,可以在Spring Boot项目中实现智能对话、内容生成等多种功能。本文将详细介绍如何在Spring Boot中调用DeepSeek接口,并给出详细的介入步骤和代码示例。

1、申请DeepSeek API Key

在调用DeepSeek接口之前,首先需要申请一个API Key。这是访问DeepSeek API的凭证,用于验证请求者的身份和权限。

1) 访问DeepSeek官网

打开浏览器,输入DeepSeek的官网地址(如https://platform.deepseek.com/usage),进入DeepSeek的开放平台页面。

2) 创建API Key

在开放平台页面中,找到API keys相关选项,点击进入API Key管理页面。点击“创建API Key”按钮,根据提示填写相关信息,如应用名称、描述等。创建完成后,系统会生成一个唯一的API Key,务必妥善保存,因为关闭页面后将无法再次查看。

2、创建Spring Boot项目

接下来,我们需要创建一个Spring Boot项目来调用DeepSeek接口。可以使用Spring Initializr(https://start.spring.io/)来快速生成项目结构。

1) 访问Spring Initializr

打开浏览器,输入Spring Initializr的地址,进入项目生成页面。

2)配置项目参数

  • Project:选择项目构建工具(如Maven或Gradle),设置项目语言(Java)、Spring Boot版本等。
  • Dependencies:添加必要的依赖项。由于我们需要调用DeepSeek的HTTP接口,因此需要添加spring-boot-starter-web依赖。此外,还可以根据需要添加其他依赖项,如日志框架(spring-boot-starter-logging)、数据库连接池(spring-boot-starter-data-jpa)等。

3) 生成项目

配置完成后,点击“Generate”按钮生成项目结构。将生成的项目文件下载到本地,并导入到IDE(如IntelliJ IDEA或Eclipse)中进行开发。

3、 配置application.yml

在Spring Boot项目中,通常使用`application.yml``文件来配置应用的相关参数。为了调用DeepSeek接口,我们需要在配置文件中添加DeepSeek的API Key和请求URL。
添加以下配置:

deepseek:api:key: sk-63************5f  # 替换为你的DeepSeek API Keyurl: https://api.deepseek.com/chat/completions  # DeepSeek API请求URL

4、编写配置类

为了更方便地管理DeepSeek API的配置信息,我们可以编写一个配置类来读取application.yml中的配置。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Getter;@Configuration
@Getter
public class DeepSeekConfig {@Value("${deepseek.api.key}")private String apiKey;@Value("${deepseek.api.url}")private String apiUrl;
}

5 编写请求/响应模型

在调用DeepSeek接口时,我们需要定义请求和响应的数据结构。根据DeepSeek API的文档,请求体通常包含模型名称、消息列表等字段,而响应体则包含生成的回复选项等字段。

import lombok.Data;
import java.util.List;@Data
public class DeepSeekRequest {private String model;private List<Message> messages;private boolean stream;@Datapublic static class Message {private String role;private String content;}
}@Data
public class DeepSeekResponse {private List<Choice> choices;@Datapublic static class Choice {private Delta delta;@Datapublic static class Delta {private String content;}}
}

6 编写服务类

服务类用于封装向DeepSeek发出查询的过程。我们将使用RestTemplate来发送HTTP请求,并处理响应结果。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.util.UriComponentsBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;@Service
public class DeepSeekService {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate DeepSeekConfig deepSeekConfig;private final ObjectMapper objectMapper = new ObjectMapper();public String askDeepSeek(String question) throws JsonProcessingException {DeepSeekRequest request = new DeepSeekRequest();request.setModel("deepseek-chat");request.setStream(false);List<DeepSeekRequest.Message> messages = List.of(new DeepSeekRequest.Message("user", question));request.setMessages(messages);HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);headers.setAuthorization("Bearer " + deepSeekConfig.getApiKey());HttpEntity<String> entity = new HttpEntity<>(objectMapper.writeValueAsString(request), headers);UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(deepSeekConfig.getApiUrl());ResponseEntity<String> response = restTemplate.postForEntity(builder.toUriString(), entity, String.class);if (response.getStatusCode().is2xxSuccessful()) {DeepSeekResponse deepSeekResponse = objectMapper.readValue(response.getBody(), DeepSeekResponse.class);if (deepSeekResponse != null && deepSeekResponse.getChoices() != null && !deepSeekResponse.getChoices().isEmpty()) {return deepSeekResponse.getChoices().get(0).getDelta().getContent();}}return "No valid response from DeepSeek";}
}

7 编写控制器类

控制器类用于处理HTTP请求,并调用服务类的方法来获取DeepSeek的响应结果。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;@RestController
public class DeepSeekController {@Autowiredprivate DeepSeekService deepSeekService;@GetMapping("/ask")public ResponseEntity<String> askDeepSeek(@RequestParam String question) {try {String response = deepSeekService.askDeepSeek(question);return ResponseEntity.ok(response);} catch (Exception e) {return ResponseEntity.status(500).body("Error occurred while communicating with DeepSeek: " + e.getMessage());}}
}

8 测试与验证

完成以上步骤后,我们可以启动Spring Boot应用,并通过浏览器或Postman等工具来测试DeepSeek接口是否调用成功。

1)启动Spring Boot应用

在IDE中运行@SpringBootApplication主类,观察控制台输出:

2024-02-20T14:30:00.000+08:00 INFO 8080 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http)

2) 构造测试请求

使用Postman发送GET请求:

GET http://localhost:8080/ask?question=如何学习Spring Boot框架?

3) 验证正常响应

应收到JSON格式的AI响应:

{"content": "学习Spring Boot可以从以下几个步骤入手...(具体学习建议)"
}

4) 异常场景测试

  • 例如:无效API Key测试
    deepseek.api.key=sk-invalid_key
    
    应收到401 Unauthorized错误:
    {"code": "DEEPSEEK_API_ERROR","message": "Invalid API Key"
    }
    

总结

本文介绍了如何在Spring Boot项目中调用DeepSeek接口实现智能对话功能。首先,需要申请DeepSeek API Key并创建Spring Boot项目。接着,在application.yml中配置API Key和请求URL,并编写配置类来管理这些配置。然后,定义请求/响应模型,编写服务类使用RestTemplate发送HTTP请求并处理响应。最后,编写控制器类处理HTTP请求,并测试验证接口调用是否成功。通过这些步骤,可以在Spring Boot项目中轻松集成DeepSeek大模型,实现智能对话和内容生成等功能。


文章转载自:

http://5IFYggCO.skmzm.cn
http://WJbEQMSy.skmzm.cn
http://oxpsCeOK.skmzm.cn
http://VQhPp1Qs.skmzm.cn
http://MeMLLZo1.skmzm.cn
http://T2Zmjuew.skmzm.cn
http://Zb5n7xW5.skmzm.cn
http://j13O0s1b.skmzm.cn
http://OlF3ODOg.skmzm.cn
http://jMMkHyPq.skmzm.cn
http://PbIiqebf.skmzm.cn
http://x2pUpzva.skmzm.cn
http://FQo78DFE.skmzm.cn
http://bvtQcDEa.skmzm.cn
http://8UQaTE3u.skmzm.cn
http://ZrSo8H5C.skmzm.cn
http://mDwumiTW.skmzm.cn
http://Xt2dZa61.skmzm.cn
http://qhVxNk11.skmzm.cn
http://pl2H5anL.skmzm.cn
http://FfL5vvcw.skmzm.cn
http://qIZLvvDQ.skmzm.cn
http://Wj3H2nU4.skmzm.cn
http://pBrKzXcU.skmzm.cn
http://e6HFEHbC.skmzm.cn
http://ninMwWT8.skmzm.cn
http://h1ZXCbEi.skmzm.cn
http://9Q7egWlH.skmzm.cn
http://oDHpuisL.skmzm.cn
http://vX8VvVdL.skmzm.cn
http://www.dtcms.com/wzjs/731798.html

相关文章:

  • 做a动漫视频在线观看网站上海网站公司
  • 网页设计教程孟宪宁课后题答案seo网站优化专员
  • 现在学网站开发网站建设优化石家庄
  • 寿光专业做网站安卓上搭建wordpress
  • 电脑网站建设规划制作图片视频的软件有哪些
  • 仪器网站模版谁做违法网站
  • 网站关键字代码sns社交网站.net源码
  • php企业网站模板免费下载当今做那些网站能致富
  • 打电话叫人做网站wordpress 投稿 图片
  • dw怎么做购物网站网站推广一般在哪个网做
  • 电商网站seo优化目标分解用flash做的经典网站
  • iis2008如何做网站建设专业网站运营团队
  • 数据库网站建设多少钱wordpress模板转为emlog
  • 一个网站有几个域名路北网站制作
  • 你知道的在线视频观看的vue做网站如何优化seo
  • 企业网站建设的一般要素包括6建网站要大约多少钱
  • 用html5做的音乐网站wordpress背景图像
  • 哪个网站上做ppt比较好看网站建设更新
  • 网站中的ppt链接怎么做广元城乡建设部网站首页
  • 网站自适应 如何做杭州互联网大厂
  • 做网站英文编辑有前途吗余姚做网站
  • 查备案网站两学一做 答题 网站
  • 网站托管平台东莞营销型高端网站建设
  • 运营一个网站的成本友情链接搜读
  • 网站数据库建设access做电视的视频网站
  • 遵义网站优化青岛网站建设咨询
  • 网上如何做网站洪梅镇仿做网站
  • 济南企业建站改行做网站
  • 企业官网门户网站管理系统网站菜单导航怎么做
  • 做商务网站公司代码实现wordpress百度地图