|
| 1 | +package io.pinecone.integration.inference; |
| 2 | + |
| 3 | +import io.pinecone.clients.Inference; |
| 4 | +import io.pinecone.clients.Pinecone; |
| 5 | +import org.junit.jupiter.api.Assertions; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.openapitools.inference.client.ApiException; |
| 8 | +import org.openapitools.inference.client.model.RerankResult; |
| 9 | + |
| 10 | +import java.util.*; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 13 | + |
| 14 | +public class RerankTest { |
| 15 | + private static final Pinecone pinecone = new Pinecone |
| 16 | + .Builder(System.getenv("PINECONE_API_KEY")) |
| 17 | + .withSourceTag("pinecone_test") |
| 18 | + .build(); |
| 19 | + private static final Inference inference = pinecone.getInferenceClient(); |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testRerank() throws ApiException { |
| 23 | + String model = "bge-reranker-v2-m3"; |
| 24 | + String query = "The tech company Apple is known for its innovative products like the iPhone."; |
| 25 | + List<Map<String, String>> documents = new ArrayList<>(); |
| 26 | + |
| 27 | + Map<String, String> doc1 = new HashMap<>(); |
| 28 | + doc1.put("id", "vec1"); |
| 29 | + doc1.put("my_field", "Apple is a popular fruit known for its sweetness and crisp texture."); |
| 30 | + documents.add(doc1); |
| 31 | + |
| 32 | + Map<String, String> doc2 = new HashMap<>(); |
| 33 | + doc2.put("id", "vec2"); |
| 34 | + doc2.put("my_field", "Many people enjoy eating apples as a healthy snack."); |
| 35 | + documents.add(doc2); |
| 36 | + |
| 37 | + Map<String, String> doc3 = new HashMap<>(); |
| 38 | + doc3.put("id", "vec3"); |
| 39 | + doc3.put("my_field", "Apple Inc. has revolutionized the tech industry with its sleek designs and user-friendly interfaces."); |
| 40 | + documents.add(doc3); |
| 41 | + |
| 42 | + Map<String, String> doc4 = new HashMap<>(); |
| 43 | + doc4.put("id", "vec4"); |
| 44 | + doc4.put("my_field", "An apple a day keeps the doctor away, as the saying goes."); |
| 45 | + documents.add(doc4); |
| 46 | + |
| 47 | + List<String> rankFields = Arrays.asList("my_field"); |
| 48 | + int topN = 2; |
| 49 | + boolean returnDocuments = true; |
| 50 | + Map<String, Object> parameters = new HashMap<>(); |
| 51 | + parameters.put("truncate", "END"); |
| 52 | + |
| 53 | + RerankResult result = inference.rerank(model, query, documents, rankFields, topN, returnDocuments, parameters); |
| 54 | + |
| 55 | + assertNotNull(result); |
| 56 | + Assertions.assertEquals(result.getData().size(), topN); |
| 57 | + Assertions.assertEquals(result.getData().get(0).getIndex(), 2); |
| 58 | + Assertions.assertEquals(result.getData().get(0).getDocument().get("my_field"), doc3.get("my_field")); |
| 59 | + Assertions.assertEquals(result.getData().size(), 2); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testRerankWithRequiredParameters() throws ApiException { |
| 64 | + String model = "bge-reranker-v2-m3"; |
| 65 | + String query = "The tech company Apple is known for its innovative products like the iPhone."; |
| 66 | + List<Map<String, String>> documents = new ArrayList<>(); |
| 67 | + |
| 68 | + Map<String, String> doc1 = new HashMap<>(); |
| 69 | + doc1.put("id", "vec1"); |
| 70 | + doc1.put("text", "Apple is a popular fruit known for its sweetness and crisp texture."); |
| 71 | + documents.add(doc1); |
| 72 | + |
| 73 | + Map<String, String> doc2 = new HashMap<>(); |
| 74 | + doc2.put("id", "vec2"); |
| 75 | + doc2.put("text", "Many people enjoy eating apples as a healthy snack."); |
| 76 | + documents.add(doc2); |
| 77 | + |
| 78 | + Map<String, String> doc3 = new HashMap<>(); |
| 79 | + doc3.put("id", "vec3"); |
| 80 | + doc3.put("text", "Apple Inc. has revolutionized the tech industry with its sleek designs and user-friendly interfaces."); |
| 81 | + documents.add(doc3); |
| 82 | + |
| 83 | + Map<String, String> doc4 = new HashMap<>(); |
| 84 | + doc4.put("id", "vec4"); |
| 85 | + doc4.put("text", "An apple a day keeps the doctor away, as the saying goes."); |
| 86 | + documents.add(doc4); |
| 87 | + |
| 88 | + RerankResult result = inference.rerank(model, query, documents); |
| 89 | + |
| 90 | + assertNotNull(result); |
| 91 | + Assertions.assertEquals(result.getData().size(), documents.size()); |
| 92 | + Assertions.assertEquals(result.getData().get(0).getIndex(), 2); |
| 93 | + Assertions.assertEquals(result.getData().get(0).getDocument().get("text"), doc3.get("text")); |
| 94 | + } |
| 95 | +} |
0 commit comments