Files
semantic-kernel/python/semantic_kernel/skill_definition/functions_view.py
T
f60d7ba0d7 Fix functions_view.py (#1213)
### Motivation and Context
#1212 

### Description
Fixed a bug in the process of checking native function.


---------

Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Devis Lucato <[email protected]>
2023-06-09 14:21:34 -07:00

60 lines
2.1 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
from typing import Dict, List
from semantic_kernel.kernel_exception import KernelException
from semantic_kernel.skill_definition.function_view import FunctionView
class FunctionsView:
_semantic_functions: Dict[str, List[FunctionView]]
_native_functions: Dict[str, List[FunctionView]]
def __init__(self) -> None:
self._semantic_functions = {}
self._native_functions = {}
def add_function(self, view: FunctionView) -> "FunctionsView":
if view.is_semantic:
if view.skill_name not in self._semantic_functions:
self._semantic_functions[view.skill_name] = []
self._semantic_functions[view.skill_name].append(view)
else:
if view.skill_name not in self._native_functions:
self._native_functions[view.skill_name] = []
self._native_functions[view.skill_name].append(view)
return self
def is_semantic(self, skill_name: str, function_name: str) -> bool:
as_sf = self._semantic_functions.get(skill_name, [])
as_sf = any(f.name == function_name for f in as_sf)
as_nf = self._native_functions.get(skill_name, [])
as_nf = any(f.name == function_name for f in as_nf)
if as_sf and as_nf:
raise KernelException(
KernelException.ErrorCodes.AmbiguousImplementation,
f"There are 2 functions with the same name: {function_name}."
f"One is native and the other semantic.",
)
return as_sf
def is_native(self, skill_name: str, function_name: str) -> bool:
as_sf = self._semantic_functions.get(skill_name, [])
as_sf = any(f.name == function_name for f in as_sf)
as_nf = self._native_functions.get(skill_name, [])
as_nf = any(f.name == function_name for f in as_nf)
if as_sf and as_nf:
raise KernelException(
KernelException.ErrorCodes.AmbiguousImplementation,
f"There are 2 functions with the same name: {function_name}."
f"One is native and the other semantic.",
)
return as_nf