From db2a00f5df9990e55ebbdf16cf8c68c3d64fe09d Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期二, 28 七月 2026 16:26:43 +0800
Subject: [PATCH] Merge branch 'master' into master-任务组
---
smartor/src/main/java/com/smartor/common/DeepSeekApi.java | 178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 178 insertions(+), 0 deletions(-)
diff --git a/smartor/src/main/java/com/smartor/common/DeepSeekApi.java b/smartor/src/main/java/com/smartor/common/DeepSeekApi.java
new file mode 100644
index 0000000..aafd84f
--- /dev/null
+++ b/smartor/src/main/java/com/smartor/common/DeepSeekApi.java
@@ -0,0 +1,178 @@
+package com.smartor.common;
+
+;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+import okhttp3.*;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.IOException;
+import java.util.*;
+
+@Slf4j
+public class DeepSeekApi {
+ // 鏇挎崲涓轰綘鑷繁鐨刱ey
+ private static final String API_KEY = "sk-4e489637e105411596be6c72e2dacdbd";
+ private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
+
+ public static void main(String[] args) {
+ String questionText = "鎮ㄥソ,璇烽棶鎮ㄦ槸鎮h�呮湰浜鸿繕鏄灞烇紵\n";
+ String voiceText = "鎴戣嚜宸�";
+// List<String> options = new ArrayList<>(Arrays.asList("鏈汉","浜插睘","鍏朵粬"));
+ String optionText = "浜插睘";
+ Integer resp = matchQuestionAnswer(questionText, voiceText, optionText);
+ System.out.println("DeepSeek杩斿洖缁撴灉锛歕n" + resp);
+ }
+
+ public static String chatCompletion(String systemPrompt, String userPrompt) {
+ OkHttpClient client = new OkHttpClient();
+
+ // 1. 缁勮娑堟伅浣�
+ List<Map<String, String>> messages = new ArrayList<>();
+ // 绯荤粺瑙掕壊锛堝彲閫夛紝鐢ㄤ簬璁惧畾AI韬唤锛�
+ Map<String, String> sysMsg = new HashMap<>();
+ sysMsg.put("role", "system");
+ sysMsg.put("content", systemPrompt);
+ messages.add(sysMsg);
+ // 鐢ㄦ埛鎻愰棶
+ Map<String, String> userMsg = new HashMap<>();
+ userMsg.put("role", "user");
+ userMsg.put("content", userPrompt);
+ messages.add(userMsg);
+
+ // 2. 璇锋眰鍙傛暟
+ Map<String, Object> requestBody = new HashMap<>();
+ requestBody.put("model", "deepseek-chat");
+ requestBody.put("messages", messages);
+ requestBody.put("temperature", 0.7); // 闅忔満鎬�0~1锛岃秺浣庤秺涓ヨ皑
+ requestBody.put("max_tokens", 1024);
+
+ // 3. 鏋勫缓璇锋眰
+ String jsonBody = JSON.toJSONString(requestBody);
+ RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonBody);
+
+ Request request = new Request.Builder()
+ .url(API_URL)
+ .header("Authorization", "Bearer " + API_KEY)
+ .header("Content-Type", "application/json")
+ .post(body)
+ .build();
+
+ // 4. 鍙戦�佽姹�
+ try (Response response = client.newCall(request).execute()) {
+ if (!response.isSuccessful()) {
+ return "璇锋眰澶辫触锛岀姸鎬佺爜锛�" + response.code() + "锛岄敊璇俊鎭細" + response.body().string();
+ }
+ String resJson = response.body().string();
+ JSONObject jsonObj = JSON.parseObject(resJson);
+ // 鎻愬彇AI鍥炵瓟鍐呭
+ return jsonObj.getJSONArray("choices")
+ .getJSONObject(0)
+ .getJSONObject("message")
+ .getString("content");
+ } catch (IOException e) {
+ e.printStackTrace();
+ return "鎺ュ彛璋冪敤寮傚父锛�" + e.getMessage();
+ }
+ }
+
+ /**
+ * 鍒ゆ柇璇煶鏂囨湰鏈�鎺ヨ繎鍝釜閫夐」锛岃繑鍥為�夐」鍦ㄥ垪琛ㄤ腑鐨勪笅鏍囥��
+ *
+ * @param voiceText 璇煶璇嗗埆寰楀埌鐨勬枃鏈�
+ * @param options 鍊欓�夐�夐」鍒楄〃
+ * @return 鍛戒腑閫夐」鐨勪笅鏍囷紙浠� 0 寮�濮嬶級锛涙棤娉曞尮閰嶄换涓�閫夐」鏃惰繑鍥� {@code -1}
+ */
+ public static int matchOptionIndex(String questionText, String voiceText, List<String> options) {
+ if (StringUtils.isBlank(voiceText) || options == null || options.isEmpty()) {
+ return -1;
+ }
+ // 鍙湁涓�涓�夐」鏃舵棤闇�璋冪敤妯″瀷
+ if (options.size() == 1) {
+ return 0;
+ }
+
+ StringBuilder optionText = new StringBuilder();
+ for (int i = 0; i < options.size(); i++) {
+ optionText.append(i + 1).append(". ").append(options.get(i)).append('\n');
+ }
+
+ String systemPrompt = "\"浣犳槸涓�涓笓涓氱殑璇煶璇嗗埆鏂囨湰璇箟鍖归厤鍔╂墜銆備綘鐨勪换鍔℃槸鍒ゆ柇鐢ㄦ埛鐨勮闊虫枃鏈湪璇箟涓婃渶绗﹀悎鐨勯�夐」銆傜敤鎴蜂細缁欏嚭涓�娈佃闊宠瘑鍒枃鏈拰鑻ュ共涓甫缂栧彿鐨勯�夐」锛�"
+ + "璇峰垽鏂繖娈垫枃鏈湪璇箟涓婃渶鎺ヨ繎鍝竴涓�夐」銆傚彧鍏佽浠庣粰瀹氶�夐」涓�夋嫨锛�"
+ + "涓嶈鍋氫换浣曡В閲娿�傜洿鎺ヨ緭鍑烘渶鍖归厤閫夐」鐨勭紪鍙锋暟瀛楋紱鑻ユ病鏈変换浣曢�夐」涓庢枃鏈浉鍏筹紝鍒欒緭鍑� 0銆�";
+ String userPrompt = "璇锋牴鎹互涓嬩俊鎭繘琛岃涔夊尮閰嶅垽鏂細\n" +
+ "- 闂鏂囨湰锛�" + questionText + "\n\n"
+ + "- 璇煶璇嗗埆鏂囨湰锛�" + voiceText + "\n\n"
+ + "- 閫夐」锛歕n" + optionText
+ + "\n璇峰彧杈撳嚭涓�涓暟瀛楋紙鏈�鍖归厤閫夐」鐨勭紪鍙凤紝娌℃湁鍖归厤鍒欒緭鍑� 0锛夈��";
+
+ String content = chatCompletion(systemPrompt, userPrompt);
+ if (StringUtils.isBlank(content)) {
+ return -1;
+ }
+
+ Integer number = extractFirstNumber(content);
+ if (number == null || number <= 0 || number > options.size()) {
+ log.warn("DeepSeek 閫夐」鍖归厤鏈懡涓紝voiceText={}, options={}, modelReturn={}", voiceText, options, content);
+ return -1;
+ }
+ return number - 1;
+ }
+
+ /**
+ * 鍒ゆ柇璇煶鏂囨湰鏈�鎺ヨ繎鍝釜閫夐」锛岃繑鍥為�夐」鍦ㄥ垪琛ㄤ腑鐨勪笅鏍囥��
+ *
+ * @param questionText 璇煶璇嗗埆寰楀埌鐨勬枃鏈�
+ * @param answerText 绛旀鏂囨湰
+ * @param optionText 閫夐」鏂囨湰
+ * @return 鍛戒腑閫夐」鐨勪笅鏍囷紙浠� 0 寮�濮嬶級锛涙棤娉曞尮閰嶄换涓�閫夐」鏃惰繑鍥� {@code -1}
+ */
+ public static int matchQuestionAnswer(String questionText, String answerText, String optionText) {
+ if (StringUtils.isBlank(questionText) || StringUtils.isBlank(answerText) || StringUtils.isBlank(optionText)) {
+ return -1;
+ }
+
+ String systemPrompt = "\"浣犳槸涓�涓笓涓氱殑璇煶璇嗗埆鏂囨湰璇箟鍖归厤鍔╂墜銆備綘鐨勪换鍔℃槸鍒ゆ柇鐢ㄦ埛鐨勮闊虫枃鏈湪璇箟涓婃渶绗﹀悎鐨勯�夐」銆傜敤鎴蜂細缁欏嚭涓�娈佃闊宠瘑鍒枃鏈拰閫夐」鏂囨湰锛�"
+ + "璇峰垽鏂繖娈垫枃鏈湪璇箟涓婃槸鍚﹀尮閰嶉�夐」, 鍖归厤鍒欒緭鍑� 1锛屾病鏈夊尮閰嶅垯杈撳嚭 0, 鍙厑璁歌緭鍑�1鎴栬��0锛�"
+ + "涓嶈鍋氫换浣曡В閲娿�傜洿鎺ヨ緭鍑烘渶鍖归厤閫夐」鐨勭紪鍙锋暟瀛楋紱鑻ユ病鏈変换浣曢�夐」涓庢枃鏈浉鍏筹紝鍒欒緭鍑� 0銆�";
+ String userPrompt = "璇锋牴鎹互涓嬩俊鎭繘琛岃涔夊尮閰嶅垽鏂細\n" +
+ "- 闂鏂囨湰锛�" + questionText + "\n\n"
+ + "- 璇█璇嗗埆鏂囨湰锛�" + answerText + "\n\n"
+ + "- 閫夐」鏂囨湰锛�" + optionText + "\n\n"
+ + "\n璇峰彧杈撳嚭涓�涓暟瀛楋紙鍖归厤鍒欒緭鍑� 1锛屾病鏈夊尮閰嶅垯杈撳嚭 0,鍙厑璁歌緭鍑�1鎴栬��0锛夈��";
+
+ String content = chatCompletion(systemPrompt, userPrompt);
+ if (StringUtils.isBlank(content)) {
+ return -1;
+ }
+ return extractFirstNumber(content);
+ }
+
+ /**
+ * 浠庢ā鍨嬪洖澶嶄腑鎻愬彇绗竴涓暣鏁般�傛ā鍨嬪伓灏斾細鍥炲 鈥滈�夐」2鈥� 鈥�2銆傗�� 涔嬬被锛屽仛涓�娆″厹搴曡В鏋愩��
+ */
+ private static Integer extractFirstNumber(String text) {
+ List<Character> digits = new ArrayList<>();
+ for (int i = 0; i < text.length(); i++) {
+ char c = text.charAt(i);
+ if (c >= '0' && c <= '9') {
+ digits.add(c);
+ } else if (!digits.isEmpty()) {
+ break;
+ }
+ }
+ if (digits.isEmpty()) {
+ return null;
+ }
+ StringBuilder sb = new StringBuilder();
+ for (char c : digits) {
+ sb.append(c);
+ }
+ try {
+ return Integer.parseInt(sb.toString());
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.3