-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Problem Add embed endpoint for Java SDK to allow users to create embeddings for text data such as passage or query using a specified model. More details on inference api can be found [here](https://docs.pinecone.io/guides/inference/understanding-inference). ## Solution Users can now call embed endpoint with the following parameters: 1. `String model`: Accepts a string from the specified [models](https://docs.pinecone.io/models/overview) . 2. `Map<String, Object> parameters`: Accepts `input_type` and `truncate` as keys with their corresponding values in a Map. The values are expected to be scalar. Please note that the default value of `truncate` is set to `END` if not specified. 3. `List<String> inputs`: The list must be of size atleast 1. As a part of this change, I have added a `getInferenceClient()` in `Pinecone` class to follow a similar pattern with other SDKs. The underlying client uses `OkHTTPClient` for REST calls. The method `getInferenceClient()` returns an instance of `Inference` class which I added as a wrapper to the Inference API. So far, this wrapper contains `embed()` endpoint only, with the plan of adding `rerank()` soon. Lastly, I have added docstrings and updated README with an example of the embed endpoint. ## Type of Change - [X] New feature (non-breaking change which adds functionality) ## Test Plan Added integration tests.
- Loading branch information
1 parent
27ea488
commit 6f43b93
Showing
4 changed files
with
186 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/integration/java/io/pinecone/integration/inference/EmbedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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<String> 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<String, Object> parameters = new HashMap<>(); | ||
parameters.put("input_type", "query"); | ||
parameters.put("truncate", "END"); | ||
EmbeddingsList embeddings = inference.embed(embeddingModel, parameters, inputs); | ||
|
||
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 | ||
public void testGenerateEmbeddingsInvalidInputs() throws ApiException { | ||
String embeddingModel = "multilingual-e5-large"; | ||
List<String> inputs = new ArrayList<>(); | ||
Map<String, Object> parameters = new HashMap<>(); | ||
parameters.put("input_type", "query"); | ||
parameters.put("truncate", "END"); | ||
|
||
Exception exception = assertThrows(Exception.class, () -> { | ||
inference.embed(embeddingModel, parameters, inputs); | ||
}); | ||
|
||
Assertions.assertTrue(exception.getMessage().contains("Must specify at least one input")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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.Map; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* 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. | ||
* <p> | ||
* This class utilizes the {@link InferenceApi} to make API calls to the Pinecone inference service. | ||
* | ||
*/ | ||
|
||
public class Inference { | ||
|
||
private final InferenceApi inferenceApi; | ||
|
||
/** | ||
* Constructs an instance of {@link Inference} class. | ||
* | ||
* @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<String, Object> parameters, List<String> inputs) throws ApiException { | ||
EmbedRequestParameters embedRequestParameters = new EmbedRequestParameters(); | ||
parameters.forEach(embedRequestParameters::putAdditionalProperty); | ||
|
||
EmbedRequest embedRequest = new EmbedRequest() | ||
.model(model) | ||
.parameters(embedRequestParameters) | ||
.inputs(convertToEmbedInputs(inputs)); | ||
|
||
return inferenceApi.embed(embedRequest); | ||
} | ||
|
||
/** | ||
* Converts a list of input strings to EmbedRequestInputsInner objects. | ||
* | ||
* @param inputs A list of input strings. | ||
* @return A list of EmbedRequestInputsInner objects containing the input data. | ||
*/ | ||
private List<EmbedRequestInputsInner> convertToEmbedInputs(List<String> inputs) { | ||
return inputs.stream() | ||
.map(input -> new EmbedRequestInputsInner().text(input)) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters