springboot+deepseek实现AI接口调用

张开发
2026/4/9 5:42:19 15 分钟阅读

分享文章

springboot+deepseek实现AI接口调用
deepseek注册流程就不复述了需要的小伙伴可以留言单独指导。需要调用deepseek大模型接口的来看看了直接上代码DsControllerpackage com.example.demo.controller; import com.example.demo.service.DsService; 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 DsController { Autowired private DsService deepSeekService; GetMapping(/ask) public ResponseEntityString 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()); } } }DsServicepackage com.example.demo.service; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.example.demo.config.DsConfig; import com.example.demo.util.DeepSeekRequest; import com.example.demo.util.DeepSeekResponse; 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; import javax.annotation.Resource; import java.util.List; Service public class DsService { Resource private RestTemplate restTemplate; Autowired private DsConfig 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); ListDeepSeekRequest.Message messages List.of( new DeepSeekRequest.Message(user, question) ); request.setMessages(messages); HttpHeaders headers new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setBearerAuth(deepSeekConfig.getApiKey()); //headers.setLocation(Bearer deepSeekConfig.getApiKey()); HttpEntityString entity new HttpEntity(objectMapper.writeValueAsString(request), headers); UriComponentsBuilder builder UriComponentsBuilder.fromHttpUrl(deepSeekConfig.getApiUrl()); ResponseEntityString 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; } public static void main(String[] args) { String url https://api.deepseek.com/chat/completions; String key sk-98339e*****************; HttpRequest get HttpUtil.createPost(url); get.contentType(application/json).header(Content-Type, application/json).header(Authorization,Bearer key); JSONObject json new JSONObject(); json.put(model,deepseek-chat); JSONArray messages new JSONArray(); JSONObject message new JSONObject(); message.put(role,system); message.put(content,You are a helpful assistant.); JSONObject message2 new JSONObject(); message2.put(role,user); message2.put(content,Hello!); messages.add(message); messages.add(message2); json.put(messages,messages); json.put(stream,false); System.out.println(json.toJSONString()); get.body(json.toJSONString(),application/json); //System.out.println(get); HttpResponse execute get.execute(); // 打印响应内容 if (execute.isOk()) { System.out.println(Response: execute.body()); } else { System.err.println(execute); System.out.println(Request failed, status code: execute.getStatus()); } } }DsConfigpackage com.example.demo.config; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; /** * Project:demo 业务数据中心 * Date2025/4/3 15:01 * Authorlb * Description */ Configuration Data public class DsConfig { Value(${ds.api.key}) private String apiKey; Value(${ds.api.url}) private String apiUrl; Bean public RestTemplate restTemplate() { return new RestTemplate(); } }yml配置ds: api: key: 98339e7******* url: https://api.deepseek.com/chat/completions

更多文章