|
| 1 | +package com.microsoft.semantickernel.extensions; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.microsoft.semantickernel.builders.SKBuilders; |
| 5 | +import com.microsoft.semantickernel.semanticfunctions.PromptTemplate; |
| 6 | +import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; |
| 7 | +import com.microsoft.semantickernel.semanticfunctions.SemanticFunctionConfig; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | +import java.io.File; |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.charset.Charset; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +public class KernelExtensions { |
| 19 | + private static final Logger LOGGER = |
| 20 | + LoggerFactory.getLogger(KernelExtensions.class); |
| 21 | + |
| 22 | + public static Map<String, SemanticFunctionConfig> importSemanticSkillFromDirectory( |
| 23 | + String parentDirectory, String skillDirectoryName) { |
| 24 | + |
| 25 | + String CONFIG_FILE = "config.json"; |
| 26 | + String PROMPT_FILE = "skprompt.txt"; |
| 27 | + |
| 28 | + // Verify.ValidSkillName(skillDirectoryName); |
| 29 | + File skillDir = new File(parentDirectory, skillDirectoryName); |
| 30 | + // Verify.DirectoryExists(skillDir); |
| 31 | + |
| 32 | + File[] files = skillDir.listFiles(); |
| 33 | + if (files == null) { |
| 34 | + return Collections.unmodifiableMap(new HashMap<>()); |
| 35 | + } |
| 36 | + |
| 37 | + HashMap<String, SemanticFunctionConfig> skills = new HashMap<>(); |
| 38 | + |
| 39 | + for (File dir : files) { |
| 40 | + try { |
| 41 | + // Continue only if prompt template exists |
| 42 | + File promptPath = new File(dir, PROMPT_FILE); |
| 43 | + if (!promptPath.exists()) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + // Load prompt configuration. Note: the configuration is |
| 48 | + // optional. |
| 49 | + PromptTemplateConfig config = new PromptTemplateConfig("", "", null); |
| 50 | + |
| 51 | + File configPath = new File(dir, CONFIG_FILE); |
| 52 | + if (configPath.exists()) { |
| 53 | + config = new ObjectMapper().readValue(configPath, PromptTemplateConfig.class); |
| 54 | + |
| 55 | + // Verify.NotNull(config, $"Invalid prompt template |
| 56 | + // configuration, unable to parse {configPath}"); |
| 57 | + } |
| 58 | + |
| 59 | + // kernel.Log.LogTrace("Config {0}: {1}", functionName, |
| 60 | + // config.ToJson()); |
| 61 | + |
| 62 | + // Load prompt template |
| 63 | + PromptTemplate template = SKBuilders.promptTemplate() |
| 64 | + .build(new String( |
| 65 | + Files.readAllBytes(promptPath.toPath()), |
| 66 | + Charset.defaultCharset()), |
| 67 | + config); |
| 68 | + |
| 69 | + skills.put(dir.getName(), new SemanticFunctionConfig(config, template)); |
| 70 | + } catch (IOException e) { |
| 71 | + LOGGER.error("Failed to read file", e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return skills; |
| 76 | + } |
| 77 | +} |
0 commit comments