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.

copilot-stack

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.

kernel-flow
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 com.microsoft.semantic-kernel. 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.

Banner


Kafka 3.9 Adds Dynamic KRaft Quorums
16/12/2024

Kafka 3.9 has been released. The team says this is a major release and the final in the 3.x line. It This will also be the final major release to feature the deprecated Apache ZooKeeper mode. Kafka is [ ... ]



PHP 8.4 Adds Property Hooks
26/11/2024

PHP 8.4 is available with improvements including property hooks, asymmetric visibility, and an updated DOM API.


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Tuesday, 29 August 2023 )