Semantic Kernel实战:微软AI编排框架的Java落地指南

前言

Semantic Kernel(SK)是微软推出的 AI 编排框架,帮助开发者将大模型能力无缝集成到现有应用中。虽然官方主推 C#,但 Java 开发者也能使用 SK 的核心功能。本文带你快速上手 Semantic Kernel Java。

一、核心概念

  • Kernel:AI 编排的核心,管理所有技能和插件
  • Skill:一组相关能力的集合(如文本处理、搜索)
  • Plugin:外部工具的封装,供大模型调用
  • Planner:自动规划任务分解和执行顺序

二、环境准备

<dependency>
    <groupId>com.microsoft.semantic-kernel</groupId>
    <artifactId>kernel-core</artifactId>
    <version>1.0.0-beta1</version>
</dependency>
<dependency>
    <groupId>com.microsoft.semantic-kernel</groupId>
    <artifactId>connectors-ai-openai</artifactId>
    <version>1.0.0-beta1</version>
</dependency>

三、初始化 Kernel

import com.microsoft.semantic kernel.Kernel;
import com.microsoft.semantic kernel.services AIServiceRequestHandlerConfig;

public class SemanticKernelDemo {
    public static void main(String[] args) {
        // 配置 OpenAI
        OpenAIChatCompletion model = OpenAIChatCompletion.builder()
            .withModelId("gpt-4o-mini")
            .withAPIKey(System.getenv("OPENAI_API_KEY"))
            .build();

        // 创建 Kernel
        Kernel kernel = Kernel.builder()
            .withDefaultAIService(ChatCompletionService.class, model)
            .build();

        System.out.println("Semantic Kernel initialized successfully");
    }
}

四、创建原生函数(Native Functions)

import com.microsoft.semantic kernel.skills.annotation.KernelFunction;

public class WeatherPlugin {

    @KernelFunction(name = "get_weather", description = "获取指定城市的天气")
    public String getWeather(String location) {
        // 实际项目中调用天气 API
        return location + "今天晴,气温22-28度,适合出行";
    }

    @KernelFunction(name = "get_date", description = "获取当前日期")
    public String getCurrentDate() {
        return LocalDate.now().toString();
    }
}

五、注册插件并调用

import com.microsoft.semantic kernel.skills.core.SkillsFactory;

public class PluginDemo {
    public static void main(String[] args) {
        Kernel kernel = createKernel();

        // 注册插件
        kernel.importSkill(new WeatherPlugin(), "weather");

        // 执行自然语言任务
        String prompt = "帮我查一下北京今天的天气,并告诉我今天是几号";
        String result = kernel.run(prompt);

        System.out.println(result);
        // 输出:北京今天晴,气温22-28度,适合出行。今天是2026-04-02。
    }
}

六、使用 Memory 实现记忆

import com.microsoft.semantic kernel.memory.MemorySkill;
import com.microsoft.semantic kernel.memory.builder.InMemoryTextMemory;

public class MemoryDemo {
    public static void main(String[] args) {
        InMemoryTextMemory memory = new InMemoryTextMemory();

        // 保存对话记忆
        memory.save("user_name", "张三");
        memory.save("user_preference", "喜欢Java技术");

        // 检索记忆
        String name = memory.get("user_name").join();
        System.out.println("User name: " + name);
        // 输出:User name: 张三
    }
}

七、Planner 自动任务规划

public class PlannerDemo {
    public static void main(String[] args) {
        Kernel kernel = createKernel();
        kernel.importSkill(new WeatherPlugin(), "weather");
        kernel.importSkill(new SearchPlugin(), "search");

        // 复杂任务:自动分解为多个步骤
        String userGoal = "先查北京天气,然后搜索相关的旅游攻略";

        // SK 会自动规划执行步骤
        // Step 1: 调用 weather.get_weather(location="北京")
        // Step 2: 调用 search.search(query=北京旅游攻略)
        // Step 3: 整合结果返回给用户
    }
}

总结

Semantic Kernel 为 Java 开发者提供了接入大模型的统一编排方案。核心优势:

  • 统一抽象:一套代码支持 OpenAI、Azure OpenAI、HuggingFace 等
  • 插件生态:丰富的内置技能和第三方插件
  • Planner 能力:自动任务分解,降低开发复杂度
  • 企业级支持:微软官方维护,可靠性有保障

觉得有帮助请点赞收藏!有问题欢迎评论区交流

文章摘自:https://www.cnblogs.com/czlws/p/19813041/semantic-kernel-java-ai-microsoft