微软推出基于ChatGPT的Java语义内核


Microsoft Semantic Kernel for Java的第一个版本发布了,这个Java 库通过将 OpenAI 和 Azure OpenAI 等 AI 服务与传统和惯用的编程无缝集成,为开发人员开辟了新的可能性。

现在,您可以创建尖端的 AI 应用程序,在一个紧密结合的环境中结合两个世界的优点。

Java 的语义内核是什么?
Semantic Kernel for Java 是一个开源库,使开发人员能够在使用 Java 进行编码时利用 AI 的力量。它与 Java 8 及更高版本兼容,确保广大 Java 开发人员的灵活性和可访问性。通过将 AI 服务集成到 Java 应用程序中,您可以释放人工智能和大型语言模型的全部潜力,而无需牺牲熟悉的 Java 开发环境。

开源并获得 MIT 许可
我们自豪地告诉大家,Java 语义内核是一个开源项目。它是在宽松的 MIT 许可证下发布的,让您可以自由地探索、修改和为 SK 做出贡献。您可以在 GitHub 上找到源代码:github.com/microsoft/semantic-kernel。我们欢迎您做出贡献,帮助塑造该项目的未来。

与 Maven 和 Gradle 轻松集成
您可以在 Maven Central 上找到 alpha 版本 0.2.6-alpha。这意味着您可以轻松地将 SK 作为依赖项添加到 Maven 或 Gradle 项目中。请参阅我们的示例文件夹,了解有关工件、其坐标以及如何将 SK 添加到您的项目的说明。alpha 版本0.2.6-alpha可以在 Maven Central 上找到。

下面我们向您展示如何在应用程序代码中使用 SK for Java 的片段。

第一步是创建一个 OpenAI 客户端:

    OpenAISettings settings = AIProviderSettings.getOpenAISettingsFromSystemProperties();
    NonAzureOpenAIKeyCredential credential = new NonAzureOpenAIKeyCredential(settings.getKey());
    OpenAIAsyncClient client = new OpenAIClientBuilder()
        .credential(credential)
        .buildAsyncClient();


接下来,我们创建一个为 text-davinci-003 模型配置的 TextCompletion 服务实例,并将其注册到我们的内核配置中:

    TextCompletion textCompletionService = SKBuilders.textCompletionService()
                                                      .build(client, "text-davinci-003");
    KernelConfig config = SKBuilders.kernelConfig()
                                    .addTextCompletionService("davinci", k -> textCompletionService)
                                    .build();

为了完成 Kernel 的实例化,我们的示例需要一定的技巧。这些技能在名为 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);
    }
  }

为了在实例化内核时将这些技能注册到内核中,我们执行以下操作:

   Kernel kernel = SKBuilders.kernel().withKernelConfig(config).build();
   kernel.importSkill(new MyAppSkills(), "MyAppSkills");


最后,下一步是在我们的应用程序中执行一些智能操作。我们的用例是一个应用程序,该应用程序编辑消息中的密码,并将此类事件通知系统管理员。
我们得到的提示是“对于任何带有密码的输入,请编辑密码并将编辑后的输入发送到 sysadmin@corp.net”。

我们使用称为Planner的概念 ,它根据提示创建计划,并结合技能来执行用户期望的一组操作。让我们看看它是什么样子的:

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);

当然,上面的用例很肤浅,也可以用一行代码来实现,但请考虑一下利用生成式人工智能制定更复杂的计划时的可能性。对于上面的示例,输出为:

<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 ******