semantic-kernel
semantic-kernel copied to clipboard
FunctionTypeNotSupported Error raised in delegate_inference.py
Describe the bug While inferring the delegate type of a method, there is an error in checking when the inputs are string and SKcontext.
@staticmethod
@_infers(DelegateTypes.InStringAndContext)
def infer_in_string_and_context(signature: Signature, awaitable: bool) -> bool:
matches = _first_param_is_str(signature)
matches = matches and _first_param_is_context(signature)
matches = matches and _no_return(signature)
matches = matches and not awaitable
return matches
To Reproduce What I've added as a native function is:
from semantic_kernel.orchestration.sk_context import SKContext
from semantic_kernel.skill_definition import sk_function
class SkillForTest:
@sk_function(description="skill for test")
def in_string_and_context_func(self, input: str, context: SKContext):
return
Then, I've run code:
from semantic_kernel import Kernel
from semantic_kernel.skills.skill_for_test import SkillForTest
kernel = Kernel()
kernel.import_skill(SkillForTest())
This will result in an error like the one below.
Traceback (most recent call last):
File "/Users/jin/opt/anaconda3/envs/sk/lib/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Users/jin/opt/anaconda3/envs/sk/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/jin/git/semantic-kernel/python/tests/unit/skills/test_skill_for_test.py", line 5, in <module>
kernel.import_skill(SkillForTest())
File "/Users/jin/git/semantic-kernel/python/semantic_kernel/kernel.py", line 273, in import_skill
SKFunction.from_native_method(candidate, skill_name, self.logger)
File "/Users/jin/git/semantic-kernel/python/semantic_kernel/orchestration/sk_function.py", line 86, in from_native_method
delegate_type=DelegateInference.infer_delegate_type(method),
File "/Users/jin/git/semantic-kernel/python/semantic_kernel/orchestration/delegate_inference.py", line 243, in infer_delegate_type
if wrapped(function_signature, awaitable):
File "/Users/jin/git/semantic-kernel/python/semantic_kernel/orchestration/delegate_inference.py", line 227, in infer_unknown
raise KernelException(
semantic_kernel.kernel_exception.KernelException: (<ErrorCodes.FunctionTypeNotSupported: 3>, 'Invalid function type detected, unable to infer DelegateType.', None)
Expected behavior Therefore, the parameter check logic should be modified as follows:
@staticmethod
@_infers(DelegateTypes.InStringAndContext)
def infer_in_string_and_context(signature: Signature, awaitable: bool) -> bool:
matches = _first_param_is_str(signature, only=False)
matches = matches and _has_two_params_second_is_context(signature)
matches = matches and _no_return(signature)
matches = matches and not awaitable
return matches
This will prevent errors from occurring when performing the same script.
Screenshots
Desktop (please complete the following information):
- OS: macOS
- IDE: VSCode
- NuGet Package Version [e.g. 0.1.0]
Additional context
@mkarle can you take a look - it might be similar to #1069
Closing this, please re-raise it if it's still an issue!