Skip to content

Commit fad4a59

Browse files
committed
allow customising the reading of memory records. Fixes microsoft#3060
1 parent d16b3eb commit fad4a59

3 files changed

Lines changed: 141 additions & 27 deletions

File tree

‎java/connectors/semantickernel-connectors-memory-azurecognitivesearch/pom.xml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
<artifactId>log4j-slf4j2-impl</artifactId>
5050
<scope>test</scope>
5151
</dependency>
52+
<dependency>
53+
<groupId>com.microsoft.semantic-kernel</groupId>
54+
<artifactId>semantickernel-core</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.microsoft.semantic-kernel</groupId>
59+
<artifactId>semantickernel-connectors-ai-openai</artifactId>
60+
<scope>test</scope>
61+
</dependency>
5262
</dependencies>
5363

5464
</project>

‎java/connectors/semantickernel-connectors-memory-azurecognitivesearch/src/main/java/com/microsoft/semantickernel/connectors/memory/azurecognitivesearch/AzureCognitiveSearchMemoryStore.java‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
import java.util.Locale;
3636
import java.util.Map;
3737
import java.util.concurrent.ConcurrentHashMap;
38+
import java.util.function.Function;
3839
import java.util.stream.Collectors;
3940
import javax.annotation.Nonnull;
41+
import javax.annotation.Nullable;
4042
import reactor.core.publisher.Flux;
4143
import reactor.core.publisher.Mono;
4244
import reactor.util.function.Tuple2;
@@ -54,6 +56,7 @@ public class AzureCognitiveSearchMemoryStore implements MemoryStore {
5456
private final SearchIndexAsyncClient _adminClient;
5557

5658
private final Map<String, SearchAsyncClient> _clientsByIndex = new ConcurrentHashMap<>();
59+
private final Function<SearchDocument, MemoryRecord> memoryRecordMapper;
5760

5861
/// <summary>
5962
/// Create a new instance of memory storage using Azure Cognitive Search.
@@ -74,7 +77,8 @@ public AzureCognitiveSearchMemoryStore(@Nonnull String endpoint, @Nonnull String
7477
.endpoint(endpoint)
7578
.credential(new AzureKeyCredential(apiKey))
7679
.clientOptions(clientOptions())
77-
.buildAsyncClient());
80+
.buildAsyncClient(),
81+
null);
7882
}
7983

