|
| 1 | +// EXAMPLE: home_vecsets |
| 2 | +// STEP_START import |
| 3 | +// Redis client (Lettuce) and vector set APIs |
| 4 | +import io.lettuce.core.RedisClient; |
| 5 | +import io.lettuce.core.RedisURI; |
| 6 | +import io.lettuce.core.api.StatefulRedisConnection; |
| 7 | +import io.lettuce.core.api.sync.RedisCommands; |
| 8 | +import io.lettuce.core.VAddArgs; |
| 9 | +import io.lettuce.core.VSimArgs; |
| 10 | + |
| 11 | +// Tokenizer to generate vectors (kept consistent with HomeQueryVec.java) |
| 12 | +import ai.djl.huggingface.tokenizers.HuggingFaceTokenizer; |
| 13 | + |
| 14 | +// Data & utils |
| 15 | +import java.util.*; |
| 16 | +// STEP_END |
| 17 | + |
| 18 | +public class HomeVecSets { |
| 19 | + // Keep the same tokenizer model style as HomeQueryVec.java |
| 20 | + private static final int DIM = 768; // fixed dimension to pad/truncate token ids |
| 21 | + |
| 22 | + // Helper: convert tokenizer ids to a fixed-length Double[] of size DIM |
| 23 | + private static Double[] idsToDoubleVector(long[] ids) { |
| 24 | + Double[] out = new Double[DIM]; |
| 25 | + int n = Math.min(ids.length, DIM); |
| 26 | + for (int i = 0; i < n; i++) out[i] = (double) ids[i]; |
| 27 | + for (int i = n; i < DIM; i++) out[i] = 0.0d; // pad |
| 28 | + return out; |
| 29 | + } |
| 30 | + |
| 31 | + // Simple container for people data |
| 32 | + private static class Person { |
| 33 | + final int born; |
| 34 | + final int died; |
| 35 | + final String description; |
| 36 | + Person(int born, int died, String description) { |
| 37 | + this.born = born; this.died = died; this.description = description; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public static void main(String[] args) throws Exception { |
| 42 | + // STEP_START model |
| 43 | + // Tokenizer configured like HomeQueryVec.java (acts as a simple, deterministic vectorizer here) |
| 44 | + HuggingFaceTokenizer tokenizer = HuggingFaceTokenizer.newInstance( |
| 45 | + "sentence-transformers/all-mpnet-base-v2", |
| 46 | + Map.of("maxLength", String.valueOf(DIM), "modelMaxLength", String.valueOf(DIM)) |
| 47 | + ); |
| 48 | + // STEP_END |
| 49 | + |
| 50 | + // STEP_START data |
| 51 | + Map<String, Person> peopleData = new LinkedHashMap<>(); |
| 52 | + peopleData.put("Marie Curie", new Person( |
| 53 | + 1867, 1934, |
| 54 | + """ |
| 55 | + Polish-French chemist and physicist. The only person ever to win |
| 56 | + two Nobel prizes for two different sciences. |
| 57 | + """.trim() |
| 58 | + )); |
| 59 | + peopleData.put("Linus Pauling", new Person( |
| 60 | + 1901, 1994, |
| 61 | + """ |
| 62 | + American chemist and peace activist. One of only two people to win two |
| 63 | + Nobel prizes in different fields (chemistry and peace). |
| 64 | + """.trim() |
| 65 | + )); |
| 66 | + peopleData.put("Freddie Mercury", new Person( |
| 67 | + 1946, 1991, |
| 68 | + """ |
| 69 | + British musician, best known as the lead singer of the rock band |
| 70 | + Queen. |
| 71 | + """.trim() |
| 72 | + )); |
| 73 | + peopleData.put("Marie Fredriksson", new Person( |
| 74 | + 1958, 2019, |
| 75 | + """ |
| 76 | + Swedish multi-instrumentalist, mainly known as the lead singer and |
| 77 | + keyboardist of the band Roxette. |
| 78 | + """.trim() |
| 79 | + )); |
| 80 | + peopleData.put("Paul Erdos", new Person( |
| 81 | + 1913, 1996, |
| 82 | + """ |
| 83 | + Hungarian mathematician, known for his eccentric personality almost |
| 84 | + as much as his contributions to many different fields of mathematics. |
| 85 | + """.trim() |
| 86 | + )); |
| 87 | + peopleData.put("Maryam Mirzakhani", new Person( |
| 88 | + 1977, 2017, |
| 89 | + """ |
| 90 | + Iranian mathematician. The first woman ever to win the Fields medal |
| 91 | + for her contributions to mathematics. |
| 92 | + """.trim() |
| 93 | + )); |
| 94 | + peopleData.put("Masako Natsume", new Person( |
| 95 | + 1957, 1985, |
| 96 | + """ |
| 97 | + Japanese actress. She was very famous in Japan but was primarily |
| 98 | + known elsewhere in the world for her portrayal of Tripitaka in the |
| 99 | + TV series Monkey. |
| 100 | + """.trim() |
| 101 | + )); |
| 102 | + peopleData.put("Chaim Topol", new Person( |
| 103 | + 1935, 2023, |
| 104 | + """ |
| 105 | + Israeli actor and singer, usually credited simply as 'Topol'. He was |
| 106 | + best known for his many appearances as Tevye in the musical Fiddler |
| 107 | + on the Roof. |
| 108 | + """.trim() |
| 109 | + )); |
| 110 | + // STEP_END |
| 111 | + |
| 112 | + // STEP_START add_data |
| 113 | + RedisClient client = RedisClient.create(RedisURI.Builder.redis("localhost", 6379).build()); |
| 114 | + StatefulRedisConnection<String, String> conn = null; |
| 115 | + try { |
| 116 | + conn = client.connect(); |
| 117 | + RedisCommands<String, String> cmd = conn.sync(); |
| 118 | + |
| 119 | + for (Map.Entry<String, Person> e : peopleData.entrySet()) { |
| 120 | + String name = e.getKey(); |
| 121 | + Person p = e.getValue(); |
| 122 | + |
| 123 | + // Vector from description |
| 124 | + Double[] vec = idsToDoubleVector(tokenizer.encode(p.description).getIds()); |
| 125 | + |
| 126 | + // Add with attributes using VADD (vector sets API) |
| 127 | + VAddArgs addArgs = new VAddArgs() |
| 128 | + .attributes(String.format("{\"born\": %d, \"died\": %d}", p.born, p.died)); |
| 129 | + |
| 130 | + // Create set and add element + vector in one call |
| 131 | + Boolean added = cmd.vadd("famousPeople", name, addArgs, vec); |
| 132 | + if (Boolean.FALSE.equals(added)) { |
| 133 | + // If element exists, you could update attributes via vsetattr |
| 134 | + cmd.vsetattr("famousPeople", name, String.format("{\"born\": %d, \"died\": %d}", p.born, p.died)); |
| 135 | + } |
| 136 | + } |
| 137 | + } finally { |
| 138 | + if (conn != null) conn.close(); |
| 139 | + client.shutdown(); |
| 140 | + } |
| 141 | + // STEP_END |
| 142 | + |
| 143 | + // Reconnect for queries (explicitly, to mirror example flow) |
| 144 | + client = RedisClient.create(RedisURI.Builder.redis("localhost", 6379).build()); |
| 145 | + try (StatefulRedisConnection<String, String> qconn = client.connect()) { |
| 146 | + RedisCommands<String, String> q = qconn.sync(); |
| 147 | + |
| 148 | + // STEP_START basic_query |
| 149 | + String queryValue = "actors"; |
| 150 | + List<String> actors = q.vsim("famousPeople", idsToDoubleVector(tokenizer.encode(queryValue).getIds())); |
| 151 | + System.out.println("'actors': " + String.join(", ", actors)); |
| 152 | + // STEP_END |
| 153 | + |
| 154 | + // STEP_START limited_query |
| 155 | + queryValue = "actors"; |
| 156 | + VSimArgs twoCount = new VSimArgs().count(2L); |
| 157 | + List<String> twoActors = q.vsim("famousPeople", twoCount, idsToDoubleVector(tokenizer.encode(queryValue).getIds())); |
| 158 | + System.out.println("'actors (2)': " + String.join(", ", twoActors)); |
| 159 | + // >>> 'actors (2)': Masako Natsume, Chaim Topol |
| 160 | + // STEP_END |
| 161 | + |
| 162 | + // STEP_START entertainer_query |
| 163 | + queryValue = "entertainer"; |
| 164 | + List<String> entertainers = q.vsim("famousPeople", idsToDoubleVector(tokenizer.encode(queryValue).getIds())); |
| 165 | + System.out.println("'entertainer': " + String.join(", ", entertainers)); |
| 166 | + // >>> 'entertainer': Chaim Topol, Freddie Mercury, Marie Fredriksson, ... |
| 167 | + // STEP_END |
| 168 | + |
| 169 | + queryValue = "science"; |
| 170 | + List<String> science = q.vsim("famousPeople", idsToDoubleVector(tokenizer.encode(queryValue).getIds())); |
| 171 | + System.out.println("'science': " + String.join(", ", science)); |
| 172 | + |
| 173 | + // STEP_START filtered_query |
| 174 | + queryValue = "science"; |
| 175 | + VSimArgs filtered = new VSimArgs().filter(".died < 2000"); |
| 176 | + List<String> science2000 = q.vsim("famousPeople", filtered, idsToDoubleVector(tokenizer.encode(queryValue).getIds())); |
| 177 | + System.out.println("'science2000': " + String.join(", ", science2000)); |
| 178 | + // STEP_END |
| 179 | + } finally { |
| 180 | + client.shutdown(); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
0 commit comments