Skip to content
Open
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
6 changes: 3 additions & 3 deletions PACKAGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ A BOM is provided that can be used to define the versions of all Semantic Kernel
: Provides a connector that can be used to interact with the OpenAI API.

`semantickernel-aiservices-voyageai`
: Provides connectors for VoyageAI's embedding and reranking services, including text embeddings, contextualized embeddings, multimodal embeddings, and document reranking.
: Provides connectors for VoyageAI by MongoDB's embedding and reranking services, including text embeddings (e.g. voyage-4-large, voyage-4, voyage-code-4), contextualized embeddings (voyage-context-4), multimodal embeddings (voyage-multimodal-3.5), and document reranking (rerank-2.5).

## Example Configurations

Expand Down Expand Up @@ -75,9 +75,9 @@ POM XML for a simple project that uses OpenAI.
</project>
```

### Example: VoyageAI Embeddings and Reranking
### Example: VoyageAI by MongoDB Embeddings and Reranking

POM XML for a project that uses VoyageAI for embeddings and reranking.
POM XML for a project that uses VoyageAI by MongoDB for embeddings and reranking.

```xml

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import java.util.stream.Collectors;

/**
* VoyageAI contextualized embedding generation service.
* VoyageAI by MongoDB contextualized embedding generation service.
* Generates embeddings that capture both local chunk details and global document-level metadata.
* Supports models like voyage-3.
* Supports voyage-context-4 (current) and voyage-context-3.
*/
public final class VoyageAIContextualizedEmbeddingGenerationService implements TextEmbeddingGenerationService {

Expand All @@ -35,7 +35,7 @@ public final class VoyageAIContextualizedEmbeddingGenerationService implements T
* Creates a new instance of VoyageAI contextualized embedding generation service.
*
* @param client VoyageAI client
* @param modelId Model ID (e.g., "voyage-3")
* @param modelId Model ID (e.g., "voyage-context-4")
* @param serviceId Optional service ID
*/
public VoyageAIContextualizedEmbeddingGenerationService(
Expand Down Expand Up @@ -66,9 +66,14 @@ public String getModelId() {
}

/**
* Generates contextualized embeddings for document chunks.
* Generates contextualized embeddings for pre-chunked documents.
*
* @param inputs List of lists where each inner list contains document chunks
* <p>Sends the inputs in the nested form of the official
* {@code inputs: Union[List[List[str]], List[str]]} specification, where each inner
* list holds one document's chunks and is embedded as a group so every chunk is
* encoded in the context of the others.
*
* @param inputs List of lists where each inner list contains one document's chunks
* @return A Mono containing a list of embeddings for all chunks across all documents
*/
public Mono<List<Embedding>> generateContextualizedEmbeddingsAsync(List<List<String>> inputs) {
Expand All @@ -84,6 +89,41 @@ public Mono<List<Embedding>> generateContextualizedEmbeddingsAsync(List<List<Str
request.setInputs(inputs);
request.setModel(modelId);

return sendRequest(request);
}

/**
* Generates contextualized embeddings from a flat list of full-document strings.
*
* <p>Sends the inputs in the flat form of the official
* {@code inputs: Union[List[List[str]], List[str]]} specification and enables
* server-side auto-chunking, so each document is split and every resulting chunk is
* embedded with document-level context. Per the API contract the flat, document-typed
* form requires auto-chunking, so {@code input_type} is set to {@code document} and
* {@code enable_auto_chunking} to {@code true}.
*
* @param documents flat list of full-document strings
* @return A Mono containing a list of embeddings for all chunks across all documents
*/
public Mono<List<Embedding>> generateContextualizedEmbeddingsForDocumentsAsync(List<String> documents) {
if (documents == null || documents.isEmpty()) {
return Mono.just(Collections.emptyList());
}

LOGGER.debug("Generating contextualized embeddings for {} documents (auto-chunking) using model {}",
documents.size(), modelId);

VoyageAIModels.ContextualizedEmbeddingRequest request =
new VoyageAIModels.ContextualizedEmbeddingRequest();
request.setFlatInputs(documents);
request.setModel(modelId);
request.setInputType("document");
request.setEnableAutoChunking(true);

return sendRequest(request);
}

private Mono<List<Embedding>> sendRequest(VoyageAIModels.ContextualizedEmbeddingRequest request) {
return client.sendRequestAsync(
"contextualizedembeddings",
request,
Expand Down Expand Up @@ -173,7 +213,7 @@ public Builder withClient(VoyageAIClient client) {
/**
* Sets the model ID.
*
* @param modelId Model ID (e.g., "voyage-3")
* @param modelId Model ID (e.g., "voyage-context-4")
* @return This builder
*/
public Builder withModelId(String modelId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.concurrent.TimeUnit;

/**
* HTTP client for VoyageAI API.
* HTTP client for the VoyageAI by MongoDB API.
*/
public final class VoyageAIClient {
private static final Logger LOGGER = LoggerFactory.getLogger(VoyageAIClient.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;

/**
* VoyageAI API request and response models.
* VoyageAI by MongoDB API request and response models.
*/
public class VoyageAIModels {

Expand Down Expand Up @@ -122,6 +122,7 @@ public void setUsage(EmbeddingUsage usage) {
/**
* Embedding data item.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public static class EmbeddingDataItem {
@JsonProperty("object")
private String object;
Expand Down Expand Up @@ -275,6 +276,7 @@ public void setUsage(EmbeddingUsage usage) {
/**
* Rerank data item.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public static class RerankDataItem {
@JsonProperty("index")
private int index;
Expand Down Expand Up @@ -303,11 +305,18 @@ public void setRelevanceScore(double relevanceScore) {

/**
* Request model for contextualized embeddings.
*
* <p>The VoyageAI API accepts {@code inputs} as either a nested list of chunks
* ({@code List<List<String>>}, one inner list per document) or a flat list of
* full-document strings ({@code List<String>}), matching the official
* {@code inputs: Union[List[List[str]], List[str]]} specification. The flat form
* is used together with server-side auto-chunking.
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class ContextualizedEmbeddingRequest {
// Holds either List<List<String>> (nested chunks) or List<String> (flat documents).
@JsonProperty("inputs")
private List<List<String>> inputs;
private Object inputs;

@JsonProperty("model")
private String model;
Expand All @@ -324,16 +333,41 @@ public static class ContextualizedEmbeddingRequest {
@JsonProperty("output_dtype")
private String outputDtype;

@JsonProperty("enable_auto_chunking")
private Boolean enableAutoChunking;

@SuppressFBWarnings("EI_EXPOSE_REP")
public List<List<String>> getInputs() {
public Object getInputs() {
return inputs;
}

@SuppressFBWarnings("EI_EXPOSE_REP2")
/**
* Sets the inputs as a nested list of chunks, one inner list per document.
*
* @param inputs nested list of document chunks
*/
public void setInputs(List<List<String>> inputs) {
this.inputs = inputs;
}

/**
* Sets the inputs as a flat list of full-document strings. Use together with
* server-side auto-chunking (see {@link #setEnableAutoChunking(Boolean)}).
*
* @param inputs flat list of full-document strings
*/
public void setFlatInputs(List<String> inputs) {
this.inputs = inputs;
}

public Boolean getEnableAutoChunking() {
return enableAutoChunking;
}

public void setEnableAutoChunking(Boolean enableAutoChunking) {
this.enableAutoChunking = enableAutoChunking;
}

public String getModel() {
return model;
}
Expand Down Expand Up @@ -435,6 +469,7 @@ public void setEmbeddings(List<EmbeddingItem> embeddings) {
/**
* Embedding item with chunk information.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public static class EmbeddingItem {
@JsonProperty("embedding")
private float[] embedding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import java.util.stream.Collectors;

/**
* VoyageAI multimodal embedding generation service.
* VoyageAI by MongoDB multimodal embedding generation service.
* Generates embeddings for text, images, or interleaved text and images.
* Supports the voyage-multimodal-3 model.
* Supports voyage-multimodal-3.5 (current) and voyage-multimodal-3.
* <p>
* Constraints:
* - Maximum 1,000 inputs per request
Expand All @@ -41,7 +41,7 @@ public final class VoyageAIMultimodalEmbeddingGenerationService implements TextE
* Creates a new instance of VoyageAI multimodal embedding generation service.
*
* @param client VoyageAI client
* @param modelId Model ID (e.g., "voyage-multimodal-3")
* @param modelId Model ID (e.g., "voyage-multimodal-3.5")
* @param serviceId Optional service ID
*/
public VoyageAIMultimodalEmbeddingGenerationService(
Expand Down Expand Up @@ -196,7 +196,7 @@ public Builder withClient(VoyageAIClient client) {
/**
* Sets the model ID.
*
* @param modelId Model ID (e.g., "voyage-multimodal-3")
* @param modelId Model ID (e.g., "voyage-multimodal-3.5")
* @return This builder
*/
public Builder withModelId(String modelId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import java.util.stream.Collectors;

/**
* VoyageAI implementation of {@link TextRerankingService}.
* Supports models like rerank-2, rerank-2-lite.
* VoyageAI by MongoDB implementation of {@link TextRerankingService}.
* Supports current models such as rerank-2.5 and rerank-2.5-lite (and preview models
* rerank-3, rerank-3-lite), as well as older models like rerank-2 and rerank-2-lite.
*/
public final class VoyageAITextRerankingService implements TextRerankingService {

Expand All @@ -33,7 +34,7 @@ public final class VoyageAITextRerankingService implements TextRerankingService
* Creates a new instance of VoyageAI text reranking service.
*
* @param client VoyageAI client
* @param modelId Model ID (e.g., "rerank-2")
* @param modelId Model ID (e.g., "rerank-2.5")
* @param serviceId Optional service ID
* @param topK Optional top K results to return
*/
Expand Down Expand Up @@ -140,7 +141,7 @@ public Builder withClient(VoyageAIClient client) {
/**
* Sets the model ID.
*
* @param modelId Model ID (e.g., "rerank-2")
* @param modelId Model ID (e.g., "rerank-2.5")
* @return This builder
*/
public Builder withModelId(String modelId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.util.stream.Collectors;

/**
* VoyageAI implementation of {@link TextEmbeddingGenerationService}.
* Supports models like voyage-3-large, voyage-3.5, voyage-code-3, voyage-finance-2, voyage-law-2.
* VoyageAI by MongoDB implementation of {@link TextEmbeddingGenerationService}.
* Supports current models such as voyage-4-large, voyage-4, voyage-4-lite, voyage-code-4,
* voyage-finance-2 and voyage-law-2, as well as older models like voyage-3-large,
* voyage-3.5, voyage-3.5-lite and voyage-code-3.
*/
public final class VoyageAITextEmbeddingGenerationService implements TextEmbeddingGenerationService {

Expand All @@ -33,7 +35,7 @@ public final class VoyageAITextEmbeddingGenerationService implements TextEmbeddi
* Creates a new instance of VoyageAI text embedding generation service.
*
* @param client VoyageAI client
* @param modelId Model ID (e.g., "voyage-3-large")
* @param modelId Model ID (e.g., "voyage-4-large")
* @param serviceId Optional service ID
*/
public VoyageAITextEmbeddingGenerationService(
Expand Down Expand Up @@ -143,7 +145,7 @@ public Builder withClient(VoyageAIClient client) {
/**
* Sets the model ID.
*
* @param modelId Model ID (e.g., "voyage-3-large")
* @param modelId Model ID (e.g., "voyage-4-large")
* @return This builder
*/
public Builder withModelId(String modelId) {
Expand Down
Loading