Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.plugin;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.semantickernel.contextvariables.CaseInsensitiveMap;
import com.microsoft.semantickernel.exceptions.SKException;
import com.microsoft.semantickernel.implementation.EmbeddedResourceLoader;
import com.microsoft.semantickernel.implementation.EmbeddedResourceLoader.ResourceLocation;
import com.microsoft.semantickernel.localization.SemanticKernelResources;
import com.microsoft.semantickernel.semanticfunctions.InputVariable;
import com.microsoft.semantickernel.semanticfunctions.KernelFunction;
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionFromMethod;
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionFromPrompt;
import com.microsoft.semantickernel.semanticfunctions.KernelPromptTemplateFactory;
import com.microsoft.semantickernel.semanticfunctions.OutputVariable;
import com.microsoft.semantickernel.semanticfunctions.PromptTemplate;
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig;
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateFactory;
import com.microsoft.semantickernel.semanticfunctions.annotations.DefineKernelFunction;
import com.microsoft.semantickernel.semanticfunctions.annotations.KernelFunctionParameter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Factory for creating {@link KernelPlugin} instances. {@code KernelPlugin}s can be created from a
* Java object, from loading a directory of plugins, or from loading plugins from a resource.
*/
public class KernelPluginFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(KernelPluginFactory.class);
private static final String CONFIG_FILE = "config.json";
private static final String PROMPT_FILE = "skprompt.txt";
private static final CaseInsensitiveMap> PRIMITIVE_CLASS_NAMES = new CaseInsensitiveMap<>();
private static final CaseInsensitiveMap> COMMON_CLASS_NAMES = new CaseInsensitiveMap<>();
private static final Map, Class>> BOXED_FROM_PRIMITIVE = new HashMap<>();
static {
PRIMITIVE_CLASS_NAMES.put("void", void.class);
PRIMITIVE_CLASS_NAMES.put("int", int.class);
PRIMITIVE_CLASS_NAMES.put("double", double.class);
PRIMITIVE_CLASS_NAMES.put("boolean", boolean.class);
PRIMITIVE_CLASS_NAMES.put("float", float.class);
PRIMITIVE_CLASS_NAMES.put("long", long.class);
PRIMITIVE_CLASS_NAMES.put("short", short.class);
PRIMITIVE_CLASS_NAMES.put("byte", byte.class);
PRIMITIVE_CLASS_NAMES.put("char", char.class);
COMMON_CLASS_NAMES.put("integer", int.class);
COMMON_CLASS_NAMES.put("string", String.class);
COMMON_CLASS_NAMES.put("list", ArrayList.class);
COMMON_CLASS_NAMES.put("map", HashMap.class);
COMMON_CLASS_NAMES.put("set", HashSet.class);
BOXED_FROM_PRIMITIVE.put(void.class, Void.class);
BOXED_FROM_PRIMITIVE.put(int.class, Integer.class);
BOXED_FROM_PRIMITIVE.put(double.class, Double.class);
BOXED_FROM_PRIMITIVE.put(boolean.class, Boolean.class);
BOXED_FROM_PRIMITIVE.put(float.class, Float.class);
BOXED_FROM_PRIMITIVE.put(long.class, Long.class);
BOXED_FROM_PRIMITIVE.put(short.class, Short.class);
BOXED_FROM_PRIMITIVE.put(byte.class, Byte.class);
BOXED_FROM_PRIMITIVE.put(char.class, Character.class);
}
/**
* Creates a plugin that wraps the specified target object. Methods decorated with
* {@code {@literal @}DefineSKFunction} will be included in the plugin.
*
* @param target The instance of the class to be wrapped.
* @param pluginName Name of the plugin for function collection and prompt templates. If the
* value is {@code null}, a plugin name is derived from the type of the
* target.
* @return The new plugin.
*/
public static KernelPlugin createFromObject(Object target, String pluginName) {
Class> clazz = target.getClass();
return KernelPluginFactory.createFromObject(clazz, target, pluginName);
}
/**
* Creates a plugin that wraps the specified target object. Methods decorated with
* {@code {@literal @}DefineSKFunction} will be included in the plugin.
*
* @param clazz The class to be wrapped.
* @param target The instance of the class to be wrapped.
* @param pluginName Name of the plugin for function collection and prompt templates. If the
* value is {@code null}, a plugin name is derived from the type of the
* target.
* @return The new plugin.
*/
public static KernelPlugin createFromObject(Class> clazz, Object target, String pluginName) {
if (!clazz.isInstance(target)) {
throw new SKException("Target object is not an instance of the provided class");
}
List> methods = Arrays.stream(clazz.getMethods())
.filter(method -> method.isAnnotationPresent(DefineKernelFunction.class))
.map(method -> {
DefineKernelFunction annotation = method.getAnnotation(DefineKernelFunction.class);
Class> returnType = getReturnType(annotation, method);
OutputVariable> kernelReturnParameterMetadata = new OutputVariable<>(
annotation.returnDescription(),
returnType);
KernelFunctionFromMethod.Builder