SIGN IN SIGN UP

Integrate cutting-edge LLM technology quickly and easily into your apps

0 0 30 C#
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
# Copyright (c) Microsoft. All rights reserved.
from collections.abc import AsyncGenerator, Iterable
from typing import Annotated, Any
Python: fix for kernel function type_object when not using Annotations. Add more unit tests. (#7338) ### Motivation and Context The current kernel function decorator class parses function params, but had a miss related to how the underlying param's type is parsed if not using Annotations. <!-- 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 properly handles the type_object for params that don't using typing.Annotations. - Adds unit tests to exercise the code. - Adds more kernel.py unit tests for near 100% test coverage. - Adds some planner tests to improve the test coverage. - Closes #7300 <!-- 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 :smile:
2024-07-18 01:12:08 -04:00
from unittest.mock import Mock
Python: rebuilt exceptions structure; pythonic version (#5231) ### Motivation and Context <!-- 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. --> The existing Exceptions structure was very much inspired by dotnet, this is now replaced with a pythonic implementations. This means all Exceptions derive from KernelException and then specialise for different purposes. Closes #2194 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added folder for all exception types, all can be imported through `from semantic_kernel.exceptions import ...` no need for a user to know which file the relevant one is in, but keeps things tidy for developers. Removed old KernelException, added back subtypes for the errorcodes. Went through everything to make sure the `raise ... from exc` pattern is used as much as possible as that returns a better stacktrace. ### 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 :smile:
2024-02-28 19:53:53 +01:00
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
import pytest
from semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion import OpenAIChatCompletion
2024-03-12 10:27:55 +01:00
from semantic_kernel.exceptions import FunctionExecutionException, FunctionInitializationError
Python: fix for kernel function type_object when not using Annotations. Add more unit tests. (#7338) ### Motivation and Context The current kernel function decorator class parses function params, but had a miss related to how the underlying param's type is parsed if not using Annotations. <!-- 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 properly handles the type_object for params that don't using typing.Annotations. - Adds unit tests to exercise the code. - Adds more kernel.py unit tests for near 100% test coverage. - Adds some planner tests to improve the test coverage. - Closes #7300 <!-- 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 :smile:
2024-07-18 01:12:08 -04:00
from semantic_kernel.filters.functions.function_invocation_context import FunctionInvocationContext
from semantic_kernel.filters.kernel_filters_extension import _rebuild_function_invocation_context
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
from semantic_kernel.functions.function_result import FunctionResult
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.functions.kernel_function import KernelFunction
from semantic_kernel.functions.kernel_function_decorator import kernel_function
Python: Unsafe input handling (#6003) ### Motivation and Context <!-- 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. --> Implements dealing with unsafe content, by doing HTML parsing on variables and function results. Closes: #5889 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Adds parameter `allow_dangerously_set_content` to: - InputVariable - PromptTemplateConfig - PromptTemplateBase The behavior is that if the flag is set to True on the template itself (KernelPromptTemplate, Jinja2PromptTemplate or HandlebarsPromptTemplate) the behavior is the same, no encoding is done on inputs. Otherwise: - variables are encoded by default, this can be switched off using the InputVariables class for that variable. - function output is encoded by default, this can be switched off using the flag in the PromptTemplateConfig, this is not yet possible to do on a per function basis. ### 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 :smile:
2024-05-16 15:02:54 +02:00
from semantic_kernel.functions.kernel_function_from_method import KernelFunctionFromMethod
Python: Increase unit test coverage (#7211) ### Motivation and Context We've been working on increasing the test coverage. These tests aren't specific to the CQ spike, but I noticed that there was covering lacking for them, and they were "low hanging fruit." <!-- 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 Increasing the test coverage for some classes that have current test coverage but were missing some line coverage here and there. At this point in time, the test coverage report is showing 95% covered now. <!-- 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 :smile: --------- Co-authored-by: Gil LaHaye <gillahaye@microsoft.com>
2024-07-11 16:05:06 -04:00
from semantic_kernel.functions.kernel_parameter_metadata import KernelParameterMetadata
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
from semantic_kernel.kernel import Kernel
2024-03-12 10:27:55 +01:00
from semantic_kernel.kernel_pydantic import KernelBaseModel
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
Python: fix for kernel function type_object when not using Annotations. Add more unit tests. (#7338) ### Motivation and Context The current kernel function decorator class parses function params, but had a miss related to how the underlying param's type is parsed if not using Annotations. <!-- 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 properly handles the type_object for params that don't using typing.Annotations. - Adds unit tests to exercise the code. - Adds more kernel.py unit tests for near 100% test coverage. - Adds some planner tests to improve the test coverage. - Closes #7300 <!-- 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 :smile:
2024-07-18 01:12:08 -04:00
class CustomType(KernelBaseModel):
id: str
name: str
class CustomTypeNonPydantic:
id: str
name: str
def __init__(self, id: str, name: str):
self.id = id
self.name = name
@pytest.fixture
def get_custom_type_function_pydantic():
@kernel_function
def func_default(param: list[CustomType]):
return input
return KernelFunction.from_method(func_default, "test")
@pytest.fixture
def get_custom_type_function_nonpydantic():
@kernel_function
def func_default(param: list[CustomTypeNonPydantic]):
return input
return KernelFunction.from_method(func_default, "test")
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
def test_init_native_function_with_input_description():
@kernel_function(description="Mock description", name="mock_function")
def mock_function(input: Annotated[str, "input"], arguments: "KernelArguments") -> None:
pass
mock_method = mock_function
native_function = KernelFunction.from_method(method=mock_method, plugin_name="MockPlugin")
assert native_function.method == mock_method
assert native_function.parameters[0].name == "input"
assert native_function.parameters[0].description == "input"
assert not native_function.parameters[0].default_value
assert native_function.parameters[0].type_ == "str"
Python: Complex Type support for Method functions (#5260) ### Motivation and Context <!-- 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. --> Added support for complex types (classes) for KernelFunctionFromMethod. Implements #4635 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This PR adds a field to the KernelParameterMetadata class for type_object When that field is filled and supplied by a KernelFunctionFromMethod decorator, it is used during invoke to cast the type. It checks if it is a pydantic type and calls `model_validate_json` in that case, otherwise invokes it directly as a init (like with a str), this then also casts strs to ints for example. ### 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 :smile:
2024-03-01 20:21:07 +01:00
assert native_function.parameters[0].is_required is True
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
assert native_function.parameters[1].name == "arguments"
Python: Add json schema handling. Add experimental tag to OpenAPI and Memory Connectors. (#6335) ### Motivation and Context The Python code base could handle some primitive types for the schema for tool call objects and kernel parameter metadata. However, it couldn't properly handle the more complex JSON schemas for tool call objects. <!-- 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 introduces: - JSON schema handling for KernelParameterMetadata and tool call objects. - Updates to the tool call utils method to properly recurse on the KernelParameterMetadata's stucture. - Adds unit tests for this code. - Add experimental_class/experimental_functions to various parts of the code base like Memory Connectors and OpenAPI plugin. - Fixes #6310 - Fixes #6280 <!-- 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 :smile:
2024-05-20 17:56:30 -04:00
assert native_function.parameters[1].description is None
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
assert not native_function.parameters[1].default_value
assert native_function.parameters[1].type_ == "KernelArguments"
Python: Complex Type support for Method functions (#5260) ### Motivation and Context <!-- 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. --> Added support for complex types (classes) for KernelFunctionFromMethod. Implements #4635 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This PR adds a field to the KernelParameterMetadata class for type_object When that field is filled and supplied by a KernelFunctionFromMethod decorator, it is used during invoke to cast the type. It checks if it is a pydantic type and calls `model_validate_json` in that case, otherwise invokes it directly as a init (like with a str), this then also casts strs to ints for example. ### 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 :smile:
2024-03-01 20:21:07 +01:00
assert native_function.parameters[1].is_required is True
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
def test_init_native_function_without_input_description():
@kernel_function()
def mock_function(arguments: "KernelArguments") -> 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_parameters__ = [
{
"name": "arguments",
"description": "Param 1 description",
"default_value": "default_param1_value",
Python: Complex Type support for Method functions (#5260) ### Motivation and Context <!-- 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. --> Added support for complex types (classes) for KernelFunctionFromMethod. Implements #4635 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This PR adds a field to the KernelParameterMetadata class for type_object When that field is filled and supplied by a KernelFunctionFromMethod decorator, it is used during invoke to cast the type. It checks if it is a pydantic type and calls `model_validate_json` in that case, otherwise invokes it directly as a init (like with a str), this then also casts strs to ints for example. ### 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 :smile:
2024-03-01 20:21:07 +01:00
"is_required": True,
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
}
]
mock_method = mock_function
native_function = KernelFunction.from_method(method=mock_method, plugin_name="MockPlugin")
assert native_function.method == mock_method
assert native_function.parameters[0].name == "arguments"
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_ == "str"
Python: Complex Type support for Method functions (#5260) ### Motivation and Context <!-- 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. --> Added support for complex types (classes) for KernelFunctionFromMethod. Implements #4635 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This PR adds a field to the KernelParameterMetadata class for type_object When that field is filled and supplied by a KernelFunctionFromMethod decorator, it is used during invoke to cast the type. It checks if it is a pydantic type and calls `model_validate_json` in that case, otherwise invokes it directly as a init (like with a str), this then also casts strs to ints for example. ### 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 :smile:
2024-03-01 20:21:07 +01:00
assert native_function.parameters[0].is_required is True
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
def test_init_native_function_from_kernel_function_decorator():
@kernel_function(
description="Test description",
name="test_function",
)
def decorated_function(input: Annotated[str | None, "Test input description"] = "test_default_value") -> None:
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
pass
assert decorated_function.__kernel_function__ is True
assert decorated_function.__kernel_function_description__ == "Test description"
assert decorated_function.__kernel_function_name__ == "test_function"
native_function = KernelFunction.from_method(method=decorated_function, plugin_name="MockPlugin")
assert native_function.method == 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_ == "str"
Python: Complex Type support for Method functions (#5260) ### Motivation and Context <!-- 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. --> Added support for complex types (classes) for KernelFunctionFromMethod. Implements #4635 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This PR adds a field to the KernelParameterMetadata class for type_object When that field is filled and supplied by a KernelFunctionFromMethod decorator, it is used during invoke to cast the type. It checks if it is a pydantic type and calls `model_validate_json` in that case, otherwise invokes it directly as a init (like with a str), this then also casts strs to ints for example. ### 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 :smile:
2024-03-01 20:21:07 +01:00
assert native_function.parameters[0].is_required is False
Python: Increase unit test coverage (#7211) ### Motivation and Context We've been working on increasing the test coverage. These tests aren't specific to the CQ spike, but I noticed that there was covering lacking for them, and they were "low hanging fruit." <!-- 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 Increasing the test coverage for some classes that have current test coverage but were missing some line coverage here and there. At this point in time, the test coverage report is showing 95% covered now. <!-- 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 :smile: --------- Co-authored-by: Gil LaHaye <gillahaye@microsoft.com>
2024-07-11 16:05:06 -04:00
assert type(native_function.return_parameter) is KernelParameterMetadata
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
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__ is None
assert decorated_function.__kernel_function_name__ == "decorated_function"
native_function = KernelFunction.from_method(method=decorated_function, plugin_name="MockPlugin")
assert native_function.method == decorated_function
assert len(native_function.parameters) == 0
def test_init_method_is_none():
Python: rebuilt exceptions structure; pythonic version (#5231) ### Motivation and Context <!-- 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. --> The existing Exceptions structure was very much inspired by dotnet, this is now replaced with a pythonic implementations. This means all Exceptions derive from KernelException and then specialise for different purposes. Closes #2194 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added folder for all exception types, all can be imported through `from semantic_kernel.exceptions import ...` no need for a user to know which file the relevant one is in, but keeps things tidy for developers. Removed old KernelException, added back subtypes for the errorcodes. Went through everything to make sure the `raise ... from exc` pattern is used as much as possible as that returns a better stacktrace. ### 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 :smile:
2024-02-28 19:53:53 +01:00
with pytest.raises(FunctionInitializationError):
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
KernelFunction.from_method(method=None, plugin_name="MockPlugin")
def test_init_method_is_not_kernel_function():
def not_kernel_function():
pass
Python: rebuilt exceptions structure; pythonic version (#5231) ### Motivation and Context <!-- 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. --> The existing Exceptions structure was very much inspired by dotnet, this is now replaced with a pythonic implementations. This means all Exceptions derive from KernelException and then specialise for different purposes. Closes #2194 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added folder for all exception types, all can be imported through `from semantic_kernel.exceptions import ...` no need for a user to know which file the relevant one is in, but keeps things tidy for developers. Removed old KernelException, added back subtypes for the errorcodes. Went through everything to make sure the `raise ... from exc` pattern is used as much as possible as that returns a better stacktrace. ### 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 :smile:
2024-02-28 19:53:53 +01:00
with pytest.raises(FunctionInitializationError):
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
KernelFunction.from_method(method=not_kernel_function, plugin_name="MockPlugin")
def test_init_invalid_name():
@kernel_function(name="invalid name")
def invalid_name():
pass
Python: rebuilt exceptions structure; pythonic version (#5231) ### Motivation and Context <!-- 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. --> The existing Exceptions structure was very much inspired by dotnet, this is now replaced with a pythonic implementations. This means all Exceptions derive from KernelException and then specialise for different purposes. Closes #2194 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added folder for all exception types, all can be imported through `from semantic_kernel.exceptions import ...` no need for a user to know which file the relevant one is in, but keeps things tidy for developers. Removed old KernelException, added back subtypes for the errorcodes. Went through everything to make sure the `raise ... from exc` pattern is used as much as possible as that returns a better stacktrace. ### 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 :smile:
2024-02-28 19:53:53 +01:00
with pytest.raises(FunctionInitializationError):
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
KernelFunction.from_method(method=invalid_name, plugin_name="MockPlugin")
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async def test_invoke_non_async(kernel: Kernel):
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
@kernel_function()
def non_async_function() -> str:
return ""
native_function = KernelFunction.from_method(method=non_async_function, plugin_name="MockPlugin")
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
result = await native_function.invoke(kernel=kernel, arguments=None)
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
assert result.value == ""
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
with pytest.raises(NotImplementedError):
async for _ in native_function.invoke_stream(kernel=kernel, arguments=None):
pass
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async def test_invoke_async(kernel: Kernel):
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
@kernel_function()
async def async_function() -> str:
return ""
native_function = KernelFunction.from_method(method=async_function, plugin_name="MockPlugin")
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
result = await native_function.invoke(kernel=kernel, arguments=None)
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
assert result.value == ""
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
with pytest.raises(NotImplementedError):
async for _ in native_function.invoke_stream(kernel=kernel, arguments=None):
pass
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async def test_invoke_gen(kernel: Kernel):
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
@kernel_function()
def gen_function() -> Iterable[str]:
yield ""
native_function = KernelFunction.from_method(method=gen_function, plugin_name="MockPlugin")
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
result = await native_function.invoke(kernel=kernel, arguments=None)
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
assert result.value == [""]
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async for partial_result in native_function.invoke_stream(kernel=kernel, arguments=None):
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
assert partial_result == ""
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async def test_invoke_gen_async(kernel: Kernel):
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
@kernel_function()
async def async_gen_function() -> AsyncGenerator[str, Any]:
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
yield ""
native_function = KernelFunction.from_method(method=async_gen_function, plugin_name="MockPlugin")
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
result = await native_function.invoke(kernel=kernel, arguments=None)
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
assert result.value == [""]
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async for partial_result in native_function.invoke_stream(kernel=kernel, arguments=None):
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
assert partial_result == ""
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async def test_service_execution(kernel: Kernel, openai_unit_test_env):
Python: Introduce Pydantic settings (#6193) ### Motivation and Context SK Python is tightly coupled to the use of a `.env` file to read all secrets, keys, endpoints, and more. This doesn't scale well for users who wish to be able to use environment variables with their SK Applications. By introducing Pydantic Settings, it is possible to use both environment variables as well as have a fall-back to a `.env` file (via a `env_file_path` parameter), if desired. By introducing Pydantic Settings, we are removing the requirement to have to create Text/Embedding/Chat completion objects with an `api_key` or other previously required information (in the case of AzureChatCompletion that means an `endpoint`, an `api_key`, a `deployment_name`, and an `api_version`). When the AI connector is created, the Pydantic settings are loaded either via env vars or the fall-back `.env` file, and that means the user can create a chat completion object like: ```python chat_completion = OpenAIChatCompletion(service_id="test") ``` or, to optionally override the `ai_model_id` env var: ```python chat_completion = OpenAIChatCompletion(service_id="test", ai_model_id="gpt-4-1106") ``` Note: we have left the ability to specific an `api_key`/`org_id` for `OpenAIChatCompletion` or a `deployment_name`, `endpoint`, `base_url`, and `api_version` for `AzureChatCompletion` as before, but if your settings are configured to use env vars/.env file then there is no need to pass this information. <!-- 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 The PR introduces the use of Pydantic settings and removes the use of the python-dotenv library. - Closes #1779 - Updates notebooks, samples, code and tests to remove the explicit config of api_key or other previous .env files values. - Adds new unit test config using monkeypatch to simulate env variables for testing - All unit and integration tests passing <!-- 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 - [ ] I didn't break anyone :smile:
2024-05-16 07:44:40 -04:00
service = OpenAIChatCompletion(service_id="test", ai_model_id="test")
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
req_settings = service.get_prompt_execution_settings_class()(service_id="test")
req_settings.temperature = 0.5
kernel.add_service(service)
arguments = KernelArguments(settings=req_settings)
@kernel_function(name="function")
def my_function(kernel, service, execution_settings, arguments) -> str:
assert kernel is not None
assert isinstance(kernel, Kernel)
assert service is not None
assert isinstance(service, OpenAIChatCompletion)
assert execution_settings is not None
assert isinstance(execution_settings, req_settings.__class__)
assert execution_settings.temperature == 0.5
assert arguments is not None
assert isinstance(arguments, KernelArguments)
return "ok"
func = KernelFunction.from_method(my_function, "test")
result = await func.invoke(kernel, arguments)
assert result.value == "ok"
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async def test_required_param_not_supplied(kernel: Kernel):
Python: split kernelfunction into fromprompt and frommethod classes (#5159) ### Motivation and Context <!-- 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. --> Closes #4633 and brings this part of the python sdk in sync with dotnet, also makes for a much more readable set of classes. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added: - KernelFunctionFromPrompt class - KernelFunctionFromMethod class - tests for both. Changed: - removed parts of KernelFunction that are specific to one of the two subtypes - removed expose attribute of KernelParameterMetadata as that is no longer needed ### 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 - [ ] I didn't break anyone :smile: --------- Co-authored-by: Evan Mattson <evan.mattson@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-02-26 21:20:25 +01:00
@kernel_function()
def my_function(input: str) -> str:
return input
func = KernelFunction.from_method(my_function, "test")
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
with pytest.raises(FunctionExecutionException):
await func.invoke(kernel=kernel, arguments=KernelArguments())
Python: Complex Type support for Method functions (#5260) ### Motivation and Context <!-- 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. --> Added support for complex types (classes) for KernelFunctionFromMethod. Implements #4635 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This PR adds a field to the KernelParameterMetadata class for type_object When that field is filled and supplied by a KernelFunctionFromMethod decorator, it is used during invoke to cast the type. It checks if it is a pydantic type and calls `model_validate_json` in that case, otherwise invokes it directly as a init (like with a str), this then also casts strs to ints for example. ### 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 :smile:
2024-03-01 20:21:07 +01:00
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async def test_service_execution_with_complex_object(kernel: Kernel):
Python: Complex Type support for Method functions (#5260) ### Motivation and Context <!-- 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. --> Added support for complex types (classes) for KernelFunctionFromMethod. Implements #4635 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This PR adds a field to the KernelParameterMetadata class for type_object When that field is filled and supplied by a KernelFunctionFromMethod decorator, it is used during invoke to cast the type. It checks if it is a pydantic type and calls `model_validate_json` in that case, otherwise invokes it directly as a init (like with a str), this then also casts strs to ints for example. ### 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 :smile:
2024-03-01 20:21:07 +01:00
class InputObject(KernelBaseModel):
arg1: str
arg2: int
@kernel_function(name="function")
def my_function(input_obj: InputObject) -> str:
assert input_obj is not None
assert isinstance(input_obj, InputObject)
assert input_obj.arg1 == "test"
assert input_obj.arg2 == 5
return f"{input_obj.arg1} {input_obj.arg2}"
func = KernelFunction.from_method(my_function, "test")
arguments = KernelArguments(input_obj=InputObject(arg1="test", arg2=5))
result = await func.invoke(kernel, arguments)
assert result.value == "test 5"
class InputObject(KernelBaseModel):
arg1: str
arg2: int
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async def test_service_execution_with_complex_object_from_str(kernel: Kernel):
Python: Complex Type support for Method functions (#5260) ### Motivation and Context <!-- 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. --> Added support for complex types (classes) for KernelFunctionFromMethod. Implements #4635 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This PR adds a field to the KernelParameterMetadata class for type_object When that field is filled and supplied by a KernelFunctionFromMethod decorator, it is used during invoke to cast the type. It checks if it is a pydantic type and calls `model_validate_json` in that case, otherwise invokes it directly as a init (like with a str), this then also casts strs to ints for example. ### 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 :smile:
2024-03-01 20:21:07 +01:00
@kernel_function(name="function")
def my_function(input_obj: InputObject) -> str:
assert input_obj is not None
assert isinstance(input_obj, InputObject)
assert input_obj.arg1 == "test"
assert input_obj.arg2 == 5
return f"{input_obj.arg1} {input_obj.arg2}"
func = KernelFunction.from_method(my_function, "test")
arguments = KernelArguments(input_obj={"arg1": "test", "arg2": 5})
result = await func.invoke(kernel, arguments)
assert result.value == "test 5"
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async def test_service_execution_with_complex_object_from_str_mixed(kernel: Kernel):
Python: Complex Type support for Method functions (#5260) ### Motivation and Context <!-- 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. --> Added support for complex types (classes) for KernelFunctionFromMethod. Implements #4635 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This PR adds a field to the KernelParameterMetadata class for type_object When that field is filled and supplied by a KernelFunctionFromMethod decorator, it is used during invoke to cast the type. It checks if it is a pydantic type and calls `model_validate_json` in that case, otherwise invokes it directly as a init (like with a str), this then also casts strs to ints for example. ### 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 :smile:
2024-03-01 20:21:07 +01:00
@kernel_function(name="function")
def my_function(input_obj: InputObject, input_str: str) -> str:
assert input_obj is not None
assert isinstance(input_obj, InputObject)
assert input_obj.arg1 == "test"
assert input_obj.arg2 == 5
return f"{input_obj.arg1} {input_str} {input_obj.arg2}"
func = KernelFunction.from_method(my_function, "test")
arguments = KernelArguments(input_obj={"arg1": "test", "arg2": 5}, input_str="test2")
result = await func.invoke(kernel, arguments)
assert result.value == "test test2 5"
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async def test_service_execution_with_complex_object_from_str_mixed_multi(kernel: Kernel):
Python: Complex Type support for Method functions (#5260) ### Motivation and Context <!-- 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. --> Added support for complex types (classes) for KernelFunctionFromMethod. Implements #4635 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This PR adds a field to the KernelParameterMetadata class for type_object When that field is filled and supplied by a KernelFunctionFromMethod decorator, it is used during invoke to cast the type. It checks if it is a pydantic type and calls `model_validate_json` in that case, otherwise invokes it directly as a init (like with a str), this then also casts strs to ints for example. ### 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 :smile:
2024-03-01 20:21:07 +01:00
@kernel_function(name="function")
def my_function(input_obj: InputObject, input_str: str | int) -> str:
Python: Complex Type support for Method functions (#5260) ### Motivation and Context <!-- 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. --> Added support for complex types (classes) for KernelFunctionFromMethod. Implements #4635 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This PR adds a field to the KernelParameterMetadata class for type_object When that field is filled and supplied by a KernelFunctionFromMethod decorator, it is used during invoke to cast the type. It checks if it is a pydantic type and calls `model_validate_json` in that case, otherwise invokes it directly as a init (like with a str), this then also casts strs to ints for example. ### 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 :smile:
2024-03-01 20:21:07 +01:00
assert input_obj is not None
assert isinstance(input_obj, InputObject)
assert input_obj.arg1 == "test"
assert input_obj.arg2 == 5
return f"{input_obj.arg1} {input_str} {input_obj.arg2}"
func = KernelFunction.from_method(my_function, "test")
arguments = KernelArguments(input_obj={"arg1": "test", "arg2": 5}, input_str="test2")
result = await func.invoke(kernel, arguments)
assert result.value == "test test2 5"
def test_function_from_lambda():
func = KernelFunctionFromMethod(method=kernel_function(lambda x: x**2, name="square"), plugin_name="math")
assert func is not None
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
Python: Fix schema handling. Fix function result return for type list. (#6370) ### Motivation and Context Building the tools json payload from the kernel parameter metadata wasn't properly including an object of type `array`. <!-- 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 Correctly include the object type `array` so that the tool call doesn't return a bad request. Add unit tests. - Closes #6367 - Closes #6360 - Fixes the FunctionResult return for a type string -- if the FunctionResult is of type KernelContent then return the first element of the list, otherwise return the complete list. - Fix the kernel function from method to include the proper type_object for the return parameter so that the schema can be created properly. - Add retry logic for a sometimes flaky function calling stepwise planner integration test. - Add a check during function calling that makes sure the model is returning the proper number of arguments based on how many function arguments are required. <!-- 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 :smile:
2024-05-23 11:00:29 -07:00
async def test_function_invoke_return_list_type(kernel: Kernel):
@kernel_function(name="list_func")
def test_list_func() -> list[str]:
return ["test1", "test2"]
func = KernelFunction.from_method(test_list_func, "test")
result = await kernel.invoke(function=func)
assert str(result) == "test1,test2"
Python: implement filters (#5681) ### Motivation and Context <!-- 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. --> This pull request includes significant changes across multiple files, mainly related to the addition of hooks and the modification of function invocations in the `semantic_kernel` module. The changes also include the addition of a new sample and a YAML file, and modifications to the `__init__.py` files. Removals: * [`python/semantic_kernel/events`](diffhunk://#diff-ebda9504832b19ab83239a92c9a6d5f8c744deff9fef86071c13956ec92bb010L1-L11): Removed the previously used events. New Exceptions: * [`python/semantic_kernel/exceptions/kernel_exceptions.py`](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48): Added new exception classes `OperationCancelledException` and `HookInvalidSignatureError`. [[1]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR41-R48) [[2]](diffhunk://#diff-450aaa5595a8b22cd6ee212eb79b7d6b0d4e9c1072063ef32018a3e7d3fdf21dR57-R58) Fixes: #3038 Fixes: #6276 ### 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 - [ ] I didn't break anyone :smile:
2024-05-17 23:05:56 +02:00
async def test_function_invocation_filters(kernel: Kernel):
func = KernelFunctionFromMethod(method=kernel_function(lambda input: input**2, name="square"), plugin_name="math")
kernel.add_function(plugin_name="math", function=func)
pre_call_count = 0
post_call_count = 0
async def custom_filter(context, next):
nonlocal pre_call_count
pre_call_count += 1
await next(context)
nonlocal post_call_count
post_call_count += 1
kernel.add_filter("function_invocation", custom_filter)
result = await kernel.invoke(plugin_name="math", function_name="square", arguments=KernelArguments(input=2))
assert result.value == 4
assert pre_call_count == 1
assert post_call_count == 1
async def test_function_invocation_multiple_filters(kernel: Kernel):
call_stack = []
@kernel_function(name="square")
def func(input: int):
nonlocal call_stack
call_stack.append("func")
return input**2
kernel.add_function(plugin_name="math", function=func)
async def custom_filter1(context, next):
nonlocal call_stack
call_stack.append("custom_filter1_pre")
await next(context)
call_stack.append("custom_filter1_post")
async def custom_filter2(context, next):
nonlocal call_stack
call_stack.append("custom_filter2_pre")
await next(context)
call_stack.append("custom_filter2_post")
kernel.add_filter("function_invocation", custom_filter1)
kernel.add_filter("function_invocation", custom_filter2)
result = await kernel.invoke(plugin_name="math", function_name="square", arguments=KernelArguments(input=2))
assert result.value == 4
assert call_stack == [
"custom_filter1_pre",
"custom_filter2_pre",
"func",
"custom_filter2_post",
"custom_filter1_post",
]
async def test_function_invocation_filters_streaming(kernel: Kernel):
call_stack = []
@kernel_function(name="square")
async def func(input: int):
nonlocal call_stack
call_stack.append("func1")
yield input**2
call_stack.append("func2")
yield input**3
kernel.add_function(plugin_name="math", function=func)
async def custom_filter(context, next):
nonlocal call_stack
call_stack.append("custom_filter_pre")
await next(context)
async def override_stream(stream):
nonlocal call_stack
async for partial in stream:
call_stack.append("overridden_func")
yield partial * 2
stream = context.result.value
context.result = FunctionResult(function=context.result.function, value=override_stream(stream))
call_stack.append("custom_filter_post")
kernel.add_filter("function_invocation", custom_filter)
index = 0
async for partial in kernel.invoke_stream(
plugin_name="math", function_name="square", arguments=KernelArguments(input=2)
):
assert partial == 8 if index == 0 else 16
index += 1
assert call_stack == [
"custom_filter_pre",
"custom_filter_post",
"func1",
"overridden_func",
"func2",
"overridden_func",
]
async def test_default_handling(kernel: Kernel):
@kernel_function
def func_default(input: str = "test"):
return input
func = kernel.add_function(plugin_name="test", function_name="func_default", function=func_default)
res = await kernel.invoke(func)
assert str(res) == "test"
async def test_default_handling_2(kernel: Kernel):
@kernel_function
def func_default(base: str, input: str = "test"):
return input
func = kernel.add_function(plugin_name="test", function_name="func_default", function=func_default)
res = await kernel.invoke(func, base="base")
assert str(res) == "test"
Python: fix for kernel function type_object when not using Annotations. Add more unit tests. (#7338) ### Motivation and Context The current kernel function decorator class parses function params, but had a miss related to how the underlying param's type is parsed if not using Annotations. <!-- 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 properly handles the type_object for params that don't using typing.Annotations. - Adds unit tests to exercise the code. - Adds more kernel.py unit tests for near 100% test coverage. - Adds some planner tests to improve the test coverage. - Closes #7300 <!-- 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 :smile:
2024-07-18 01:12:08 -04:00
def test_parse_list_of_objects(get_custom_type_function_pydantic):
func = get_custom_type_function_pydantic
param_type = list[CustomType]
value = [{"id": "1", "name": "John"}, {"id": "2", "name": "Jane"}]
result = func._parse_parameter(value, param_type)
assert isinstance(result, list)
assert len(result) == 2
assert all(isinstance(item, CustomType) for item in result)
def test_parse_individual_object(get_custom_type_function_pydantic):
value = {"id": "2", "name": "Jane"}
func = get_custom_type_function_pydantic
result = func._parse_parameter(value, CustomType)
assert isinstance(result, CustomType)
assert result.id == "2"
assert result.name == "Jane"
def test_parse_non_list_raises_exception(get_custom_type_function_pydantic):
func = get_custom_type_function_pydantic
param_type = list[CustomType]
value = {"id": "2", "name": "Jane"}
with pytest.raises(FunctionExecutionException, match=r"Expected a list for .*"):
func._parse_parameter(value, param_type)
def test_parse_invalid_dict_raises_exception(get_custom_type_function_pydantic):
func = get_custom_type_function_pydantic
value = {"id": "1"}
with pytest.raises(FunctionExecutionException, match=r"Parameter is expected to be parsed to .*"):
func._parse_parameter(value, CustomType)
def test_parse_invalid_value_raises_exception(get_custom_type_function_pydantic):
func = get_custom_type_function_pydantic
value = "invalid_value"
with pytest.raises(FunctionExecutionException, match=r"Parameter is expected to be parsed to .*"):
func._parse_parameter(value, CustomType)
def test_parse_invalid_list_raises_exception(get_custom_type_function_pydantic):
func = get_custom_type_function_pydantic
param_type = list[CustomType]
value = ["invalid_value"]
with pytest.raises(FunctionExecutionException, match=r"Parameter is expected to be parsed to .*"):
func._parse_parameter(value, param_type)
def test_parse_dict_with_init_non_pydantic(get_custom_type_function_nonpydantic):
func = get_custom_type_function_nonpydantic
value = {"id": "3", "name": "Alice"}
result = func._parse_parameter(value, CustomTypeNonPydantic)
assert isinstance(result, CustomTypeNonPydantic)
assert result.id == "3"
assert result.name == "Alice"
def test_parse_invalid_dict_raises_exception_new(get_custom_type_function_nonpydantic):
func = get_custom_type_function_nonpydantic
value = {"wrong_key": "3", "name": "Alice"}
with pytest.raises(FunctionExecutionException, match=r"Parameter is expected to be parsed to .*"):
func._parse_parameter(value, CustomTypeNonPydantic)
def test_gather_function_parameters_exception_handling(get_custom_type_function_pydantic):
kernel = Mock(spec=Kernel) # Mock kernel
func = get_custom_type_function_pydantic
_rebuild_function_invocation_context()
context = FunctionInvocationContext(kernel=kernel, function=func, arguments=KernelArguments(param="test"))
with pytest.raises(FunctionExecutionException, match=r"Parameter param is expected to be parsed to .* but is not."):
func.gather_function_parameters(context)
Python: Exclude certain fields from KernelFunction model dump (#11081) ### Motivation and Context When trying to do a model dump for `KernelFunction` models, they are failing with a message related to: ``` pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'type'> ``` There are some class-level attributes that cannot be properly serialized. <!-- 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 Exclude dumping certain model attributes from the model dump, which allows the main KernelFunction metadata and its underlying parameters to be serialized. - Closes #11078 - Adds unit tests to ensure we have coverage and can dump Pydantic models. <!-- 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 :smile:
2025-03-20 11:16:30 +09:00
@pytest.mark.parametrize(
("mode"),
[
("python"),
("json"),
],
)
def test_function_model_dump(get_custom_type_function_pydantic, mode):
func: KernelFunctionFromMethod = get_custom_type_function_pydantic
model_dump = func.model_dump(mode=mode)
assert isinstance(model_dump, dict)
assert "metadata" in model_dump
assert len(model_dump["metadata"]["parameters"]) == 1
def test_function_model_dump_json(get_custom_type_function_pydantic):
func = get_custom_type_function_pydantic
model_dump = func.model_dump_json()
assert isinstance(model_dump, str)
assert "metadata" in model_dump