|
| 1 | +package com.example; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.sql.Connection; |
| 5 | +import java.sql.DriverManager; |
| 6 | +import java.sql.PreparedStatement; |
| 7 | +import java.sql.ResultSet; |
| 8 | +import java.sql.SQLException; |
| 9 | +import java.sql.Statement; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import ai.djl.ModelException; |
| 13 | +import ai.djl.huggingface.translator.TextEmbeddingTranslatorFactory; |
| 14 | +import ai.djl.inference.Predictor; |
| 15 | +import ai.djl.repository.zoo.Criteria; |
| 16 | +import ai.djl.repository.zoo.ZooModel; |
| 17 | +import ai.djl.translate.TranslateException; |
| 18 | +import com.pgvector.PGvector; |
| 19 | + |
| 20 | +public class Example { |
| 21 | + public static void main(String[] args) throws IOException, ModelException, SQLException, TranslateException { |
| 22 | + Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pgvector_example"); |
| 23 | + |
| 24 | + Statement setupStmt = conn.createStatement(); |
| 25 | + setupStmt.executeUpdate("CREATE EXTENSION IF NOT EXISTS vector"); |
| 26 | + setupStmt.executeUpdate("DROP TABLE IF EXISTS documents"); |
| 27 | + |
| 28 | + PGvector.addVectorType(conn); |
| 29 | + |
| 30 | + Statement createStmt = conn.createStatement(); |
| 31 | + createStmt.executeUpdate("CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(384))"); |
| 32 | + |
| 33 | + ZooModel<String, float[]> model = loadModel("sentence-transformers/all-MiniLM-L6-v2"); |
| 34 | + |
| 35 | + String[] input = { |
| 36 | + "The dog is barking", |
| 37 | + "The cat is purring", |
| 38 | + "The bear is growling" |
| 39 | + }; |
| 40 | + List<float[]> embeddings = generateEmbeddings(model, input); |
| 41 | + |
| 42 | + for (int i = 0; i < input.length; i++) { |
| 43 | + PreparedStatement insertStmt = conn.prepareStatement("INSERT INTO documents (content, embedding) VALUES (?, ?)"); |
| 44 | + insertStmt.setString(1, input[i]); |
| 45 | + insertStmt.setObject(2, new PGvector(embeddings.get(i))); |
| 46 | + insertStmt.executeUpdate(); |
| 47 | + } |
| 48 | + |
| 49 | + long documentId = 2; |
| 50 | + PreparedStatement neighborStmt = conn.prepareStatement("SELECT * FROM documents WHERE id != ? ORDER BY embedding <=> (SELECT embedding FROM documents WHERE id = ?) LIMIT 5"); |
| 51 | + neighborStmt.setLong(1, documentId); |
| 52 | + neighborStmt.setLong(2, documentId); |
| 53 | + ResultSet rs = neighborStmt.executeQuery(); |
| 54 | + while (rs.next()) { |
| 55 | + System.out.println(rs.getString("content")); |
| 56 | + } |
| 57 | + |
| 58 | + conn.close(); |
| 59 | + } |
| 60 | + |
| 61 | + private static ZooModel<String, float[]> loadModel(String id) throws IOException, ModelException { |
| 62 | + return Criteria.builder() |
| 63 | + .setTypes(String.class, float[].class) |
| 64 | + .optModelUrls("djl://ai.djl.huggingface.pytorch/" + id) |
| 65 | + .optEngine("PyTorch") |
| 66 | + .optTranslatorFactory(new TextEmbeddingTranslatorFactory()) |
| 67 | + .build() |
| 68 | + .loadModel(); |
| 69 | + } |
| 70 | + |
| 71 | + private static List<float[]> generateEmbeddings(ZooModel<String, float[]> model, String[] input) throws TranslateException { |
| 72 | + Predictor<String, float[]> predictor = model.newPredictor(); |
| 73 | + List<float[]> embeddings = new ArrayList<>(input.length); |
| 74 | + for (String text : input) { |
| 75 | + embeddings.add(predictor.predict(text)); |
| 76 | + } |
| 77 | + return embeddings; |
| 78 | + } |
| 79 | +} |
0 commit comments