6
6
import com .microsoft .openai .samples .rag .approaches .RAGApproach ;
7
7
import com .microsoft .openai .samples .rag .approaches .RAGOptions ;
8
8
import com .microsoft .openai .samples .rag .approaches .RAGResponse ;
9
- import com .microsoft .openai .samples .rag .retrieval .semantickernel .CognitiveSearchPlugin ;
10
9
import com .microsoft .openai .samples .rag .proxy .CognitiveSearchProxy ;
11
10
import com .microsoft .openai .samples .rag .proxy .OpenAIProxy ;
11
+ import com .microsoft .openai .samples .rag .retrieval .semantickernel .CognitiveSearchPlugin ;
12
12
import com .microsoft .semantickernel .Kernel ;
13
- import com .microsoft .semantickernel .SKBuilders ;
14
- import com .microsoft .semantickernel .orchestration .SKContext ;
13
+ import com .microsoft .semantickernel .orchestration .FunctionResult ;
14
+ import com .microsoft .semantickernel .plugin .KernelPluginFactory ;
15
+ import com .microsoft .semantickernel .semanticfunctions .KernelFunctionArguments ;
16
+ import com .microsoft .semantickernel .services .chatcompletion .ChatCompletionService ;
17
+ import org .slf4j .Logger ;
18
+ import org .slf4j .LoggerFactory ;
19
+ import org .springframework .beans .factory .annotation .Value ;
20
+ import org .springframework .stereotype .Component ;
21
+
15
22
import java .io .OutputStream ;
16
23
import java .util .Arrays ;
17
24
import java .util .Collections ;
18
25
import java .util .List ;
19
26
import java .util .Objects ;
20
27
import java .util .stream .Collectors ;
21
- import org .slf4j .Logger ;
22
- import org .slf4j .LoggerFactory ;
23
- import org .springframework .beans .factory .annotation .Value ;
24
- import org .springframework .stereotype .Component ;
25
28
26
29
/**
27
30
* Use Java Semantic Kernel framework with semantic and native functions chaining. It uses an
@@ -36,8 +39,8 @@ public class JavaSemanticKernelChainsApproach implements RAGApproach<String, RAG
36
39
LoggerFactory .getLogger (JavaSemanticKernelChainsApproach .class );
37
40
private static final String PLAN_PROMPT =
38
41
"""
39
- Take the input as a question and answer it finding any information needed
40
- """ ;
42
+ Take the input as a question and answer it finding any information needed
43
+ """ ;
41
44
private final CognitiveSearchProxy cognitiveSearchProxy ;
42
45
43
46
private final OpenAIProxy openAIProxy ;
@@ -69,21 +72,24 @@ public RAGResponse run(String question, RAGOptions options) {
69
72
70
73
// STEP 1: Retrieve relevant documents using user question. It reuses the
71
74
// CognitiveSearchRetriever appraoch through the CognitiveSearchPlugin native function.
72
- SKContext searchContext =
73
- semanticKernel
74
- .runAsync (
75
- question ,
76
- semanticKernel
77
- .getSkill ("InformationFinder" )
78
- .getFunction ("SearchFromQuestion" , null ))
79
- .block ();
75
+ FunctionResult <String > searchContext = semanticKernel
76
+ .getPlugin ("InformationFinder" )
77
+ .get ("SearchFromQuestion" )
78
+ .invokeAsync (semanticKernel )
79
+ .withArguments (
80
+ KernelFunctionArguments .builder ()
81
+ .withInput (question )
82
+ .build ()
83
+ )
84
+ .withResultType (String .class )
85
+ .block ();
80
86
81
87
var sources = formSourcesList (searchContext .getResult ());
82
88
83
89
// STEP 2: Build a SK context with the sources retrieved from the memory store and the user
84
90
// question.
85
91
var answerVariables =
86
- SKBuilders . variables ()
92
+ KernelFunctionArguments . builder ()
87
93
.withVariable ("sources" , searchContext .getResult ())
88
94
.withVariable ("input" , question )
89
95
.build ();
@@ -93,12 +99,12 @@ public RAGResponse run(String question, RAGOptions options) {
93
99
* (a.k.a. skill) from the SK skills registry and provide it with the pre-built context.
94
100
* Triggering Open AI to get an answerVariables.
95
101
*/
96
- SKContext answerExecutionContext =
97
- semanticKernel
98
- . runAsync (
99
- answerVariables ,
100
- semanticKernel . getSkill ( "RAG" ). getFunction ( "AnswerQuestion" , null ))
101
- . block ();
102
+ FunctionResult < String > answerExecutionContext = semanticKernel
103
+ . invokeAsync ( "RAG" , "AnswerQuestion" )
104
+ . withArguments ( answerVariables )
105
+ . withResultType ( String . class )
106
+ . block ();
107
+
102
108
return new RAGResponse .Builder ()
103
109
.prompt ("Prompt is managed by Semantic Kernel" )
104
110
.answer (answerExecutionContext .getResult ())
@@ -144,20 +150,28 @@ private List<ContentSource> formSourcesList(String result) {
144
150
* @return
145
151
*/
146
152
private Kernel buildSemanticKernel (RAGOptions options ) {
147
- Kernel kernel =
148
- SKBuilders .kernel ()
149
- .withDefaultAIService (
150
- SKBuilders .chatCompletion ()
151
- .withModelId (gptChatDeploymentModelId )
152
- .withOpenAIClient (this .openAIAsyncClient )
153
- .build ())
154
- .build ();
155
-
156
- kernel .importSkill (
157
- new CognitiveSearchPlugin (this .cognitiveSearchProxy , this .openAIProxy , options ),
158
- "InformationFinder" );
159
- kernel .importSkillFromResources ("semantickernel/Plugins" , "RAG" , "AnswerQuestion" , null );
160
-
161
- return kernel ;
153
+ return Kernel .builder ()
154
+ .withAIService (
155
+ ChatCompletionService .class ,
156
+ ChatCompletionService .builder ()
157
+ .withModelId (gptChatDeploymentModelId )
158
+ .withOpenAIAsyncClient (this .openAIAsyncClient )
159
+ .build ()
160
+ )
161
+ .withPlugin (
162
+ KernelPluginFactory .createFromObject (
163
+ new CognitiveSearchPlugin (this .cognitiveSearchProxy , this .openAIProxy , options ),
164
+ "InformationFinder" )
165
+ )
166
+ .withPlugin (
167
+ KernelPluginFactory .importPluginFromResourcesDirectory (
168
+ "semantickernel/Plugins" ,
169
+ "RAG" ,
170
+ "AnswerQuestion" ,
171
+ null ,
172
+ String .class
173
+ )
174
+ )
175
+ .build ();
162
176
}
163
177
}
0 commit comments