Skip to content

Commit f8f0cdc

Browse files
committed
Merge 'main' into 'postgres'
2 parents 4d1d920 + c102f66 commit f8f0cdc

28 files changed

+315
-248
lines changed

aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/chatcompletion/OpenAIChatCompletion.java

+24-21
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private static class ChatMessages {
184184

185185
private final List<ChatRequestMessage> newMessages;
186186
private final List<ChatRequestMessage> allMessages;
187-
private final List<OpenAIChatMessageContent> newChatMessageContent;
187+
private final List<OpenAIChatMessageContent<?>> newChatMessageContent;
188188

189189
public ChatMessages(List<ChatRequestMessage> allMessages) {
190190
this.allMessages = Collections.unmodifiableList(allMessages);
@@ -195,7 +195,7 @@ public ChatMessages(List<ChatRequestMessage> allMessages) {
195195
private ChatMessages(
196196
List<ChatRequestMessage> allMessages,
197197
List<ChatRequestMessage> newMessages,
198-
List<OpenAIChatMessageContent> newChatMessageContent) {
198+
List<OpenAIChatMessageContent<?>> newChatMessageContent) {
199199
this.allMessages = Collections.unmodifiableList(allMessages);
200200
this.newMessages = Collections.unmodifiableList(newMessages);
201201
this.newChatMessageContent = Collections.unmodifiableList(newChatMessageContent);
@@ -219,8 +219,8 @@ public ChatMessages add(ChatRequestMessage requestMessage) {
219219
}
220220

221221
@CheckReturnValue
222-
public ChatMessages addChatMessage(List<OpenAIChatMessageContent> chatMessageContent) {
223-
ArrayList<OpenAIChatMessageContent> tmpChatMessageContent = new ArrayList<>(
222+
public ChatMessages addChatMessage(List<OpenAIChatMessageContent<?>> chatMessageContent) {
223+
ArrayList<OpenAIChatMessageContent<?>> tmpChatMessageContent = new ArrayList<>(
224224
newChatMessageContent);
225225
tmpChatMessageContent.addAll(chatMessageContent);
226226

@@ -357,19 +357,16 @@ private Mono<ChatMessages> internalChatMessageContentsAsync(
357357
// If we don't want to attempt to invoke any functions
358358
// Or if we are auto-invoking, but we somehow end up with other than 1 choice even though only 1 was requested
359359
if (autoInvokeAttempts == 0 || responseMessages.size() != 1) {
360-
return getChatMessageContentsAsync(completions)
361-
.flatMap(m -> {
362-
return Mono.just(messages.addChatMessage(m));
363-
});
360+
List<OpenAIChatMessageContent<?>> chatMessageContents = getChatMessageContentsAsync(completions);
361+
return Mono.just(messages.addChatMessage(chatMessageContents));
364362
}
365363
// Or if there are no tool calls to be done
366364
ChatResponseMessage response = responseMessages.get(0);
367365
List<ChatCompletionsToolCall> toolCalls = response.getToolCalls();
368366
if (toolCalls == null || toolCalls.isEmpty()) {
369-
return getChatMessageContentsAsync(completions)
370-
.flatMap(m -> {
371-
return Mono.just(messages.addChatMessage(m));
372-
});
367+
List<OpenAIChatMessageContent<?>> chatMessageContents = getChatMessageContentsAsync(
368+
completions);
369+
return Mono.just(messages.addChatMessage(chatMessageContents));
373370
}
374371

375372
ChatRequestAssistantMessage requestMessage = new ChatRequestAssistantMessage(
@@ -592,7 +589,7 @@ private OpenAIFunctionToolCall extractOpenAIFunctionToolCall(
592589
arguments);
593590
}
594591

595-
private Mono<List<OpenAIChatMessageContent>> getChatMessageContentsAsync(
592+
private List<OpenAIChatMessageContent<?>> getChatMessageContentsAsync(
596593
ChatCompletions completions) {
597594
FunctionResultMetadata<CompletionsUsage> completionMetadata = FunctionResultMetadata.build(
598595
completions.getId(),
@@ -606,22 +603,28 @@ private Mono<List<OpenAIChatMessageContent>> getChatMessageContentsAsync(
606603
.filter(Objects::nonNull)
607604
.collect(Collectors.toList());
608605

609-
return Flux.fromIterable(responseMessages)
610-
.flatMap(response -> {
606+
List<OpenAIChatMessageContent<?>> chatMessageContent =
607+
responseMessages
608+
.stream()
609+
.map(response -> {
611610
try {
612-
return Mono.just(new OpenAIChatMessageContent(
611+
return new OpenAIChatMessageContent<>(
613612
AuthorRole.ASSISTANT,
614613
response.getContent(),
615614
this.getModelId(),
616615
null,
617616
null,
618617
completionMetadata,
619-
formOpenAiToolCalls(response)));
620-
} catch (Exception e) {
621-
return Mono.error(e);
618+
formOpenAiToolCalls(response));
619+
} catch (SKCheckedException e) {
620+
LOGGER.warn("Failed to form chat message content", e);
621+
return null;
622622
}
623623
})
624-
.collectList();
624+
.filter(Objects::nonNull)
625+
.collect(Collectors.toList());
626+
627+
return chatMessageContent;
625628
}
626629

627630
private List<ChatMessageContent<?>> toOpenAIChatMessageContent(
@@ -931,7 +934,7 @@ private static boolean hasToolCallBeenExecuted(List<ChatRequestMessage> chatRequ
931934
}
932935

933936
private static List<ChatRequestMessage> getChatRequestMessages(
934-
List<? extends ChatMessageContent> messages) {
937+
List<? extends ChatMessageContent<?>> messages) {
935938
if (messages == null || messages.isEmpty()) {
936939
return new ArrayList<>();
937940
}

aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/chatcompletion/OpenAIChatMessageContent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public OpenAIChatMessageContent(
3636
@Nullable String modelId,
3737
@Nullable T innerContent,
3838
@Nullable Charset encoding,
39-
@Nullable FunctionResultMetadata metadata,
39+
@Nullable FunctionResultMetadata<?> metadata,
4040
@Nullable List<OpenAIFunctionToolCall> toolCall) {
4141
super(authorRole, content, modelId, innerContent, encoding, metadata);
4242

samples/semantickernel-concepts/semantickernel-syntax-examples/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/memory/AzureAISearch_DataStorage.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
1313
import com.microsoft.semantickernel.aiservices.openai.textembedding.OpenAITextEmbeddingGenerationService;
1414
import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStore;
1515
import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStoreOptions;
16-
import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStoreRecordCollection;
16+
import com.microsoft.semantickernel.data.VectorStoreRecordCollection;
1717
import com.microsoft.semantickernel.data.recordattributes.VectorStoreRecordDataAttribute;
1818
import com.microsoft.semantickernel.data.recordattributes.VectorStoreRecordKeyAttribute;
1919
import com.microsoft.semantickernel.data.recordattributes.VectorStoreRecordVectorAttribute;
20-
import reactor.core.publisher.Flux;
21-
import reactor.core.publisher.Mono;
22-
2320
import java.nio.charset.StandardCharsets;
2421
import java.util.Arrays;
2522
import java.util.Base64;
2623
import java.util.Collections;
2724
import java.util.List;
2825
import java.util.Map;
2926
import java.util.stream.Collectors;
27+
import reactor.core.publisher.Flux;
28+
import reactor.core.publisher.Mono;
3029

3130
public class AzureAISearch_DataStorage {
31+
3232
private static final String CLIENT_KEY = System.getenv("CLIENT_KEY");
3333
private static final String AZURE_CLIENT_KEY = System.getenv("AZURE_CLIENT_KEY");
3434

@@ -45,6 +45,7 @@ public class AzureAISearch_DataStorage {
4545
private static final int EMBEDDING_DIMENSIONS = 1536;
4646

4747
static class GitHubFile {
48+
4849
@VectorStoreRecordKeyAttribute()
4950
private final String id;
5051
@VectorStoreRecordDataAttribute(hasEmbedding = true, embeddingFieldName = "embedding")
@@ -120,7 +121,9 @@ public static void dataStorageWithAzureAISearch(
120121
.build();
121122

122123
String collectionName = "skgithubfiles";
123-
var collection = azureAISearchVectorStore.getCollection(collectionName, GitHubFile.class,
124+
var collection = azureAISearchVectorStore.getCollection(
125+
collectionName,
126+
GitHubFile.class,
124127
null);
125128

126129
// Create collection if it does not exist and store data
@@ -140,7 +143,7 @@ public static void dataStorageWithAzureAISearch(
140143
}
141144

142145
private static Mono<List<String>> storeData(
143-
AzureAISearchVectorStoreRecordCollection<GitHubFile> recordStore,
146+
VectorStoreRecordCollection<String, GitHubFile> recordStore,
144147
OpenAITextEmbeddingGenerationService embeddingGeneration,
145148
Map<String, String> data) {
146149

samples/semantickernel-concepts/semantickernel-syntax-examples/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/memory/InMemory_DataStorage.java

+7-17
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,22 @@
55
import com.azure.ai.openai.OpenAIClientBuilder;
66
import com.azure.core.credential.AzureKeyCredential;
77
import com.azure.core.credential.KeyCredential;
8-
import com.azure.core.util.ClientOptions;
9-
import com.azure.core.util.MetricsOptions;
10-
import com.azure.core.util.TracingOptions;
11-
import com.azure.search.documents.indexes.SearchIndexAsyncClient;
12-
import com.azure.search.documents.indexes.SearchIndexClientBuilder;
138
import com.microsoft.semantickernel.aiservices.openai.textembedding.OpenAITextEmbeddingGenerationService;
14-
import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStore;
15-
import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStoreOptions;
16-
import com.microsoft.semantickernel.connectors.data.azureaisearch.AzureAISearchVectorStoreRecordCollection;
9+
import com.microsoft.semantickernel.data.VectorStoreRecordCollection;
1710
import com.microsoft.semantickernel.data.VolatileVectorStore;
18-
import com.microsoft.semantickernel.data.VolatileVectorStoreRecordCollection;
1911
import com.microsoft.semantickernel.data.recordattributes.VectorStoreRecordDataAttribute;
2012
import com.microsoft.semantickernel.data.recordattributes.VectorStoreRecordKeyAttribute;
2113
import com.microsoft.semantickernel.data.recordattributes.VectorStoreRecordVectorAttribute;
22-
import reactor.core.publisher.Flux;
23-
import reactor.core.publisher.Mono;
24-
25-
import java.nio.charset.StandardCharsets;
2614
import java.util.Arrays;
27-
import java.util.Base64;
2815
import java.util.Collections;
2916
import java.util.List;
3017
import java.util.Map;
3118
import java.util.stream.Collectors;
19+
import reactor.core.publisher.Flux;
20+
import reactor.core.publisher.Mono;
3221

3322
public class InMemory_DataStorage {
23+
3424
private static final String CLIENT_KEY = System.getenv("CLIENT_KEY");
3525
private static final String AZURE_CLIENT_KEY = System.getenv("AZURE_CLIENT_KEY");
3626

@@ -43,6 +33,7 @@ public class InMemory_DataStorage {
4333
private static final int EMBEDDING_DIMENSIONS = 1536;
4434

4535
static class GitHubFile {
36+
4637
@VectorStoreRecordKeyAttribute()
4738
private final String id;
4839
@VectorStoreRecordDataAttribute(hasEmbedding = true, embeddingFieldName = "embedding")
@@ -72,8 +63,7 @@ public String getDescription() {
7263
}
7364

7465
static String encodeId(String realId) {
75-
byte[] bytes = Base64.getUrlEncoder().encode(realId.getBytes(StandardCharsets.UTF_8));
76-
return new String(bytes, StandardCharsets.UTF_8);
66+
return AzureAISearch_DataStorage.GitHubFile.encodeId(realId);
7767
}
7868
}
7969

@@ -126,7 +116,7 @@ public static void inMemoryDataStorage(
126116
}
127117

128118
private static Mono<List<String>> storeData(
129-
VolatileVectorStoreRecordCollection<GitHubFile> recordCollection,
119+
VectorStoreRecordCollection<String, GitHubFile> recordCollection,
130120
OpenAITextEmbeddingGenerationService embeddingGeneration,
131121
Map<String, String> data) {
132122

0 commit comments

Comments
 (0)