Repository navigation
Add AzureAISearch VectorStore #74
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
62e2cdf
Add AzureAISearch connector
bced9f8
Add fixes, suggestions, docs
ae2f992
Merge branch 'remove-java' into azure-ai-search
johnoliver 5344ce5
Merge with relocation
johnoliver b9e3484
Merge remote-tracking branch 'upstream/main' into azure-ai-search
johnoliver 4c8b996
Merge branch 'main' of https://github.com/microsoft/semantic-kernel-j…
2145403
Fix tests
ebb117e
Merge branch 'azure-ai-search' of https://github.com/milderhc/semanti…
8ce7ec9
Fix tests
923eaea
Go back to azure-ai-openai 1.0.0-beta.8
f093763
Fix test
48c9afb
Add suggestions and fixes
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
108 changes: 108 additions & 0 deletions
108
.../semantickernel/aiservices/openai/textembedding/OpenAITextEmbeddingGenerationService.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,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); | ||
| } | ||
| } | ||
| } |
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
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
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,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> |
181 changes: 181 additions & 0 deletions
181
...rosoft/semantickernel/connectors/memory/azureaisearch/AzureAISearchVectorRecordStore.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,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") | ||
| 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); | ||
| } | ||
| } | ||
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.