Skip to content

Commit f14456f

Browse files
author
Milder Hernandez Cagua
committed
Find sources based on conversation
1 parent a7a23ba commit f14456f

File tree

1 file changed

+56
-14
lines changed

1 file changed

+56
-14
lines changed

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

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import com.microsoft.semantickernel.connectors.ai.openai.chatcompletion.OpenAIChatHistory;
2121
import com.microsoft.semantickernel.memory.MemoryQueryResult;
2222
import com.microsoft.semantickernel.memory.MemoryRecord;
23+
import com.microsoft.semantickernel.orchestration.SKContext;
24+
import com.microsoft.semantickernel.orchestration.SKFunction;
25+
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig;
2326
import org.slf4j.Logger;
2427
import org.slf4j.LoggerFactory;
2528
import org.springframework.beans.factory.annotation.Value;
@@ -87,22 +90,12 @@ public RAGResponse run(ChatGPTConversation questionOrConversation, RAGOptions op
8790
//Build semantic kernel with Azure Cognitive Search as memory store. AnswerQuestion skill is imported from resources.
8891
Kernel semanticKernel = buildSemanticKernel(options);
8992

90-
/**
91-
* Use semantic kernel built-in memory.searchAsync. It uses OpenAI to generate embeddings for the provided question.
92-
* Question embeddings are provided to cognitive search via search options.
93-
*/
94-
List<MemoryQueryResult> memoryResult = semanticKernel.getMemory().searchAsync(
95-
indexName,
96-
question,
97-
options.getTop(),
98-
0.5f,
99-
false)
100-
.block();
93+
List<MemoryQueryResult> sourcesResult = getSourcesFromConversation(questionOrConversation, semanticKernel, options);
10194

102-
LOGGER.info("Total {} sources found in cognitive vector store for search query[{}]", memoryResult.size(), question);
95+
LOGGER.info("Total {} sources found in cognitive vector store for search query[{}]", sourcesResult.size(), question);
10396

104-
String sources = buildSourcesText(memoryResult);
105-
List<ContentSource> sourcesList = buildSources(memoryResult);
97+
String sources = buildSourcesText(sourcesResult);
98+
List<ContentSource> sourcesList = buildSources(sourcesResult);
10699

107100
// Use ChatCompletion Service to generate a reply
108101
OpenAIChatCompletion chat = (OpenAIChatCompletion) semanticKernel.getService(null, ChatCompletion.class);
@@ -125,6 +118,55 @@ public void runStreaming(ChatGPTConversation questionOrConversation, RAGOptions
125118
throw new IllegalStateException("Streaming not supported for this approach");
126119
}
127120

121+
private List<MemoryQueryResult> getSourcesFromConversation (ChatGPTConversation conversation, Kernel kernel, RAGOptions options) {
122+
String searchQueryPrompt = """
123+
Generate a search query for the below conversation.
124+
Do not include cited source filenames and document names e.g info.txt or doc.pdf in the search query terms.
125+
Do not include any text inside [] or <<>> in the search query terms.
126+
Do not enclose the search query in quotes or double quotes.
127+
conversation:
128+
{{$conversation}}
129+
""" ;
130+
131+
SKContext skcontext = SKBuilders.context().build()
132+
.setVariable("conversation", ChatGPTUtils.formatAsChatML(conversation.toOpenAIChatMessages()));
133+
134+
SKFunction searchQuery = kernel
135+
.getSemanticFunctionBuilder()
136+
.withPromptTemplate(searchQueryPrompt)
137+
.withFunctionName("searchQuery")
138+
.withCompletionConfig(
139+
new PromptTemplateConfig.CompletionConfig(
140+
0.2,
141+
1,
142+
0.0,
143+
0.0,
144+
1024
145+
)
146+
)
147+
.build();
148+
149+
Mono<SKContext> result = searchQuery.invokeAsync(skcontext);
150+
String query = result.block().getResult();
151+
152+
LOGGER.info("SEARCH QUERY");
153+
LOGGER.info(query);
154+
155+
/**
156+
* Use semantic kernel built-in memory.searchAsync. It uses OpenAI to generate embeddings for the provided question.
157+
* Question embeddings are provided to cognitive search via search options.
158+
*/
159+
List<MemoryQueryResult> memoryResult = kernel.getMemory().searchAsync(
160+
indexName,
161+
query,
162+
options.getTop(),
163+
0.5f,
164+
false)
165+
.block();
166+
167+
return memoryResult;
168+
}
169+
128170
private OpenAIChatHistory buildChatHistory(ChatGPTConversation conversation, RAGOptions options, OpenAIChatCompletion chat,
129171
String sources) {
130172
String systemMessage = SYSTEM_CHAT_MESSAGE_TEMPLATE.formatted(

0 commit comments

Comments
 (0)