-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java: Fix a number of planner issues, add blog example (#2077)
- 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
1 parent
e20b6f4
commit ef175ea
Showing
3 changed files
with
160 additions
and
3 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
...mple-code/src/main/java/com/microsoft/semantickernel/Example_PlanWithNativeFunctions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters