diff --git a/smart-modules/smart-system/src/main/java/com/zhentao/system/controller/DeepSeekAIController.java b/smart-modules/smart-system/src/main/java/com/zhentao/system/controller/DeepSeekAIController.java new file mode 100644 index 0000000000000000000000000000000000000000..54156449617b556ebd5f5485545b8143614deba6 --- /dev/null +++ b/smart-modules/smart-system/src/main/java/com/zhentao/system/controller/DeepSeekAIController.java @@ -0,0 +1,25 @@ +package com.zhentao.system.controller; + +import com.zhentao.common.core.web.domain.AjaxResult; +import com.zhentao.system.service.DeepSeekAIService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @ClassName: DeepSeekAIController + * @Author: 振涛教育_Q + * @Date: 2025/2/24 10:27 + */ +@RestController +@RequestMapping("/deepseek") +public class DeepSeekAIController { + @Autowired + private DeepSeekAIService deepSeekAIService; + + @GetMapping("/inputMsg") + public AjaxResult inputMsg(String msg) { + return deepSeekAIService.callWithSession(msg); + } +} diff --git a/smart-modules/smart-system/src/main/java/com/zhentao/system/service/DeepSeekAIService.java b/smart-modules/smart-system/src/main/java/com/zhentao/system/service/DeepSeekAIService.java new file mode 100644 index 0000000000000000000000000000000000000000..48680d80c02884909aab9f30107fb4eec36d6937 --- /dev/null +++ b/smart-modules/smart-system/src/main/java/com/zhentao/system/service/DeepSeekAIService.java @@ -0,0 +1,12 @@ +package com.zhentao.system.service; + +import com.zhentao.common.core.web.domain.AjaxResult; + +/** + * @ClassName: DeepSeekAIService + * @Author: 振涛教育_Q + * @Date: 2025/2/24 10:27 + */ +public interface DeepSeekAIService { + AjaxResult callWithSession(String msg); +} diff --git a/smart-modules/smart-system/src/main/java/com/zhentao/system/service/impl/DeepSeekAIServiceImpl.java b/smart-modules/smart-system/src/main/java/com/zhentao/system/service/impl/DeepSeekAIServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..9ba8eba906bc1454a8ac04892a912389797ce76d --- /dev/null +++ b/smart-modules/smart-system/src/main/java/com/zhentao/system/service/impl/DeepSeekAIServiceImpl.java @@ -0,0 +1,29 @@ +package com.zhentao.system.service.impl; + +import com.alibaba.dashscope.exception.InputRequiredException; +import com.alibaba.dashscope.exception.NoApiKeyException; +import com.zhentao.common.core.web.domain.AjaxResult; +import com.zhentao.system.service.DeepSeekAIService; +import com.zhentao.system.utils.DeepSeekAIUtils; +import org.springframework.stereotype.Service; + +/** + * @ClassName: DeepSeekAIServiceImpl + * @Author: 振涛教育_Q + * @Date: 2025/2/24 10:28 + */ +@Service +public class DeepSeekAIServiceImpl implements DeepSeekAIService { + @Override + public AjaxResult callWithSession(String msg) { + String message = ""; + try { + message = DeepSeekAIUtils.callWithSession(msg); + } catch (NoApiKeyException e) { + throw new RuntimeException(e); + } catch (InputRequiredException e) { + throw new RuntimeException(e); + } + return AjaxResult.success(message); + } +} diff --git a/smart-modules/smart-system/src/main/java/com/zhentao/system/utils/DeepSeekAIUtils.java b/smart-modules/smart-system/src/main/java/com/zhentao/system/utils/DeepSeekAIUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..7f70929c59ff2e740dac24ec9545105762e58914 --- /dev/null +++ b/smart-modules/smart-system/src/main/java/com/zhentao/system/utils/DeepSeekAIUtils.java @@ -0,0 +1,36 @@ +package com.zhentao.system.utils; + +import com.alibaba.dashscope.app.Application; +import com.alibaba.dashscope.app.ApplicationParam; +import com.alibaba.dashscope.app.ApplicationResult; +import com.alibaba.dashscope.exception.ApiException; +import com.alibaba.dashscope.exception.InputRequiredException; +import com.alibaba.dashscope.exception.NoApiKeyException; + +/** + * @ClassName: DeepSeekAIUtils + * @Author: 振涛教育_Q + * @Date: 2025/2/24 9:05 + */ +public class DeepSeekAIUtils { + public static String callWithSession(String msg) + throws ApiException, NoApiKeyException, InputRequiredException { + ApplicationParam param = ApplicationParam.builder() + // 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 + .apiKey("sk-54f979255dc94eb68de85a0342307231") + // 替换为实际的应用 ID + .appId("50638760bfc443a28b1a114143fc607a") + .prompt(msg) + .build(); + + Application application = new Application(); + ApplicationResult result = application.call(param); + + param.setSessionId(result.getOutput().getSessionId()); + + result = application.call(param); + System.out.printf("%s\n, session_id: %s\n", + result.getOutput().getText(), result.getOutput().getSessionId()); + return result.getOutput().getText(); + } +}