-
Notifications
You must be signed in to change notification settings - Fork 13
Add embed endpoint #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add embed endpoint #153
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8dda099
add embed endpoint
rohanshah18 900bad8
update EmbeddingRequestParameter
rohanshah18 67084ee
add docstrings and update README
rohanshah18 5a5b306
update docstrings and method name
rohanshah18 b594711
clean up docstrings and examples
rohanshah18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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) { | ||
rohanshah18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
rohanshah18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public EmbeddingsList embed(String model, Map<String, Object> parameters, List<String> inputs) throws ApiException { | ||
rohanshah18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
rohanshah18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param inputs A list of input strings. | ||
rohanshah18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.