Skip to content
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

Draft: add embed endpoint #151

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.pinecone.integration.inference;

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.List;

public class EmbedTest {
public static void main(String[] args) throws ApiException {
Pinecone pinecone = new Pinecone.Builder("PINECONE_API_KEY").build();
Inference inference = pinecone.getInferenceClient();
List<String> inputs = new ArrayList<>(1);
inputs.add("The quick brown fox jumps over the lazy dog.");
// passing null for truncate should default to END
EmbeddingsList list = inference.embed("model", null, "someInputType", inputs);
}
}
41 changes: 41 additions & 0 deletions src/main/java/io/pinecone/clients/Inference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.pinecone.clients;

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;

public class Inference {
InferenceApi inferenceApi;

public Inference() {
inferenceApi = new InferenceApi();
}

// ToDo: Add serialization unit test for checking if truncate and input_type are correctly serialized when passed via map
// Cond1: when input_type and truncate are set -> both should have the set values especially truncate's default value END should be overwritten
// Cond2: when only input_type is set, truncate should be by default set to END
public EmbeddingsList embed(String model, Map<String, Object> parameters, List<String> inputs) throws ApiException {
EmbedRequestParameters embedRequestParameters = new EmbedRequestParameters();
parameters.forEach(embedRequestParameters::putAdditionalProperty);
List<EmbedRequestInputsInner> EmbedRequestInputsInnerList = convertInputStringToEmbedRequestInputsInner(inputs);
EmbedRequest embedRequest = new EmbedRequest()
.model(model)
.parameters(embedRequestParameters)
.inputs(EmbedRequestInputsInnerList);

return inferenceApi.embed(embedRequest);
}

private List<EmbedRequestInputsInner> convertInputStringToEmbedRequestInputsInner(List<String> inputs) {
return inputs.stream()
.map(input -> new EmbedRequestInputsInner().text(input))
.collect(Collectors.toList());
}
}
4 changes: 4 additions & 0 deletions src/main/java/io/pinecone/clients/Pinecone.java
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,10 @@ public AsyncIndex getAsyncIndexConnection(String indexName) throws PineconeValid
return new AsyncIndex(connection, indexName);
}

public Inference getInferenceClient() {
return new Inference();
}

PineconeConnection getConnection(String indexName) {
return connectionsMap.computeIfAbsent(indexName, key -> new PineconeConnection(config));
}
Expand Down
Loading