From 8dda0997b01c3c005be35602658c83c1b66faaa4 Mon Sep 17 00:00:00 2001 From: rohanshah18 Date: Fri, 20 Sep 2024 18:36:18 -0400 Subject: [PATCH 1/5] add embed endpoint --- .../integration/inference/EmbedTest.java | 52 +++++++++++++++++++ .../java/io/pinecone/clients/Inference.java | 40 ++++++++++++++ .../java/io/pinecone/clients/Pinecone.java | 4 ++ 3 files changed, 96 insertions(+) create mode 100644 src/integration/java/io/pinecone/integration/inference/EmbedTest.java create mode 100644 src/main/java/io/pinecone/clients/Inference.java diff --git a/src/integration/java/io/pinecone/integration/inference/EmbedTest.java b/src/integration/java/io/pinecone/integration/inference/EmbedTest.java new file mode 100644 index 00000000..cb14b28c --- /dev/null +++ b/src/integration/java/io/pinecone/integration/inference/EmbedTest.java @@ -0,0 +1,52 @@ +package io.pinecone.integration.inference; + +import io.pinecone.clients.Inference; +import io.pinecone.clients.Pinecone; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.openapitools.control.client.ApiException; +import org.openapitools.control.client.model.EmbeddingsList; + +import java.util.*; + +import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class EmbedTest { + + private static final Pinecone pinecone = new Pinecone + .Builder(System.getenv("PINECONE_API_KEY")) + .withSourceTag("pinecone_test") + .build(); + private static final Inference inference = pinecone.getInferenceClient(); + + @Test + public void testGenerateEmbeddings() throws ApiException { + List inputs = new ArrayList<>(1); + inputs.add("The quick brown fox jumps over the lazy dog."); + inputs.add("Lorem ipsum"); + String embeddingModel = "multilingual-e5-large"; + EmbeddingsList embeddings = inference.embed(embeddingModel, "query", "END", inputs); + try { + assertNotNull(embeddings, "Expected embedding to be not null"); + Assertions.assertEquals(embeddingModel, embeddings.getModel()); + System.out.println(embeddings); + Assertions.assertEquals(1024, embeddings.getData().get(0).getValues().size()); + Assertions.assertEquals(2, embeddings.getData().size()); + } catch (Exception e) { + Assertions.fail("Embedding request should not have thrown an exception: " + e.getMessage()); + } + } + + @Test + public void testGenerateEmbeddingsInvalidInputs() throws ApiException { + String embeddingModel = "multilingual-e5-large"; + List inputs = new ArrayList<>(); + + Exception exception = assertThrows(Exception.class, () -> { + inference.embed(embeddingModel, "query", "END", inputs); + }); + + Assertions.assertTrue(exception.getMessage().contains("Must specify at least one input")); + } +} diff --git a/src/main/java/io/pinecone/clients/Inference.java b/src/main/java/io/pinecone/clients/Inference.java new file mode 100644 index 00000000..f86adc1a --- /dev/null +++ b/src/main/java/io/pinecone/clients/Inference.java @@ -0,0 +1,40 @@ +package io.pinecone.clients; + +import org.openapitools.control.client.ApiClient; +import org.openapitools.control.client.ApiException; +import org.openapitools.control.client.api.InferenceApi; +import org.openapitools.control.client.model.EmbedRequest; +import org.openapitools.control.client.model.EmbedRequestInputsInner; +import org.openapitools.control.client.model.EmbedRequestParameters; +import org.openapitools.control.client.model.EmbeddingsList; + +import java.util.List; +import java.util.stream.Collectors; + +public class Inference { + + InferenceApi inferenceApi; + + public Inference(ApiClient apiClient) { + inferenceApi = new InferenceApi(apiClient); + } + + public EmbeddingsList embed(String model, String inputType, String truncate, List inputs) throws ApiException { + EmbedRequestParameters embedRequestParameters = new EmbedRequestParameters() + .inputType(inputType) + .truncate(truncate); + List EmbedRequestInputsInnerList = convertInputStringToEmbedRequestInputsInner(inputs); + EmbedRequest embedRequest = new EmbedRequest() + .model(model) + .parameters(embedRequestParameters) + .inputs(EmbedRequestInputsInnerList); + + return inferenceApi.embed(embedRequest); + } + + private List convertInputStringToEmbedRequestInputsInner(List inputs) { + return inputs.stream() + .map(input -> new EmbedRequestInputsInner().text(input)) + .collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/src/main/java/io/pinecone/clients/Pinecone.java b/src/main/java/io/pinecone/clients/Pinecone.java index 3523383a..d81ffd78 100644 --- a/src/main/java/io/pinecone/clients/Pinecone.java +++ b/src/main/java/io/pinecone/clients/Pinecone.java @@ -871,6 +871,10 @@ public AsyncIndex getAsyncIndexConnection(String indexName) throws PineconeValid return new AsyncIndex(connection, indexName); } + public Inference getInferenceClient() { + return new Inference(manageIndexesApi.getApiClient()); + } + PineconeConnection getConnection(String indexName) { return connectionsMap.computeIfAbsent(indexName, key -> new PineconeConnection(config)); } From 900bad88197b8fd08ae40019f7ed7ef2ee8e6534 Mon Sep 17 00:00:00 2001 From: rohanshah18 Date: Mon, 23 Sep 2024 16:25:50 -0400 Subject: [PATCH 2/5] update EmbeddingRequestParameter --- .../integration/inference/EmbedTest.java | 16 +++++++++++++--- src/main/java/io/pinecone/clients/Inference.java | 9 +++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/integration/java/io/pinecone/integration/inference/EmbedTest.java b/src/integration/java/io/pinecone/integration/inference/EmbedTest.java index cb14b28c..6c08893a 100644 --- a/src/integration/java/io/pinecone/integration/inference/EmbedTest.java +++ b/src/integration/java/io/pinecone/integration/inference/EmbedTest.java @@ -25,12 +25,19 @@ public void testGenerateEmbeddings() throws ApiException { List inputs = new ArrayList<>(1); inputs.add("The quick brown fox jumps over the lazy dog."); inputs.add("Lorem ipsum"); + String embeddingModel = "multilingual-e5-large"; - EmbeddingsList embeddings = inference.embed(embeddingModel, "query", "END", inputs); + + Map parameters = new HashMap<>(); + parameters.put("input_type", "query"); + parameters.put("truncate", "END"); + + EmbeddingsList embeddings = inference.embed(embeddingModel, parameters, inputs); + try { assertNotNull(embeddings, "Expected embedding to be not null"); Assertions.assertEquals(embeddingModel, embeddings.getModel()); - System.out.println(embeddings); + Assertions.assertEquals(1024, embeddings.getData().get(0).getValues().size()); Assertions.assertEquals(2, embeddings.getData().size()); } catch (Exception e) { @@ -42,9 +49,12 @@ public void testGenerateEmbeddings() throws ApiException { public void testGenerateEmbeddingsInvalidInputs() throws ApiException { String embeddingModel = "multilingual-e5-large"; List inputs = new ArrayList<>(); + Map parameters = new HashMap<>(); + parameters.put("input_type", "query"); + parameters.put("truncate", "END"); Exception exception = assertThrows(Exception.class, () -> { - inference.embed(embeddingModel, "query", "END", inputs); + inference.embed(embeddingModel, parameters, inputs); }); Assertions.assertTrue(exception.getMessage().contains("Must specify at least one input")); diff --git a/src/main/java/io/pinecone/clients/Inference.java b/src/main/java/io/pinecone/clients/Inference.java index f86adc1a..01d65480 100644 --- a/src/main/java/io/pinecone/clients/Inference.java +++ b/src/main/java/io/pinecone/clients/Inference.java @@ -9,6 +9,7 @@ import org.openapitools.control.client.model.EmbeddingsList; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class Inference { @@ -19,10 +20,10 @@ public Inference(ApiClient apiClient) { inferenceApi = new InferenceApi(apiClient); } - public EmbeddingsList embed(String model, String inputType, String truncate, List inputs) throws ApiException { - EmbedRequestParameters embedRequestParameters = new EmbedRequestParameters() - .inputType(inputType) - .truncate(truncate); + public EmbeddingsList embed(String model, Map parameters, List inputs) throws ApiException { + EmbedRequestParameters embedRequestParameters = new EmbedRequestParameters(); + parameters.forEach(embedRequestParameters::putAdditionalProperty); + List EmbedRequestInputsInnerList = convertInputStringToEmbedRequestInputsInner(inputs); EmbedRequest embedRequest = new EmbedRequest() .model(model) From 67084eef4fbd526ce7b556a68e07bd5df410844d Mon Sep 17 00:00:00 2001 From: rohanshah18 Date: Mon, 23 Sep 2024 17:32:01 -0400 Subject: [PATCH 3/5] add docstrings and update README --- README.md | 32 +++++++++++ .../java/io/pinecone/clients/Inference.java | 55 ++++++++++++++++++- .../java/io/pinecone/clients/Pinecone.java | 8 +++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba2b5b1f..f437ac5d 100644 --- a/README.md +++ b/README.md @@ -542,6 +542,38 @@ Pinecone pinecone = new Pinecone.Builder("PINECONE_API_KEY").build(); pinecone.deleteCollection("example-collection"); ``` +## Inference + +The Pinecone SDK now supports creating embeddings via the [Inference API](https://docs.pinecone.io/guides/inference/understanding-inference). + +```java +import io.pinecone.clients.Inference; +import io.pinecone.clients.Pinecone; +import org.openapitools.control.client.ApiException; +import org.openapitools.control.client.model.EmbeddingsList; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +... + +Pinecone pinecone = new Pinecone.Builder("PINECONE_API_KEY").build(); +Inference inference = pinecone.getInferenceClient(); + +List inputs = new ArrayList<>(1); +inputs.add("The quick brown fox jumps over the lazy dog."); +inputs.add("Lorem ipsum"); + +String embeddingModel = "multilingual-e5-large"; + +Map parameters = new HashMap<>(); +parameters.put("input_type", "query"); +parameters.put("truncate", "END"); + +EmbeddingsList embeddings = inference.embed(embeddingModel, parameters, inputs); +``` + ## Examples - The data and control plane operation examples can be found in `io/pinecone/integration` folder. diff --git a/src/main/java/io/pinecone/clients/Inference.java b/src/main/java/io/pinecone/clients/Inference.java index 01d65480..88800f37 100644 --- a/src/main/java/io/pinecone/clients/Inference.java +++ b/src/main/java/io/pinecone/clients/Inference.java @@ -12,14 +12,61 @@ import java.util.Map; import java.util.stream.Collectors; +/** + * The Inference class provides methods to interact with Pinecone's embedding API through the Java SDK. It allows users + * to send input data to generate embeddings using a specified model. + *

+ * This class utilizes the {@link InferenceApi} to make API calls to the Pinecone embedding service. + * + *

{@code
+ *      import io.pinecone.clients.Inference;
+ *      import io.pinecone.clients.Pinecone;
+ *      import org.openapitools.control.client.ApiException;
+ *      import org.openapitools.control.client.model.EmbeddingsList;
+ *
+ *      import java.util.ArrayList;
+ *      import java.util.HashMap;
+ *      import java.util.List;
+ *      import java.util.Map;
+ *
+ *     // Prepare input sentences to be embedded
+ *     List inputs = new ArrayList<>(1);
+ *     inputs.add("The quick brown fox jumps over the lazy dog.");
+ *     inputs.add("Lorem ipsum");
+ *
+ *     // Specify the embedding model and parameters
+ *     String embeddingModel = "multilingual-e5-large";
+ *
+ *     Map parameters = new HashMap<>();
+ *     parameters.put("input_type", "query");
+ *     parameters.put("truncate", "END");
+ *
+ *     // Generate embeddings for the input data
+ *     EmbeddingsList embeddings = inference.embed(embeddingModel, parameters, inputs);
+ * }
+ */ public class Inference { - InferenceApi inferenceApi; + private final InferenceApi inferenceApi; + /** + * Constructs an Inference instance. + * + * @param apiClient The ApiClient object used to configure the API connection. + */ public Inference(ApiClient apiClient) { inferenceApi = new InferenceApi(apiClient); } + /** + * Sends input data and parameters to the embedding model and returns a list of embeddings. + * + * @param model The embedding model to use. + * @param parameters A map containing model-specific parameters. + * @param inputs A list of input strings to generate embeddings for. + * @return EmbeddingsList containing the embeddings for the provided inputs. + * @throws ApiException If the API call fails, an ApiException is thrown. + */ public EmbeddingsList embed(String model, Map parameters, List inputs) throws ApiException { EmbedRequestParameters embedRequestParameters = new EmbedRequestParameters(); parameters.forEach(embedRequestParameters::putAdditionalProperty); @@ -33,6 +80,12 @@ public EmbeddingsList embed(String model, Map parameters, List convertInputStringToEmbedRequestInputsInner(List inputs) { return inputs.stream() .map(input -> new EmbedRequestInputsInner().text(input)) diff --git a/src/main/java/io/pinecone/clients/Pinecone.java b/src/main/java/io/pinecone/clients/Pinecone.java index d81ffd78..00788588 100644 --- a/src/main/java/io/pinecone/clients/Pinecone.java +++ b/src/main/java/io/pinecone/clients/Pinecone.java @@ -871,6 +871,14 @@ public AsyncIndex getAsyncIndexConnection(String indexName) throws PineconeValid return new AsyncIndex(connection, indexName); } + /** + * A method to create and return a new instance of the {@link Inference} client. + *

+ * This method initializes the Inference client using the current ApiClient + * from the {@link ManageIndexesApi}. The Inference client can then be used + * to interact with Pinecone's embedding API. + * @return A new {@link Inference} client instance. + */ public Inference getInferenceClient() { return new Inference(manageIndexesApi.getApiClient()); } From 5a5b30680ddbb3fe00fab22692a787e746f510b9 Mon Sep 17 00:00:00 2001 From: rohanshah18 Date: Tue, 24 Sep 2024 14:06:10 -0400 Subject: [PATCH 4/5] update docstrings and method name --- README.md | 17 ++++- .../integration/inference/EmbedTest.java | 14 +--- .../java/io/pinecone/clients/Inference.java | 75 +++++++++++-------- .../java/io/pinecone/clients/Pinecone.java | 7 +- 4 files changed, 68 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index f437ac5d..b93c1029 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,11 @@ The `Pinecone` class is your main entry point into the Pinecone Java SDK. You ca your `apiKey`, either by passing it as an argument in your code or by setting it as an environment variable called `PINECONE_API_KEY`. -Note: for pod-based indexes, you will also need an `environment` variable. You can set pass this as an argument in -your code or set it as an environment variable called `PINECONE_ENVIRONMENT`. +This internally instantiates a single shared OkHttpClient instance, which is used for both control plane and inference +operations. Note that the OkHttpClient performs best when you create a single `OkHttpClient` instance and reuse it +for all of your HTTP calls. This is because each client holds its own connection pool and thread pools. Reusing +connections and threads reduces latency and saves memory. Conversely, creating a client for each request wastes +resources on idle pools. More details on the OkHttpClient can be found [here](https://github.com/square/okhttp/blob/f2771425cb714a5b0b27238bd081b2516b4d640f/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt#L54). ```java import io.pinecone.clients.Pinecone; @@ -550,7 +553,9 @@ The Pinecone SDK now supports creating embeddings via the [Inference API](https: import io.pinecone.clients.Inference; import io.pinecone.clients.Pinecone; import org.openapitools.control.client.ApiException; +import org.openapitools.control.client.model.Embedding; import org.openapitools.control.client.model.EmbeddingsList; +import org.openapitools.control.client.model.EmbeddingsListUsage; import java.util.ArrayList; import java.util.HashMap; @@ -572,6 +577,14 @@ parameters.put("input_type", "query"); parameters.put("truncate", "END"); EmbeddingsList embeddings = inference.embed(embeddingModel, parameters, inputs); +// Get model +String embeddingsModel = embeddings.getModel(); + +// Get embedded data +List embeddedData = embeddings.getData(); + +// Get total usage tokens +int tokens = embeddings.getUsage().getTotalTokens(); ``` ## Examples diff --git a/src/integration/java/io/pinecone/integration/inference/EmbedTest.java b/src/integration/java/io/pinecone/integration/inference/EmbedTest.java index 6c08893a..0eecbfb6 100644 --- a/src/integration/java/io/pinecone/integration/inference/EmbedTest.java +++ b/src/integration/java/io/pinecone/integration/inference/EmbedTest.java @@ -31,18 +31,12 @@ public void testGenerateEmbeddings() throws ApiException { Map parameters = new HashMap<>(); parameters.put("input_type", "query"); parameters.put("truncate", "END"); - EmbeddingsList embeddings = inference.embed(embeddingModel, parameters, inputs); - try { - assertNotNull(embeddings, "Expected embedding to be not null"); - Assertions.assertEquals(embeddingModel, embeddings.getModel()); - - Assertions.assertEquals(1024, embeddings.getData().get(0).getValues().size()); - Assertions.assertEquals(2, embeddings.getData().size()); - } catch (Exception e) { - Assertions.fail("Embedding request should not have thrown an exception: " + e.getMessage()); - } + assertNotNull(embeddings, "Expected embedding to be not null"); + Assertions.assertEquals(embeddingModel, embeddings.getModel()); + Assertions.assertEquals(1024, embeddings.getData().get(0).getValues().size()); + Assertions.assertEquals(2, embeddings.getData().size()); } @Test diff --git a/src/main/java/io/pinecone/clients/Inference.java b/src/main/java/io/pinecone/clients/Inference.java index 88800f37..6cd7ae68 100644 --- a/src/main/java/io/pinecone/clients/Inference.java +++ b/src/main/java/io/pinecone/clients/Inference.java @@ -16,35 +16,10 @@ * The Inference class provides methods to interact with Pinecone's embedding API through the Java SDK. It allows users * to send input data to generate embeddings using a specified model. *

- * This class utilizes the {@link InferenceApi} to make API calls to the Pinecone embedding service. + * This class utilizes the {@link InferenceApi} to make API calls to the Pinecone inference service. * - *

{@code
- *      import io.pinecone.clients.Inference;
- *      import io.pinecone.clients.Pinecone;
- *      import org.openapitools.control.client.ApiException;
- *      import org.openapitools.control.client.model.EmbeddingsList;
- *
- *      import java.util.ArrayList;
- *      import java.util.HashMap;
- *      import java.util.List;
- *      import java.util.Map;
- *
- *     // Prepare input sentences to be embedded
- *     List inputs = new ArrayList<>(1);
- *     inputs.add("The quick brown fox jumps over the lazy dog.");
- *     inputs.add("Lorem ipsum");
- *
- *     // Specify the embedding model and parameters
- *     String embeddingModel = "multilingual-e5-large";
- *
- *     Map parameters = new HashMap<>();
- *     parameters.put("input_type", "query");
- *     parameters.put("truncate", "END");
- *
- *     // Generate embeddings for the input data
- *     EmbeddingsList embeddings = inference.embed(embeddingModel, parameters, inputs);
- * }
*/ + public class Inference { private final InferenceApi inferenceApi; @@ -61,21 +36,57 @@ public Inference(ApiClient apiClient) { /** * Sends input data and parameters to the embedding model and returns a list of embeddings. * - * @param model The embedding model to use. + * @param model The embedding model to use. * @param parameters A map containing model-specific parameters. - * @param inputs A list of input strings to generate embeddings for. + * @param inputs A list of input strings to generate embeddings for. * @return EmbeddingsList containing the embeddings for the provided inputs. * @throws ApiException If the API call fails, an ApiException is thrown. + * + *
{@code
+     *      import io.pinecone.clients.Inference;
+     *      import io.pinecone.clients.Pinecone;
+     *      import org.openapitools.control.client.ApiException;
+     *      import org.openapitools.control.client.model.EmbeddingsList;
+     *
+     *      import java.util.ArrayList;
+     *      import java.util.HashMap;
+     *      import java.util.List;
+     *      import java.util.Map;
+     *
+     *     // Prepare input sentences to be embedded
+     *      List inputs = List.of(
+     *          "The quick brown fox jumps over the lazy dog.",
+     *          "Lorem ipsum"
+     *      );
+     *     // Specify the embedding model and parameters
+     *     String embeddingModel = "multilingual-e5-large";
+     *
+     *     Map parameters = Map.of(
+     *        "input_type", "query",
+     *        "truncate", "END"
+     *     );
+     *
+     *     // Generate embeddings for the input data
+     *     EmbeddingsList embeddings = inference.embed(embeddingModel, parameters, inputs);
+     *
+     *     // Get model
+     *     String embeddingsModel = embeddings.getModel();
+     *
+     *     // Get embedded data
+     *     List embeddedData = embeddings.getData();
+     *
+     *     // Get total usage tokens
+     *     int tokens = embeddings.getUsage().getTotalTokens();
+     * } 
*/ public EmbeddingsList embed(String model, Map parameters, List inputs) throws ApiException { EmbedRequestParameters embedRequestParameters = new EmbedRequestParameters(); parameters.forEach(embedRequestParameters::putAdditionalProperty); - List EmbedRequestInputsInnerList = convertInputStringToEmbedRequestInputsInner(inputs); EmbedRequest embedRequest = new EmbedRequest() .model(model) .parameters(embedRequestParameters) - .inputs(EmbedRequestInputsInnerList); + .inputs(convertToEmbedInputs(inputs)); return inferenceApi.embed(embedRequest); } @@ -86,7 +97,7 @@ public EmbeddingsList embed(String model, Map parameters, List convertInputStringToEmbedRequestInputsInner(List inputs) { + private List convertToEmbedInputs(List inputs) { return inputs.stream() .map(input -> new EmbedRequestInputsInner().text(input)) .collect(Collectors.toList()); diff --git a/src/main/java/io/pinecone/clients/Pinecone.java b/src/main/java/io/pinecone/clients/Pinecone.java index 00788588..d4588a32 100644 --- a/src/main/java/io/pinecone/clients/Pinecone.java +++ b/src/main/java/io/pinecone/clients/Pinecone.java @@ -18,7 +18,12 @@ /** * The Pinecone class is the main entry point for interacting with Pinecone via the Java SDK. - * It is used to create, delete, and manage your indexes and collections. + * It is used to create, delete, and manage your indexes and collections, along with the inference api. + * Note that the Pinecone class instantiates a single shared {@link OkHttpClient} instance, + * which is used for both control plane and inference operations.The OkHttpClient performs best when you create a single + * `OkHttpClient` instance and reuse it for all of your HTTP calls. This is because each client holds its own connection + * pool and thread pools. Reusing connections and threads reduces latency and saves memory. Conversely, creating a + * client for each request wastes resources on idle pools. *

* To instantiate the Pinecone class, use the {@link Pinecone.Builder} class to pass * an API key and any other optional configuration. From b594711ab9d83ab136c9c1e01f31e0920bfaa7a1 Mon Sep 17 00:00:00 2001 From: rohanshah18 Date: Wed, 25 Sep 2024 12:40:50 -0400 Subject: [PATCH 5/5] clean up docstrings and examples --- README.md | 17 +++----- .../java/io/pinecone/clients/Inference.java | 41 +------------------ .../java/io/pinecone/clients/Pinecone.java | 4 +- 3 files changed, 10 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index b93c1029..e0f427be 100644 --- a/README.md +++ b/README.md @@ -550,44 +550,39 @@ pinecone.deleteCollection("example-collection"); The Pinecone SDK now supports creating embeddings via the [Inference API](https://docs.pinecone.io/guides/inference/understanding-inference). ```java -import io.pinecone.clients.Inference; import io.pinecone.clients.Pinecone; import org.openapitools.control.client.ApiException; import org.openapitools.control.client.model.Embedding; import org.openapitools.control.client.model.EmbeddingsList; -import org.openapitools.control.client.model.EmbeddingsListUsage; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; ... - + Pinecone pinecone = new Pinecone.Builder("PINECONE_API_KEY").build(); Inference inference = pinecone.getInferenceClient(); -List inputs = new ArrayList<>(1); +// Prepare input sentences to be embedded +List inputs = new ArrayList<>(); inputs.add("The quick brown fox jumps over the lazy dog."); inputs.add("Lorem ipsum"); +// Specify the embedding model and parameters String embeddingModel = "multilingual-e5-large"; Map parameters = new HashMap<>(); parameters.put("input_type", "query"); parameters.put("truncate", "END"); +// Generate embeddings for the input data EmbeddingsList embeddings = inference.embed(embeddingModel, parameters, inputs); -// Get model -String embeddingsModel = embeddings.getModel(); // Get embedded data List embeddedData = embeddings.getData(); - -// Get total usage tokens -int tokens = embeddings.getUsage().getTotalTokens(); ``` ## Examples -- The data and control plane operation examples can be found in `io/pinecone/integration` folder. -- A full end-to-end Semantic Search example can be found in the [Java Examples](https://github.com/pinecone-io/java-examples/tree/main) repo on Github. \ No newline at end of file +- The data and control plane operation examples can be found in `io/pinecone/integration` folder. \ No newline at end of file diff --git a/src/main/java/io/pinecone/clients/Inference.java b/src/main/java/io/pinecone/clients/Inference.java index 6cd7ae68..3fc500d2 100644 --- a/src/main/java/io/pinecone/clients/Inference.java +++ b/src/main/java/io/pinecone/clients/Inference.java @@ -13,7 +13,7 @@ import java.util.stream.Collectors; /** - * The Inference class provides methods to interact with Pinecone's embedding API through the Java SDK. It allows users + * The Inference class provides methods to interact with Pinecone's inference API through the Java SDK. It allows users * to send input data to generate embeddings using a specified model. *

* This class utilizes the {@link InferenceApi} to make API calls to the Pinecone inference service. @@ -25,7 +25,7 @@ public class Inference { private final InferenceApi inferenceApi; /** - * Constructs an Inference instance. + * Constructs an instance of {@link Inference} class. * * @param apiClient The ApiClient object used to configure the API connection. */ @@ -41,43 +41,6 @@ public Inference(ApiClient apiClient) { * @param inputs A list of input strings to generate embeddings for. * @return EmbeddingsList containing the embeddings for the provided inputs. * @throws ApiException If the API call fails, an ApiException is thrown. - * - *

{@code
-     *      import io.pinecone.clients.Inference;
-     *      import io.pinecone.clients.Pinecone;
-     *      import org.openapitools.control.client.ApiException;
-     *      import org.openapitools.control.client.model.EmbeddingsList;
-     *
-     *      import java.util.ArrayList;
-     *      import java.util.HashMap;
-     *      import java.util.List;
-     *      import java.util.Map;
-     *
-     *     // Prepare input sentences to be embedded
-     *      List inputs = List.of(
-     *          "The quick brown fox jumps over the lazy dog.",
-     *          "Lorem ipsum"
-     *      );
-     *     // Specify the embedding model and parameters
-     *     String embeddingModel = "multilingual-e5-large";
-     *
-     *     Map parameters = Map.of(
-     *        "input_type", "query",
-     *        "truncate", "END"
-     *     );
-     *
-     *     // Generate embeddings for the input data
-     *     EmbeddingsList embeddings = inference.embed(embeddingModel, parameters, inputs);
-     *
-     *     // Get model
-     *     String embeddingsModel = embeddings.getModel();
-     *
-     *     // Get embedded data
-     *     List embeddedData = embeddings.getData();
-     *
-     *     // Get total usage tokens
-     *     int tokens = embeddings.getUsage().getTotalTokens();
-     * } 
*/ public EmbeddingsList embed(String model, Map parameters, List inputs) throws ApiException { EmbedRequestParameters embedRequestParameters = new EmbedRequestParameters(); diff --git a/src/main/java/io/pinecone/clients/Pinecone.java b/src/main/java/io/pinecone/clients/Pinecone.java index d4588a32..082336ce 100644 --- a/src/main/java/io/pinecone/clients/Pinecone.java +++ b/src/main/java/io/pinecone/clients/Pinecone.java @@ -880,8 +880,8 @@ public AsyncIndex getAsyncIndexConnection(String indexName) throws PineconeValid * A method to create and return a new instance of the {@link Inference} client. *

* This method initializes the Inference client using the current ApiClient - * from the {@link ManageIndexesApi}. The Inference client can then be used - * to interact with Pinecone's embedding API. + * from the {@link ManageIndexesApi}. The {@link Inference} client can then be used + * to interact with Pinecone's inference API. * @return A new {@link Inference} client instance. */ public Inference getInferenceClient() {