ProbeTargetAiModels.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import java.io.*;
  2. import java.net.HttpURLConnection;
  3. import java.net.URL;
  4. import java.nio.charset.StandardCharsets;
  5. import java.sql.*;
  6. /** Probe admin_ai_model by provider / identifier patterns (Java 8). */
  7. public class ProbeTargetAiModels {
  8. private static final String JDBC = "jdbc:mysql://cq-cdb-8fjmemkb.sql.tencentcdb.com:27220/ylrz_saas"
  9. + "?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8";
  10. private static final String PROBE_MSG = "\u4f60\u597d\uff0c\u8bf7\u7528\u4e00\u53e5\u8bdd\u56de\u590dOK";
  11. public static void main(String[] a) throws Exception {
  12. Class.forName("com.mysql.cj.jdbc.Driver");
  13. int ok = 0;
  14. int fail = 0;
  15. String sql = "SELECT id, model_name, provider_code, model_identifier, api_endpoint, api_key, status "
  16. + "FROM admin_ai_model WHERE status=1 AND ("
  17. + "model_name LIKE '%2.0-lite%' OR model_identifier LIKE '%2-0-lite%' OR model_identifier LIKE '%2.0-lite%' "
  18. + "OR model_name LIKE '%2.1-pro%' OR model_identifier LIKE '%2-1-pro%' OR model_identifier LIKE '%2.1-pro%' "
  19. + "OR provider_code='deepseek' OR model_name LIKE 'DeepSeek%' OR model_identifier LIKE 'deepseek%'"
  20. + ") ORDER BY id";
  21. try (Connection c = DriverManager.getConnection(JDBC, "root", "Ylrz_1q2w3e4r5t6y");
  22. Statement st = c.createStatement();
  23. ResultSet rs = st.executeQuery(sql)) {
  24. while (rs.next()) {
  25. long id = rs.getLong("id");
  26. String modelName = rs.getString("model_name");
  27. String provider = rs.getString("provider_code");
  28. String modelId = rs.getString("model_identifier");
  29. String endpoint = rs.getString("api_endpoint");
  30. String key = rs.getString("api_key");
  31. System.out.println("\n=== " + modelName + " (id=" + id + ") ===");
  32. System.out.println("provider=" + provider + " model=" + modelId);
  33. System.out.println("endpoint=" + endpoint);
  34. System.out.println("keyLen=" + (key == null ? 0 : key.length())
  35. + (key != null && key.contains("*") ? " [MASKED_IN_DB]" : ""));
  36. if (key == null || key.isEmpty() || key.contains("*")) {
  37. System.out.println("[FAIL] api_key empty or masked");
  38. fail++;
  39. continue;
  40. }
  41. String ep = normalizeChatEndpoint(endpoint, provider);
  42. if (probeChat(ep, key, modelId)) {
  43. ok++;
  44. System.out.println("[PASS] HTTP chat probe OK");
  45. } else {
  46. fail++;
  47. System.out.println("[FAIL] HTTP chat probe failed");
  48. }
  49. }
  50. }
  51. System.out.println("\n=== SUMMARY: " + ok + " passed, " + fail + " failed ===");
  52. if (ok == 0) {
  53. System.out.println("No matching enabled models found in DB query");
  54. System.exit(2);
  55. }
  56. if (fail > 0) System.exit(1);
  57. }
  58. private static String normalizeChatEndpoint(String endpoint, String provider) {
  59. if (endpoint == null || endpoint.isEmpty()) {
  60. if ("doubao".equalsIgnoreCase(provider)) {
  61. return "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
  62. }
  63. return endpoint;
  64. }
  65. if (endpoint.contains("/responses")) {
  66. return endpoint.replace("/responses", "/chat/completions");
  67. }
  68. return endpoint;
  69. }
  70. private static boolean probeChat(String endpoint, String key, String modelId) throws Exception {
  71. if (endpoint == null || endpoint.isEmpty()) {
  72. System.out.println("no endpoint");
  73. return false;
  74. }
  75. String body = "{\"model\":\"" + modelId + "\",\"max_tokens\":64,\"temperature\":0.3,"
  76. + "\"messages\":[{\"role\":\"user\",\"content\":\"" + PROBE_MSG + "\"}]}";
  77. HttpURLConnection conn = (HttpURLConnection) new URL(endpoint).openConnection();
  78. conn.setConnectTimeout(20000);
  79. conn.setReadTimeout(90000);
  80. conn.setRequestMethod("POST");
  81. conn.setDoOutput(true);
  82. conn.setRequestProperty("Authorization", "Bearer " + key);
  83. conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
  84. byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
  85. conn.setRequestProperty("Content-Length", String.valueOf(bytes.length));
  86. try (OutputStream os = conn.getOutputStream()) {
  87. os.write(bytes);
  88. }
  89. int code = conn.getResponseCode();
  90. InputStream is = code >= 400 ? conn.getErrorStream() : conn.getInputStream();
  91. String resp = readStream(is);
  92. System.out.println("HTTP " + code);
  93. if (resp.length() > 400) resp = resp.substring(0, 400) + "...";
  94. System.out.println(resp);
  95. return code >= 200 && code < 300 && resp != null && !resp.isEmpty();
  96. }
  97. private static String readStream(InputStream is) throws IOException {
  98. if (is == null) return "";
  99. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  100. byte[] buf = new byte[4096];
  101. int n;
  102. while ((n = is.read(buf)) != -1) bos.write(buf, 0, n);
  103. return bos.toString("UTF-8");
  104. }
  105. }