mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Java: Add header to Java openai call (#4972)
### 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:
@@ -42,7 +42,15 @@
|
||||
<version>1.0-2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.microsoft.semantickernel.aiservices.openai;
|
||||
|
||||
import com.azure.core.http.HttpHeaderName;
|
||||
import com.azure.core.http.rest.RequestOptions;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public final class OpenAIRequestSettings {
|
||||
|
||||
private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(
|
||||
OpenAIRequestSettings.class);
|
||||
|
||||
private static final String SEMANTIC_KERNEL_VERSION_PROPERTY_NAME = "semantic-kernel.version";
|
||||
private static final String SEMANTIC_KERNEL_VERSION_PROPERTIES_FILE = "semantic-kernel-version.properties";
|
||||
private static final String version;
|
||||
|
||||
|
||||
static {
|
||||
version = loadVersion();
|
||||
}
|
||||
|
||||
private static String loadVersion() {
|
||||
|
||||
String version = "Java/unknown";
|
||||
|
||||
try (InputStream settingsFile = OpenAIRequestSettings.class.getResourceAsStream(
|
||||
SEMANTIC_KERNEL_VERSION_PROPERTIES_FILE)) {
|
||||
|
||||
Properties props = new Properties();
|
||||
props.load(settingsFile);
|
||||
if (props.containsKey(SEMANTIC_KERNEL_VERSION_PROPERTY_NAME)) {
|
||||
String skVersion = props.getProperty(SEMANTIC_KERNEL_VERSION_PROPERTY_NAME);
|
||||
if (skVersion != null && !skVersion.isEmpty()) {
|
||||
return "Java/" + skVersion;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
//Ignore
|
||||
LOGGER.trace("Failed to load Semantic Kernel version from properties file", e);
|
||||
}
|
||||
return version;
|
||||
}
|
||||
|
||||
public static RequestOptions getRequestOptions() {
|
||||
return new RequestOptions()
|
||||
.setHeader(HttpHeaderName.fromString("Semantic-Kernel-Version"), version);
|
||||
}
|
||||
}
|
||||
+11
-1
@@ -21,12 +21,14 @@ 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.OpenAIRequestSettings;
|
||||
import com.microsoft.semantickernel.chatcompletion.AuthorRole;
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatCompletionService;
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatHistory;
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatMessageContent;
|
||||
import com.microsoft.semantickernel.chatcompletion.StreamingChatMessageContent;
|
||||
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.PreChatCompletionEvent;
|
||||
@@ -165,7 +167,15 @@ public class OpenAIChatCompletion implements ChatCompletionService {
|
||||
.getOptions();
|
||||
|
||||
return 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());
|
||||
})
|
||||
.filter(choices -> choices.getChoices() != null && !choices.getChoices().isEmpty())
|
||||
.map(this::accumulateResponses)
|
||||
.map(responses ->
|
||||
|
||||
+11
-2
@@ -4,7 +4,9 @@ import com.azure.ai.openai.OpenAIAsyncClient;
|
||||
import com.azure.ai.openai.models.CompletionsOptions;
|
||||
import com.azure.ai.openai.models.CompletionsUsage;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.aiservices.openai.OpenAIRequestSettings;
|
||||
import com.microsoft.semantickernel.exceptions.AIException;
|
||||
import com.microsoft.semantickernel.exceptions.AIException.ErrorCodes;
|
||||
import com.microsoft.semantickernel.orchestration.FunctionResultMetadata;
|
||||
import com.microsoft.semantickernel.orchestration.PromptExecutionSettings;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariable;
|
||||
@@ -90,9 +92,16 @@ public class OpenAITextGenerationService implements TextGenerationService {
|
||||
CompletionsOptions completionsOptions = getCompletionsOptions(text, requestSettings);
|
||||
|
||||
return client
|
||||
.getCompletions(getModelId(), completionsOptions)
|
||||
.getCompletionsWithResponse(getModelId(), completionsOptions,
|
||||
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());
|
||||
})
|
||||
.map(completions -> {
|
||||
|
||||
FunctionResultMetadata metadata = FunctionResultMetadata.build(
|
||||
completions.getId(),
|
||||
completions.getUsage(),
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
semantic-kernel.version=${project.version}
|
||||
Reference in New Issue
Block a user