Skip to content

Commit 93889d8

Browse files
committed
Fixes from review
1 parent 96f1411 commit 93889d8

2 files changed

Lines changed: 84 additions & 7 deletions

File tree

aiservices/google/src/main/java/com/microsoft/semantickernel/aiservices/google/chatcompletion/GeminiChatCompletion.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.google.protobuf.Value;
1717
import com.microsoft.semantickernel.Kernel;
1818
import com.microsoft.semantickernel.aiservices.google.GeminiService;
19+
import com.microsoft.semantickernel.aiservices.google.GeminiServiceBuilder;
1920
import com.microsoft.semantickernel.aiservices.google.implementation.MonoConverter;
2021
import com.microsoft.semantickernel.contextvariables.ContextVariableTypes;
2122
import com.microsoft.semantickernel.exceptions.AIException;
@@ -36,15 +37,14 @@
3637
import com.microsoft.semantickernel.services.chatcompletion.ChatCompletionService;
3738
import com.microsoft.semantickernel.services.chatcompletion.ChatHistory;
3839
import com.microsoft.semantickernel.services.chatcompletion.ChatMessageContent;
39-
import com.microsoft.semantickernel.aiservices.google.GeminiServiceBuilder;
40+
import com.microsoft.semantickernel.services.chatcompletion.StreamingChatContent;
4041
import java.io.IOException;
4142
import java.time.OffsetDateTime;
4243
import java.util.ArrayList;
4344
import java.util.List;
4445
import java.util.UUID;
4546
import java.util.stream.Collectors;
4647
import javax.annotation.Nullable;
47-
import com.microsoft.semantickernel.services.chatcompletion.StreamingChatContent;
4848
import org.slf4j.Logger;
4949
import org.slf4j.LoggerFactory;
5050
import reactor.core.publisher.Flux;
@@ -78,17 +78,49 @@ public Mono<List<ChatMessageContent<?>>> getChatMessageContentsAsync(String prom
7878

7979
@Override
8080
public Flux<StreamingChatContent<?>> getStreamingChatMessageContentsAsync(
81-
ChatHistory chatHistory, @Nullable Kernel kernel,
81+
ChatHistory chatHistory,
82+
@Nullable Kernel kernel,
8283
@Nullable InvocationContext invocationContext) {
83-
throw new UnsupportedOperationException(
84-
"Streaming chat message contents are not supported");
84+
85+
LOGGER.warn("Streaming has been called on GeminiChatCompletion service. "
86+
+ "This is currently not supported in Gemini. "
87+
+ "The results will be returned in a non streaming fashion.");
88+
89+
return getChatMessageContentsAsync(chatHistory, kernel, invocationContext)
90+
.flatMapIterable(chatMessageContents -> chatMessageContents)
91+
.map(content -> {
92+
return new GeminiStreamingChatMessageContent(
93+
content.getAuthorRole(),
94+
content.getContent(),
95+
getModelId(),
96+
content.getInnerContent(),
97+
content.getEncoding(),
98+
content.getMetadata(),
99+
null,
100+
UUID.randomUUID().toString());
101+
});
85102
}
86103

87104
@Override
88105
public Flux<StreamingChatContent<?>> getStreamingChatMessageContentsAsync(String prompt,
89106
@Nullable Kernel kernel, @Nullable InvocationContext invocationContext) {
90-
throw new UnsupportedOperationException(
91-
"Streaming chat message contents are not supported");
107+
LOGGER.warn("Streaming has been called on GeminiChatCompletion service. "
108+
+ "This is currently not supported in Gemini. "
109+
+ "The results will be returned in a non streaming fashion.");
110+
111+
return getChatMessageContentsAsync(prompt, kernel, invocationContext)
112+
.flatMapIterable(chatMessageContents -> chatMessageContents)
113+
.map(content -> {
114+
return new GeminiStreamingChatMessageContent(
115+
content.getAuthorRole(),
116+
content.getContent(),
117+
getModelId(),
118+
content.getInnerContent(),
119+
content.getEncoding(),
120+
content.getMetadata(),
121+
null,
122+
UUID.randomUUID().toString());
123+
});
92124
}
93125

94126
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
package com.microsoft.semantickernel.aiservices.google.chatcompletion;
3+
4+
import com.microsoft.semantickernel.orchestration.FunctionResultMetadata;
5+
import com.microsoft.semantickernel.services.chatcompletion.AuthorRole;
6+
import com.microsoft.semantickernel.services.chatcompletion.StreamingChatContent;
7+
import java.nio.charset.Charset;
8+
import java.util.List;
9+
import javax.annotation.Nullable;
10+
11+
/**
12+
* Represents the content of a chat message.
13+
*
14+
* @param <T> The type of the inner content.
15+
*/
16+
public class GeminiStreamingChatMessageContent<T> extends GeminiChatMessageContent<T> implements
17+
StreamingChatContent<T> {
18+
19+
private final String id;
20+
21+
/**
22+
* Creates a new instance of the {@link GeminiChatMessageContent} class.
23+
*
24+
* @param authorRole The author role that generated the content.
25+
* @param content The content.
26+
* @param modelId The model id.
27+
* @param innerContent The inner content.
28+
* @param encoding The encoding.
29+
* @param metadata The metadata.
30+
* @param geminiFunctionCalls The function calls.
31+
*/
32+
public GeminiStreamingChatMessageContent(AuthorRole authorRole, String content,
33+
@Nullable String modelId, @Nullable T innerContent, @Nullable Charset encoding,
34+
@Nullable FunctionResultMetadata metadata,
35+
@Nullable List<GeminiFunctionCall> geminiFunctionCalls,
36+
String id) {
37+
super(authorRole, content, modelId, innerContent, encoding, metadata, geminiFunctionCalls);
38+
this.id = id;
39+
}
40+
41+
@Override
42+
public String getId() {
43+
return id;
44+
}
45+
}

0 commit comments

Comments
 (0)