2024-02-26 21:20:25 +01:00
|
|
|
# Copyright (c) Microsoft. All rights reserved.
|
2024-05-21 18:33:08 +02:00
|
|
|
from collections.abc import AsyncGenerator, Iterable
|
|
|
|
|
from typing import Annotated, Any
|
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
|
|
|
|
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
|
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
|
2024-05-17 23:05:56 +02:00
|
|
|
from semantic_kernel.functions.function_result import FunctionResult
|
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
|
2024-07-11 16:05:06 -04:00
|
|
|
from semantic_kernel.functions.kernel_parameter_metadata import KernelParameterMetadata
|
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
|
2024-02-26 21:20:25 +01:00
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
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"
|
2024-03-01 20:21:07 +01:00
|
|
|
assert native_function.parameters[0].is_required is True
|
2024-02-26 21:20:25 +01:00
|
|
|
assert native_function.parameters[1].name == "arguments"
|
2024-05-20 17:56:30 -04:00
|
|
|
assert native_function.parameters[1].description is None
|
2024-02-26 21:20:25 +01:00
|
|
|
assert not native_function.parameters[1].default_value
|
|
|
|
|
assert native_function.parameters[1].type_ == "KernelArguments"
|
2024-03-01 20:21:07 +01:00
|
|
|
assert native_function.parameters[1].is_required is True
|
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",
|
2024-03-01 20:21:07 +01:00
|
|
|
"is_required": True,
|
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"
|
2024-03-01 20:21:07 +01:00
|
|
|
assert native_function.parameters[0].is_required is True
|
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",
|
|
|
|
|
)
|
2024-05-21 18:33:08 +02:00
|
|
|
def decorated_function(input: Annotated[str | None, "Test input description"] = "test_default_value") -> None:
|
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"
|
2024-03-01 20:21:07 +01:00
|
|
|
assert native_function.parameters[0].is_required is False
|
2024-07-11 16:05:06 -04:00
|
|
|
assert type(native_function.return_parameter) is KernelParameterMetadata
|
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):
|
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):
|
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):
|
2024-02-26 21:20:25 +01:00
|
|
|
KernelFunction.from_method(method=invalid_name, plugin_name="MockPlugin")
|
|
|
|
|
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
async def test_invoke_non_async(kernel: Kernel):
|
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")
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
result = await native_function.invoke(kernel=kernel, arguments=None)
|
2024-02-26 21:20:25 +01:00
|
|
|
assert result.value == ""
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
|
async for _ in native_function.invoke_stream(kernel=kernel, arguments=None):
|
|
|
|
|
pass
|
2024-02-26 21:20:25 +01:00
|
|
|
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
async def test_invoke_async(kernel: Kernel):
|
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")
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
result = await native_function.invoke(kernel=kernel, arguments=None)
|
2024-02-26 21:20:25 +01:00
|
|
|
assert result.value == ""
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
|
async for _ in native_function.invoke_stream(kernel=kernel, arguments=None):
|
|
|
|
|
pass
|
2024-02-26 21:20:25 +01:00
|
|
|
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
async def test_invoke_gen(kernel: Kernel):
|
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")
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
result = await native_function.invoke(kernel=kernel, arguments=None)
|
2024-02-26 21:20:25 +01:00
|
|
|
assert result.value == [""]
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
async for partial_result in native_function.invoke_stream(kernel=kernel, arguments=None):
|
2024-02-26 21:20:25 +01:00
|
|
|
assert partial_result == ""
|
|
|
|
|
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
async def test_invoke_gen_async(kernel: Kernel):
|
2024-02-26 21:20:25 +01:00
|
|
|
@kernel_function()
|
2024-04-16 16:21:29 +02:00
|
|
|
async def async_gen_function() -> AsyncGenerator[str, Any]:
|
2024-02-26 21:20:25 +01:00
|
|
|
yield ""
|
|
|
|
|
|
|
|
|
|
native_function = KernelFunction.from_method(method=async_gen_function, plugin_name="MockPlugin")
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
result = await native_function.invoke(kernel=kernel, arguments=None)
|
2024-02-26 21:20:25 +01:00
|
|
|
assert result.value == [""]
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
async for partial_result in native_function.invoke_stream(kernel=kernel, arguments=None):
|
2024-02-26 21:20:25 +01:00
|
|
|
assert partial_result == ""
|
|
|
|
|
|
|
|
|
|
|
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")
|
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"
|
|
|
|
|
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
async def test_required_param_not_supplied(kernel: Kernel):
|
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")
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
with pytest.raises(FunctionExecutionException):
|
|
|
|
|
await func.invoke(kernel=kernel, arguments=KernelArguments())
|
2024-03-01 20:21:07 +01:00
|
|
|
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
async def test_service_execution_with_complex_object(kernel: Kernel):
|
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
|
|
|
|
|
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
async def test_service_execution_with_complex_object_from_str(kernel: Kernel):
|
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"
|
|
|
|
|
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
async def test_service_execution_with_complex_object_from_str_mixed(kernel: Kernel):
|
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"
|
|
|
|
|
|
|
|
|
|
|
2024-05-17 23:05:56 +02:00
|
|
|
async def test_service_execution_with_complex_object_from_str_mixed_multi(kernel: Kernel):
|
2024-03-01 20:21:07 +01:00
|
|
|
@kernel_function(name="function")
|
2024-05-21 18:33:08 +02:00
|
|
|
def my_function(input_obj: InputObject, input_str: str | int) -> str:
|
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"
|
2024-05-14 19:02:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_function_from_lambda():
|
|
|
|
|
func = KernelFunctionFromMethod(method=kernel_function(lambda x: x**2, name="square"), plugin_name="math")
|
|
|
|
|
assert func is not None
|
2024-05-17 23:05:56 +02:00
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
]
|
2024-05-22 15:22:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
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)
|
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
|