Skip to content

Commit 6866ede

Browse files
author
Milder Hernandez Cagua
committed
Disable streaming for SK
1 parent 8219092 commit 6866ede

File tree

3 files changed

+17
-51
lines changed

3 files changed

+17
-51
lines changed

app/backend/src/main/java/com/microsoft/openai/samples/rag/chat/approaches/semantickernel/JavaSemanticKernelChainsChatApproach.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public void runStreaming(
9595
ChatGPTConversation questionOrConversation,
9696
RAGOptions options,
9797
OutputStream outputStream) {
98+
throw new IllegalStateException("Streaming not supported for this approach");
9899
}
99100

100101
private List<ContentSource> formSourcesList(String result) {

app/backend/src/main/java/com/microsoft/openai/samples/rag/chat/approaches/semantickernel/JavaSemanticKernelWithMemoryChatApproach.java

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -102,48 +102,7 @@ public RAGResponse run(ChatGPTConversation questionOrConversation, RAGOptions op
102102

103103
@Override
104104
public void runStreaming(ChatGPTConversation questionOrConversation, RAGOptions options, OutputStream outputStream) {
105-
String question = ChatGPTUtils.getLastUserQuestion(questionOrConversation.getMessages());
106-
107-
// STEP 1: Build semantic kernel with Azure Cognitive Search as memory store. AnswerQuestion skill is imported from resources.
108-
Kernel semanticKernel = buildSemanticKernel(options);
109-
110-
// STEP 2: Retrieve relevant documents using keywords extracted from the chat history
111-
String conversation = ChatGPTUtils.formatAsChatML(questionOrConversation.toOpenAIChatMessages());
112-
List<MemoryQueryResult> sourcesResult = getSourcesFromConversation(conversation, semanticKernel, options);
113-
114-
LOGGER.info("Total {} sources found in cognitive vector store for search query[{}]", sourcesResult.size(), question);
115-
116-
String sources = buildSourcesText(sourcesResult);
117-
List<ContentSource> sourcesList = buildSources(sourcesResult);
118-
119-
// STEP 3: Generate a contextual and content specific answer using the search results and chat history
120-
SKFunction answerConversation = semanticKernel.getFunction("RAG", "AnswerConversation");
121-
SKContext skcontext = SKBuilders.context().build()
122-
.setVariable("sources", sources)
123-
.setVariable("conversation", conversation)
124-
.setVariable("suggestions", String.valueOf(options.isSuggestFollowupQuestions()))
125-
.setVariable("input", question);
126-
127-
SKContext reply = (SKContext) answerConversation.invokeAsync(skcontext).block();
128-
129-
RAGResponse ragResponse =
130-
new RAGResponse.Builder()
131-
.question(
132-
ChatGPTUtils.getLastUserQuestion(
133-
questionOrConversation.getMessages()))
134-
.prompt("placeholders for prompt")
135-
.answer(reply.getResult())
136-
.sources(sourcesList)
137-
.sourcesAsText(sources)
138-
.build();
139-
140-
try {
141-
String value = objectMapper.writeValueAsString(ChatResponse.buildChatResponse(ragResponse)) + "\n";
142-
outputStream.write(value.getBytes());
143-
outputStream.flush();
144-
} catch (IOException e) {
145-
throw new RuntimeException(e);
146-
}
105+
throw new IllegalStateException("Streaming not supported for this approach");
147106
}
148107

149108
private List<MemoryQueryResult> getSourcesFromConversation (String conversation, Kernel kernel, RAGOptions options) {

app/frontend/src/pages/chat/Chat.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const Chat = () => {
3535
const [retrievalMode, setRetrievalMode] = useState<RetrievalMode>(RetrievalMode.Hybrid);
3636
const [useSemanticRanker, setUseSemanticRanker] = useState<boolean>(true);
3737
const [shouldStream, setShouldStream] = useState<boolean>(true);
38+
const [streamAvailable, setStreamAvailable] = useState<boolean>(true);
3839
const [useSemanticCaptions, setUseSemanticCaptions] = useState<boolean>(false);
3940
const [excludeCategory, setExcludeCategory] = useState<string>("");
4041
const [useSuggestFollowupQuestions, setUseSuggestFollowupQuestions] = useState<boolean>(false);
@@ -112,9 +113,10 @@ const Chat = () => {
112113
{ content: a[1].choices[0].message.content, role: "assistant" }
113114
]);
114115

116+
const stream = streamAvailable && shouldStream;
115117
const request: ChatAppRequest = {
116118
messages: [...messages, { content: question, role: "user" }],
117-
stream: shouldStream,
119+
stream: stream,
118120
context: {
119121
overrides: {
120122
prompt_template: promptTemplate.length === 0 ? undefined : promptTemplate,
@@ -138,7 +140,7 @@ const Chat = () => {
138140
if (!response.body) {
139141
throw Error("No response body");
140142
}
141-
if (shouldStream) {
143+
if (stream) {
142144
const parsedResponse: ChatAppResponse = await handleAsyncRequest(question, answers, setAnswers, response.body);
143145
setAnswers([...answers, [question, parsedResponse]]);
144146
} else {
@@ -186,7 +188,9 @@ const Chat = () => {
186188
};
187189

188190
const onApproachChange = (_ev?: React.FormEvent<HTMLElement | HTMLInputElement>, option?: IChoiceGroupOption) => {
189-
setApproach((option?.key as Approaches) || Approaches.JAVA_OPENAI_SDK);
191+
const newApproach = (option?.key as Approaches);
192+
setApproach(newApproach || Approaches.JAVA_OPENAI_SDK);
193+
setStreamAvailable(newApproach === Approaches.JAVA_OPENAI_SDK);
190194
};
191195

192196
const onUseSemanticRankerChange = (_ev?: React.FormEvent<HTMLElement | HTMLInputElement>, checked?: boolean) => {
@@ -450,12 +454,14 @@ const Chat = () => {
450454
required
451455
onChange={onRetrievalModeChange}
452456
/>
453-
<Checkbox
454-
className={styles.chatSettingsSeparator}
455-
checked={shouldStream}
456-
label="Stream chat completion responses"
457-
onChange={onShouldStreamChange}
458-
/>
457+
{streamAvailable &&
458+
<Checkbox
459+
className={styles.chatSettingsSeparator}
460+
checked={shouldStream}
461+
label="Stream chat completion responses"
462+
onChange={onShouldStreamChange}
463+
/>
464+
}
459465

460466
{useLogin && <TokenClaimsDisplay />}
461467
</Panel>

0 commit comments

Comments
 (0)