当前位置: 首页> 游戏> 网游 > 做网站优化排名_教育机构网站建设_技术短期培训班_手机网页链接制作

做网站优化排名_教育机构网站建设_技术短期培训班_手机网页链接制作

时间:2025/7/11 1:33:45来源:https://blog.csdn.net/weixin_46085718/article/details/146198640 浏览次数:1次
做网站优化排名_教育机构网站建设_技术短期培训班_手机网页链接制作

1、效果截图:

 2、代码部分:

application.properties

server.port=8080deepseek.api.token=sk-d34e929e887b4881813395241df2f745
deepseek.api.url=https://api.deepseek.com/chat/completions

 controller部分 请求参数可以缩短,写成实体类形式

package com.example.springbootai.demos.web;import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;import java.util.*;/*** @author caojun*/
@RestController
public class TestController {public static final RestTemplate restTemplate = new RestTemplate();@Value("${deepseek.api.token}")private String apiToken;@Value("${deepseek.api.url}")private String apiUrl;@PostMapping("/deepSeek")public String callDeepSeek(@RequestBody String question) {// 创建消息列表List<Map<String, Object>> messages = new ArrayList<>();// 添加系统消息Map<String, Object> systemMessage = new HashMap<>();systemMessage.put("role", "system");systemMessage.put("content", "You are a helpful assistant");messages.add(systemMessage);// 添加用户消息Map<String, Object> userMessage = new HashMap<>();userMessage.put("role", "user");userMessage.put("content", question);messages.add(userMessage);// 创建请求 MapMap<String, Object> requestMap = new HashMap<>();requestMap.put("model", "deepseek-chat");requestMap.put("messages", messages);requestMap.put("frequency_penalty", 0);requestMap.put("max_tokens", 2048);requestMap.put("presence_penalty", 0);requestMap.put("response_format", Collections.singletonMap("type", "text"));requestMap.put("stop", null);requestMap.put("stream", false);requestMap.put("stream_options", null);requestMap.put("temperature", 1);requestMap.put("top_p", 1);requestMap.put("tools", null);requestMap.put("tool_choice", "none");requestMap.put("logprobs", false);requestMap.put("top_logprobs", null);// 将 requestMap 转换为 JSON 字符串ObjectMapper objectMapper = new ObjectMapper();String requestBody = null;try {requestBody = objectMapper.writeValueAsString(requestMap);} catch (JsonProcessingException e) {throw new RuntimeException(e);}HttpHeaders headers = new HttpHeaders();headers.set("Authorization", "Bearer " + apiToken);headers.set("Content-Type", "application/json");HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class);// 解析响应 JSONJSONObject jsonResponse = JSONObject.parseObject(response.getBody());String generatedText = jsonResponse.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");return generatedText;}@GetMapping("/getBalance")public String getBalance() {HttpHeaders headers = new HttpHeaders();headers.set("Authorization", "Bearer " + apiToken);headers.set("Content-Type", "application/json");HttpEntity<String> entity = new HttpEntity<>(headers);// 发送 GET 请求ResponseEntity<String> response = restTemplate.exchange("https://api.deepseek.com/user/balance",HttpMethod.GET,entity,String.class);System.out.println(response.getBody());return response.getBody();}}

3、依赖

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.83</version></dependency>

controller可以简化这样的

@PostMapping("/deepSeek")public String callDeepSeek(@RequestBody String question) {// 创建消息列表List<Map<String, Object>> messages = new ArrayList<>();// 添加系统消息Map<String, Object> systemMessage = new HashMap<>();systemMessage.put("role", "system");systemMessage.put("content", "You are a helpful assistant");messages.add(systemMessage);// 添加用户消息Map<String, Object> userMessage = new HashMap<>();userMessage.put("role", "user");userMessage.put("content", question);messages.add(userMessage);// 创建请求 MapMap<String, Object> requestMap = new HashMap<>();requestMap.put("model", "deepseek-chat");requestMap.put("messages", messages);requestMap.put("stream", false);// 将 requestMap 转换为 JSON 字符串ObjectMapper objectMapper = new ObjectMapper();String requestBody = null;try {requestBody = objectMapper.writeValueAsString(requestMap);} catch (JsonProcessingException e) {throw new RuntimeException(e);}HttpHeaders headers = new HttpHeaders();headers.set("Authorization", "Bearer " + apiToken);headers.set("Content-Type", "application/json");HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class);// 解析响应 JSONJSONObject jsonResponse = JSONObject.parseObject(response.getBody());String generatedText = jsonResponse.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");return generatedText;}

关键字:做网站优化排名_教育机构网站建设_技术短期培训班_手机网页链接制作

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: