|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +from semantic_kernel.orchestration.sk_function import SKFunction |
| 4 | +from semantic_kernel.skill_definition.sk_function_decorator import sk_function |
| 5 | + |
| 6 | + |
| 7 | +def test_init_native_function_with_input_description(): |
| 8 | + class MockMethodWithInputDescription: |
| 9 | + __sk_function__ = True |
| 10 | + __sk_function_name__ = "mock_function" |
| 11 | + __sk_function_description__ = "Mock description" |
| 12 | + __sk_function_input_description__ = "Mock input description" |
| 13 | + __sk_function_input_default_value__ = "default_input_value" |
| 14 | + __sk_function_context_parameters__ = [ |
| 15 | + { |
| 16 | + "name": "param1", |
| 17 | + "description": "Param 1 description", |
| 18 | + "default_value": "default_param1_value", |
| 19 | + } |
| 20 | + ] |
| 21 | + |
| 22 | + mock_method = MockMethodWithInputDescription |
| 23 | + |
| 24 | + native_function = SKFunction.from_native_method(mock_method, "MockSkill") |
| 25 | + |
| 26 | + assert native_function._function == mock_method |
| 27 | + assert native_function._parameters[0].name == "input" |
| 28 | + assert native_function._parameters[0].description == "Mock input description" |
| 29 | + assert native_function._parameters[0].default_value == "default_input_value" |
| 30 | + assert native_function._parameters[1].name == "param1" |
| 31 | + assert native_function._parameters[1].description == "Param 1 description" |
| 32 | + assert native_function._parameters[1].default_value == "default_param1_value" |
| 33 | + |
| 34 | + |
| 35 | +def test_init_native_function_without_input_description(): |
| 36 | + class MockMethodWithoutInputDescription: |
| 37 | + __sk_function__ = True |
| 38 | + __sk_function_name__ = "mock_function_no_input_desc" |
| 39 | + __sk_function_description__ = "Mock description no input desc" |
| 40 | + __sk_function_context_parameters__ = [ |
| 41 | + { |
| 42 | + "name": "param1", |
| 43 | + "description": "Param 1 description", |
| 44 | + "default_value": "default_param1_value", |
| 45 | + } |
| 46 | + ] |
| 47 | + |
| 48 | + mock_method = MockMethodWithoutInputDescription |
| 49 | + |
| 50 | + native_function = SKFunction.from_native_method(mock_method, "MockSkill") |
| 51 | + |
| 52 | + assert native_function._function == mock_method |
| 53 | + assert native_function._parameters[0].name == "param1" |
| 54 | + assert native_function._parameters[0].description == "Param 1 description" |
| 55 | + assert native_function._parameters[0].default_value == "default_param1_value" |
| 56 | + |
| 57 | + |
| 58 | +def test_init_native_function_from_sk_function_decorator(): |
| 59 | + @sk_function( |
| 60 | + description="Test description", |
| 61 | + name="test_function", |
| 62 | + input_description="Test input description", |
| 63 | + input_default_value="test_default_value", |
| 64 | + ) |
| 65 | + def decorated_function(): |
| 66 | + pass |
| 67 | + |
| 68 | + assert decorated_function.__sk_function__ is True |
| 69 | + assert decorated_function.__sk_function_description__ == "Test description" |
| 70 | + assert decorated_function.__sk_function_name__ == "test_function" |
| 71 | + assert ( |
| 72 | + decorated_function.__sk_function_input_description__ == "Test input description" |
| 73 | + ) |
| 74 | + assert ( |
| 75 | + decorated_function.__sk_function_input_default_value__ == "test_default_value" |
| 76 | + ) |
| 77 | + |
| 78 | + native_function = SKFunction.from_native_method(decorated_function, "MockSkill") |
| 79 | + |
| 80 | + assert native_function._function == decorated_function |
| 81 | + assert native_function._parameters[0].name == "input" |
| 82 | + assert native_function._parameters[0].description == "Test input description" |
| 83 | + assert native_function._parameters[0].default_value == "test_default_value" |
| 84 | + |
| 85 | + |
| 86 | +def test_init_native_function_from_sk_function_decorator_defaults(): |
| 87 | + @sk_function() |
| 88 | + def decorated_function(): |
| 89 | + pass |
| 90 | + |
| 91 | + assert decorated_function.__sk_function__ is True |
| 92 | + assert decorated_function.__sk_function_description__ == "" |
| 93 | + assert decorated_function.__sk_function_name__ == "decorated_function" |
| 94 | + assert decorated_function.__sk_function_input_description__ == "" |
| 95 | + assert decorated_function.__sk_function_input_default_value__ == "" |
| 96 | + |
| 97 | + native_function = SKFunction.from_native_method(decorated_function, "MockSkill") |
| 98 | + |
| 99 | + assert native_function._function == decorated_function |
| 100 | + assert len(native_function._parameters) == 0 |
0 commit comments