Java: Add header for chat completion requests (#5768)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
This commit is contained in:
John Oliver
2024-04-04 19:11:23 +01:00
committed by GitHub
parent 9e27b9d302
commit 8012ec2b04
@@ -20,9 +20,11 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.aiservices.openai.implementation.OpenAIRequestSettings;
import com.microsoft.semantickernel.contextvariables.ContextVariable;
import com.microsoft.semantickernel.contextvariables.ContextVariableTypes;
import com.microsoft.semantickernel.exceptions.AIException;
import com.microsoft.semantickernel.exceptions.AIException.ErrorCodes;
import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.hooks.KernelHooks;
import com.microsoft.semantickernel.hooks.PostChatCompletionEvent;
@@ -154,7 +156,15 @@ public class OpenAIChatCompletion implements ChatCompletionService {
.getOptions();
Mono<List<? extends ChatMessageContent>> result = client
.getChatCompletions(getModelId(), options)
.getChatCompletionsWithResponse(getModelId(), options,
OpenAIRequestSettings.getRequestOptions())
.flatMap(completionsResult -> {
if (completionsResult.getStatusCode() >= 400) {
return Mono.error(new AIException(ErrorCodes.SERVICE_ERROR,
"Request failed: " + completionsResult.getStatusCode()));
}
return Mono.just(completionsResult.getValue());
})
.flatMap(completions -> {
List<ChatResponseMessage> responseMessages = completions
.getChoices()