Skip to content

Commit 6576fbe

Browse files
authored
Merge pull request #74 from milderhc/azure-ai-search
Add AzureAISearch connector
2 parents b4c5864 + 48c9afb commit 6576fbe

28 files changed

Lines changed: 1331 additions & 13 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
package com.microsoft.semantickernel.aiservices.openai.textembedding;
3+
4+
import com.azure.ai.openai.OpenAIAsyncClient;
5+
import com.azure.ai.openai.models.EmbeddingItem;
6+
import com.azure.ai.openai.models.Embeddings;
7+
import com.azure.ai.openai.models.EmbeddingsOptions;
8+
import com.microsoft.semantickernel.aiservices.openai.OpenAiService;
9+
import com.microsoft.semantickernel.exceptions.AIException;
10+
import com.microsoft.semantickernel.services.openai.OpenAiServiceBuilder;
11+
import com.microsoft.semantickernel.services.textcompletion.TextGenerationService;
12+
import com.microsoft.semantickernel.services.textembedding.Embedding;
13+
import com.microsoft.semantickernel.services.textembedding.TextEmbeddingGenerationService;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
import reactor.core.publisher.Mono;
17+
18+
import javax.annotation.Nullable;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
/**
23+
* An OpenAI implementation of a {@link TextEmbeddingGenerationService}.
24+
*
25+
*/
26+
public class OpenAITextEmbeddingGenerationService extends OpenAiService
27+
implements TextEmbeddingGenerationService {
28+
private static final Logger LOGGER = LoggerFactory
29+
.getLogger(OpenAITextEmbeddingGenerationService.class);
30+
31+
/**
32+
* Creates a new {@link OpenAITextEmbeddingGenerationService}.
33+
*
34+
* @param client OpenAI client
35+
* @param deploymentName deployment name
36+
* @param modelId OpenAI model id
37+
* @param serviceId Service id
38+
*/
39+
public OpenAITextEmbeddingGenerationService(
40+
OpenAIAsyncClient client,
41+
String deploymentName,
42+
String modelId,
43+
@Nullable String serviceId) {
44+
super(client, serviceId, modelId, deploymentName);
45+
}
46+
47+
/**
48+
* Creates a builder for creating a {@link OpenAITextEmbeddingGenerationService}.
49+
*
50+
* @return A new {@link OpenAITextEmbeddingGenerationService} builder.
51+
*/
52+
public static Builder builder() {
53+
return new Builder();
54+
}
55+
56+
/**
57+
* Generates embeddings for the given data.
58+
*
59+
* @param data The data to generate embeddings for.
60+
* @return A Mono that completes with the embeddings.
61+
*/
62+
@Override
63+
public Mono<List<Embedding>> generateEmbeddingsAsync(List<String> data) {
64+
return this.internalGenerateTextEmbeddingsAsync(data);
65+
}
66+
67+
protected Mono<List<Embedding>> internalGenerateTextEmbeddingsAsync(List<String> data) {
68+
EmbeddingsOptions options = new EmbeddingsOptions(data)
69+
.setModel(getModelId())
70+
.setInputType("string");
71+
72+
return getClient()
73+
.getEmbeddings(getModelId(), options)
74+
.flatMapIterable(Embeddings::getData)
75+
.mapNotNull(EmbeddingItem::getEmbedding)
76+
.map(ArrayList::new)
77+
.mapNotNull(Embedding::new)
78+
.collectList();
79+
}
80+
81+
/**
82+
* A builder for creating a {@link OpenAITextEmbeddingGenerationService}.
83+
*/
84+
public static class Builder extends
85+
OpenAiServiceBuilder<OpenAITextEmbeddingGenerationService, OpenAITextEmbeddingGenerationService.Builder> {
86+
@Override
87+
public OpenAITextEmbeddingGenerationService build() {
88+
89+
if (this.client == null) {
90+
throw new AIException(AIException.ErrorCodes.INVALID_REQUEST,
91+
"OpenAI client must be provided");
92+
}
93+
94+
if (this.modelId == null || modelId.isEmpty()) {
95+
throw new AIException(AIException.ErrorCodes.INVALID_REQUEST,
96+
"OpenAI model id must be provided");
97+
}
98+
99+
if (deploymentName == null) {
100+
LOGGER.debug("Deployment name is not provided, using model id as deployment name");
101+
deploymentName = modelId;
102+
}
103+
104+
return new OpenAITextEmbeddingGenerationService(client, deploymentName, modelId,
105+
serviceId);
106+
}
107+
}
108+
}

