|
| 1 | +package io.pinecone.integration.dataPlane; |
| 2 | + |
| 3 | +import io.pinecone.clients.Index; |
| 4 | +import io.pinecone.clients.Pinecone; |
| 5 | +import io.pinecone.helpers.RandomStringBuilder; |
| 6 | +import org.junit.jupiter.api.Assertions; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.openapitools.db_control.client.model.CreateIndexForModelRequest; |
| 9 | +import org.openapitools.db_control.client.model.CreateIndexForModelRequestEmbed; |
| 10 | +import org.openapitools.db_control.client.model.DeletionProtection; |
| 11 | +import org.openapitools.db_data.client.ApiException; |
| 12 | +import org.openapitools.db_data.client.model.SearchRecordsRequestQuery; |
| 13 | +import org.openapitools.db_data.client.model.SearchRecordsRequestRerank; |
| 14 | +import org.openapitools.db_data.client.model.SearchRecordsResponse; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +public class UpsertAndSearchRecordsTest { |
| 19 | + @Test |
| 20 | + public void upsertAndSearchRecordsTest() throws ApiException, org.openapitools.db_control.client.ApiException, InterruptedException { |
| 21 | + Pinecone pinecone = new Pinecone.Builder(System.getenv("PINECONE_API_KEY")).build(); |
| 22 | + String indexName = RandomStringBuilder.build("inf", 8); |
| 23 | + HashMap<String, String> fieldMap = new HashMap<>(); |
| 24 | + fieldMap.put("text", "chunk_text"); |
| 25 | + CreateIndexForModelRequestEmbed embed = new CreateIndexForModelRequestEmbed() |
| 26 | + .model("multilingual-e5-large") |
| 27 | + .fieldMap(fieldMap); |
| 28 | + pinecone.createIndexForModel(indexName, CreateIndexForModelRequest.CloudEnum.AWS, "us-west-2", embed, DeletionProtection.DISABLED, new HashMap<>()); |
| 29 | + |
| 30 | + // Wait for index to be created |
| 31 | + Thread.sleep(10000); |
| 32 | + |
| 33 | + Index index = pinecone.getIndexConnection(indexName); |
| 34 | + ArrayList<Map<String, String>> upsertRecords = new ArrayList<>(); |
| 35 | + |
| 36 | + HashMap<String, String> record1 = new HashMap<>(); |
| 37 | + record1.put("_id", "rec1"); |
| 38 | + record1.put("category", "digestive system"); |
| 39 | + record1.put("chunk_text", "Apples are a great source of dietary fiber, which supports digestion and helps maintain a healthy gut."); |
| 40 | + |
| 41 | + HashMap<String, String> record2 = new HashMap<>(); |
| 42 | + record2.put("_id", "rec2"); |
| 43 | + record2.put("category", "cultivation"); |
| 44 | + record2.put("chunk_text", "Apples originated in Central Asia and have been cultivated for thousands of years, with over 7,500 varieties available today."); |
| 45 | + |
| 46 | + HashMap<String, String> record3 = new HashMap<>(); |
| 47 | + record3.put("_id", "rec3"); |
| 48 | + record3.put("category", "immune system"); |
| 49 | + record3.put("chunk_text", "Rich in vitamin C and other antioxidants, apples contribute to immune health and may reduce the risk of chronic diseases."); |
| 50 | + |
| 51 | + HashMap<String, String> record4 = new HashMap<>(); |
| 52 | + record4.put("_id", "rec4"); |
| 53 | + record4.put("category", "endocrine system"); |
| 54 | + record4.put("chunk_text", "The high fiber content in apples can also help regulate blood sugar levels, making them a favorable snack for people with diabetes."); |
| 55 | + |
| 56 | + upsertRecords.add(record1); |
| 57 | + upsertRecords.add(record2); |
| 58 | + upsertRecords.add(record3); |
| 59 | + upsertRecords.add(record4); |
| 60 | + |
| 61 | + index.upsertRecords("example-namespace", upsertRecords); |
| 62 | + |
| 63 | + String namespace = "example-namespace"; |
| 64 | + HashMap<String, String> inputsMap = new HashMap<>(); |
| 65 | + inputsMap.put("text", "Disease prevention"); |
| 66 | + SearchRecordsRequestQuery query = new SearchRecordsRequestQuery() |
| 67 | + .topK(4) |
| 68 | + .inputs(inputsMap); |
| 69 | + |
| 70 | + List<String> fields = new ArrayList<>(); |
| 71 | + fields.add("category"); |
| 72 | + fields.add("chunk_text"); |
| 73 | + |
| 74 | + // Wait for vectors to be upserted |
| 75 | + Thread.sleep(5000); |
| 76 | + |
| 77 | + SearchRecordsResponse recordsResponse = index.searchRecords(namespace, query, fields, null); |
| 78 | + Assertions.assertEquals(upsertRecords.size(), recordsResponse.getResult().getHits().size()); |
| 79 | + Assertions.assertEquals(record3.get("_id"), recordsResponse.getResult().getHits().get(0).getId()); |
| 80 | + |
| 81 | + recordsResponse = index.searchRecordsById(record1.get("_id"), namespace, fields, 1, null, null); |
| 82 | + Assertions.assertEquals(1, recordsResponse.getResult().getHits().size()); |
| 83 | + Assertions.assertEquals(record1.get("_id"), recordsResponse.getResult().getHits().get(0).getId()); |
| 84 | + |
| 85 | + SearchRecordsRequestRerank rerank = new SearchRecordsRequestRerank() |
| 86 | + .model("bge-reranker-v2-m3") |
| 87 | + .topN(2) |
| 88 | + .rankFields(Arrays.asList("chunk_text")); |
| 89 | + |
| 90 | + recordsResponse = index.searchRecordsByText("Disease prevention", namespace, fields, 4, null, rerank); |
| 91 | + Assertions.assertEquals(record3.get("_id"), recordsResponse.getResult().getHits().get(0).getId()); |
| 92 | + |
| 93 | + pinecone.deleteIndex(indexName); |
| 94 | + } |
| 95 | +} |
0 commit comments