mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### Motivation and Context Solves #4864 The KernelFunction was configured as a Python class without regards to using Pydantic fields and its validations. As an aside, while running a kernel example related to function/tool calling, it didn't succeed. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description This PR cleans up the KernelFunction by creating Pydantic fields. We're also able to remove the KernelFunctionBase as it didn't contain a lot of logic after removing the properties. While testing other code, I found that the tool call kernel example wasn't succeeding. The tool_call payload needed a small reformat, and we needed to handle the tool_call_id and correctly handle the ToolCall object. As further work towards v1, we should look at removing support for FunctionCall as it is deprecated in Azure OpenAI. We now see: ``` User:> What is 3+3? Mosscap:> Ah, the timeless question of arithmetic! The sum of 3 and 3 is undoubtedly a splendid number. By utilizing my mathematical prowess, I can confidently declare that 3 plus 3 equals 6. It is a harmonious union of two threes, joining forces to create a new entity, greater in magnitude. The addition of these two numbers results in a perfect balance, a symphony of numerical harmony. ``` <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄 --------- Co-authored-by: Evan Mattson <[email protected]>
115 lines
4.8 KiB
Python
115 lines
4.8 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from semantic_kernel.orchestration.kernel_function import KernelFunction
|
|
from semantic_kernel.plugin_definition.kernel_function_decorator import kernel_function
|
|
|
|
if TYPE_CHECKING:
|
|
from semantic_kernel.orchestration.kernel_context import KernelContext
|
|
|
|
|
|
def test_init_native_function_with_input_description():
|
|
def mock_function(input: str, context: "KernelContext") -> None:
|
|
pass
|
|
|
|
mock_function.__kernel_function__ = True
|
|
mock_function.__kernel_function_name__ = "mock_function"
|
|
mock_function.__kernel_function_description__ = "Mock description"
|
|
mock_function.__kernel_function_input_description__ = "Mock input description"
|
|
mock_function.__kernel_function_input_default_value__ = "default_input_value"
|
|
mock_function.__kernel_function_context_parameters__ = [
|
|
{
|
|
"name": "param1",
|
|
"description": "Param 1 description",
|
|
"default_value": "default_param1_value",
|
|
}
|
|
]
|
|
|
|
mock_method = mock_function
|
|
|
|
native_function = KernelFunction.from_native_method(mock_method, "MockPlugin")
|
|
|
|
assert native_function.function == mock_method
|
|
assert native_function.parameters[0].name == "input"
|
|
assert native_function.parameters[0].description == "Mock input description"
|
|
assert native_function.parameters[0].default_value == "default_input_value"
|
|
assert native_function.parameters[0].type_ == "string"
|
|
assert native_function.parameters[0].required is False
|
|
assert native_function.parameters[1].name == "param1"
|
|
assert native_function.parameters[1].description == "Param 1 description"
|
|
assert native_function.parameters[1].default_value == "default_param1_value"
|
|
assert native_function.parameters[1].type_ == "string"
|
|
assert native_function.parameters[1].required is False
|
|
|
|
|
|
def test_init_native_function_without_input_description():
|
|
def mock_function(context: "KernelContext") -> None:
|
|
pass
|
|
|
|
mock_function.__kernel_function__ = True
|
|
mock_function.__kernel_function_name__ = "mock_function_no_input_desc"
|
|
mock_function.__kernel_function_description__ = "Mock description no input desc"
|
|
mock_function.__kernel_function_context_parameters__ = [
|
|
{
|
|
"name": "param1",
|
|
"description": "Param 1 description",
|
|
"default_value": "default_param1_value",
|
|
"required": True,
|
|
}
|
|
]
|
|
|
|
mock_method = mock_function
|
|
|
|
native_function = KernelFunction.from_native_method(mock_method, "MockPlugin")
|
|
|
|
assert native_function.function == mock_method
|
|
assert native_function.parameters[0].name == "param1"
|
|
assert native_function.parameters[0].description == "Param 1 description"
|
|
assert native_function.parameters[0].default_value == "default_param1_value"
|
|
assert native_function.parameters[0].type_ == "string"
|
|
assert native_function.parameters[0].required is True
|
|
|
|
|
|
def test_init_native_function_from_kernel_function_decorator():
|
|
@kernel_function(
|
|
description="Test description",
|
|
name="test_function",
|
|
input_description="Test input description",
|
|
input_default_value="test_default_value",
|
|
)
|
|
def decorated_function() -> None:
|
|
pass
|
|
|
|
assert decorated_function.__kernel_function__ is True
|
|
assert decorated_function.__kernel_function_description__ == "Test description"
|
|
assert decorated_function.__kernel_function_name__ == "test_function"
|
|
assert decorated_function.__kernel_function_input_description__ == "Test input description"
|
|
assert decorated_function.__kernel_function_input_default_value__ == "test_default_value"
|
|
|
|
native_function = KernelFunction.from_native_method(decorated_function, "MockPlugin")
|
|
|
|
assert native_function.function == decorated_function
|
|
assert native_function.parameters[0].name == "input"
|
|
assert native_function.parameters[0].description == "Test input description"
|
|
assert native_function.parameters[0].default_value == "test_default_value"
|
|
assert native_function.parameters[0].type_ == "string"
|
|
assert native_function.parameters[0].required is False
|
|
|
|
|
|
def test_init_native_function_from_kernel_function_decorator_defaults():
|
|
@kernel_function()
|
|
def decorated_function() -> None:
|
|
pass
|
|
|
|
assert decorated_function.__kernel_function__ is True
|
|
assert decorated_function.__kernel_function_description__ == ""
|
|
assert decorated_function.__kernel_function_name__ == "decorated_function"
|
|
assert decorated_function.__kernel_function_input_description__ == ""
|
|
assert decorated_function.__kernel_function_input_default_value__ == ""
|
|
|
|
native_function = KernelFunction.from_native_method(decorated_function, "MockPlugin")
|
|
|
|
assert native_function.function == decorated_function
|
|
assert len(native_function.parameters) == 0
|