8084
/**
@@ -90,16 +94,20 @@ public AzureCognitiveSearchMemoryStore(
9094
.endpoint(endpoint)
9195
.credential(credentials)
9296
.clientOptions(clientOptions())
93-
.buildAsyncClient());
97+
.buildAsyncClient(),
98+
null);
9499
}
95100

96101
/**
97102
* Create a new instance of memory storage using Azure Cognitive Search.
98103
*
99104
* @param searchIndexAsyncClient the Azure search documents index client to use
100105
*/
101-
public AzureCognitiveSearchMemoryStore(@Nonnull SearchIndexAsyncClient searchIndexAsyncClient) {
106+
public AzureCognitiveSearchMemoryStore(
107+
@Nonnull SearchIndexAsyncClient searchIndexAsyncClient,
108+
@Nullable Function<SearchDocument, MemoryRecord> memoryRecordMapper) {
102109
this._adminClient = searchIndexAsyncClient;
110+
this.memoryRecordMapper = memoryRecordMapper;
103111
}
104112

105113
@Override
@@ -215,12 +223,18 @@ public Mono<Collection<Tuple2<MemoryRecord, Float>>> getNearestMatchesAsync(
215223
SearchOptions searchOptions = new SearchOptions().setVectors(searchVector);
216224

217225
return client.search(null, searchOptions)
218-
.filter(result -> (double) minRelevanceScore <= result.getScore())
226+
.filter(result -> (double) minRelevanceScore >= result.getScore())
219227
.map(
220228
result -> {
221-
MemoryRecord memoryRecord =
222-
result.getDocument(AzureCognitiveSearchMemoryRecord.class)
223-
.toMemoryRecord(withEmbedding);
229+
MemoryRecord memoryRecord;
230+
if (memoryRecordMapper != null) {
231+
SearchDocument doc = result.getDocument(SearchDocument.class);
232+
memoryRecord = memoryRecordMapper.apply(doc);
233+
} else {
234+
AzureCognitiveSearchMemoryRecord doc =
235+
result.getDocument(AzureCognitiveSearchMemoryRecord.class);
236+
memoryRecord = doc.toMemoryRecord(withEmbedding);
237+
}
224238
float score = (float) result.getScore();
225239
return Tuples.of(memoryRecord, score);
226240
})
@@ -418,7 +432,7 @@ record -> {
418432
* @param indexName Index name
419433
* @return Search client ready to read/write
420434
*/
421-
protected SearchAsyncClient getSearchClient(@Nonnull String indexName) {
435+
private SearchAsyncClient getSearchClient(@Nonnull String indexName) {
422436
String normalizedIndexName = normalizeIndexName(indexName);
423437
return _clientsByIndex.computeIfAbsent(
424438
normalizedIndexName, _adminClient::getSearchAsyncClient);

‎java/connectors/semantickernel-connectors-memory-azurecognitivesearch/src/test/java/com/microsoft/semantickernel/connectors/memory/azurecognitivesearch/AzureCognitiveSearchMemoryTests.java‎

Lines changed: 109 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,41 @@
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

7-
import com.azure.core.credential.AzureKeyCredential;
87
import com.azure.core.http.HttpHeaders;
98
import com.azure.core.http.HttpPipeline;
109
import com.azure.core.http.HttpRequest;
1110
import com.azure.core.http.HttpResponse;
12-
import com.azure.core.http.policy.HttpLogDetailLevel;
13-
import com.azure.core.http.policy.HttpLogOptions;
11+
import com.azure.core.http.rest.Response;
1412
import com.azure.core.util.Context;
1513
import com.azure.core.util.tracing.Tracer;
14+
import com.azure.search.documents.SearchAsyncClient;
15+
import com.azure.search.documents.SearchDocument;
1616
import com.azure.search.documents.indexes.SearchIndexAsyncClient;
17-
import com.azure.search.documents.indexes.SearchIndexClientBuilder;
17+
import com.azure.search.documents.models.SearchResult;
18+
import com.azure.search.documents.util.SearchPagedFlux;
1819
import com.microsoft.semantickernel.Kernel;
1920
import com.microsoft.semantickernel.SKBuilders;
2021
import com.microsoft.semantickernel.ai.embeddings.Embedding;
2122
import com.microsoft.semantickernel.ai.embeddings.EmbeddingGeneration;
2223
import com.microsoft.semantickernel.memory.MemoryQueryResult;
24+
import com.microsoft.semantickernel.memory.MemoryRecord;
25+
import com.microsoft.semantickernel.memory.MemoryRecordMetadata;
2326
import java.nio.charset.StandardCharsets;
2427
import java.util.ArrayList;
2528
import java.util.Arrays;
2629
import java.util.Base64;
2730
import java.util.List;
2831
import java.util.function.Function;
32+
import javax.annotation.Nullable;
2933
import org.junit.jupiter.api.BeforeEach;
3034
import org.junit.jupiter.api.Disabled;
3135
import org.junit.jupiter.api.Test;
3236
import org.mockito.Mockito;
3337
import org.mockito.invocation.InvocationOnMock;
38+
import reactor.core.publisher.Flux;
3439
import reactor.core.publisher.Mono;
3540

36-
@Disabled("Pending https://github.com/microsoft/semantic-kernel/issues/1797")
41+
// @Disabled("Pending https://github.com/microsoft/semantic-kernel/issues/1797")
3742
class AzureCognitiveSearchMemoryTests {
3843

3944
// com.azure.core.implementation.http.rest.RestProxyBase.RestProxyBase
@@ -85,22 +90,51 @@ public boolean isEnabled() {
8590

8691
@BeforeEach
8792
void setUp() {
93+
kernel = buildKernel(null);
94+
}
8895

89-
String apiKey = "fake-key";
90-
HttpPipeline pipeline = setUpHttpPipeline();
96+
private Kernel buildKernel(
97+
@Nullable Function<SearchDocument, MemoryRecord> memoryRecordMapper) {
98+
SearchAsyncClient searchAsyncClient = Mockito.mock(SearchAsyncClient.class);
99+
SearchIndexAsyncClient searchIndexAsyncClient = Mockito.mock(SearchIndexAsyncClient.class);
100+
Response response = Mockito.mock(Response.class);
101+
AzureCognitiveSearchMemoryRecord azureCognitiveSearchMemoryRecord =
102+
Mockito.mock(AzureCognitiveSearchMemoryRecord.class);
103+
MemoryRecord memoryRecord = Mockito.mock(MemoryRecord.class);
104+
SearchPagedFlux searchPagedFlux = Mockito.mock(SearchPagedFlux.class);
105+
SearchResult searchResult = Mockito.mock(SearchResult.class);
106+
107+
Mockito.when(searchResult.getDocument(Mockito.any())).thenReturn(new SearchDocument());
108+
Mockito.when(searchIndexAsyncClient.getSearchAsyncClient(Mockito.any()))
109+
.thenReturn(searchAsyncClient);
110+
111+
Mockito.when(searchPagedFlux.filter(Mockito.any())).thenReturn(Flux.just(searchResult));
112+
113+
Mockito.when(
114+
searchAsyncClient.getDocumentWithResponse(
115+
Mockito.any(), Mockito.any(), Mockito.any()))
116+
.thenReturn(Mono.just(response));
117+
118+
Mockito.when(searchAsyncClient.search(Mockito.any(), Mockito.any()))
119+
.thenReturn(searchPagedFlux);
120+
Mockito.when(response.getValue()).thenReturn(azureCognitiveSearchMemoryRecord);
121+
Mockito.when(azureCognitiveSearchMemoryRecord.toMemoryRecord(Mockito.anyBoolean()))
122+
.thenReturn(memoryRecord);
91123

92-
SearchIndexAsyncClient searchIndexAsyncClient =
93-
new SearchIndexClientBuilder()
124+
EmbeddingGeneration<String> embeddingGeneration =
125+
new EmbeddingGeneration<String>() {
126+
/*
127+
new SearchIndexClientBuilder()
94128
.endpoint("https://fake-random-test-host/fake-path")
95129
.credential(new AzureKeyCredential(apiKey))
96130
.pipeline(pipeline)
97131
.httpLogOptions(
98-
new HttpLogOptions()
99-
.setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
132+
new HttpLogOptions()
133+
.setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
100134
.buildAsyncClient();
101135
102-
EmbeddingGeneration<String> embeddingGeneration =
103-
new EmbeddingGeneration<String>() {
136+
*/
137+
104138
List<Embedding> embeddings = new ArrayList<>();
105139

106140
{
@@ -113,12 +147,12 @@ public Mono<List<Embedding>> generateEmbeddingsAsync(List<String> data) {
113147
}
114148
};
115149

116-
kernel =
117-
SKBuilders.kernel()
118-
.withMemoryStorage(
119-
new AzureCognitiveSearchMemoryStore(searchIndexAsyncClient))
120-
.withDefaultAIService(embeddingGeneration)
121-
.build();
150+
return SKBuilders.kernel()
151+
.withMemoryStorage(
152+
new AzureCognitiveSearchMemoryStore(
153+
searchIndexAsyncClient, memoryRecordMapper))
154+
.withDefaultAIService(embeddingGeneration)
155+
.build();
122156
}
123157

124158
private HttpPipeline setUpHttpPipeline() {
@@ -158,6 +192,7 @@ private Mono<HttpResponse> answerHttpRequest(InvocationOnMock invocation) {
158192
return Mono.just(httpResponse);
159193
}
160194

195+
@Disabled("Pending https://github.com/microsoft/semantic-kernel/issues/1797")
161196
@Test
162197
void azureCognitiveSearchMemoryStoreShouldBeProperlyInitialized() {
163198
// Arrange
@@ -172,6 +207,7 @@ void azureCognitiveSearchMemoryStoreShouldBeProperlyInitialized() {
172207
assertTrue(collections.contains("fake-index"));
173208
}
174209

210+
@Disabled("Pending https://github.com/microsoft/semantic-kernel/issues/1797")
175211
@Test
176212
void azureCognitiveSearchMemoryStoreCanSaveReferenceAsync() {
177213
// Arrange
@@ -205,6 +241,7 @@ void azureCognitiveSearchMemoryStoreCanSaveReferenceAsync() {
205241
assertEquals("fake-key", key);
206242
}
207243

244+
@Disabled("Pending https://github.com/microsoft/semantic-kernel/issues/1797")
208245
@Test
209246
void azureCognitiveSearchMemoryStoreCanSaveInformationAsync() {
210247
// Arrange
@@ -237,6 +274,7 @@ void azureCognitiveSearchMemoryStoreCanSaveInformationAsync() {
237274
assertEquals("fake-key", key);
238275
}
239276

277+
@Disabled("Pending https://github.com/microsoft/semantic-kernel/issues/1797")
240278
@Test
241279
void azureCognitiveSearchMemoryStoreGetAsync() {
242280
// Arrange
@@ -267,6 +305,7 @@ void azureCognitiveSearchMemoryStoreGetAsync() {
267305
assertEquals(key, result.getMetadata().getId());
268306
}
269307

308+
@Disabled("Pending https://github.com/microsoft/semantic-kernel/issues/1797")
270309
@Test
271310
void azureCognitiveSearchMemoryStoreRemoveAsync() {
272311
// Arrange
@@ -298,6 +337,7 @@ void azureCognitiveSearchMemoryStoreRemoveAsync() {
298337
// Assert
299338
}
300339

340+
@Disabled("Pending https://github.com/microsoft/semantic-kernel/issues/1797")
301341
@Test
302342
void azureCognitiveSearchMemoryStoreSearchAsync() {
303343
// Arrange
@@ -342,4 +382,54 @@ void azureCognitiveSearchMemoryStoreSearchAsync() {
342382
assertEquals(1, results.size());
343383
assertEquals(key, results.get(0).getMetadata().getId());
344384
}
385+
386+
@Test
387+
void azureCognitiveSearchMemoryStoreGetAsyncWithCustomMapping() {
388+
// Do not convert to lambda, it will break mockito
389+
Function<SearchDocument, MemoryRecord> transform =
390+
Mockito.spy(
391+
new Function<SearchDocument, MemoryRecord>() {
392+
@Override
393+
public MemoryRecord apply(SearchDocument searchDocument) {
394+
return new MemoryRecord(
395+
new MemoryRecordMetadata(
396+
false,
397+
"an-id",
398+
"fake-text",
399+
"fake-description",
400+
"fake-additionalmetadata",
401+
"fake-externalsourcename"),
402+
new Embedding(Arrays.asList(1.0f, 2.0f, 3.0f)),
403+
"an-id",
404+
null);
405+
}
406+
});
407+
408+
kernel = buildKernel(transform);
409+
// Arrange
410+
String key = "fake-key";
411+
String base64EncodedKey =
412+
new String(
413+
Base64.getUrlEncoder().encode("fake-key".getBytes(StandardCharsets.UTF_8)));
414+
415+
responseBody =
416+
httpRequest ->
417+
"{"
418+
+ " \"Id\": \""
419+
+ base64EncodedKey
420+
+ "\","
421+
+ " \"Text\": \"fake-text\","
422+
+ " \"Description\": \"fake-description\","
423+
+ " \"AdditionalMetadata\": \"fake-additionalmetadata\","
424+
+ " \"ExternalSourceName\": \"fake-externalsourcename\","
425+
+ " \"Reference\": false"
426+
+ "}";
427+
428+
responseStatusCode = httpRequest -> 200;
429+
430+
// Act
431+
kernel.getMemory().searchAsync("fake-index", "a-query", 1, 0.0f, false).block();
432+
433+
Mockito.verify(transform, Mockito.atLeastOnce()).apply(Mockito.any());
434+
}
345435
}

0 commit comments

Comments
 (0)