‎aiservices/openai/src/test/java/com/microsoft/semantickernel/aiservices/openai/chatcompletion/OpenAiChatCompletionTest.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public ChatCompletions getValue() {
107107
return new ObjectMapper()
108108
.readValue(String.format(message, "Snuggles"),
109109
ChatCompletions.class);
110-
111110
} catch (Exception e) {
112111
throw new RuntimeException(e);
113112
}

‎api-test/integration-tests/src/test/java/com/microsoft/semantickernel/tests/RenderingTest.java‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,12 @@ public List<Integer> WithListReturn() {
210210

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

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

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

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

253249
private Kernel buildTextKernel() {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.microsoft.semantic-kernel</groupId>
8+
<artifactId>semantickernel-parent</artifactId>
9+
<version>1.1.6-SNAPSHOT</version>
10+
<relativePath>../../../pom.xml</relativePath>
11+
</parent>
12+
13+
<groupId>com.microsoft</groupId>
14+
<artifactId>semantickernel-connectors-memory-azureaisearch</artifactId>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.microsoft.semantic-kernel</groupId>
19+
<artifactId>semantickernel-api</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.azure</groupId>
23+
<artifactId>azure-search-documents</artifactId>
24+
</dependency>
25+
</dependencies>
26+
27+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
package com.microsoft.semantickernel.connectors.memory.azureaisearch;
3+
4+
import com.azure.search.documents.SearchAsyncClient;
5+
import com.azure.search.documents.SearchDocument;
6+
import com.azure.search.documents.indexes.SearchIndexAsyncClient;
7+
import com.azure.search.documents.models.IndexDocumentsResult;
8+
import com.azure.search.documents.models.IndexingResult;
9+
import com.microsoft.semantickernel.exceptions.SKException;
10+
import com.microsoft.semantickernel.memory.VectorRecordStore;
11+
import com.microsoft.semantickernel.memory.VectorStoreRecordMapper;
12+
import com.microsoft.semantickernel.memory.recorddefinition.VectorStoreRecordDataField;
13+
import com.microsoft.semantickernel.memory.recorddefinition.VectorStoreRecordDefinition;
14+
import com.microsoft.semantickernel.memory.recordoptions.DeleteRecordOptions;
15+
import com.microsoft.semantickernel.memory.recordoptions.GetRecordOptions;
16+
import com.microsoft.semantickernel.memory.recordoptions.UpsertRecordOptions;
17+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18+
import reactor.core.publisher.Flux;
19+
import reactor.core.publisher.Mono;
20+
21+
import javax.annotation.Nonnull;
22+
import javax.annotation.Nullable;
23+
import java.util.ArrayList;
24+
import java.util.Collection;
25+
import java.util.Collections;
26+
import java.util.Iterator;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.concurrent.ConcurrentHashMap;
30+
import java.util.stream.Collectors;
31+
32+
public class AzureAISearchVectorRecordStore<Record> implements VectorRecordStore<String, Record> {
33+
private final SearchIndexAsyncClient client;
34+
private final Map<String, SearchAsyncClient> clientsByIndex = new ConcurrentHashMap<>();
35+
private final AzureAISearchVectorStoreOptions<Record> options;
36+
private final List<String> nonVectorFields = new ArrayList<>();
37+
38+
@SuppressFBWarnings("EI_EXPOSE_REP2")
39+
public AzureAISearchVectorRecordStore(
40+
@Nonnull SearchIndexAsyncClient client,
41+
@Nonnull AzureAISearchVectorStoreOptions<Record> options) {
42+
this.client = client;
43+
44+
// If record definition is not provided, create one from the record class
45+
if (options.getRecordDefinition() == null) {
46+
this.options = AzureAISearchVectorStoreOptions.<Record>builder()
47+
.withRecordClass(options.getRecordClass())
48+
.withVectorStoreRecordMapper(options.getVectorStoreRecordMapper())
49+
.withRecordDefinition(VectorStoreRecordDefinition.create(options.getRecordClass()))
50+
.build();
51+
} else {
52+
this.options = options;
53+
}
54+
55+
// Add non-vector fields to the list
56+
nonVectorFields.add(this.options.getRecordDefinition().getKeyField().getName());
57+
nonVectorFields.addAll(this.options.getRecordDefinition().getDataFields().stream()
58+
.map(VectorStoreRecordDataField::getName)
59+
.collect(Collectors.toList()));
60+
}
61+
62+
private String resolveCollectionName(@Nullable String collectionName) {
63+
if (collectionName != null) {
64+
return collectionName;
65+
}
66+
if (options.getDefaultCollectionName() != null) {
67+
return options.getDefaultCollectionName();
68+
}
69+
throw new SKException("A collection name is required");
70+
}
71+
72+
@Override
73+
public Mono<Record> getAsync(
74+
@Nonnull String key, GetRecordOptions options) {
75+
String indexName = resolveCollectionName(
76+
options != null ? options.getCollectionName() : null);
77+
SearchAsyncClient client = this.getSearchClient(indexName);
78+
79+
// If vectors are not requested, only fetch non-vector fields
80+
List<String> selectedFields = null;
81+
if (options != null && !options.includeVectors()) {
82+
selectedFields = Collections.unmodifiableList(nonVectorFields);
83+
}
84+
85+
VectorStoreRecordMapper<Record, SearchDocument> mapper = this.options
86+
.getVectorStoreRecordMapper();
87+
88+
// Use custom mapper if available
89+
if (mapper != null && mapper.getToRecordMapper() != null) {
90+
return client.getDocument(key, SearchDocument.class)
91+
.map(this.options.getVectorStoreRecordMapper()::toRecord);
92+
}
93+
94+
return client.getDocumentWithResponse(key, this.options.getRecordClass(), selectedFields)
95+
.map(response -> {
96+
if (response.getStatusCode() == 404) {
97+
throw new SKException("Record not found: " + key);
98+
}
99+
return response.getValue();
100+
});
101+
102+
}
103+
104+
@Override
105+
public Mono<Collection<Record>> getBatchAsync(
106+
@Nonnull Collection<String> keys,
107+
GetRecordOptions options) {
108+
return Flux.fromIterable(keys)
109+
.flatMap(key -> getAsync(key, options).flux())
110+
.collect(Collectors.toList());
111+
}
112+
113+
@Override
114+
public Mono<String> upsertAsync(@Nonnull Record record, UpsertRecordOptions options) {
115+
return upsertBatchAsync(Collections.singletonList(record), options)
116+
.map(Collection::iterator)
117+
.map(Iterator::next);
118+
}
119+
120+
@Override
121+
public Mono<Collection<String>> upsertBatchAsync(
122+
@Nonnull Collection<Record> records, UpsertRecordOptions options) {
123+
if (records.isEmpty()) {
124+
return Mono.just(Collections.emptyList());
125+
}
126+
127+
String indexName = resolveCollectionName(
128+
options != null ? options.getCollectionName() : null);
129+
SearchAsyncClient client = this.getSearchClient(indexName);
130+
131+
VectorStoreRecordMapper<Record, SearchDocument> mapper = this.options
132+
.getVectorStoreRecordMapper();
133+
Iterable<?> documents;
134+
135+
// Use custom mapper if available
136+
if (mapper != null && mapper.getToStorageModelMapper() != null) {
137+
documents = records.stream()
138+
.map(this.options.getVectorStoreRecordMapper()::toStorageModel)
139+
.collect(Collectors.toList());
140+
} else {
141+
documents = records;
142+
}
143+
144+
return client.uploadDocuments(documents)
145+
.map(IndexDocumentsResult::getResults)
146+
.map(
147+
results -> results.stream()
148+
.map(IndexingResult::getKey)
149+
.collect(Collectors.toList()));
150+
}
151+
152+
@Override
153+
public Mono<Void> deleteAsync(String key, DeleteRecordOptions options) {
154+
return deleteBatchAsync(Collections.singletonList(key), options);
155+
}
156+
157+
@Override
158+
public Mono<Void> deleteBatchAsync(Collection<String> keys, DeleteRecordOptions options) {
159+
String indexName = resolveCollectionName(
160+
options != null ? options.getCollectionName() : null);
161+
SearchAsyncClient client = this.getSearchClient(indexName);
162+
163+
return client.deleteDocuments(keys.stream().map(key -> {
164+
SearchDocument document = new SearchDocument();
165+
document.put(this.options.getRecordDefinition().getKeyField().getName(), key);
166+
return document;
167+
}).collect(Collectors.toList())).then();
168+
}
169+
170+
/**
171+
* Get a search client for the index specified. Note: the index might not exist, but we avoid
172+
* checking everytime and the extra latency.
173+
*
174+
* @param indexName Index name
175+
* @return Search client ready to read/write
176+
*/
177+
protected SearchAsyncClient getSearchClient(@Nonnull String indexName) {
178+
return clientsByIndex.computeIfAbsent(
179+
indexName, client::getSearchAsyncClient);
180+
}
181+
}

0 commit comments

Comments
 (0)