Skip to content

Commit

Permalink
Java: Fix a number of planner issues, add blog example (#2077)
Browse files Browse the repository at this point in the history
- Fix the planner overwriting the result even when it is being bound to
a variable.
- Fix incorrect handling of default values for planner functions 
- Add example for blog post

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#dev-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
  • Loading branch information
johnoliver authored Jul 20, 2023
1 parent e20b6f4 commit ef175ea
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.microsoft.semantickernel;

import com.azure.ai.openai.OpenAIAsyncClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.connectors.ai.openai.util.AIProviderSettings;
import com.microsoft.semantickernel.connectors.ai.openai.util.AzureOpenAISettings;
import com.microsoft.semantickernel.orchestration.SKContext;
import com.microsoft.semantickernel.planner.actionplanner.Plan;
import com.microsoft.semantickernel.planner.sequentialplanner.SequentialPlanner;
import com.microsoft.semantickernel.skilldefinition.annotations.DefineSKFunction;
import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionInputAttribute;
import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionParameters;
import com.microsoft.semantickernel.textcompletion.TextCompletion;

import java.io.IOException;
import java.nio.file.Paths;

public class Example_PlanWithNativeFunctions {

public static void main(String[] args) throws IOException {
AzureOpenAISettings settings = AIProviderSettings.getAzureOpenAISettingsFromFile(
Paths.get(System.getProperty("user.home"), ".sk", "conf.properties").toAbsolutePath().toString()
);

OpenAIAsyncClient client =
new OpenAIClientBuilder()
.endpoint(settings.getEndpoint())
.credential(new AzureKeyCredential(settings.getKey()))
.buildAsyncClient();

TextCompletion textCompletionService = SKBuilders.textCompletionService().build(client, "text-davinci-003");

KernelConfig config = SKBuilders.kernelConfig()
.addTextCompletionService("davinci",
kernel -> textCompletionService)
.build();

Kernel kernel = SKBuilders.kernel()
.withKernelConfig(config)
.build();

kernel.importSkill(new StringFunctions(), "StringFunctions");
kernel.importSkill(new Emailer(), "Emailer");
kernel.importSkill(new Names(), "Names");

SequentialPlanner planner = new SequentialPlanner(kernel, null, null);


Plan plan = planner
.createPlanAsync("Send the input as an email to Steven and make sure I use his preferred name in the email")
.block();

System.out.println("\n\n=============================== Plan to execute ===============================");
System.out.println(plan.toPlanString());
System.out.println("===============================================================================");


SKContext context = SKBuilders.context().build();
context.setVariable("subject", "Update");

String message = "Hay Steven, just wanted to let you know I have finished.";

System.out.println("\n\nExecuting plan...");
SKContext planResult = plan.invokeAsync(message, context, null).block();

System.out.println("\n\nPlan Result: " + planResult.getResult());
}

public static class StringFunctions {
@DefineSKFunction(
name = "stringReplace",
description = "Takes a message and substitutes string 'from' to 'to'")
public String stringReplace(
@SKFunctionInputAttribute
@SKFunctionParameters(name = "input", description = "The string to perform the replacement on")
String input,
@SKFunctionParameters(name = "from", description = "The string to replace")
String from,
@SKFunctionParameters(name = "to", description = "The string to replace with")
String to
) {
return input.replaceAll(from, to);
}
}

public static class Names {
@DefineSKFunction(name = "getNickName", description = "Retrieves the nick name for a given user")
public String getNickName(
@SKFunctionInputAttribute
@SKFunctionParameters(name = "name", description = "The name of the person to get an nick name for")
String name) {
switch (name) {
case "Steven":
return "Code King";
default:
throw new RuntimeException("Unknown user: " + name);
}
}

}

public static class Emailer {
@DefineSKFunction(name = "getEmailAddress", description = "Retrieves the email address for a given user")
public String getEmailAddress(
@SKFunctionInputAttribute
@SKFunctionParameters(name = "name", description = "The name of the person to get an email address for")
String name) {
switch (name) {
case "Steven":
return "[email protected]";
default:
throw new RuntimeException("Unknown user: " + name);
}
}

@DefineSKFunction(name = "sendEmail", description = "Sends an email")
public String sendEmail(
@SKFunctionParameters(name = "subject", description = "The email subject") String subject,
@SKFunctionParameters(name = "message", description = "The message to email") String message,
@SKFunctionParameters(name = "emailAddress", description = "The emailAddress to send the message to") String emailAddress) {
System.out.println("================= Sending Email ====================");
System.out.printf("To: %s%n", emailAddress);
System.out.printf("Subject: %s%n", subject);
System.out.printf("Message: %s%n", message);
System.out.println("====================================================");
return "Message sent";
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.microsoft.semantickernel.skilldefinition.KernelSkillsSupplier;
import com.microsoft.semantickernel.skilldefinition.ParameterView;
import com.microsoft.semantickernel.skilldefinition.ReadOnlySkillCollection;
import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionParameters;

import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -188,7 +189,12 @@ public String toManualString() {
parameter -> {
String defaultValueString;
if (parameter.getDefaultValue() == null
|| parameter.getDefaultValue().isEmpty()) {
|| parameter.getDefaultValue().isEmpty()
|| parameter
.getDefaultValue()
.equals(
SKFunctionParameters
.NO_DEFAULT_VALUE)) {
defaultValueString = "";
} else {
defaultValueString =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
import com.microsoft.semantickernel.Verify;
import com.microsoft.semantickernel.builders.SKBuilders;
import com.microsoft.semantickernel.memory.SemanticTextMemory;
import com.microsoft.semantickernel.orchestration.*;
import com.microsoft.semantickernel.orchestration.AbstractSkFunction;
import com.microsoft.semantickernel.orchestration.ContextVariables;
import com.microsoft.semantickernel.orchestration.SKContext;
import com.microsoft.semantickernel.orchestration.SKFunction;
import com.microsoft.semantickernel.orchestration.WritableContextVariables;
import com.microsoft.semantickernel.planner.PlanningException;
import com.microsoft.semantickernel.skilldefinition.FunctionView;
import com.microsoft.semantickernel.skilldefinition.KernelSkillsSupplier;
import com.microsoft.semantickernel.skilldefinition.ParameterView;
import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionParameters;
import com.microsoft.semantickernel.textcompletion.CompletionRequestSettings;

import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -290,6 +295,17 @@ public Mono<Plan> invokeNextStepAsync(
}
});

// If this function produces an output, don't overwrite the current
// result
if (step.outputs.size() > 0) {
this.state =
this.state
.writableClone()
.setVariable(
ContextVariables.MAIN_KEY,
context.getResult());
}

sink.next(this);
});
}
Expand Down Expand Up @@ -455,7 +471,9 @@ private ContextVariables getNextStepVariables(ContextVariables variables, Plan s
// - Plan.Description

String input = "";
if (step.parameters != null && !Verify.isNullOrEmpty(step.parameters.getInput())) {
if (step.parameters != null
&& !Verify.isNullOrEmpty(step.parameters.getInput())
&& !step.parameters.getInput().equals(SKFunctionParameters.NO_DEFAULT_VALUE)) {
input =
this.expandFromVariables(
variables, Objects.requireNonNull(step.parameters.getInput()));
Expand Down

0 comments on commit ef175ea

Please sign in to comment.