6565import com .microsoft .semantickernel .orchestration .ToolCallBehavior ;
6666import com .microsoft .semantickernel .orchestration .responseformat .JsonResponseSchema ;
6767import com .microsoft .semantickernel .orchestration .responseformat .JsonSchemaResponseFormat ;
68- import com .microsoft .semantickernel .semanticfunctions .KernelFunction ;
6968import com .microsoft .semantickernel .semanticfunctions .KernelArguments ;
69+ import com .microsoft .semantickernel .semanticfunctions .KernelFunction ;
7070import com .microsoft .semantickernel .services .chatcompletion .AuthorRole ;
7171import com .microsoft .semantickernel .services .chatcompletion .ChatCompletionService ;
7272import com .microsoft .semantickernel .services .chatcompletion .ChatHistory ;
@@ -149,7 +149,7 @@ public Mono<List<ChatMessageContent<?>>> getChatMessageContentsAsync(
149149
150150 if (invocationContext != null
151151 && invocationContext
152- .returnMode () == InvocationReturnMode .LAST_MESSAGE_ONLY ) {
152+ .returnMode () == InvocationReturnMode .LAST_MESSAGE_ONLY ) {
153153 chatHistoryResult = new ChatHistory (
154154 Collections .singletonList (
155155 CollectionUtil .getLastOrNull (chatHistoryResult .getMessages ())));
@@ -183,7 +183,7 @@ public Mono<List<ChatMessageContent<?>>> getChatMessageContentsAsync(
183183
184184 if (invocationContext != null
185185 && invocationContext
186- .returnMode () == InvocationReturnMode .LAST_MESSAGE_ONLY ) {
186+ .returnMode () == InvocationReturnMode .LAST_MESSAGE_ONLY ) {
187187 result = new ChatHistory (
188188 Collections .singletonList (
189189 CollectionUtil .getLastOrNull (result .getMessages ())));
@@ -443,31 +443,31 @@ private Mono<ChatMessages> internalChatMessageContentsAsync(
443443 .getOptions ();
444444
445445 return Mono .deferContextual (contextView -> {
446- ChatCompletionSpan span = ChatCompletionSpan .startChatCompletionSpan (
447- SemanticKernelTelemetry .getTelemetry (invocationContext ),
448- contextView ,
449- getModelId (),
450- SemanticKernelTelemetry .OPEN_AI_PROVIDER ,
451- options .getMaxTokens (),
452- options .getTemperature (),
453- options .getTopP ());
454-
455- return getClient ()
456- .getChatCompletionsWithResponse (getDeploymentName (), options ,
457- OpenAIRequestSettings .getRequestOptions ())
458- .contextWrite (span .getReactorContextModifier ())
459- .flatMap (completionsResult -> {
460- if (completionsResult .getStatusCode () >= 400 ) {
461- return Mono .error (new AIException (ErrorCodes .SERVICE_ERROR ,
462- "Request failed: " + completionsResult .getStatusCode ()));
463- }
446+ ChatCompletionSpan span = ChatCompletionSpan .startChatCompletionSpan (
447+ SemanticKernelTelemetry .getTelemetry (invocationContext ),
448+ contextView ,
449+ getModelId (),
450+ SemanticKernelTelemetry .OPEN_AI_PROVIDER ,
451+ options .getMaxTokens (),
452+ options .getTemperature (),
453+ options .getTopP ());
454+
455+ return getClient ()
456+ .getChatCompletionsWithResponse (getDeploymentName (), options ,
457+ OpenAIRequestSettings .getRequestOptions ())
458+ .contextWrite (span .getReactorContextModifier ())
459+ .flatMap (completionsResult -> {
460+ if (completionsResult .getStatusCode () >= 400 ) {
461+ return Mono .error (new AIException (ErrorCodes .SERVICE_ERROR ,
462+ "Request failed: " + completionsResult .getStatusCode ()));
463+ }
464464
465- return Mono .just (completionsResult .getValue ());
466- })
467- .doOnError (span ::endSpanWithError )
468- .doOnSuccess (span ::endSpanWithUsage )
469- .doOnTerminate (span ::close );
470- })
465+ return Mono .just (completionsResult .getValue ());
466+ })
467+ .doOnError (span ::endSpanWithError )
468+ .doOnSuccess (span ::endSpanWithUsage )
469+ .doOnTerminate (span ::close );
470+ })
471471 .flatMap (completions -> {
472472 List <ChatResponseMessage > responseMessages = completions
473473 .getChoices ()
@@ -920,7 +920,8 @@ private static ChatCompletionsOptions getCompletionsOptions(
920920 }
921921
922922 Map <String , Integer > logit = null ;
923- if (promptExecutionSettings .getTokenSelectionBiases () != null ) {
923+ if (promptExecutionSettings .getTokenSelectionBiases () != null
924+ && !promptExecutionSettings .getTokenSelectionBiases ().isEmpty ()) {
924925 logit = promptExecutionSettings
925926 .getTokenSelectionBiases ()
926927 .entrySet ()
@@ -937,12 +938,13 @@ private static ChatCompletionsOptions getCompletionsOptions(
937938 .setFrequencyPenalty (promptExecutionSettings .getFrequencyPenalty ())
938939 .setPresencePenalty (promptExecutionSettings .getPresencePenalty ())
939940 .setMaxTokens (promptExecutionSettings .getMaxTokens ())
941+ .setMaxCompletionTokens (promptExecutionSettings .getMaxCompletionTokens ())
940942 .setN (promptExecutionSettings .getResultsPerPrompt ())
941943 // Azure OpenAI WithData API does not allow to send empty array of stop sequences
942944 // Gives back "Validation error at #/stop/str: Input should be a valid string\nValidation error at #/stop/list[str]: List should have at least 1 item after validation, not 0"
943945 .setStop (promptExecutionSettings .getStopSequences () == null
944946 || promptExecutionSettings .getStopSequences ().isEmpty () ? null
945- : promptExecutionSettings .getStopSequences ())
947+ : promptExecutionSettings .getStopSequences ())
946948 .setUser (promptExecutionSettings .getUser ())
947949 .setLogitBias (logit );
948950
@@ -1147,7 +1149,7 @@ private static OpenAIToolCallConfig getToolCallBehaviorConfig(
11471149 toolChoice ,
11481150 toolCallBehavior .isAutoInvokeAllowed ()
11491151 && requestIndex < Math .min (MAXIMUM_INFLIGHT_AUTO_INVOKES ,
1150- toolCallBehavior .getMaximumAutoInvokeAttempts ()),
1152+ toolCallBehavior .getMaximumAutoInvokeAttempts ()),
11511153 null );
11521154 }
11531155
@@ -1262,11 +1264,11 @@ private static ChatRequestAssistantMessage formAssistantMessage(
12621264
12631265 String args = arguments != null && !arguments .isEmpty ()
12641266 ? arguments .entrySet ().stream ()
1265- .map (entry -> String .format ("\" %s\" : \" %s\" " ,
1266- StringEscapeUtils .escapeJson (entry .getKey ()),
1267- StringEscapeUtils .escapeJson (
1268- entry .getValue ().toPromptString ())))
1269- .collect (Collectors .joining ("," , "{" , "}" ))
1267+ .map (entry -> String .format ("\" %s\" : \" %s\" " ,
1268+ StringEscapeUtils .escapeJson (entry .getKey ()),
1269+ StringEscapeUtils .escapeJson (
1270+ entry .getValue ().toPromptString ())))
1271+ .collect (Collectors .joining ("," , "{" , "}" ))
12701272 : "{}" ;
12711273
12721274 String prefix = "" ;
0 commit comments