Skip to content

Commit 921f883

Browse files
author
Milder Hernandez Cagua
committed
Merge branch 'main' of https://github.com/microsoft/semantic-kernel-java into jdbc
2 parents 4d17c1f + 57db64f commit 921f883

File tree

26 files changed

+450
-80
lines changed

26 files changed

+450
-80
lines changed

BUILD.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ To build the Semantic Kernel for Java, you will need:
3636

3737
1. Clone this repository
3838

39-
git clone -b java-v1 https://github.com/microsoft/semantic-kernel/
39+
git clone https://github.com/microsoft/semantic-kernel-java
4040

4141
2. Build the project with the Maven Wrapper
4242

43-
cd semantic-kernel/java
43+
cd semantic-kernel
4444
./mvnw install
4545

4646
3. (Optional) To run a FULL build including static analysis and end-to-end tests that might require a valid OpenAI key,
@@ -104,8 +104,7 @@ Also ensure that:
104104
- All new code is covered by unit tests
105105
- All new code is covered by integration tests
106106

107-
Once your proposal is ready, submit a pull request to the `java-v1` branch. The pull request will be reviewed by the
108-
project maintainers.
107+
Once your proposal is ready, submit a pull request. The pull request will be reviewed by the project maintainers.
109108

110109
Make sure your pull request has an objective title and a clear description explaining the problem and solution.
111110

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# 1.2.1
2+
3+
- Fix bug in `FunctionInvocation` not using per-invocation type conversion when calling `withResultType`.
4+
- Fix bug in Global Hooks not being invoked under certain circumstances.
5+
- Add fluent returns to `ChatHistory` `addXMessage` methods.
6+
- Add user agent opt-out for OpenAI requests by setting the property `semantic-kernel.useragent-disable` to `true`.
7+
- Add several convenience `invokePromptAsync` methods to `Kernel`.
8+
9+
#### Non-API Changes
10+
11+
- Add custom type Conversion example, CustomTypes_Example
12+
113
# 1.2.0
214

315
- Add ability to use image_url as content for a OpenAi chat completion

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
# Semantic Kernel for Java
77

