Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.aiservices.openai.textembedding;

import com.azure.ai.openai.OpenAIAsyncClient;
import com.azure.ai.openai.models.EmbeddingItem;
import com.azure.ai.openai.models.Embeddings;
import com.azure.ai.openai.models.EmbeddingsOptions;
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 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
implements TextEmbeddingGenerationService {
private static final Logger LOGGER = LoggerFactory
.getLogger(OpenAITextEmbeddingGenerationService.class);

/**
* Creates a new {@link OpenAITextEmbeddingGenerationService}.
*
* @param client OpenAI client
* @param deploymentName deployment name
* @param modelId OpenAI model id
* @param serviceId Service id
*/
public OpenAITextEmbeddingGenerationService(
OpenAIAsyncClient client,
String deploymentName,
String modelId,
@Nullable String serviceId) {
super(client, serviceId, modelId, deploymentName);
}

/**
* Creates a builder for creating a {@link OpenAITextEmbeddingGenerationService}.
*
* @return A new {@link OpenAITextEmbeddingGenerationService} builder.
*/
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<List<Embedding>> generateEmbeddingsAsync(List<String> data) {
return this.internalGenerateTextEmbeddingsAsync(data);
}

protected Mono<List<Embedding>> internalGenerateTextEmbeddingsAsync(List<String> data) {
EmbeddingsOptions options = new EmbeddingsOptions(data)
.setModel(getModelId())
.setInputType("string");

return getClient()
.getEmbeddings(getModelId(), options)
.flatMapIterable(Embeddings::getData)
.mapNotNull(EmbeddingItem::getEmbedding)
.map(ArrayList::new)
.mapNotNull(Embedding::new)
.collectList();
}

/**
* A builder for creating a {@link OpenAITextEmbeddingGenerationService}.
*/
public static class Builder extends
OpenAiServiceBuilder<OpenAITextEmbeddingGenerationService, OpenAITextEmbeddingGenerationService.Builder> {
@Override
public OpenAITextEmbeddingGenerationService build() {

if (this.client == null) {
throw new AIException(AIException.ErrorCodes.INVALID_REQUEST,
"OpenAI client must be provided");
}

if (this.modelId == null || modelId.isEmpty()) {
throw new AIException(AIException.ErrorCodes.INVALID_REQUEST,
"OpenAI model id must be provided");
}

if (deploymentName == null) {
LOGGER.debug("Deployment name is not provided, using model id as deployment name");
deploymentName = modelId;
}

return new OpenAITextEmbeddingGenerationService(client, deploymentName, modelId,
serviceId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public ChatCompletions getValue() {
return new ObjectMapper()
.readValue(String.format(message, "Snuggles"),
ChatCompletions.class);

} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@ public List<Integer> WithListReturn() {

@DefineKernelFunction(name = "WithListReturn2", returnType = "java.util.List")
public List<Integer> WithListReturn2() {
return List.of(1, 2, 3);
return List.of(4, 5, 6);
}

@DefineKernelFunction(name = "WithListReturn3", returnType = "java.util.List")
public Mono<List<Integer>> WithListReturn3() {
return Mono.just(List.of(1, 2, 3));
return Mono.just(List.of(7, 8, 9));
}

@DefineKernelFunction(name = "WithListReturn4", returnType = "java.util.List")
Expand All @@ -239,15 +239,11 @@ public void canHandleIterableReturnFromFunction() {
.build())
.block();

Assertions.assertTrue(
wm.getAllServeEvents().get(0).getRequest().getBodyAsString().equals(
"{\"messages\":["
+ "{\"role\":\"user\",\"content\":\"[]\"},"
+ "{\"role\":\"user\",\"content\":\"[1, 2, 3]\"},"
+ "{\"role\":\"user\",\"content\":\"[1, 2, 3]\"},"
+ "{\"role\":\"user\",\"content\":\"[1, 2, 3]\"},"
+ "{\"role\":\"user\",\"content\":\"[]\"}"
+ "],\"model\":\"gpt-35-turbo-2\"}"));
String requestBody = wm.getAllServeEvents().get(0).getRequest().getBodyAsString();
Assertions.assertTrue(requestBody.contains("{\"role\":\"user\",\"content\":\"[]"));
Assertions.assertTrue(requestBody.contains("{\"role\":\"user\",\"content\":\"[1, 2, 3]"));
Assertions.assertTrue(requestBody.contains("{\"role\":\"user\",\"content\":\"[4, 5, 6]"));
Assertions.assertTrue(requestBody.contains("{\"role\":\"user\",\"content\":\"[7, 8, 9]"));
}

private Kernel buildTextKernel() {
Expand Down
27 changes: 27 additions & 0 deletions connectors/memory/azure-ai-search/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-parent</artifactId>
<version>1.1.6-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>

<groupId>com.microsoft</groupId>
<artifactId>semantickernel-connectors-memory-azureaisearch</artifactId>

<dependencies>
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-api</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-search-documents</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.connectors.memory.azureaisearch;

import com.azure.search.documents.SearchAsyncClient;
import com.azure.search.documents.SearchDocument;
import com.azure.search.documents.indexes.SearchIndexAsyncClient;
import com.azure.search.documents.models.IndexDocumentsResult;
import com.azure.search.documents.models.IndexingResult;
import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.memory.VectorRecordStore;
import com.microsoft.semantickernel.memory.VectorStoreRecordMapper;
import com.microsoft.semantickernel.memory.recorddefinition.VectorStoreRecordDataField;
import com.microsoft.semantickernel.memory.recorddefinition.VectorStoreRecordDefinition;
import com.microsoft.semantickernel.memory.recordoptions.DeleteRecordOptions;
import com.microsoft.semantickernel.memory.recordoptions.GetRecordOptions;
import com.microsoft.semantickernel.memory.recordoptions.UpsertRecordOptions;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class AzureAISearchVectorRecordStore<Record> implements VectorRecordStore<String, Record> {
private final SearchIndexAsyncClient client;
private final Map<String, SearchAsyncClient> clientsByIndex = new ConcurrentHashMap<>();
private final AzureAISearchVectorStoreOptions<Record> options;
private final List<String> nonVectorFields = new ArrayList<>();

@SuppressFBWarnings("EI_EXPOSE_REP2")
Comment thread
milderhc marked this conversation as resolved.
Resolved
public AzureAISearchVectorRecordStore(
@Nonnull SearchIndexAsyncClient client,
@Nonnull AzureAISearchVectorStoreOptions<Record> options) {
this.client = client;

// If record definition is not provided, create one from the record class
if (options.getRecordDefinition() == null) {
this.options = AzureAISearchVectorStoreOptions.<Record>builder()
.withRecordClass(options.getRecordClass())
.withVectorStoreRecordMapper(options.getVectorStoreRecordMapper())
.withRecordDefinition(VectorStoreRecordDefinition.create(options.getRecordClass()))
.build();
} else {
this.options = options;
}

// Add non-vector fields to the list
nonVectorFields.add(this.options.getRecordDefinition().getKeyField().getName());
nonVectorFields.addAll(this.options.getRecordDefinition().getDataFields().stream()
.map(VectorStoreRecordDataField::getName)
.collect(Collectors.toList()));
}

private String resolveCollectionName(@Nullable String collectionName) {
if (collectionName != null) {
return collectionName;
}
if (options.getDefaultCollectionName() != null) {
return options.getDefaultCollectionName();
}
throw new SKException("A collection name is required");
}

@Override
public Mono<Record> getAsync(
@Nonnull String key, GetRecordOptions options) {
String indexName = resolveCollectionName(
options != null ? options.getCollectionName() : null);
SearchAsyncClient client = this.getSearchClient(indexName);

// If vectors are not requested, only fetch non-vector fields
List<String> selectedFields = null;
if (options != null && !options.includeVectors()) {
selectedFields = Collections.unmodifiableList(nonVectorFields);
}

VectorStoreRecordMapper<Record, SearchDocument> mapper = this.options
.getVectorStoreRecordMapper();

// Use custom mapper if available
if (mapper != null && mapper.getToRecordMapper() != null) {
return client.getDocument(key, SearchDocument.class)
.map(this.options.getVectorStoreRecordMapper()::toRecord);
}

return client.getDocumentWithResponse(key, this.options.getRecordClass(), selectedFields)
.map(response -> {
if (response.getStatusCode() == 404) {
throw new SKException("Record not found: " + key);
}
return response.getValue();
});

}

@Override
public Mono<Collection<Record>> getBatchAsync(
@Nonnull Collection<String> keys,
GetRecordOptions options) {
return Flux.fromIterable(keys)
.flatMap(key -> getAsync(key, options).flux())
.collect(Collectors.toList());
}

@Override
public Mono<String> upsertAsync(@Nonnull Record record, UpsertRecordOptions options) {
return upsertBatchAsync(Collections.singletonList(record), options)
.map(Collection::iterator)
.map(Iterator::next);
}

@Override
public Mono<Collection<String>> upsertBatchAsync(
@Nonnull Collection<Record> records, UpsertRecordOptions options) {
if (records.isEmpty()) {
return Mono.just(Collections.emptyList());
}

String indexName = resolveCollectionName(
options != null ? options.getCollectionName() : null);
SearchAsyncClient client = this.getSearchClient(indexName);

VectorStoreRecordMapper<Record, SearchDocument> mapper = this.options
.getVectorStoreRecordMapper();
Iterable<?> documents;

// Use custom mapper if available
if (mapper != null && mapper.getToStorageModelMapper() != null) {
documents = records.stream()
.map(this.options.getVectorStoreRecordMapper()::toStorageModel)
.collect(Collectors.toList());
} else {
documents = records;
}

return client.uploadDocuments(documents)
.map(IndexDocumentsResult::getResults)
.map(
results -> results.stream()
.map(IndexingResult::getKey)
.collect(Collectors.toList()));
}

@Override
public Mono<Void> deleteAsync(String key, DeleteRecordOptions options) {
return deleteBatchAsync(Collections.singletonList(key), options);
}

@Override
public Mono<Void> deleteBatchAsync(Collection<String> keys, DeleteRecordOptions options) {
String indexName = resolveCollectionName(
options != null ? options.getCollectionName() : null);
SearchAsyncClient client = this.getSearchClient(indexName);

return client.deleteDocuments(keys.stream().map(key -> {
SearchDocument document = new SearchDocument();
document.put(this.options.getRecordDefinition().getKeyField().getName(), key);
return document;
}).collect(Collectors.toList())).then();
}

/**
* Get a search client for the index specified. Note: the index might not exist, but we avoid
* checking everytime and the extra latency.
*
* @param indexName Index name
* @return Search client ready to read/write
*/
protected SearchAsyncClient getSearchClient(@Nonnull String indexName) {
return clientsByIndex.computeIfAbsent(
indexName, client::getSearchAsyncClient);
}
}
Loading