-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathimport_skills.py
66 lines (50 loc) · 2.32 KB
/
import_skills.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright (c) Microsoft. All rights reserved.
import glob
import os
from typing import Dict
from semantic_kernel.kernel_extensions.extends_kernel import ExtendsKernel
from semantic_kernel.orchestration.sk_function_base import SKFunctionBase
from semantic_kernel.semantic_functions.prompt_template import PromptTemplate
from semantic_kernel.semantic_functions.prompt_template_config import (
PromptTemplateConfig,
)
from semantic_kernel.semantic_functions.semantic_function_config import (
SemanticFunctionConfig,
)
from semantic_kernel.utils.validation import validate_skill_name
class ImportSkills(ExtendsKernel):
def import_semantic_skill_from_directory(
self, parent_directory: str, skill_directory_name: str
) -> Dict[str, SKFunctionBase]:
CONFIG_FILE = "config.json"
PROMPT_FILE = "skprompt.txt"
kernel = self.kernel()
validate_skill_name(skill_directory_name)
skill_directory = os.path.join(parent_directory, skill_directory_name)
skill_directory = os.path.abspath(skill_directory)
if not os.path.exists(skill_directory):
raise ValueError(f"Skill directory does not exist: {skill_directory_name}")
skill = {}
directories = glob.glob(skill_directory + "/*/")
for directory in directories:
dir_name = os.path.dirname(directory)
function_name = os.path.basename(dir_name)
prompt_path = os.path.join(directory, PROMPT_FILE)
# Continue only if the prompt template exists
if not os.path.exists(prompt_path):
continue
config = PromptTemplateConfig()
config_path = os.path.join(directory, CONFIG_FILE)
with open(config_path, "r") as config_file:
config = config.from_json(config_file.read())
# Load Prompt Template
with open(prompt_path, "r") as prompt_file:
template = PromptTemplate(
prompt_file.read(), kernel.prompt_template_engine, config
)
# Prepare lambda wrapping AI logic
function_config = SemanticFunctionConfig(config, template)
skill[function_name] = kernel.register_semantic_function(
skill_directory_name, function_name, function_config
)
return skill