8-
Welcome to the Semantic Kernel for Java. For detailed documentation, visit [Microsoft Learn](https://learn.microsoft.com/en-us/semantic-kernel/overview/?tabs=Java).
8+
Welcome to the Semantic Kernel for Java. For detailed documentation, visit [Microsoft Learn](https://learn.microsoft.com/en-us/semantic-kernel/overview/?tabs=Java&pivots=programming-language-java).
99

1010
[Semantic Kernel](https://learn.microsoft.com/en-us/semantic-kernel/overview/) is an SDK that integrates Large Language Models (LLMs) like [OpenAI](https://platform.openai.com/docs/introduction), [Azure OpenAI](https://azure.microsoft.com/en-us/products/ai-services/openai-service), and [Hugging Face](https://huggingface.co/)
11-
with conventional programming languages like C#, Python, and Java. Semantic Kernel achieves this by allowing you to define [plugins](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/plugins) that can be chained together in just a [few lines of code](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/chaining-functions?tabs=Java#using-the-runasync-method-to-simplify-your-code).
11+
with conventional programming languages like C#, Python, and Java. Semantic Kernel achieves this by allowing you to define [plugins](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/plugins??tabs=Java&pivots=programming-language-java) that can be chained together in just a [few lines of code](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/chaining-functions?tabs=Java&pivots=programming-language-java#using-the-runasync-method-to-simplify-your-code).
1212

13-
What makes Semantic Kernel _special_, however, is its ability to _automatically_ orchestrate plugins with AI. With Semantic Kernel [planners](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/planner), you can ask an LLM to generate a plan that achieves a user's unique goal. Afterwards, Semantic Kernel will execute the plan for the user.
13+
What makes Semantic Kernel _special_, however, is its ability to _automatically_ orchestrate plugins with AI. With Semantic Kernel [planners](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/planner?tabs=Java&pivots=programming-language-java), you can ask an LLM to generate a plan that achieves a user's unique goal. Afterwards, Semantic Kernel will execute the plan for the user.
1414

1515
For C#, Python and other language support, see [microsoft/semantic-kernel](https://github.com/microsoft/semantic-kernel).
1616

@@ -23,20 +23,20 @@ For C#, Python and other language support, see [microsoft/semantic-kernel](https
2323
The quickest way to get started with the basics is to get an API key from either OpenAI or Azure OpenAI and to run one of the Java console applications/scripts below.
2424

2525
1. Clone the repository: `git clone https://github.com/microsoft/semantic-kernel-java.git`
26-
2. Follow the instructions [Start learning how to use Semantic Kernel](https://learn.microsoft.com/en-us/semantic-kernel/get-started/quick-start-guide?tabs=Java).
26+
2. Follow the instructions [Start learning how to use Semantic Kernel](https://learn.microsoft.com/en-us/semantic-kernel/get-started/quick-start-guide?tabs=Java&pivots=programming-language-java).
2727

2828
## Documentation: Learning how to use Semantic Kernel
2929

3030
The fastest way to learn how to use Semantic Kernel is with our walkthroughs
3131
on our Learn site.
3232

33-
1. 📖 [Overview of the kernel](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/?tabs=Java)
34-
1. 🔌 [Understanding AI plugins](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/plugins?tabs=Java)
35-
1. 👄 [Creating semantic functions](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/semantic-functions?tabs=Java)
36-
1. 💽 [Creating native functions](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/native-functions?tabs=Java)
37-
1. ⛓️ [Chaining functions together](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/chaining-functions?tabs=Java)
38-
1. 🤖 [Auto create plans with planner](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/planner?tabs=Java)
39-
1. 💡 [Create and run a ChatGPT plugin](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/chatgpt-plugins?tabs=Java)
33+
1. 📖 [Overview of the kernel](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/?tabs=Java&pivots=programming-language-java)
34+
1. 🔌 [Understanding AI plugins](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/plugins?tabs=Java&pivots=programming-language-java)
35+
1. 👄 [Creating semantic functions](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/semantic-functions?tabs=Java&pivots=programming-language-java)
36+
1. 💽 [Creating native functions](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/native-functions?tabs=Java&pivots=programming-language-java)
37+
1. ⛓️ [Chaining functions together](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/chaining-functions?tabs=Java&pivots=programming-language-java)
38+
1. 🤖 [Auto create plans with planner](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/planner?tabs=Java&pivots=programming-language-java)
39+
1. 💡 [Create and run a ChatGPT plugin](https://learn.microsoft.com/en-us/semantic-kernel/ai-orchestration/chatgpt-plugins?tabs=Java&pivots=programming-language-java)
4040

4141
## Join the community
4242

@@ -50,8 +50,8 @@ in a different direction, but also to consider the impact on the larger ecosyste
5050

5151
To learn more and get started:
5252

53-
- Read the [documentation](https://learn.microsoft.com/en-us/semantic-kernel/overview/?tabs=Java)
54-
- Learn how to [contribute](https://learn.microsoft.com/en-us/semantic-kernel/get-started/contributing) to the project
53+
- Read the [documentation](https://learn.microsoft.com/en-us/semantic-kernel/overview/?tabs=Java&pivots=programming-language-java)
54+
- Learn how to [contribute](https://learn.microsoft.com/en-us/semantic-kernel/get-started/contributing?tabs=Java&pivots=programming-language-java) to the project
5555
- Join the [Discord community](https://aka.ms/SKDiscord)
5656
- Attend [regular office hours and SK community events](COMMUNITY.md)
5757
- Follow the team on our [blog](https://aka.ms/sk/blog)

aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/OpenAiService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
package com.microsoft.semantickernel.aiservices.openai;
33

4-
import com.azure.ai.openai.OpenAIAsyncClient;
54
import com.microsoft.semantickernel.services.AIService;
65
import javax.annotation.Nullable;
76

87
/**
98
* Provides OpenAI service.
109
*/
11-
public abstract class OpenAiService implements AIService {
10+
public abstract class OpenAiService<Client> implements AIService {
1211

13-
private final OpenAIAsyncClient client;
12+
private final Client client;
1413
@Nullable
1514
private final String serviceId;
1615
private final String modelId;
1716
private final String deploymentName;
1817

1918
protected OpenAiService(
20-
OpenAIAsyncClient client,
19+
Client client,
2120
@Nullable String serviceId,
2221
String modelId,
2322
String deploymentName) {
@@ -39,7 +38,7 @@ public String getServiceId() {
3938
return serviceId;
4039
}
4140

42-
protected OpenAIAsyncClient getClient() {
41+
protected Client getClient() {
4342
return client;
4443
}
4544

aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/audio/OpenAiAudioToTextService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Provides OpenAi implementation of audio to text service.
2020
*/
21-
public class OpenAiAudioToTextService extends OpenAiService implements AudioToTextService {
21+
public class OpenAiAudioToTextService extends OpenAiService<OpenAIAsyncClient> implements AudioToTextService {
2222

2323
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAiAudioToTextService.class);
2424

aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/audio/OpenAiTextToAudioService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* Provides OpenAi implementation of text to audio service.
1919
*/
20-
public class OpenAiTextToAudioService extends OpenAiService implements TextToAudioService {
20+
public class OpenAiTextToAudioService extends OpenAiService<OpenAIAsyncClient> implements TextToAudioService {
2121

2222
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAiTextToAudioService.class);
2323

aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/chatcompletion/OpenAIChatCompletion.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
/**
8080
* OpenAI chat completion service.
8181
*/
82-
public class OpenAIChatCompletion extends OpenAiService implements ChatCompletionService {
82+
public class OpenAIChatCompletion extends OpenAiService<OpenAIAsyncClient> implements ChatCompletionService {
8383

8484
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAIChatCompletion.class);
8585

@@ -311,6 +311,7 @@ private Mono<ChatMessages> internalChatMessageContentsAsync(
311311

312312
ChatCompletionsOptions options = executeHook(
313313
invocationContext,
314+
kernel,
314315
new PreChatCompletionEvent(
315316
getCompletionsOptions(
316317
this,
@@ -349,7 +350,7 @@ private Mono<ChatMessages> internalChatMessageContentsAsync(
349350
.collect(Collectors.toList());
350351

351352
// execute post chat completion hook
352-
executeHook(invocationContext, new PostChatCompletionEvent(completions));
353+
executeHook(invocationContext, kernel, new PostChatCompletionEvent(completions));
353354

354355
// Just return the result:
355356
// If we don't want to attempt to invoke any functions
@@ -517,11 +518,12 @@ private Mono<FunctionResult<String>> invokeFunctionTool(
517518
pluginName,
518519
openAIFunctionToolCall.getFunctionName());
519520

520-
PreToolCallEvent hookResult = executeHook(invocationContext, new PreToolCallEvent(
521-
openAIFunctionToolCall.getFunctionName(),
522-
openAIFunctionToolCall.getArguments(),
523-
function,
524-
contextVariableTypes));
521+
PreToolCallEvent hookResult = executeHook(invocationContext, kernel,
522+
new PreToolCallEvent(
523+
openAIFunctionToolCall.getFunctionName(),
524+
openAIFunctionToolCall.getArguments(),
525+
function,
526+
contextVariableTypes));
525527

526528
function = hookResult.getFunction();
527529
KernelFunctionArguments arguments = hookResult.getArguments();
@@ -537,12 +539,21 @@ private Mono<FunctionResult<String>> invokeFunctionTool(
537539

538540
private static <T extends KernelHookEvent> T executeHook(
539541
@Nullable InvocationContext invocationContext,
542+
@Nullable Kernel kernel,
540543
T event) {
541-
KernelHooks kernelHooks = invocationContext != null
542-
&& invocationContext.getKernelHooks() != null
543-
? invocationContext.getKernelHooks()
544-
: new KernelHooks();
545-
544+
KernelHooks kernelHooks = null;
545+
if (kernel == null) {
546+
if (invocationContext != null) {
547+
kernelHooks = invocationContext.getKernelHooks();
548+
}
549+
} else {
550+
kernelHooks = KernelHooks.merge(
551+
kernel.getGlobalKernelHooks(),
552+
invocationContext != null ? invocationContext.getKernelHooks() : null);
553+
}
554+
if (kernelHooks == null) {
555+
return event;
556+
}
546557
return kernelHooks.executeHooks(event);
547558
}
548559

@@ -1044,7 +1055,7 @@ static ChatRequestMessage getChatRequestMessage(
10441055
/**
10451056
* Builder for creating a new instance of {@link OpenAIChatCompletion}.
10461057
*/
1047-
public static class Builder extends OpenAiServiceBuilder<OpenAIChatCompletion, Builder> {
1058+
public static class Builder extends OpenAiServiceBuilder<OpenAIAsyncClient, OpenAIChatCompletion, Builder> {
10481059

10491060
@Override
10501061
public OpenAIChatCompletion build() {

aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/implementation/OpenAIRequestSettings.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,26 @@ public final class OpenAIRequestSettings {
2020

2121
private static final String SEMANTIC_KERNEL_VERSION_PROPERTY_NAME = "semantic-kernel.version";
2222
private static final String SEMANTIC_KERNEL_VERSION_PROPERTIES_FILE = "semantic-kernel-version.properties";
23-
private static final String useragent;
2423

24+
private static final String useragent;
2525
private static final String header;
2626

27+
public static final String SEMANTIC_KERNEL_DISABLE_USERAGENT_PROPERTY = "semantic-kernel.useragent-disable";
28+
29+
private static final boolean disabled;
30+
2731
static {
32+
disabled = isDisabled();
2833
String version = loadVersion();
2934
useragent = "semantic-kernel-java/" + version;
3035
header = "java/" + version;
3136
}
3237

38+
private static boolean isDisabled() {
39+
return Boolean.parseBoolean(
40+
System.getProperty(SEMANTIC_KERNEL_DISABLE_USERAGENT_PROPERTY, "false"));
41+
}
42+
3343
private static String loadVersion() {
3444

3545
String version = "unknown";
@@ -58,9 +68,14 @@ private static String loadVersion() {
5868
* @return The request options
5969
*/
6070
public static RequestOptions getRequestOptions() {
61-
return new RequestOptions()
71+
RequestOptions requestOptions = new RequestOptions();
72+
73+
if (disabled) {
74+
return requestOptions;
75+
}
76+
77+
return requestOptions
6278
.setHeader(HttpHeaderName.fromString("Semantic-Kernel-Version"), header)
63-
.setContext(
64-
new Context(UserAgentPolicy.APPEND_USER_AGENT_CONTEXT_KEY, useragent));
79+
.setContext(new Context(UserAgentPolicy.APPEND_USER_AGENT_CONTEXT_KEY, useragent));
6580
}
6681
}

aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/textcompletion/OpenAITextGenerationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/**
3131
* An OpenAI implementation of a {@link TextGenerationService}.
3232
*/
33-
public class OpenAITextGenerationService extends OpenAiService implements TextGenerationService {
33+
public class OpenAITextGenerationService extends OpenAiService<OpenAIAsyncClient> implements TextGenerationService {
3434

3535
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAITextGenerationService.class);
3636

aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/textembedding/OpenAITextEmbeddingGenerationService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* An OpenAI implementation of a {@link TextEmbeddingGenerationService}.
2424
*
2525
*/
26-
public class OpenAITextEmbeddingGenerationService extends OpenAiService
26+
public class OpenAITextEmbeddingGenerationService extends OpenAiService<OpenAIAsyncClient>
2727
implements TextEmbeddingGenerationService {
2828
private static final Logger LOGGER = LoggerFactory
2929
.getLogger(OpenAITextEmbeddingGenerationService.class);
@@ -87,7 +87,7 @@ protected Mono<List<Embedding>> internalGenerateTextEmbeddingsAsync(List<String>
8787
* A builder for creating a {@link OpenAITextEmbeddingGenerationService}.
8888
*/
8989
public static class Builder extends
90-
OpenAiServiceBuilder<OpenAITextEmbeddingGenerationService, OpenAITextEmbeddingGenerationService.Builder> {
90+
OpenAiServiceBuilder<OpenAIAsyncClient, OpenAITextEmbeddingGenerationService, OpenAITextEmbeddingGenerationService.Builder> {
9191
private int dimensions = DEFAULT_DIMENSIONS;
9292

9393
/**

0 commit comments

Comments
 (0)