1+ // Copyright (c) Microsoft. All rights reserved.
12package com .microsoft .semantickernel .agents .chatcompletion ;
23
34import com .microsoft .semantickernel .Kernel ;
@@ -37,8 +38,7 @@ private ChatCompletionAgent(
3738 KernelArguments kernelArguments ,
3839 InvocationContext context ,
3940 String instructions ,
40- PromptTemplate template
41- ) {
41+ PromptTemplate template ) {
4242 super (
4343 id ,
4444 name ,
@@ -47,8 +47,7 @@ private ChatCompletionAgent(
4747 kernelArguments ,
4848 context ,
4949 instructions ,
50- template
51- );
50+ template );
5251 }
5352
5453 /**
@@ -61,65 +60,65 @@ private ChatCompletionAgent(
6160 */
6261 @ Override
6362 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 ) {
6866 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+ });
8883 }
8984
9085 private Mono <List <ChatMessageContent <?>>> internalInvokeAsync (
9186 ChatHistory history ,
9287 AgentThread thread ,
93- @ Nullable AgentInvokeOptions options
94- ) {
88+ @ Nullable AgentInvokeOptions options ) {
9589 if (options == null ) {
9690 options = new AgentInvokeOptions ();
9791 }
9892
9993 final Kernel kernel = options .getKernel () != null ? options .getKernel () : this .kernel ;
10094 final KernelArguments arguments = mergeArguments (options .getKernelArguments ());
10195 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 ;
10399
104100 try {
105- ChatCompletionService chatCompletionService = kernel .getService (ChatCompletionService .class , arguments );
101+ ChatCompletionService chatCompletionService = kernel
102+ .getService (ChatCompletionService .class , arguments );
106103
107- PromptExecutionSettings executionSettings = invocationContext != null && invocationContext .getPromptExecutionSettings () != null
104+ PromptExecutionSettings executionSettings = invocationContext != null
105+ && invocationContext .getPromptExecutionSettings () != null
108106 ? invocationContext .getPromptExecutionSettings ()
109- : kernelArguments .getExecutionSettings ().get (chatCompletionService .getServiceId ());
107+ : kernelArguments .getExecutionSettings ()
108+ .get (chatCompletionService .getServiceId ());
110109
111110 // Build base invocation context
112111 InvocationContext .Builder builder = InvocationContext .builder ()
113- .withPromptExecutionSettings (executionSettings )
114- .withReturnMode (InvocationReturnMode .NEW_MESSAGES_ONLY );
112+ .withPromptExecutionSettings (executionSettings )
113+ .withReturnMode (InvocationReturnMode .NEW_MESSAGES_ONLY );
115114
116115 if (invocationContext != null ) {
117116 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 ());
123122 }
124123
125124 InvocationContext agentInvocationContext = builder .build ();
@@ -128,15 +127,13 @@ private Mono<List<ChatMessageContent<?>>> internalInvokeAsync(
128127 instructions -> {
129128 // Create a new chat history with the instructions
130129 ChatHistory chat = new ChatHistory (
131- instructions
132- );
130+ instructions );
133131
134132 // Add agent additional instructions
135133 if (additionalInstructions != null ) {
136134 chat .addMessage (new ChatMessageContent <>(
137- AuthorRole .SYSTEM ,
138- additionalInstructions
139- ));
135+ AuthorRole .SYSTEM ,
136+ additionalInstructions ));
140137 }
141138
142139 // Add the chat history to the new chat
@@ -145,20 +142,23 @@ private Mono<List<ChatMessageContent<?>>> internalInvokeAsync(
145142 // Retrieve the chat message contents asynchronously and notify the thread
146143 if (shouldNotifyFunctionCalls (agentInvocationContext )) {
147144 // Notify all messages including function calls
148- return chatCompletionService .getChatMessageContentsAsync (chat , kernel , agentInvocationContext )
145+ return chatCompletionService
146+ .getChatMessageContentsAsync (chat , kernel , agentInvocationContext )
149147 .flatMapMany (Flux ::fromIterable )
150- .concatMap (message -> notifyThreadOfNewMessageAsync (thread , message ).thenReturn (message ))
148+ .concatMap (message -> notifyThreadOfNewMessageAsync (thread , message )
149+ .thenReturn (message ))
151150 // 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 )
153153 .collect (Collectors .toList ());
154154 }
155155
156156 // Return chat completion messages without notifying the thread
157157 // We shouldn't add the function call content to the thread, since
158158 // 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+ } );
162162
163163 } catch (ServiceNotFoundException e ) {
164164 return Mono .error (e );
@@ -170,8 +170,10 @@ boolean shouldNotifyFunctionCalls(InvocationContext invocationContext) {
170170 return false ;
171171 }
172172
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 ();
175177 }
176178
177179 if (invocationContext .getToolCallBehavior () != null ) {
@@ -181,9 +183,9 @@ boolean shouldNotifyFunctionCalls(InvocationContext invocationContext) {
181183 return false ;
182184 }
183185
184-
185186 @ Override
186- public Mono <Void > notifyThreadOfNewMessageAsync (AgentThread thread , ChatMessageContent <?> message ) {
187+ public Mono <Void > notifyThreadOfNewMessageAsync (AgentThread thread ,
188+ ChatMessageContent <?> message ) {
187189 return Mono .defer (() -> {
188190 return thread .onNewMessageAsync (message );
189191 });
@@ -298,11 +300,10 @@ public ChatCompletionAgent build() {
298300 name ,
299301 description ,
300302 kernel ,
301- kernelArguments ,
303+ kernelArguments ,
302304 invocationContext ,
303305 instructions ,
304- template
305- );
306+ template );
306307 }
307308
308309 /**
@@ -312,17 +313,17 @@ public ChatCompletionAgent build() {
312313 * @param promptTemplateFactory The prompt template factory to use.
313314 * @return The ChatCompletionAgent instance.
314315 */
315- public ChatCompletionAgent build (PromptTemplateConfig promptTemplateConfig , PromptTemplateFactory promptTemplateFactory ) {
316+ public ChatCompletionAgent build (PromptTemplateConfig promptTemplateConfig ,
317+ PromptTemplateFactory promptTemplateFactory ) {
316318 return new ChatCompletionAgent (
317319 id ,
318320 name ,
319321 description ,
320322 kernel ,
321- kernelArguments ,
323+ kernelArguments ,
322324 invocationContext ,
323325 promptTemplateConfig .getTemplate (),
324- promptTemplateFactory .tryCreate (promptTemplateConfig )
325- );
326+ promptTemplateFactory .tryCreate (promptTemplateConfig ));
326327 }
327328 }
328329}
0 commit comments