mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Java: Fix execution of Example05_InlineFunctionDefinition (#4452)
### 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#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
This commit is contained in:
+68
@@ -0,0 +1,68 @@
|
||||
package com.microsoft.semantickernel.samples.syntaxexamples;
|
||||
|
||||
import com.azure.ai.openai.OpenAIAsyncClient;
|
||||
import com.microsoft.semantickernel.DefaultKernel;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.aiservices.azureopenai.AzureOpenAITextGenerationService;
|
||||
import com.microsoft.semantickernel.exceptions.ConfigurationException;
|
||||
import com.microsoft.semantickernel.samples.SamplesConfig;
|
||||
import com.microsoft.semantickernel.textcompletion.TextGenerationService;
|
||||
|
||||
public class Example06_TemplateLanguage {
|
||||
|
||||
public static void main(String[] args) throws ConfigurationException {
|
||||
|
||||
System.out.println("======== TemplateLanguage ========");
|
||||
|
||||
OpenAIAsyncClient client = SamplesConfig.getClient();
|
||||
|
||||
AzureOpenAITextGenerationService textGenerationService = AzureOpenAITextGenerationService.builder()
|
||||
.withOpenAIAsyncClient(client)
|
||||
.withModelId("text-davinci-003")
|
||||
.build();
|
||||
|
||||
Kernel kernel = new DefaultKernel.Builder()
|
||||
.withDefaultAIService(TextGenerationService.class, textGenerationService)
|
||||
.build();
|
||||
/*
|
||||
// Load native plugin into the kernel function collection, sharing its functions with prompt templates
|
||||
// Functions loaded here are available as "time.*"
|
||||
kernel.importPluginFromType("time");
|
||||
|
||||
// Prompt Function invoking time.Date and time.Time method functions
|
||||
String functionDefinition = """
|
||||
Today is: {{time.Date}}
|
||||
Current time is: {{time.Time}}
|
||||
|
||||
Answer to the following questions using JSON syntax, including the data used.
|
||||
Is it morning, afternoon, evening, or night (morning/afternoon/evening/night)?
|
||||
Is it weekend time (weekend/not weekend)?
|
||||
""";
|
||||
|
||||
// This allows to see the prompt before it's sent to OpenAI
|
||||
System.out.println("--- Rendered Prompt");
|
||||
var promptTemplateFactory = new KernelPromptTemplateFactory();
|
||||
var promptTemplate = promptTemplateFactory.tryCreate(
|
||||
new PromptTemplateConfig(functionDefinition));
|
||||
var renderedPrompt = promptTemplate.renderAsync(kernel, null).block();
|
||||
System.out.println(renderedPrompt);
|
||||
|
||||
// Run the prompt / prompt function
|
||||
var kindOfDay = kernel
|
||||
.createFunctionFromPrompt(
|
||||
functionDefinition,
|
||||
new PromptExecutionSettings.Builder()
|
||||
.withMaxTokens(100)
|
||||
.build(),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
// Show the result
|
||||
System.out.println("--- Prompt Function result");
|
||||
var result = kernel.invokeAsync(kindOfDay, null);
|
||||
System.out.println(result.GetValue < string > ());
|
||||
*/
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,14 @@ package com.microsoft.semantickernel;
|
||||
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatCompletionService;
|
||||
import com.microsoft.semantickernel.orchestration.KernelFunction;
|
||||
import com.microsoft.semantickernel.orchestration.PromptExecutionSettings;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariable;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableType;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableTypes;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.KernelArguments;
|
||||
import com.microsoft.semantickernel.plugin.KernelPlugin;
|
||||
import com.microsoft.semantickernel.semanticfunctions.PromptTemplate;
|
||||
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateFactory;
|
||||
import com.microsoft.semantickernel.textcompletion.TextGenerationService;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -58,6 +60,19 @@ public class DefaultKernel implements Kernel {
|
||||
return serviceProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KernelPlugin importPluginFromType(@Nullable String pluginName) {
|
||||
throw new Todo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public KernelFunction createFunctionFromPrompt(String promptTemplate,
|
||||
@Nullable PromptExecutionSettings executionSettings, @Nullable String functionName,
|
||||
@Nullable String description, @Nullable String templateFormat,
|
||||
@Nullable PromptTemplateFactory promptTemplateFactory) {
|
||||
throw new Todo();
|
||||
}
|
||||
|
||||
static class DefaultServiceProvider implements ServiceProvider {
|
||||
|
||||
private final Map<Class, AIService> services;
|
||||
|
||||
@@ -51,6 +51,16 @@ public interface Kernel extends Buildable {
|
||||
|
||||
ServiceProvider getServiceSelector();
|
||||
|
||||
KernelPlugin importPluginFromType(@Nullable String pluginName);
|
||||
|
||||
KernelFunction createFunctionFromPrompt(
|
||||
String promptTemplate,
|
||||
@Nullable PromptExecutionSettings executionSettings,
|
||||
@Nullable String functionName,
|
||||
@Nullable String description,
|
||||
@Nullable String templateFormat,
|
||||
@Nullable PromptTemplateFactory promptTemplateFactory);
|
||||
|
||||
interface Builder extends SemanticKernelBuilder<Kernel> {
|
||||
|
||||
<T extends AIService> Builder withDefaultAIService(Class<T> clazz, T aiService);
|
||||
|
||||
+16
-13
@@ -1,14 +1,5 @@
|
||||
package com.microsoft.semantickernel.semanticfunctions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.azure.core.exception.HttpResponseException;
|
||||
import com.microsoft.semantickernel.AIService;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
@@ -23,7 +14,12 @@ import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariab
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.KernelArguments;
|
||||
import com.microsoft.semantickernel.templateengine.handlebars.HandlebarsPromptTemplate;
|
||||
import com.microsoft.semantickernel.textcompletion.TextGenerationService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -66,13 +62,20 @@ public class KernelFunctionFromPrompt extends DefaultKernelFunction {
|
||||
|
||||
Flux<StreamingContent<T>> result;
|
||||
|
||||
PromptExecutionSettings executionSettings;
|
||||
if (arguments != null) {
|
||||
executionSettings = arguments.getExecutionSettings();
|
||||
} else {
|
||||
executionSettings = null;
|
||||
}
|
||||
|
||||
if (client instanceof ChatCompletionService) {
|
||||
|
||||
prompt = "<messages>" + prompt + "</messages>";
|
||||
result = ((ChatCompletionService) client)
|
||||
.getStreamingChatMessageContentsAsync(
|
||||
prompt,
|
||||
arguments.getExecutionSettings(),
|
||||
executionSettings,
|
||||
kernel)
|
||||
.flatMap(streamingChatMessageContent -> {
|
||||
T value = variableType
|
||||
@@ -81,13 +84,13 @@ public class KernelFunctionFromPrompt extends DefaultKernelFunction {
|
||||
streamingChatMessageContent.getContent());
|
||||
return Flux.just(new StreamingContent<>(value));
|
||||
});
|
||||
return result;
|
||||
return result;
|
||||
|
||||
} else if (client instanceof TextGenerationService) {
|
||||
result = ((TextGenerationService) client)
|
||||
.getStreamingTextContentsAsync(
|
||||
prompt,
|
||||
arguments.getExecutionSettings(),
|
||||
executionSettings,
|
||||
kernel)
|
||||
.flatMap(streamingTextContent -> {
|
||||
T value = variableType.getConverter().fromPromptString(
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.microsoft.semantickernel.semanticfunctions;
|
||||
|
||||
public class KernelPromptTemplateFactory implements PromptTemplateFactory{
|
||||
|
||||
@Override
|
||||
public PromptTemplate tryCreate(PromptTemplateConfig templateConfig) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user