Semantic Kernel for Java

repository·main·Indexed 42 days ago

https://github.com/microsoft/semantic-kernel-java

An SDK for integrating Large Language Models (LLMs) like OpenAI, Azure OpenAI, and Hugging Face into Java applications. It enables developers to orchestrate plugins, use AI-driven planners to automate complex tasks, and manage both native Java functions and prompt-based semantic functions through a central Kernel orchestration engine.

Tokens
9.9K
Snippets
28
Records
46
Agent score
52%

What's inside Semantic Kernel

  1. Overview of Semantic Kernel for Java

    main

    Semantic Kernel for Java is an SDK designed to integrate Large Language Models (LLMs) such as OpenAI, Azure OpenAI, and Hugging Face with conventional Java programming.

    Key capabilities include:

    • Plugins: Defining sets of functions that can be chained together.
    • Orchestration: Automatically coordinating plugins using planners, which allow an LLM to generate and execute a plan to achieve a specific user goal.
  2. What is Semantic Kernel for Java

    main

    Semantic Kernel (SK) is a lightweight foundation that allows you to integrate conventional programming languages with Large Language Model (LLM) AI prompts. It provides out-of-the-box capabilities for templating, chaining, and planning.

    The Java implementation is designed to be idiomatic for Java applications across client (desktop, mobile, CLIs) and server environments, ensuring easy integration with existing Java libraries and frameworks.

  3. How Hooks work in Semantic Kernel

    main

    Hooks are functions attached to the Kernel that are triggered during specific lifecycle events. They allow you to intercept, modify, or observe the execution of functions and AI service calls.

    There are two ways to register hooks:

    1. Global Registration: Hooks added to kernel.getGlobalKernelHooks() are effective for every operation performed by that kernel instance.

      kernel.getGlobalKernelHooks().addHook("hookName", KernelHook);
    2. Single Call Registration: Hooks passed as a parameter to invokeAsync are effective only for that specific execution.

      KernelHooks kernelHooks = new KernelHooks();
      kernelHooks.addPreChatCompletionHook(...);
      
      var result = kernel.invokeAsync(writerFunction)
          .withArguments(KernelFunctionArguments.builder().build())
          .addKernelHooks(kernelHooks)
          .block();
    // Global registration
    kernel.getGlobalKernelHooks().addHook("hookName", KernelHook);
    
    // Single call registration
    KernelHooks kernelHooks = new KernelHooks();
    kernelHooks.addPreChatCompletionHook(...);
    
    var result = kernel.invokeAsync(writerFunction)
        .withArguments(KernelFunctionArguments.builder().build())
        .addKernelHooks(kernelHooks)
        .block();
  4. Understand client settings precedence

    main

    The samples resolve client configuration settings using the following order of precedence (highest to lowest):

    1. Properties file location defined by CONF_PROPERTIES (property or environment variable).
    2. System properties defined on the command line (e.g., -Dkey=value).
    3. Environment variables.
    4. Properties file at samples/conf.properties.
    5. Properties file at ~/.sk/conf.properties.
  5. How Presidio integration works with Semantic Kernel

    main

    This sample demonstrates a privacy-preserving workflow using the semantickernel-presidio-plugin. The process follows these steps:

    1. Redaction: User text is passed through the Presidio plugin, which identifies and redacts sensitive information (e.g., names, locations, account numbers) using placeholders like PERSON1 or LOCATION1.
    2. LLM Processing: The anonymized text is sent to a Large Language Model (LLM) to perform tasks (like answering questions) without exposing PII (Personally Identifiable Information).
    3. De-anonymization: The LLM's response, which contains the anonymized placeholders, is then de-anonymized back into its original sensitive values for the end user.
  6. Use Managed Identity or Azure CLI for Azure OpenAI

    main

    When using Azure OpenAI, you can configure the credentialtype (via client.openai.credentialtype) to use Azure identity-based authentication instead of API keys.

    Supported Types:

    • managed_identity: Attempts to obtain credentials via Azure's managed identity.
    • azure_cli: Attempts to obtain credentials via the Azure CLI (requires az login).

    Requirements:

    1. The azure-identity dependency must be present in the application.
    2. Your identity must have the "Cognitive Services User" role assigned in Azure.
  7. Build and run the Presidio sample application

    main

    The Presidio sample requires running Presidio within Docker containers via docker compose. Follow these steps to set up and execute the demo:

    1. Install Semantic Kernel: Ensure you have installed the Semantic Kernel local version you intend to use by running ./mvnw install in the root repository.
    2. Configure Environment: Copy the provided example environment file to .env and update the required values:
      cp env.example .env
    3. Execute: Run the provided build and run script:
      ./buildAndRun.sh
    ./buildAndRun.sh
  8. Run tests for Semantic Kernel for Java

    main

    Depending on the scope of testing required, use the following Maven commands:

    • Run unit tests only:
      ./mvnw package
    • Run all tests (including integration tests requiring an OpenAI key):
      ./mvnw verify -Prelease,bug-check,with-samples
    ./mvnw verify -Prelease,bug-check,with-samples
  9. Install Semantic Kernel for Java via Maven

    main

    All Semantic Kernel packages are hosted under the groupId com.microsoft.semantic-kernel. To use the core public API in your Maven project, add the semantickernel-api dependency.

    <dependency>
        <groupId>com.microsoft.semantic-kernel</groupId>
        <artifactId>semantickernel-api</artifactId>
    </dependency>
  10. Use Semantic Kernel for Java in your projects

    main

    You can consume Semantic Kernel for Java in two ways:

    1. Maven Dependencies: The library is organized into a set of dependencies published to Maven Central. Refer to PACKAGES.md for the specific list of dependencies and usage instructions.
    2. Code Samples: For practical implementation patterns, refer to the samples folder in the repository for common AI-enabled scenarios.
  11. Compile and run Semantic Kernel Java samples

    main

    The samples in this repository can be compiled and executed using the Maven wrapper (mvnw).

    To compile the samples:

    ../../mvnw clean package

    To run a specific sample, use the exec:java goal and provide the sample name via the -Dsample property:

    ../../mvnw exec:java -Dsample=Example04_CombineLLMPromptsAndNativeCode
    # Compile
    ../../mvnw clean package
    
    # Run a specific sample
    ../../mvnw exec:java -Dsample=Example04_CombineLLMPromptsAndNativeCode