ApplyLobsterGepaNegativePrompt.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import java.nio.charset.StandardCharsets;
  2. import java.nio.file.Files;
  3. import java.nio.file.Paths;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * Scan all tenant DBs (schemas with lobster_system_prompt) and apply GEPA negative prompt SQL.
  12. *
  13. * Usage:
  14. * java -cp mysql-connector-j.jar;. ApplyLobsterGepaNegativePrompt
  15. * java -cp mysql-connector-j.jar;. ApplyLobsterGepaNegativePrompt fs_tenant_cs1
  16. */
  17. public class ApplyLobsterGepaNegativePrompt {
  18. private static final String HOST = "cq-cdb-8fjmemkb.sql.tencentcdb.com:27220";
  19. private static final String USER = "root";
  20. private static final String PASS = "Ylrz_1q2w3e4r5t6y";
  21. private static final String MASTER_DB = "ylrz_saas";
  22. private static final String SQL_PATH = "d:/ylrz_saas_new/java/sql/lobster_engine_prompt_gepa_negative.sql";
  23. public static void main(String[] args) throws Exception {
  24. Class.forName("com.mysql.cj.jdbc.Driver");
  25. byte[] bytes = Files.readAllBytes(Paths.get(SQL_PATH));
  26. String sql = new String(bytes, StandardCharsets.UTF_8);
  27. if (sql.startsWith("\uFEFF")) {
  28. sql = sql.substring(1);
  29. }
  30. List<String> targets = new ArrayList<String>();
  31. if (args.length > 0) {
  32. for (String arg : args) {
  33. if (arg != null && !arg.trim().isEmpty()) {
  34. targets.add(arg.trim());
  35. }
  36. }
  37. } else {
  38. targets = discoverTenantDatabases();
  39. }
  40. System.out.println("Target databases: " + targets.size());
  41. int ok = 0;
  42. int fail = 0;
  43. for (String db : targets) {
  44. try {
  45. applyOne(db, sql);
  46. ok++;
  47. } catch (Exception e) {
  48. fail++;
  49. System.out.println("[FAIL] " + db + ": " + e.getMessage());
  50. }
  51. }
  52. System.out.println("\nSummary: ok=" + ok + " fail=" + fail + " total=" + targets.size());
  53. }
  54. private static List<String> discoverTenantDatabases() throws Exception {
  55. List<String> dbs = new ArrayList<String>();
  56. Connection master = null;
  57. Statement st = null;
  58. ResultSet rs = null;
  59. try {
  60. master = open(MASTER_DB);
  61. st = master.createStatement();
  62. rs = st.executeQuery(
  63. "SELECT DISTINCT TABLE_SCHEMA FROM information_schema.TABLES "
  64. + "WHERE TABLE_NAME = 'lobster_system_prompt' "
  65. + "AND TABLE_SCHEMA NOT IN ('information_schema','mysql','performance_schema','sys') "
  66. + "ORDER BY TABLE_SCHEMA");
  67. while (rs.next()) {
  68. dbs.add(rs.getString(1));
  69. }
  70. } finally {
  71. if (rs != null) {
  72. rs.close();
  73. }
  74. if (st != null) {
  75. st.close();
  76. }
  77. if (master != null) {
  78. master.close();
  79. }
  80. }
  81. return dbs;
  82. }
  83. private static void applyOne(String db, String sql) throws Exception {
  84. System.out.println("\n=== " + db + " ===");
  85. Connection c = null;
  86. Statement st = null;
  87. try {
  88. c = open(db);
  89. st = c.createStatement();
  90. st.execute(sql);
  91. verify(st, db);
  92. } finally {
  93. if (st != null) {
  94. st.close();
  95. }
  96. if (c != null) {
  97. c.close();
  98. }
  99. }
  100. }
  101. private static void verify(Statement st, String db) throws Exception {
  102. ResultSet rs = st.executeQuery(
  103. "SELECT prompt_key, "
  104. + "(prompt_content LIKE '%{avoidBlock}%' OR prompt_content LIKE '%{negativeBlock}%') AS ok "
  105. + "FROM lobster_system_prompt "
  106. + "WHERE company_id IS NULL "
  107. + "AND prompt_key IN ('gepa_synthesize_avoid', 'gepa_mutate_skill') "
  108. + "ORDER BY prompt_key");
  109. try {
  110. while (rs.next()) {
  111. String key = rs.getString("prompt_key");
  112. boolean ok = rs.getInt("ok") > 0;
  113. System.out.println((ok ? "[OK] " : "[WARN] ") + db + " " + key);
  114. }
  115. } finally {
  116. rs.close();
  117. }
  118. }
  119. private static Connection open(String db) throws Exception {
  120. return DriverManager.getConnection(
  121. "jdbc:mysql://" + HOST + "/" + db
  122. + "?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf8&allowMultiQueries=true",
  123. USER, PASS);
  124. }
  125. }