Skip to content

Commit

Permalink
small ordering update in kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
eavanvalkenburg committed Feb 16, 2024
1 parent 6efd7cf commit 3f1f14a
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions python/semantic_kernel/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,47 +590,6 @@ def register_semantic_function(

return function

def register_native_function(
self,
plugin_name: Optional[str],
kernel_function: Callable,
) -> KernelFunction:
"""
Creates a native function from the plugin name and kernel function
Args:
plugin_name (Optional[str]): The name of the plugin. If empty, a random name will be generated.
kernel_function (Callable): The kernel function
Returns:
KernelFunction: The created native function
"""
if not hasattr(kernel_function, "__kernel_function__"):
raise KernelException(
KernelException.ErrorCodes.InvalidFunctionType,
"kernel_function argument must be decorated with @kernel_function",
)
function_name = kernel_function.__kernel_function_name__

if plugin_name is None or plugin_name == "":
plugin_name = f"p_{generate_random_ascii_name()}"
assert plugin_name is not None # for type checker

validate_plugin_name(plugin_name)
validate_function_name(function_name)

if plugin_name in self.plugins and function_name in self.plugins[plugin_name]:
raise KernelException(
KernelException.ErrorCodes.FunctionOverloadNotSupported,
"Overloaded functions are not supported, " "please differentiate function names.",
)

function = KernelFunction.from_native_method(kernel_function, plugin_name)
self.add_plugin(plugin_name, [function])
function.set_default_plugin_collection(self.plugins)

return function

def create_semantic_function(
self,
prompt_template: str,
Expand Down Expand Up @@ -676,6 +635,47 @@ def _create_semantic_function(

return function

def register_native_function(
self,
plugin_name: Optional[str],
kernel_function: Callable,
) -> KernelFunction:
"""
Creates a native function from the plugin name and kernel function
Args:
plugin_name (Optional[str]): The name of the plugin. If empty, a random name will be generated.
kernel_function (Callable): The kernel function
Returns:
KernelFunction: The created native function
"""
if not hasattr(kernel_function, "__kernel_function__"):
raise KernelException(
KernelException.ErrorCodes.InvalidFunctionType,
"kernel_function argument must be decorated with @kernel_function",
)
function_name = kernel_function.__kernel_function_name__

if plugin_name is None or plugin_name == "":
plugin_name = f"p_{generate_random_ascii_name()}"
assert plugin_name is not None # for type checker

validate_plugin_name(plugin_name)
validate_function_name(function_name)

if plugin_name in self.plugins and function_name in self.plugins[plugin_name]:
raise KernelException(
KernelException.ErrorCodes.FunctionOverloadNotSupported,
"Overloaded functions are not supported, " "please differentiate function names.",
)

function = KernelFunction.from_native_method(kernel_function, plugin_name)
self.add_plugin(plugin_name, [function])
function.set_default_plugin_collection(self.plugins)

return function

# endregion
# region Services

Expand Down

0 comments on commit 3f1f14a

Please sign in to comment.