|
| 1 | +///usr/bin/env jbang "$0" "$@" ; exit $? |
| 2 | +//DEPS com.microsoft.semantic-kernel:semantickernel-core:0.2.6-alpha |
| 3 | +//DEPS com.microsoft.semantic-kernel:semantickernel-core-skills:0.2.6-alpha |
| 4 | +//DEPS com.microsoft.semantic-kernel.connectors:semantickernel-connectors:0.2.6-alpha |
| 5 | +//DEPS org.slf4j:slf4j-jdk14:2.0.7 |
| 6 | +//SOURCES syntaxexamples/SampleSkillsUtil.java,Config.java,Example00_GettingStarted.java |
| 7 | +package com.microsoft.semantickernel.samples; |
| 8 | + |
| 9 | +import com.azure.ai.openai.OpenAIAsyncClient; |
| 10 | +import com.microsoft.semantickernel.Kernel; |
| 11 | +import com.microsoft.semantickernel.connectors.ai.openai.util.OpenAIClientProvider; |
| 12 | +import com.microsoft.semantickernel.exceptions.ConfigurationException; |
| 13 | +import com.microsoft.semantickernel.samples.syntaxexamples.SampleSkillsUtil; |
| 14 | +import com.microsoft.semantickernel.skilldefinition.ReadOnlyFunctionCollection; |
| 15 | +import com.microsoft.semantickernel.textcompletion.CompletionSKFunction; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | + |
| 19 | +/** |
| 20 | + * Using Semantic Functions stored on disk |
| 21 | + * <p> |
| 22 | + * A Semantic Skill is a collection of Semantic Functions, where each function |
| 23 | + * is defined with natural language that can be provided with a text file. |
| 24 | + * Refer to our <a href= |
| 25 | + * "https://github.com/microsoft/semantic-kernel/blob/main/docs/GLOSSARY.md">glossary</a> |
| 26 | + * for an in-depth guide to the terms. |
| 27 | + * <p> |
| 28 | + * The repository includes some examples under the <a href= |
| 29 | + * "https://github.com/microsoft/semantic-kernel/tree/main/samples">samples</a> |
| 30 | + * folder. |
| 31 | + */ |
| 32 | +public class Example02_RunningPromptsFromFile { |
| 33 | + |
| 34 | + /** |
| 35 | + * Imports skill 'FunSkill' stored in the samples folder and then returns the |
| 36 | + * semantic function 'Joke' within it. |
| 37 | + * |
| 38 | + * @param kernel |
| 39 | + with Text Completion |
| 40 | + * @return Joke function |
| 41 | + */ |
| 42 | + public static CompletionSKFunction getJokeFunction(Kernel kernel) { |
| 43 | + ReadOnlyFunctionCollection skill = kernel |
| 44 | + .importSkillFromDirectory("FunSkill", SampleSkillsUtil.detectSkillDirLocation(), "FunSkill"); |
| 45 | + |
| 46 | + return skill.getFunction("Joke", CompletionSKFunction.class); |
| 47 | + } |
| 48 | + |
| 49 | + public static void run(OpenAIAsyncClient client) throws IOException { |
| 50 | + Kernel kernel = Example00_GettingStarted.getKernel(client); |
| 51 | + CompletionSKFunction jokeFunction = getJokeFunction(kernel); |
| 52 | + |
| 53 | + System.out.println(jokeFunction.invokeAsync("time travel to dinosaur age").block().getResult()); |
| 54 | + } |
| 55 | + |
| 56 | + public static void main(String args[]) throws ConfigurationException, IOException { |
| 57 | + run(OpenAIClientProvider.getClient()); |
| 58 | + } |
| 59 | +} |
0 commit comments