Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Upgraded to openai sdk 1.0.0-beta.11
- Added convenience method `FunctionInvocation.withResultTypeAutoConversion` which sets the return type and registers a
type converter based on Jackson for the return type.
- Added localization support for error/debug messages

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ in a different direction, but also to consider the impact on the larger ecosyste
To learn more and get started:

- Read the [documentation](https://learn.microsoft.com/en-us/semantic-kernel/overview/?tabs=Java&pivots=programming-language-java)
- Learn how to [contribute](https://learn.microsoft.com/en-us/semantic-kernel/get-started/contributing?tabs=Java&pivots=programming-language-java) to the project
- Learn how to [contribute](https://learn.microsoft.com/en-us/semantic-kernel/support/contributing?tabs=Java&pivots=programming-language-java) to the project
- Join the [Discord community](https://aka.ms/SKDiscord)
- Attend [regular office hours and SK community events](COMMUNITY.md)
- Follow the team on our [blog](https://aka.ms/sk/blog)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,37 @@
import com.microsoft.semantickernel.aiservices.openai.OpenAiService;
import com.microsoft.semantickernel.exceptions.AIException;
import com.microsoft.semantickernel.services.openai.OpenAiServiceBuilder;
import com.microsoft.semantickernel.services.textcompletion.TextGenerationService;
import com.microsoft.semantickernel.services.textembedding.Embedding;
import com.microsoft.semantickernel.services.textembedding.TextEmbeddingGenerationService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

/**
* An OpenAI implementation of a {@link TextEmbeddingGenerationService}.
*
*/
public class OpenAITextEmbeddingGenerationService extends OpenAiService<OpenAIAsyncClient>
implements TextEmbeddingGenerationService {

private static final Logger LOGGER = LoggerFactory
.getLogger(OpenAITextEmbeddingGenerationService.class);
private static final int DEFAULT_DIMENSIONS = 1536;
private final int dimensions;

public static final int EMBEDDING_DIMENSIONS_SMALL = 1536;
public static final int EMBEDDING_DIMENSIONS_LARGE = 3072;

/**
* Creates a new {@link OpenAITextEmbeddingGenerationService}.
*
* @param client OpenAI client
* @param client OpenAI client
* @param deploymentName deployment name
* @param modelId OpenAI model id
* @param serviceId Service id
* @param modelId OpenAI model id
* @param serviceId Service id
*/
public OpenAITextEmbeddingGenerationService(
OpenAIAsyncClient client,
Expand All @@ -57,6 +59,24 @@ public static Builder builder() {
return new Builder();
}

/**
* Generates embeddings for the given data.
*
* @param data The data to generate embeddings for.
* @return A Mono that completes with the embeddings.
*/
@Override
public Mono<Embedding> generateEmbeddingAsync(String data) {
return this.internalGenerateTextEmbeddingsAsync(Arrays.asList(data))
.flatMap(embeddings -> {
if (embeddings.isEmpty()) {
return Mono.empty();
}

return Mono.just(embeddings.get(0));
});
}

/**
* Generates embeddings for the given data.
*
Expand Down Expand Up @@ -88,6 +108,7 @@ protected Mono<List<Embedding>> internalGenerateTextEmbeddingsAsync(List<String>
*/
public static class Builder extends
OpenAiServiceBuilder<OpenAIAsyncClient, OpenAITextEmbeddingGenerationService, OpenAITextEmbeddingGenerationService.Builder> {

private int dimensions = DEFAULT_DIMENSIONS;

/**
Expand Down
8 changes: 8 additions & 0 deletions api-test/integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@
<artifactId>jsonschema-module-jackson</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.7.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.microsoft.semantickernel.data.vectorstorage.attributes.VectorStoreRecordDataAttribute;
import com.microsoft.semantickernel.data.vectorstorage.attributes.VectorStoreRecordKeyAttribute;
import com.microsoft.semantickernel.data.vectorstorage.attributes.VectorStoreRecordVectorAttribute;
import com.microsoft.semantickernel.data.vectorstorage.definition.DistanceFunction;

import java.util.List;

Expand All @@ -24,19 +25,19 @@ public class Hotel {
private final String description;

@JsonProperty("summaryEmbedding1")
@VectorStoreRecordVectorAttribute(dimensions = 8, distanceFunction = "euclidean")
@VectorStoreRecordVectorAttribute(dimensions = 8, distanceFunction = DistanceFunction.EUCLIDEAN_DISTANCE)
private final List<Float> euclidean;

@JsonProperty("summaryEmbedding2")
@VectorStoreRecordVectorAttribute(dimensions = 8, distanceFunction = "cosineDistance")
@VectorStoreRecordVectorAttribute(dimensions = 8, distanceFunction = DistanceFunction.COSINE_DISTANCE)
private final List<Float> cosineDistance;

@JsonProperty("summaryEmbedding3")
@VectorStoreRecordVectorAttribute(dimensions = 8, distanceFunction = "dotProduct")
@VectorStoreRecordVectorAttribute(dimensions = 8, distanceFunction = DistanceFunction.DOT_PRODUCT)
private final List<Float> dotProduct;

@JsonProperty("indexedSummaryEmbedding")
@VectorStoreRecordVectorAttribute(dimensions = 8, indexKind = "hnsw", distanceFunction = "euclidean")
@VectorStoreRecordVectorAttribute(dimensions = 8, indexKind = "hnsw", distanceFunction = DistanceFunction.EUCLIDEAN_DISTANCE)
private final List<Float> indexedEuclidean;
@VectorStoreRecordDataAttribute
private double rating;
Expand Down
Loading