|
| 1 | +package com.example; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.net.http.HttpClient; |
| 6 | +import java.net.http.HttpRequest; |
| 7 | +import java.net.http.HttpRequest.BodyPublishers; |
| 8 | +import java.net.http.HttpResponse; |
| 9 | +import java.net.http.HttpResponse.BodyHandlers; |
| 10 | +import java.sql.Connection; |
| 11 | +import java.sql.DriverManager; |
| 12 | +import java.sql.PreparedStatement; |
| 13 | +import java.sql.ResultSet; |
| 14 | +import java.sql.SQLException; |
| 15 | +import java.sql.Statement; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import com.fasterxml.jackson.databind.JsonNode; |
| 22 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | +import com.pgvector.PGsparsevec; |
| 24 | +import com.pgvector.PGvector; |
| 25 | + |
| 26 | +public class Example { |
| 27 | + public static void main(String[] args) throws IOException, InterruptedException, SQLException { |
| 28 | + Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pgvector_example"); |
| 29 | + |
| 30 | + Statement setupStmt = conn.createStatement(); |
| 31 | + setupStmt.executeUpdate("CREATE EXTENSION IF NOT EXISTS vector"); |
| 32 | + setupStmt.executeUpdate("DROP TABLE IF EXISTS documents"); |
| 33 | + |
| 34 | + PGvector.addVectorType(conn); |
| 35 | + |
| 36 | + Statement createStmt = conn.createStatement(); |
| 37 | + createStmt.executeUpdate("CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding sparsevec(30522))"); |
| 38 | + |
| 39 | + String[] input = { |
| 40 | + "The dog is barking", |
| 41 | + "The cat is purring", |
| 42 | + "The bear is growling" |
| 43 | + }; |
| 44 | + List<Map<Integer, Float>> embeddings = fetchEmbeddings(input); |
| 45 | + |
| 46 | + for (int i = 0; i < input.length; i++) { |
| 47 | + PreparedStatement insertStmt = conn.prepareStatement("INSERT INTO documents (content, embedding) VALUES (?, ?)"); |
| 48 | + insertStmt.setString(1, input[i]); |
| 49 | + insertStmt.setObject(2, new PGsparsevec(embeddings.get(i), 30522)); |
| 50 | + insertStmt.executeUpdate(); |
| 51 | + } |
| 52 | + |
| 53 | + String query = "forest"; |
| 54 | + Map<Integer, Float> queryEmbedding = fetchEmbeddings(new String[] { query }).get(0); |
| 55 | + PreparedStatement neighborStmt = conn.prepareStatement("SELECT content FROM documents ORDER BY embedding <#> ? LIMIT 5"); |
| 56 | + neighborStmt.setObject(1, new PGsparsevec(queryEmbedding, 30522)); |
| 57 | + ResultSet rs = neighborStmt.executeQuery(); |
| 58 | + while (rs.next()) { |
| 59 | + System.out.println(rs.getString("content")); |
| 60 | + } |
| 61 | + |
| 62 | + conn.close(); |
| 63 | + } |
| 64 | + |
| 65 | + private static List<Map<Integer, Float>> fetchEmbeddings(String[] inputs) throws IOException, InterruptedException { |
| 66 | + ObjectMapper mapper = new ObjectMapper(); |
| 67 | + ObjectNode root = mapper.createObjectNode(); |
| 68 | + for (String v : inputs) { |
| 69 | + root.withArray("inputs").add(v); |
| 70 | + } |
| 71 | + String json = mapper.writeValueAsString(root); |
| 72 | + |
| 73 | + HttpClient client = HttpClient.newHttpClient(); |
| 74 | + HttpRequest request = HttpRequest.newBuilder() |
| 75 | + .uri(URI.create("http://localhost:3000/embed_sparse")) |
| 76 | + .header("Content-Type", "application/json") |
| 77 | + .POST(BodyPublishers.ofString(json)) |
| 78 | + .build(); |
| 79 | + HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); |
| 80 | + |
| 81 | + List<Map<Integer, Float>> embeddings = new ArrayList<>(); |
| 82 | + for (JsonNode n : mapper.readTree(response.body())) { |
| 83 | + Map<Integer, Float> embedding = new HashMap<Integer, Float>(); |
| 84 | + for (JsonNode v : n) { |
| 85 | + int index = v.get("index").asInt(); |
| 86 | + float value = (float) v.get("value").asDouble(); |
| 87 | + embedding.put(Integer.valueOf(index), Float.valueOf(value)); |
| 88 | + } |
| 89 | + embeddings.add(embedding); |
| 90 | + } |
| 91 | + return embeddings; |
| 92 | + } |
| 93 | +} |
0 commit comments