mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Java: Set Useragent for connectors. Extract settings classes to their own module (#3320)
- Set Useragent for connectors to Semantic-Kernel-Java. - Extract settings classes to their own optional module
This commit is contained in:
@@ -54,6 +54,10 @@
|
||||
<artifactId>semantickernel-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.semantic-kernel</groupId>
|
||||
<artifactId>semantickernel-settings-loader</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.semantic-kernel</groupId>
|
||||
<artifactId>semantickernel-connectors-ai-openai</artifactId>
|
||||
@@ -61,4 +65,4 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
+3
-5
@@ -23,6 +23,7 @@ import com.azure.search.documents.models.IndexDocumentsOptions;
|
||||
import com.azure.search.documents.models.IndexingResult;
|
||||
import com.azure.search.documents.models.SearchOptions;
|
||||
import com.azure.search.documents.models.SearchQueryVector;
|
||||
import com.microsoft.semantickernel.SemanticKernelHttpSettings;
|
||||
import com.microsoft.semantickernel.ai.embeddings.Embedding;
|
||||
import com.microsoft.semantickernel.connectors.memory.azurecognitivesearch.AzureCognitiveSearchMemoryException.ErrorCodes;
|
||||
import com.microsoft.semantickernel.memory.MemoryRecord;
|
||||
@@ -49,8 +50,6 @@ import reactor.util.function.Tuples;
|
||||
* Cognitive Search {@see https://learn.microsoft.com/azure/search/search-what-is-azure-search}
|
||||
*/
|
||||
public class AzureCognitiveSearchMemoryStore implements MemoryStore {
|
||||
|
||||
private static final String USER_AGENT = "Semantic-Kernel";
|
||||
private static final String SEARCH_CONFIG_NAME = "searchConfig";
|
||||
|
||||
private final SearchIndexAsyncClient _adminClient;
|
||||
@@ -477,10 +476,9 @@ public class AzureCognitiveSearchMemoryStore implements MemoryStore {
|
||||
// https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/src/DiagnosticsOptions.cs
|
||||
/// </summary>
|
||||
private static ClientOptions clientOptions() {
|
||||
return new ClientOptions()
|
||||
return SemanticKernelHttpSettings.getUserAgent()
|
||||
.setTracingOptions(new TracingOptions())
|
||||
.setMetricsOptions(new MetricsOptions())
|
||||
.setApplicationId(USER_AGENT);
|
||||
.setMetricsOptions(new MetricsOptions());
|
||||
}
|
||||
|
||||
// Index names cannot contain special chars. We use this rule to replace a few common ones
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.microsoft.semantic-kernel</groupId>
|
||||
<artifactId>semantickernel-parent</artifactId>
|
||||
<version>0.2.10-alpha-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>semantickernel-settings-loader</artifactId>
|
||||
<name>Semantic Kernel Settings Loader</name>
|
||||
<description>Semantic Kernel Settings Loader</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.semantic-kernel</groupId>
|
||||
<artifactId>semantickernel-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
package com.microsoft.semantickernel;
|
||||
|
||||
import com.azure.core.util.ClientOptions;
|
||||
import com.azure.core.util.Header;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class SemanticKernelHttpSettings {
|
||||
|
||||
public static final String DEFAULT_USER_AGENT = "Semantic-Kernel-Java";
|
||||
|
||||
public static ClientOptions getUserAgent(@Nullable Map<String, String> configuredSettings) {
|
||||
|
||||
String useragent;
|
||||
|
||||
if (configuredSettings == null) {
|
||||
useragent = DEFAULT_USER_AGENT;
|
||||
} else {
|
||||
useragent = configuredSettings.getOrDefault("useragent", DEFAULT_USER_AGENT);
|
||||
}
|
||||
|
||||
return new ClientOptions()
|
||||
.setHeaders(Collections.singletonList(new Header("User-Agent", useragent)));
|
||||
}
|
||||
|
||||
public static ClientOptions getUserAgent() {
|
||||
return getUserAgent(null);
|
||||
}
|
||||
}
|
||||
+8
@@ -7,7 +7,10 @@ import com.azure.ai.openai.OpenAIAsyncClient;
|
||||
import com.azure.ai.openai.OpenAIClientBuilder;
|
||||
import com.azure.core.credential.AzureKeyCredential;
|
||||
import com.azure.core.credential.KeyCredential;
|
||||
import com.azure.core.util.ClientOptions;
|
||||
import com.microsoft.semantickernel.SemanticKernelHttpSettings;
|
||||
import com.microsoft.semantickernel.exceptions.ConfigurationException;
|
||||
import com.microsoft.semantickernel.settings.SettingsMap;
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -24,6 +27,7 @@ import org.slf4j.Logger;
|
||||
* OpenAI client.
|
||||
*/
|
||||
public class OpenAIClientProvider {
|
||||
|
||||
private static final Logger LOGGER =
|
||||
org.slf4j.LoggerFactory.getLogger(OpenAIClientProvider.class);
|
||||
|
||||
@@ -45,6 +49,7 @@ public class OpenAIClientProvider {
|
||||
|
||||
@Nullable private final ClientType clientType;
|
||||
private final Map<String, String> configuredSettings;
|
||||
private final ClientOptions clientOptions;
|
||||
|
||||
/**
|
||||
* Create a new OpenAI client provider
|
||||
@@ -56,6 +61,7 @@ public class OpenAIClientProvider {
|
||||
Map<String, String> configuredSettings, @Nullable ClientType clientType) {
|
||||
this.configuredSettings = Collections.unmodifiableMap(configuredSettings);
|
||||
this.clientType = clientType;
|
||||
this.clientOptions = SemanticKernelHttpSettings.getUserAgent(configuredSettings);
|
||||
}
|
||||
|
||||
private static OpenAIClientProvider buildProvider(
|
||||
@@ -222,6 +228,7 @@ public class OpenAIClientProvider {
|
||||
|
||||
return new OpenAIClientBuilder()
|
||||
.credential(new KeyCredential(settings.getKey()))
|
||||
.clientOptions(clientOptions)
|
||||
.buildAsyncClient();
|
||||
}
|
||||
|
||||
@@ -239,6 +246,7 @@ public class OpenAIClientProvider {
|
||||
OpenAIClientBuilder builder =
|
||||
new OpenAIClientBuilder()
|
||||
.endpoint(settings.getEndpoint())
|
||||
.clientOptions(clientOptions)
|
||||
.credential(new AzureKeyCredential(settings.getKey()));
|
||||
|
||||
return builder.buildAsyncClient();
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
package com.microsoft.semantickernel.connectors.ai.openai.util;
|
||||
package com.microsoft.semantickernel.settings;
|
||||
|
||||
import static com.microsoft.semantickernel.exceptions.ConfigurationException.ErrorCodes.CONFIGURATION_NOT_FOUND;
|
||||
import static com.microsoft.semantickernel.exceptions.ConfigurationException.ErrorCodes.COULD_NOT_READ_CONFIGURATION;
|
||||
@@ -73,6 +73,7 @@
|
||||
|
||||
<module>extensions/semantickernel-planners</module>
|
||||
<module>extensions/semantickernel-gpt3-tokenizer</module>
|
||||
<module>extensions/semantickernel-settings-loader</module>
|
||||
|
||||
<module>plugins/semantickernel-plugin-core</module>
|
||||
|
||||
|
||||
@@ -72,6 +72,10 @@
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.semantic-kernel</groupId>
|
||||
<artifactId>semantickernel-settings-loader</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+1
-1
@@ -9,10 +9,10 @@ import com.azure.core.http.policy.RetryOptions;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.SKBuilders;
|
||||
import com.microsoft.semantickernel.connectors.ai.openai.util.AzureOpenAISettings;
|
||||
import com.microsoft.semantickernel.connectors.ai.openai.util.SettingsMap;
|
||||
import com.microsoft.semantickernel.exceptions.ConfigurationException;
|
||||
import com.microsoft.semantickernel.orchestration.SKContext;
|
||||
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig;
|
||||
import com.microsoft.semantickernel.settings.SettingsMap;
|
||||
import com.microsoft.semantickernel.textcompletion.CompletionSKFunction;
|
||||
import com.microsoft.semantickernel.textcompletion.TextCompletion;
|
||||
|
||||
|
||||
@@ -95,6 +95,11 @@
|
||||
<artifactId>semantickernel-connectors-memory-azurecognitivesearch</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.semantic-kernel</groupId>
|
||||
<artifactId>semantickernel-settings-loader</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.azure</groupId>
|
||||
|
||||
Reference in New Issue
Block a user