1
+ // Copyright (c) Microsoft. All rights reserved.
1
2
package com .microsoft .semantickernel .agents .chatcompletion ;
2
3
3
4
import com .microsoft .semantickernel .Kernel ;
@@ -37,8 +38,7 @@ private ChatCompletionAgent(
37
38
KernelArguments kernelArguments ,
38
39
InvocationContext context ,
39
40
String instructions ,
40
- PromptTemplate template
41
- ) {
41
+ PromptTemplate template ) {
42
42
super (
43
43
id ,
44
44
name ,
@@ -47,8 +47,7 @@ private ChatCompletionAgent(
47
47
kernelArguments ,
48
48
context ,
49
49
instructions ,
50
- template
51
- );
50
+ template );
52
51
}
53
52
54
53
/**
@@ -61,65 +60,65 @@ private ChatCompletionAgent(
61
60
*/
62
61
@ Override
63
62
public Mono <List <AgentResponseItem <ChatMessageContent <?>>>> invokeAsync (
64
- List <ChatMessageContent <?>> messages ,
65
- @ Nullable AgentThread thread ,
66
- @ Nullable AgentInvokeOptions options
67
- ) {
63
+ List <ChatMessageContent <?>> messages ,
64
+ @ Nullable AgentThread thread ,
65
+ @ Nullable AgentInvokeOptions options ) {
68
66
return ensureThreadExistsWithMessagesAsync (messages , thread , ChatHistoryAgentThread ::new )
69
- .cast (ChatHistoryAgentThread .class )
70
- .flatMap (agentThread -> {
71
- // Extract the chat history from the thread
72
- ChatHistory history = new ChatHistory (
73
- agentThread .getChatHistory ().getMessages ()
74
- );
75
-
76
- // Invoke the agent with the chat history
77
- return internalInvokeAsync (
78
- history ,
79
- agentThread ,
80
- options
81
- )
82
- .map (chatMessageContents ->
83
- chatMessageContents .stream ()
84
- .map (message -> new AgentResponseItem <ChatMessageContent <?>>(message , agentThread ))
85
- .collect (Collectors .toList ())
86
- );
87
- });
67
+ .cast (ChatHistoryAgentThread .class )
68
+ .flatMap (agentThread -> {
69
+ // Extract the chat history from the thread
70
+ ChatHistory history = new ChatHistory (
71
+ agentThread .getChatHistory ().getMessages ());
72
+
73
+ // Invoke the agent with the chat history
74
+ return internalInvokeAsync (
75
+ history ,
76
+ agentThread ,
77
+ options )
78
+ .map (chatMessageContents -> chatMessageContents .stream ()
79
+ .map (message -> new AgentResponseItem <ChatMessageContent <?>>(message ,
80
+ agentThread ))
81
+ .collect (Collectors .toList ()));
82
+ });
88
83
}
89
84
90
85
private Mono <List <ChatMessageContent <?>>> internalInvokeAsync (
91
86
ChatHistory history ,
92
87
AgentThread thread ,
93
- @ Nullable AgentInvokeOptions options
94
- ) {
88
+ @ Nullable AgentInvokeOptions options ) {
95
89
if (options == null ) {
96
90
options = new AgentInvokeOptions ();
97
91
}
98
92
99
93
final Kernel kernel = options .getKernel () != null ? options .getKernel () : this .kernel ;
100
94
final KernelArguments arguments = mergeArguments (options .getKernelArguments ());
101
95
final String additionalInstructions = options .getAdditionalInstructions ();
102
- final InvocationContext invocationContext = options .getInvocationContext () != null ? options .getInvocationContext () : this .invocationContext ;
96
+ final InvocationContext invocationContext = options .getInvocationContext () != null
97
+ ? options .getInvocationContext ()
98
+ : this .invocationContext ;
103
99
104
100
try {
105
- ChatCompletionService chatCompletionService = kernel .getService (ChatCompletionService .class , arguments );
101
+ ChatCompletionService chatCompletionService = kernel
102
+ .getService (ChatCompletionService .class , arguments );
106
103
107
- PromptExecutionSettings executionSettings = invocationContext != null && invocationContext .getPromptExecutionSettings () != null
104
+ PromptExecutionSettings executionSettings = invocationContext != null
105
+ && invocationContext .getPromptExecutionSettings () != null
108
106
? invocationContext .getPromptExecutionSettings ()
109
- : kernelArguments .getExecutionSettings ().get (chatCompletionService .getServiceId ());
107
+ : kernelArguments .getExecutionSettings ()
108
+ .get (chatCompletionService .getServiceId ());
110
109
111
110
// Build base invocation context
112
111
InvocationContext .Builder builder = InvocationContext .builder ()
113
- .withPromptExecutionSettings (executionSettings )
114
- .withReturnMode (InvocationReturnMode .NEW_MESSAGES_ONLY );
112
+ .withPromptExecutionSettings (executionSettings )
113
+ .withReturnMode (InvocationReturnMode .NEW_MESSAGES_ONLY );
115
114
116
115
if (invocationContext != null ) {
117
116
builder = builder
118
- .withTelemetry (invocationContext .getTelemetry ())
119
- .withFunctionChoiceBehavior (invocationContext .getFunctionChoiceBehavior ())
120
- .withToolCallBehavior (invocationContext .getToolCallBehavior ())
121
- .withContextVariableConverter (invocationContext .getContextVariableTypes ())
122
- .withKernelHooks (invocationContext .getKernelHooks ());
117
+ .withTelemetry (invocationContext .getTelemetry ())
118
+ .withFunctionChoiceBehavior (invocationContext .getFunctionChoiceBehavior ())
119
+ .withToolCallBehavior (invocationContext .getToolCallBehavior ())
120
+ .withContextVariableConverter (invocationContext .getContextVariableTypes ())
121
+ .withKernelHooks (invocationContext .getKernelHooks ());
123
122
}
124
123
125
124
InvocationContext agentInvocationContext = builder .build ();
@@ -128,15 +127,13 @@ private Mono<List<ChatMessageContent<?>>> internalInvokeAsync(
128
127
instructions -> {
129
128
// Create a new chat history with the instructions
130
129
ChatHistory chat = new ChatHistory (
131
- instructions
132
- );
130
+ instructions );
133
131
134
132
// Add agent additional instructions
135
133
if (additionalInstructions != null ) {
136
134
chat .addMessage (new ChatMessageContent <>(
137
- AuthorRole .SYSTEM ,
138
- additionalInstructions
139
- ));
135
+ AuthorRole .SYSTEM ,
136
+ additionalInstructions ));
140
137
}
141
138
142
139
// Add the chat history to the new chat
@@ -145,20 +142,23 @@ private Mono<List<ChatMessageContent<?>>> internalInvokeAsync(
145
142
// Retrieve the chat message contents asynchronously and notify the thread
146
143
if (shouldNotifyFunctionCalls (agentInvocationContext )) {
147
144
// Notify all messages including function calls
148
- return chatCompletionService .getChatMessageContentsAsync (chat , kernel , agentInvocationContext )
145
+ return chatCompletionService
146
+ .getChatMessageContentsAsync (chat , kernel , agentInvocationContext )
149
147
.flatMapMany (Flux ::fromIterable )
150
- .concatMap (message -> notifyThreadOfNewMessageAsync (thread , message ).thenReturn (message ))
148
+ .concatMap (message -> notifyThreadOfNewMessageAsync (thread , message )
149
+ .thenReturn (message ))
151
150
// Filter out function calls and their results
152
- .filter (message -> message .getContent () != null && message .getAuthorRole () != AuthorRole .TOOL )
151
+ .filter (message -> message .getContent () != null
152
+ && message .getAuthorRole () != AuthorRole .TOOL )
153
153
.collect (Collectors .toList ());
154
154
}
155
155
156
156
// Return chat completion messages without notifying the thread
157
157
// We shouldn't add the function call content to the thread, since
158
158
// we don't know if the user will execute the call. They should add it themselves.
159
- return chatCompletionService .getChatMessageContentsAsync (chat , kernel , agentInvocationContext );
160
- }
161
- );
159
+ return chatCompletionService .getChatMessageContentsAsync (chat , kernel ,
160
+ agentInvocationContext );
161
+ } );
162
162
163
163
} catch (ServiceNotFoundException e ) {
164
164
return Mono .error (e );
@@ -170,8 +170,10 @@ boolean shouldNotifyFunctionCalls(InvocationContext invocationContext) {
170
170
return false ;
171
171
}
172
172
173
- if (invocationContext .getFunctionChoiceBehavior () != null && invocationContext .getFunctionChoiceBehavior () instanceof AutoFunctionChoiceBehavior ) {
174
- return ((AutoFunctionChoiceBehavior ) invocationContext .getFunctionChoiceBehavior ()).isAutoInvoke ();
173
+ if (invocationContext .getFunctionChoiceBehavior () != null && invocationContext
174
+ .getFunctionChoiceBehavior () instanceof AutoFunctionChoiceBehavior ) {
175
+ return ((AutoFunctionChoiceBehavior ) invocationContext .getFunctionChoiceBehavior ())
176
+ .isAutoInvoke ();
175
177
}
176
178
177
179
if (invocationContext .getToolCallBehavior () != null ) {
@@ -181,9 +183,9 @@ boolean shouldNotifyFunctionCalls(InvocationContext invocationContext) {
181
183
return false ;
182
184
}
183
185
184
-
185
186
@ Override
186
- public Mono <Void > notifyThreadOfNewMessageAsync (AgentThread thread , ChatMessageContent <?> message ) {
187
+ public Mono <Void > notifyThreadOfNewMessageAsync (AgentThread thread ,
188
+ ChatMessageContent <?> message ) {
187
189
return Mono .defer (() -> {
188
190
return thread .onNewMessageAsync (message );
189
191
});
@@ -298,11 +300,10 @@ public ChatCompletionAgent build() {
298
300
name ,
299
301
description ,
300
302
kernel ,
301
- kernelArguments ,
303
+ kernelArguments ,
302
304
invocationContext ,
303
305
instructions ,
304
- template
305
- );
306
+ template );
306
307
}
307
308
308
309
/**
@@ -312,17 +313,17 @@ public ChatCompletionAgent build() {
312
313
* @param promptTemplateFactory The prompt template factory to use.
313
314
* @return The ChatCompletionAgent instance.
314
315
*/
315
- public ChatCompletionAgent build (PromptTemplateConfig promptTemplateConfig , PromptTemplateFactory promptTemplateFactory ) {
316
+ public ChatCompletionAgent build (PromptTemplateConfig promptTemplateConfig ,
317
+ PromptTemplateFactory promptTemplateFactory ) {
316
318
return new ChatCompletionAgent (
317
319
id ,
318
320
name ,
319
321
description ,
320
322
kernel ,
321
- kernelArguments ,
323
+ kernelArguments ,
322
324
invocationContext ,
323
325
promptTemplateConfig .getTemplate (),
324
- promptTemplateFactory .tryCreate (promptTemplateConfig )
325
- );
326
+ promptTemplateFactory .tryCreate (promptTemplateConfig ));
326
327
}
327
328
}
328
329
}
0 commit comments