Access LLMs From Java code With Semantic Kernel
Written by Nikos Vaggalis   
Tuesday, 29 August 2023

You can now do this thanks to Microsoft's Semantic Kernel SDK which integrates Large Language Models (LLMs)
with conventional programming languages like C# and Python. The Java binding has recently been released too.

With Semantic Kernel for Java you can now integrate the OpenAI and Azure OpenAI services with your Java code to unleash the power of AI in your apps. The project relies on Github Copilot. At the center of Copilot's stack lies an AI orchestration layer that allows us to combine AI models and plugins together to create brand new experiences. Semantic Kernel sprung out of this as an SDK when Microsoft wanted to give the developers the capability to build their own Copilot experiences. With the Kernel you can use the patterns driving Copilot, or even Bing chat, in your own code and programming languages. Previously only C# and Python were available, but now you can use Java too.


It's main components are:

  • Kernel The kernel orchestrates a user's ask. To do so, the kernel runs a pipeline / chain that is defined by a developer. While the chain is run, a common context is provided by the kernel so data can be shared between functions.
  • Memories With a specialized plugin, a developer can recall and store context in vector databases. This allows developers to simulate memory within their AI apps.
  • Plugins Modules that connect Semantic Kernel to different AI models or services, such as GPT-3, Azure Prompt Flow, or the Microsoft Graph.
  • Functions They allow your AI app to listen to users asks and respond back with a natural language response
  • Plans Sequences of actions that Semantic Kernel can execute to achieve a goal, such as booking a flight or writing some content.

To use it, first make sure that you have an Open AI API Key or Azure Open AI service key. Then you have to build the Semantic Kernel and OpenJDK 17 or newer is required.

To do so, clone this repository

git clone -b experimental-java https://github. com/microsoft/semantic-kernel/

and finally build the Semantic Kernel

cd semantic-kernel/java
. /mvnw install

The SDK is also available on Maven Central under groupId Code examples can be found on the official Github repo but the procedure is pretty much
streamlined as:

Step1: Create a Kernel

Step2: Add a Function

Step3: Instruct LLM to use this Function + Additional Settings

Step4: Collect Functions into Plugins

Step5: Represent Plugins as Java code

Step6: Give memory to the LLM

One final note is that because Java support is still experimental, the documentation is scarce compared to C#'s and Python's. So if you want to understand the concepts, have a look at the official examples of those languages too.

More Information 

Semantic Kernel on Github 

Related Articles

An Attempt To Make Java Easier To Learn 


To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


Bun 1.0 Released With Experimental Windows Version

Bun, the new JavaScript runtime, is now available in a 1.0 production-ready version, according to the developers, who have also announced an experimental Windows version.

Python Extends Its Lead In IEEE Spectrum Rankings

This is the 10th year for the annual Top Programming Languages exercise. Python coming top isn't news. It has now done so eight years in a row. What is important is that it is even further in the lead [ ... ]

More News

Summer SALE Kindle 9.99 Paperback $10 off!!




or email your comment to:

Last Updated ( Tuesday, 29 August 2023 )