Hello Java developers!
We are thrilled to announce the first release of the Microsoft Semantic Kernel for Java! This Java library opens up new developer possibilities by seamlessly integrating AI services like OpenAI and Azure OpenAI with conventional and idiomatic programming. Now, you can create cutting-edge AI applications that combine the best of both worlds in one cohesive environment.
What is the Semantic Kernel for Java?
Semantic Kernel for Java is an open source library that empowers developers to harness the power of AI while coding in Java. It is compatible with Java 8 and above, ensuring flexibility and accessibility to a wide range of Java developers. By integrating AI services into your Java applications, you can unlock the full potential of artificial intelligence and large language models without sacrificing the familiar Java development environment.
Open Source and MIT Licensed
We proudly share that the Semantic Kernel for Java is an open-source project. It is released under the permissive MIT license, giving you the freedom to explore, modify, and contribute to the SK. You can find the source code on GitHub at: github.com/microsoft/semantic-kernel. We welcome your contributions to help shape the future of this project.
Easy integration with Maven and Gradle
UPDATE 2023-07-27:Â We have a new release `0.2.7-alpha`. Details in the release notes.
You can find the initial, alpha release, 0.2.6-alpha on Maven Central. See release notes. This means you can easily add the SK as a dependency to your Maven or Gradle projects. Refer to our samples folder for instructions on the artifacts, their coordinates and how to add SK to your project. The alpha release 0.2.6-alpha can be found on Maven Central.
A glimpse at how it looks
Below we show you a snippet of how to use the SK for Java in your application code.
First step is to create an OpenAI client:
OpenAISettings settings = AIProviderSettings.getOpenAISettingsFromSystemProperties();
NonAzureOpenAIKeyCredential credential = new NonAzureOpenAIKeyCredential(settings.getKey());
OpenAIAsyncClient client = new OpenAIClientBuilder()
.credential(credential)
.buildAsyncClient();
Next, we create an instance of the TextCompletion service configured for the text-davinci-003 model and register it for our Kernel configuration:
TextCompletion textCompletionService = SKBuilders.textCompletionService()
.build(client, "text-davinci-003");
KernelConfig config = SKBuilders.kernelConfig()
.addTextCompletionService("davinci", k -> textCompletionService)
.build();
To complete the instantiation of the Kernel, we need certain skills for our example. The skills are defined in a separate class called MyAppSkills:
public class MyAppSkills {
@DefineSKFunction(name = "redactPassword", description = "Redacts passwords from a message")
public String redactPassword(
@SKFunctionInputAttribute String input) {
System.out.println("[redactPassword] Redacting passwords from input: " + input);
return input.replaceAll("password.*", "******");
}
@DefineSKFunction(name = "sendEmail", description = "Sends a message to an email")
public String sendEmail(
@SKFunctionParameters(name = "message") String message,
@SKFunctionParameters(name = "email") String email) {
return String.format("[sendEmail] Emailing to %s the following message: %n -> %s%n", email, message);
}
}
And to register these skills into the Kernel while instantiating it, we perform the following:
Kernel kernel = SKBuilders.kernel().withKernelConfig(config).build();
kernel.importSkill(new MyAppSkills(), "MyAppSkills");
Finally, the next step is to perform some intelligent action in our application. The use case we have is for an application that redacts passwords in messages, and notify the sysadmin of such event. The prompt we have is “For any input with passwords, redact the passwords and send redacted input to sysadmin@corp.net”.
We use the concept called Planner, which creates a plan based on the prompt, and combines skills to perform a set of actions expected by the user. Let’s see how it looks like:
SequentialPlanner planner = new SequentialPlanner(kernel, null, null);
Plan plan = planner.createPlanAsync(
"For any input with passwords, redact the passwords and send redacted input to sysadmin@corp.net")
.block();
System.out.println(plan.toPlanString());
String message = "Password changed to password.db=123456abc";
String result = plan.invokeAsync(message).block().getResult();
System.out.println(" === Result of the plan === ");
System.out.println(result);
Of course, the use case above is superficial and may as well be implemented with a single line of code, but think of the possibilities when having more complex plans that leverage generative AI. For the sample above, the output is:
<goal>For any input with passwords, redact the passwords and send redacted input to sysadmin@corp.net</goal>
11:58:21 DEBUG c.m.s.p.s.SequentialPlanner - Plan result: <plan>
<function.MyAppSkills.redactPassword/>
<function.MyAppSkills.sendEmail email="sysadmin@corp.net" message="$INPUT"/>
</plan><!-- END -->
Goal: For any input with passwords, redact the passwords and send redacted input to sysadmin@corp.net
Steps:
- MyAppSkills.redactPassword input: ""
- MyAppSkills.sendEmail email: "sysadmin@corp.net" message: "$INPUT"
[redactPassword] Redacting passwords from input: Password changed to password.db=123456abc
=== Result of the plan ===
[sendEmail] Emailing to sysadmin@corp.net the following message:
-> Password changed to ******
More samples to get you started
We have a repository full of samples, including the one above, to help you quickly grasp the potential of Semantic Kernel for Java. We understand that getting started with new technology can be daunting, but by exploring the samples at github.com/microsoft/semantic-kernel/tree/experimental-java/java/samples/sample-code, you can learn how to incorporate AI services into your Java applications effortlessly.
Alpha release – Join us on Discord
We are currently in the alpha release phase (version 0.2.6-alpha). We encourage you to try out the library, provide feedback, and report any issues you encounter. Your input is invaluable to us in refining the API and its features.
We have set up a Discord channel to foster collaboration and discussions among developers. Join us at aka.ms/java-sk-discord to interact with other enthusiasts, ask questions, share insights, and be a part of this growing community.
Furthermore, on our Discord channel, you can also learn more information about the project’s roadmap, what’s coming next as we progress in implementing more features, and talk directly with engineering team members. And if you really really need, we can also be contacted by email at java dash sk at microsoft dot com.
Start your AI-Java journey today!
If you’ve ever dreamt of combining the power of AI with the reliability of Java, your dream has now become a reality. Begin your AI-Java journey by exploring the SK, contributing to the project, and engaging with the developer community. Together, let’s unlock the endless possibilities that lie at the intersection of AI and conventional programming.
So go ahead and get started today with Semantic Kernel for Java.
Happy coding and happy AI experimenting!
Microsoft Semantic Kernel for Java Team
0 comments