|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +import com.azure.ai.openai.OpenAIClientBuilder; |
| 3 | +import com.azure.core.credential.AzureKeyCredential; |
| 4 | +import com.microsoft.openai.AzureOpenAiClient; |
| 5 | +import com.microsoft.openai.OpenAIAsyncClient; |
| 6 | +import com.microsoft.semantickernel.Kernel; |
| 7 | +import com.microsoft.semantickernel.KernelConfig; |
| 8 | +import com.microsoft.semantickernel.builders.SKBuilders; |
| 9 | +import com.microsoft.semantickernel.textcompletion.CompletionFunctionDefinition; |
| 10 | +import com.microsoft.semantickernel.textcompletion.CompletionSKContext; |
| 11 | +import com.microsoft.semantickernel.textcompletion.CompletionSKFunction; |
| 12 | +import com.microsoft.semantickernel.textcompletion.TextCompletion; |
| 13 | + |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.io.FileInputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Properties; |
| 22 | + |
| 23 | +public class InlineFunctionTest { |
| 24 | + public static final String AZURE_CONF_PROPERTIES = "conf.properties"; |
| 25 | + |
| 26 | + private static final Logger LOGGER = LoggerFactory.getLogger(InlineFunctionTest.class); |
| 27 | + |
| 28 | + private static final String MODEL = "text-davinci-003"; |
| 29 | + |
| 30 | + public static String getToken(String configName) throws IOException { |
| 31 | + return getConfigValue(configName, "token"); |
| 32 | + } |
| 33 | + |
| 34 | + public static String getEndpoint(String configName) throws IOException { |
| 35 | + return getConfigValue(configName, "endpoint"); |
| 36 | + } |
| 37 | + |
| 38 | + private static String getConfigValue(String configName, String propertyName) |
| 39 | + throws IOException { |
| 40 | + String home = new File(System.getProperty("user.home")).getAbsolutePath(); |
| 41 | + |
| 42 | + try (FileInputStream fis = new FileInputStream(home + "/.oai/" + configName)) { |
| 43 | + Properties props = new Properties(); |
| 44 | + props.load(fis); |
| 45 | + String apiKey = props.getProperty(propertyName); |
| 46 | + if (apiKey == null) { |
| 47 | + System.err.println("NO PROPERTY " + propertyName); |
| 48 | + return ""; |
| 49 | + } |
| 50 | + return apiKey; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void main(String[] args) throws IOException { |
| 55 | + String apiKey = getToken(AZURE_CONF_PROPERTIES); |
| 56 | + |
| 57 | + OpenAIAsyncClient client = |
| 58 | + new AzureOpenAiClient( |
| 59 | + new OpenAIClientBuilder() |
| 60 | + .endpoint(getEndpoint(AZURE_CONF_PROPERTIES)) |
| 61 | + .credential(new AzureKeyCredential(apiKey)) |
| 62 | + .buildAsyncClient()); |
| 63 | + |
| 64 | + TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, MODEL); |
| 65 | + |
| 66 | + KernelConfig kernelConfig = |
| 67 | + new KernelConfig.Builder() |
| 68 | + .addTextCompletionService(MODEL, kernel -> textCompletion) |
| 69 | + .build(); |
| 70 | + |
| 71 | + Kernel kernel = SKBuilders.kernel().setKernelConfig(kernelConfig).build(); |
| 72 | + |
| 73 | + String prompt = "{{$input}}\n" + "Summarize the content above."; |
| 74 | + |
| 75 | + CompletionFunctionDefinition summarizeDefinition = |
| 76 | + SKBuilders.completionFunctions() |
| 77 | + .createFunction( |
| 78 | + prompt, |
| 79 | + "summarize", |
| 80 | + null, |
| 81 | + null, |
| 82 | + 2000, |
| 83 | + 0.2, |
| 84 | + 0.5, |
| 85 | + 0, |
| 86 | + 0, |
| 87 | + new ArrayList<>()); |
| 88 | + |
| 89 | + CompletionSKFunction summarize = kernel.registerSemanticFunction(summarizeDefinition); |
| 90 | + |
| 91 | + String text = |
| 92 | + "Demo (ancient Greek poet)\n" |
| 93 | + + "From Wikipedia, the free encyclopedia\n" |
| 94 | + + "Demo or Damo (Greek: Δεμώ, Δαμώ; fl. c. AD 200) was a Greek woman of the" |
| 95 | + + " Roman period, known for a single epigram, engraved upon the Colossus of" |
| 96 | + + " Memnon, which bears her name. She speaks of herself therein as a lyric" |
| 97 | + + " poetess dedicated to the Muses, but nothing is known of her life.[1]\n" |
| 98 | + + "Identity\n" |
| 99 | + + "Demo was evidently Greek, as her name, a traditional epithet of Demeter," |
| 100 | + + " signifies. The name was relatively common in the Hellenistic world, in" |
| 101 | + + " Egypt and elsewhere, and she cannot be further identified. The date of her" |
| 102 | + + " visit to the Colossus of Memnon cannot be established with certainty, but" |
| 103 | + + " internal evidence on the left leg suggests her poem was inscribed there at" |
| 104 | + + " some point in or after AD 196.[2]\n" |
| 105 | + + "Epigram\n" |
| 106 | + + "There are a number of graffiti inscriptions on the Colossus of Memnon." |
| 107 | + + " Following three epigrams by Julia Balbilla, a fourth epigram, in elegiac" |
| 108 | + + " couplets, entitled and presumably authored by \"Demo\" or \"Damo\" (the" |
| 109 | + + " Greek inscription is difficult to read), is a dedication to the Muses.[2]" |
| 110 | + + " The poem is traditionally published with the works of Balbilla, though the" |
| 111 | + + " internal evidence suggests a different author.[1]\n" |
| 112 | + + "In the poem, Demo explains that Memnon has shown her special respect. In" |
| 113 | + + " return, Demo offers the gift for poetry, as a gift to the hero. At the end" |
| 114 | + + " of this epigram, she addresses Memnon, highlighting his divine status by" |
| 115 | + + " recalling his strength and holiness.[2]\n" |
| 116 | + + "Demo, like Julia Balbilla, writes in the artificial and poetic Aeolic" |
| 117 | + + " dialect. The language indicates she was knowledgeable in Homeric" |
| 118 | + + " poetry—'bearing a pleasant gift', for example, alludes to the use of that" |
| 119 | + + " phrase throughout the Iliad and Odyssey.[a][2]\n"; |
| 120 | + |
| 121 | + CompletionSKContext context = summarize.invokeAsync(text).block(); |
| 122 | + |
| 123 | + if (context != null) { |
| 124 | + LOGGER.info("Result: " + context.getResult()); |
| 125 | + } else { |
| 126 | + LOGGER.error("Null result"); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments