20
20
import com .microsoft .semantickernel .connectors .ai .openai .chatcompletion .OpenAIChatHistory ;
21
21
import com .microsoft .semantickernel .memory .MemoryQueryResult ;
22
22
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 ;
23
26
import org .slf4j .Logger ;
24
27
import org .slf4j .LoggerFactory ;
25
28
import org .springframework .beans .factory .annotation .Value ;
@@ -87,22 +90,12 @@ public RAGResponse run(ChatGPTConversation questionOrConversation, RAGOptions op
87
90
//Build semantic kernel with Azure Cognitive Search as memory store. AnswerQuestion skill is imported from resources.
88
91
Kernel semanticKernel = buildSemanticKernel (options );
89
92
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 );
101
94
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 );
103
96
104
- String sources = buildSourcesText (memoryResult );
105
- List <ContentSource > sourcesList = buildSources (memoryResult );
97
+ String sources = buildSourcesText (sourcesResult );
98
+ List <ContentSource > sourcesList = buildSources (sourcesResult );
106
99
107
100
// Use ChatCompletion Service to generate a reply
108
101
OpenAIChatCompletion chat = (OpenAIChatCompletion ) semanticKernel .getService (null , ChatCompletion .class );
@@ -125,6 +118,55 @@ public void runStreaming(ChatGPTConversation questionOrConversation, RAGOptions
125
118
throw new IllegalStateException ("Streaming not supported for this approach" );
126
119
}
127
120
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
+
128
170
private OpenAIChatHistory buildChatHistory (ChatGPTConversation conversation , RAGOptions options , OpenAIChatCompletion chat ,
129
171
String sources ) {
130
172
String systemMessage = SYSTEM_CHAT_MESSAGE_TEMPLATE .formatted (
0 commit comments