@@ -44,7 +44,7 @@ public static void main(String[] args) throws IOException, InterruptedException,
4444 "The cat is purring" ,
4545 "The bear is growling"
4646 };
47- List <float []> embeddings = fetchEmbeddings (input , apiKey );
47+ List <float []> embeddings = embed (input , apiKey );
4848
4949 for (int i = 0 ; i < input .length ; i ++) {
5050 PreparedStatement insertStmt = conn .prepareStatement ("INSERT INTO documents (content, embedding) VALUES (?, ?)" );
@@ -53,10 +53,10 @@ public static void main(String[] args) throws IOException, InterruptedException,
5353 insertStmt .executeUpdate ();
5454 }
5555
56- long documentId = 2 ;
57- PreparedStatement neighborStmt = conn . prepareStatement ( "SELECT * FROM documents WHERE id != ? ORDER BY embedding <=> (SELECT embedding FROM documents WHERE id = ?) LIMIT 5" );
58- neighborStmt . setObject ( 1 , documentId );
59- neighborStmt .setObject (2 , documentId );
56+ String query = "forest" ;
57+ float [] queryEmbedding = embed ( new String [] { query }, apiKey ). get ( 0 );
58+ PreparedStatement neighborStmt = conn . prepareStatement ( "SELECT content FROM documents ORDER BY embedding <=> ? LIMIT 5" );
59+ neighborStmt .setObject (1 , new PGvector ( queryEmbedding ) );
6060 ResultSet rs = neighborStmt .executeQuery ();
6161 while (rs .next ()) {
6262 System .out .println (rs .getString ("content" ));
@@ -65,7 +65,7 @@ public static void main(String[] args) throws IOException, InterruptedException,
6565 conn .close ();
6666 }
6767
68- private static List <float []> fetchEmbeddings (String [] input , String apiKey ) throws IOException , InterruptedException {
68+ private static List <float []> embed (String [] input , String apiKey ) throws IOException , InterruptedException {
6969 ObjectMapper mapper = new ObjectMapper ();
7070 ObjectNode root = mapper .createObjectNode ();
7171 for (String v : input ) {
0 commit comments