2023-03-16 19:54:34 -07:00
|
|
|
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
|
|
2023-12-16 11:41:41 -07:00
|
|
|
import logging
|
Python: Feature new memory stores and collections (#7614)
### 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 PR adds new vector store and vector store record collections as
well as implementations for:
- Azure AI Search
- Redis
- Qdrant
- Volatile (in-memory)
It also adds samples, tests, and unit-tests for these.
Next it adds the vector store record fields, definition and decorator,
with tests and samples.
All marked experimental. Existing Redis, Azure AI Search, Qdrant and
Volatile will be marked as deprecated in the future, once the new
collections are feature complete.
### Description
<!-- 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-08-06 22:30:26 +02:00
|
|
|
from collections.abc import AsyncGenerator, AsyncIterable, Callable
|
2025-04-11 07:22:07 +02:00
|
|
|
from contextlib import AbstractAsyncContextManager
|
2025-04-23 17:26:51 -07:00
|
|
|
from copy import copy, deepcopy
|
2024-05-31 18:37:19 +02:00
|
|
|
from typing import TYPE_CHECKING, Any, Literal, TypeVar
|
Python: Add kernel plugin collection and remove old plugin collection classes (#4764)
### Motivation and Context
This PR solves two works items: #4560 and #4667. The Python SDK had
different ways of handling plugins and collections. The work in the PR
helps align the SDK with dotnet nomenclature and structure.
<!-- 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
A `KernelPluginCollection` is introduced with an underlying
`DefaultKernelPlugin` and a `KernelPlugin` base class. Other code is
refactored to use this new `KernelPluginCollection` model which should
simplify how plugins are handled. Unit tests are added to test the new
functionality. Existing unit tests and integration tests, along with
kernel examples and notebooks were updated based on these changes.
Unlike before, we're requiring plugins to be defined with a name, which
removes the GLOBAL_PLUGIN constant name of "__GLOBAL_PLUGIN__". In some
senses, while creating a plugin or a function, a name may not be
provided and so a random name in the form of a plugin
(p_<random_ascii_chars>) or function (f_<random_ascii_chars>) is used.
<!-- 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:
---------
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-01 13:57:00 -05:00
|
|
|
|
2025-03-05 11:39:07 -08:00
|
|
|
from semantic_kernel.connectors.ai.embedding_generator_base import EmbeddingGeneratorBase
|
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.const import METADATA_EXCEPTION_KEY
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
from semantic_kernel.contents.chat_history import ChatHistory
|
|
|
|
|
from semantic_kernel.contents.function_call_content import FunctionCallContent
|
|
|
|
|
from semantic_kernel.contents.function_result_content import FunctionResultContent
|
Python: Yield FunctionResultContent in streaming chat completion path. Update tests. (#9974)
### Motivation and Context
Currently, if using SK's Python streaming chat completion path, we only
yield the following content types: `StreamingChatMessageContent` and
`FunctionCallContent`. We are not yielding `FunctionResultContent` which
is valuable for some use cases.
<!-- 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 updates the code to yield `FunctionResultContent` if it exists
in the streaming chat completion path. When we merge the function call
result content together into a `StreamingChatMessageContent` type, we
check if that message has items (which are of type
`FunctionResultContent`) and if so, we yield them. The filter path still
works because once they're yielded, we break out of the function calling
loop.
We need to include the `ai_model_id` if it exists for the current
PromptExecutionSettings because if performing a `reduce` operation to
add two streaming chat message chunks together, the
`StreamingChatMessageContent` that has the function results will break
if the `ai_model_id` is not set (the error is thrown in the `__add__`
override for the `StreamingChatMessageContent`.
Some unit tests that cover function calling were also updated -- during
the test, the test JSON function args were breaking in the `json.loads`
call because they contained single quotes and not double. We're now
sanitizing the args, just in case, so we don't break there.
This PR fixes:
- #9408
- #9968
<!-- 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-12-18 10:09:25 +09:00
|
|
|
from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent
|
2024-04-04 17:54:19 +02:00
|
|
|
from semantic_kernel.contents.streaming_content_mixin import StreamingContentMixin
|
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
|
|
|
from semantic_kernel.exceptions import (
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
FunctionCallInvalidArgumentsException,
|
|
|
|
|
FunctionExecutionException,
|
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
|
|
|
KernelFunctionNotFoundError,
|
|
|
|
|
KernelInvokeException,
|
2024-05-17 23:05:56 +02:00
|
|
|
OperationCancelledException,
|
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
|
|
|
TemplateSyntaxError,
|
|
|
|
|
)
|
Python: Feature new memory stores and collections (#7614)
### 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 PR adds new vector store and vector store record collections as
well as implementations for:
- Azure AI Search
- Redis
- Qdrant
- Volatile (in-memory)
It also adds samples, tests, and unit-tests for these.
Next it adds the vector store record fields, definition and decorator,
with tests and samples.
All marked experimental. Existing Redis, Azure AI Search, Qdrant and
Volatile will be marked as deprecated in the future, once the new
collections are feature complete.
### Description
<!-- 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-08-06 22:30:26 +02:00
|
|
|
from semantic_kernel.exceptions.kernel_exceptions import KernelServiceNotFoundError
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
from semantic_kernel.filters.auto_function_invocation.auto_function_invocation_context import (
|
|
|
|
|
AutoFunctionInvocationContext,
|
|
|
|
|
)
|
|
|
|
|
from semantic_kernel.filters.filter_types import FilterTypes
|
|
|
|
|
from semantic_kernel.filters.kernel_filters_extension import (
|
|
|
|
|
KernelFilterExtension,
|
|
|
|
|
_rebuild_auto_function_invocation_context,
|
|
|
|
|
)
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
from semantic_kernel.functions.function_result import FunctionResult
|
|
|
|
|
from semantic_kernel.functions.kernel_arguments import KernelArguments
|
Python: split kernel into kernel extensions for relevant pieces (#6361)
### 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.
-->
In order to keep better track of related pieces within the Kernel,
including easier unit testing, this PR splits the kernel into pieces
that are self-contained, but abstract.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
New:
- KernelServicesExtension
- KernelFunctionExtension
- KernelReliabilityExtension
Changed:
- Kernel, now imports the new ones.
- KernelFiltersExtension, moved to filters folder, in line with the
rest.
### 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-22 17:03:34 +02:00
|
|
|
from semantic_kernel.functions.kernel_function_extension import KernelFunctionExtension
|
2024-05-17 23:05:56 +02:00
|
|
|
from semantic_kernel.functions.kernel_function_from_prompt import KernelFunctionFromPrompt
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
from semantic_kernel.functions.kernel_plugin import KernelPlugin
|
2025-04-11 07:22:07 +02:00
|
|
|
from semantic_kernel.kernel_types import AI_SERVICE_CLIENT_TYPE, OneOrMany, OptionalOneOrMany
|
Python: split kernel into kernel extensions for relevant pieces (#6361)
### 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.
-->
In order to keep better track of related pieces within the Kernel,
including easier unit testing, this PR splits the kernel into pieces
that are self-contained, but abstract.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
New:
- KernelServicesExtension
- KernelFunctionExtension
- KernelReliabilityExtension
Changed:
- Kernel, now imports the new ones.
- KernelFiltersExtension, moved to filters folder, in line with the
rest.
### 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-22 17:03:34 +02:00
|
|
|
from semantic_kernel.prompt_template.const import KERNEL_TEMPLATE_FORMAT_NAME
|
2025-04-15 10:25:35 +02:00
|
|
|
from semantic_kernel.prompt_template.prompt_template_base import PromptTemplateBase
|
Python: .Net: Updated encoding logic in prompt templates (#12983)
### 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.
-->
Resolves: https://github.com/microsoft/semantic-kernel/issues/11821
Today, the encoding of template arguments is performed only if argument
type is `string`. In case of custom type, anonymous type or collection -
the encoding is not performed.
This PR contains changes to throw an exception in case if encoding is
enabled but complex type is used. In case of complex type, the encoding
should be performed manually according to business logic and automatic
encoding should be explicitly disabled.
This enforces stricter, but more secure template rendering rules.
**Note**: this is a breaking change for customers who use Handlebars or
Liquid template with complex type arguments. Code changes are required
when initializing template arguments:
```diff
var arguments = new KernelArguments()
{
{ "customer", new
{
- firstName = userInput.FirstName,
- lastName = userInput.LastName,
+ firstName = HttpUtility.HtmlEncode(userInput.FirstName),
+ lastName = HttpUtility.HtmlEncode(userInput.LastName),
}
}
};
var templateFactory = new LiquidPromptTemplateFactory();
var promptTemplateConfig = new PromptTemplateConfig()
{
TemplateFormat = "liquid"
+ InputVariables = new()
+ {
+ // We set AllowDangerouslySetContent to 'true' because each property of this argument is encoded manually.
+ new() { Name = "customer", AllowDangerouslySetContent = true },
+ }
};
var promptTemplate = templateFactory.Create(promptTemplateConfig);
var renderedPrompt = await promptTemplate.RenderAsync(kernel, arguments);
```
### 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:
2025-08-26 22:09:00 -07:00
|
|
|
from semantic_kernel.prompt_template.prompt_template_config import PromptTemplateConfig
|
Python: split kernel into kernel extensions for relevant pieces (#6361)
### 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.
-->
In order to keep better track of related pieces within the Kernel,
including easier unit testing, this PR splits the kernel into pieces
that are self-contained, but abstract.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
New:
- KernelServicesExtension
- KernelFunctionExtension
- KernelReliabilityExtension
Changed:
- Kernel, now imports the new ones.
- KernelFiltersExtension, moved to filters folder, in line with the
rest.
### 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-22 17:03:34 +02:00
|
|
|
from semantic_kernel.reliability.kernel_reliability_extension import KernelReliabilityExtension
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
from semantic_kernel.services.ai_service_selector import AIServiceSelector
|
2024-05-31 18:37:19 +02:00
|
|
|
from semantic_kernel.services.kernel_services_extension import KernelServicesExtension
|
2025-04-11 07:22:07 +02:00
|
|
|
from semantic_kernel.utils.feature_stage_decorator import experimental
|
Python: slight improvements in kernel and function result content, sample of local function calling with Nexus Raven (#6982)
### 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 introduces a sample that uses function calling with a local model,
Nexus Raven V2, in this case using the API, but the same model can be
loaded using the Hugging Face Connection, the class here is highly
adapted and specific to Nexus including parsing the returned function
call value, into FunctionCallContent and then running that.
It then returns one or more ChatMessageContents with the function call
content and function result content which can then be shared with a
OpenAI call.
Error handling improvements:
*
[`python/semantic_kernel/contents/chat_history.py`](diffhunk://#diff-b57a30f97fec1a91bbf6f7d15584473a8f0cf409797bdcd90d8c80bedc670892L266-R267):
Improved error logging when parsing prompts as XML fails.
*
[`python/semantic_kernel/contents/function_result_content.py`](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10):
Removed field validator for `result` and allow result to be any value,
instead of casting everything to string.
[[1]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10)
[[2]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L50-R51)
[[3]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L63-L69)
[[4]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87R92-R115)
Minor changes:
*
[`python/semantic_kernel/core_plugins/math_plugin.py`](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38):
Removed some docstrings for clarity.
[[1]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38)
[[2]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L55-R47)
*
[`python/semantic_kernel/functions/kernel_function_from_prompt.py`](diffhunk://#diff-eabd53da2ec241cce6f92de85283065e7b478d62cefb52817c5b29ff07e28189L173-R174):
Changed the order of condition checks for better readability.
*
[`python/semantic_kernel/kernel.py`](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10):
Added functionality to update kernel arguments in `invoke_stream` method
and made `function_name` and `plugin_name` optional in `invoke_prompt`
method.
[[1]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10)
[[2]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR29)
[[3]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR119-R120)
[[4]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL195-R202)
[[5]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL209-R216)
Closes #6804
Closes #6944
### Description
<!-- 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-06-28 22:17:05 +02:00
|
|
|
from semantic_kernel.utils.naming import generate_random_ascii_name
|
2023-03-16 19:54:34 -07:00
|
|
|
|
2024-03-28 16:10:28 +01:00
|
|
|
if TYPE_CHECKING:
|
2025-04-11 07:22:07 +02:00
|
|
|
from mcp.server.lowlevel.server import LifespanResultT, Server
|
|
|
|
|
|
Python: Fix KernelArgument bug in invoke_prompt functions (#8414)
### 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.
-->
In `invoke_prompt(...)` and `invoke_prompt_async(...)`, the `arguments`
parameter is tested to see if it's empty before proceeding. The type of
the `arguments` parameter is `KernelArgument`, which derives from a
`dict` type, with an additional field for execution settings. When
`arguments` contains only the execution settings, it will be considered
as an empty dictionary. This will result in the `arguments` parameter
being overridden completely, invalidating all execution settings passed
to the APIs.
To reproduce (assuming we have a plugin/function that will return the
weather):
```
result = await kernel.invoke_prompt(
"What is the weather like in my location?",
arguments=KernelArguments(
settings={
service_id: PromptExecutionSettings(
service_id=service_id,
function_choice_behavior=FunctionChoiceBehavior.Auto(),
),
}
),
)
```
In the example above, the settings will be overridden to None.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Fix the bug.
2. Fix mypy in the pre-commit hook in order to commit.
### 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-08-29 08:40:53 -07:00
|
|
|
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
|
Python: Feature new memory stores and collections (#7614)
### 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 PR adds new vector store and vector store record collections as
well as implementations for:
- Azure AI Search
- Redis
- Qdrant
- Volatile (in-memory)
It also adds samples, tests, and unit-tests for these.
Next it adds the vector store record fields, definition and decorator,
with tests and samples.
All marked experimental. Existing Redis, Azure AI Search, Qdrant and
Volatile will be marked as deprecated in the future, once the new
collections are feature complete.
### Description
<!-- 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-08-06 22:30:26 +02:00
|
|
|
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
|
Python: Updated plugins (#5827)
### 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 loading of plugins and functions into Kernel was not fully
consistent and clear, this is now simplified.
Adding a plugin can now be done in two ways:
- multiple: `kernel.add_plugins[plugin1, plugin2]`
- single plugin: `kernel.add_plugin(plugin)`
- if the plugin here is a class, with methods that have the
kernel_function decorator then that is parsed into a plugin
Adding a function can now be done in three ways:
- multiple: `kernel.add_functions([func1, func2]` or
`kernel.add_function({'func1': func1})`
- single: `kernel.add_function('plugin_name', func1)`
- from a prompt: `kernel.add_function(function_name='func1',
prompt='test prompt', ...)`
In other words, all the different methods that were available have been
simplified, these methods also return the created plugin (or updated
plugin for the `add_functions` method. The `add_function` (singular)
method has a parameter `return_plugin` to control whether you get the
created or updated plugin, instead of the created function.
**An important callout:**
One big internal change that this introduces is that a function that
gets added to a plugin (new or existing) is automatically copied, the
metadata is deep-copied and the plugin_name in the metadata is updated,
so this sequence is valid now:
```python
@kernel_function(name='test')
def f(...):
etc
func = KernelFunctionFromMethod(method=f)
func2 = kernel.add_function('plugin', func)
assert func != func2
```
Also closes: #5855
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Removed KernelPluginCollection
Updated KernelPlugin
added from_... method to load from different sources
Updated KernelFunctionFromPrompt
added from_yaml and from_directory methods.
Added and updated tests to 99% coverage of KernelPlugin and
KernelFunctionFromPrompt
### 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-04-15 15:51:59 +02:00
|
|
|
from semantic_kernel.functions.kernel_function import KernelFunction
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
|
2024-05-31 18:37:19 +02:00
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
Python: Feature new memory stores and collections (#7614)
### 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 PR adds new vector store and vector store record collections as
well as implementations for:
- Azure AI Search
- Redis
- Qdrant
- Volatile (in-memory)
It also adds samples, tests, and unit-tests for these.
Next it adds the vector store record fields, definition and decorator,
with tests and samples.
All marked experimental. Existing Redis, Azure AI Search, Qdrant and
Volatile will be marked as deprecated in the future, once the new
collections are feature complete.
### Description
<!-- 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-08-06 22:30:26 +02:00
|
|
|
TDataModel = TypeVar("TDataModel")
|
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
|
|
|
|
2023-12-16 11:41:41 -07:00
|
|
|
logger: logging.Logger = logging.getLogger(__name__)
|
|
|
|
|
|
2023-05-08 15:52:48 -07:00
|
|
|
|
Python: split kernel into kernel extensions for relevant pieces (#6361)
### 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.
-->
In order to keep better track of related pieces within the Kernel,
including easier unit testing, this PR splits the kernel into pieces
that are self-contained, but abstract.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
New:
- KernelServicesExtension
- KernelFunctionExtension
- KernelReliabilityExtension
Changed:
- Kernel, now imports the new ones.
- KernelFiltersExtension, moved to filters folder, in line with the
rest.
### 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-22 17:03:34 +02:00
|
|
|
class Kernel(KernelFilterExtension, KernelFunctionExtension, KernelServicesExtension, KernelReliabilityExtension):
|
2024-08-05 18:33:49 +02:00
|
|
|
"""The Kernel of Semantic Kernel.
|
2024-05-28 17:28:04 +02:00
|
|
|
|
2024-08-05 18:33:49 +02:00
|
|
|
This is the main entry point for Semantic Kernel. It provides the ability to run
|
|
|
|
|
functions and manage filters, plugins, and AI services.
|
2024-02-02 07:46:45 -05:00
|
|
|
|
|
|
|
|
Attributes:
|
2024-08-05 18:33:49 +02:00
|
|
|
function_invocation_filters: Filters applied during function invocation, from KernelFilterExtension.
|
|
|
|
|
prompt_rendering_filters: Filters applied during prompt rendering, from KernelFilterExtension.
|
|
|
|
|
auto_function_invocation_filters: Filters applied during auto function invocation, from KernelFilterExtension.
|
|
|
|
|
plugins: A dict with the plugins registered with the Kernel, from KernelFunctionExtension.
|
|
|
|
|
services: A dict with the services registered with the Kernel, from KernelServicesExtension.
|
|
|
|
|
ai_service_selector: The AI service selector to be used by the kernel, from KernelServicesExtension.
|
2024-02-02 07:46:45 -05:00
|
|
|
"""
|
|
|
|
|
|
2023-03-16 19:54:34 -07:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
Python: Updated plugins (#5827)
### 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 loading of plugins and functions into Kernel was not fully
consistent and clear, this is now simplified.
Adding a plugin can now be done in two ways:
- multiple: `kernel.add_plugins[plugin1, plugin2]`
- single plugin: `kernel.add_plugin(plugin)`
- if the plugin here is a class, with methods that have the
kernel_function decorator then that is parsed into a plugin
Adding a function can now be done in three ways:
- multiple: `kernel.add_functions([func1, func2]` or
`kernel.add_function({'func1': func1})`
- single: `kernel.add_function('plugin_name', func1)`
- from a prompt: `kernel.add_function(function_name='func1',
prompt='test prompt', ...)`
In other words, all the different methods that were available have been
simplified, these methods also return the created plugin (or updated
plugin for the `add_functions` method. The `add_function` (singular)
method has a parameter `return_plugin` to control whether you get the
created or updated plugin, instead of the created function.
**An important callout:**
One big internal change that this introduces is that a function that
gets added to a plugin (new or existing) is automatically copied, the
metadata is deep-copied and the plugin_name in the metadata is updated,
so this sequence is valid now:
```python
@kernel_function(name='test')
def f(...):
etc
func = KernelFunctionFromMethod(method=f)
func2 = kernel.add_function('plugin', func)
assert func != func2
```
Also closes: #5855
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Removed KernelPluginCollection
Updated KernelPlugin
added from_... method to load from different sources
Updated KernelFunctionFromPrompt
added from_yaml and from_directory methods.
Added and updated tests to 99% coverage of KernelPlugin and
KernelFunctionFromPrompt
### 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-04-15 15:51:59 +02:00
|
|
|
plugins: KernelPlugin | dict[str, KernelPlugin] | list[KernelPlugin] | None = None,
|
2024-05-17 23:05:56 +02:00
|
|
|
services: (
|
|
|
|
|
AI_SERVICE_CLIENT_TYPE | list[AI_SERVICE_CLIENT_TYPE] | dict[str, AI_SERVICE_CLIENT_TYPE] | None
|
|
|
|
|
) = None,
|
Python: Enhanced pre commit and tasks (#5512)
### 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.
-->
To further make the SK python robust and make it easier to get
high-quality PRs, some work on the pre-commit-config.yaml and adding a
mypy settings ini.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added two steps to the pre-commit-config, the first for pypy, the second
for tests with coverage.
Over time we want to mandate these checks to run against a PR before it
goes to github, that will also reduce the number of ruff and black fix
commits, and non-passing unit tests.
Added a mypy.ini, currently all but the root folder is excluded, so that
we can gradually introduce mypy coverage, did the first pieces in
kernel.py, including switch to new style annotations (using from
`__future__ import annotations`) in Kernel.
### 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-19 21:20:09 +01:00
|
|
|
ai_service_selector: AIServiceSelector | None = None,
|
2024-02-02 07:46:45 -05:00
|
|
|
**kwargs: Any,
|
2023-03-16 19:54:34 -07:00
|
|
|
) -> None:
|
2024-05-28 17:28:04 +02:00
|
|
|
"""Initialize a new instance of the Kernel class.
|
2023-11-14 06:45:04 +09:00
|
|
|
|
2024-02-02 07:46:45 -05:00
|
|
|
Args:
|
2024-08-05 18:33:49 +02:00
|
|
|
plugins: The plugins to be used by the kernel, will be rewritten to a dict with plugin name as key
|
|
|
|
|
services: The services to be used by the kernel, will be rewritten to a dict with service_id as key
|
|
|
|
|
ai_service_selector: The AI service selector to be used by the kernel,
|
2024-12-13 14:22:58 -08:00
|
|
|
default is based on order of execution settings.
|
|
|
|
|
**kwargs: Additional fields to be passed to the Kernel model, these are limited to filters.
|
Python: Add kernel plugin collection and remove old plugin collection classes (#4764)
### Motivation and Context
This PR solves two works items: #4560 and #4667. The Python SDK had
different ways of handling plugins and collections. The work in the PR
helps align the SDK with dotnet nomenclature and structure.
<!-- 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
A `KernelPluginCollection` is introduced with an underlying
`DefaultKernelPlugin` and a `KernelPlugin` base class. Other code is
refactored to use this new `KernelPluginCollection` model which should
simplify how plugins are handled. Unit tests are added to test the new
functionality. Existing unit tests and integration tests, along with
kernel examples and notebooks were updated based on these changes.
Unlike before, we're requiring plugins to be defined with a name, which
removes the GLOBAL_PLUGIN constant name of "__GLOBAL_PLUGIN__". In some
senses, while creating a plugin or a function, a name may not be
provided and so a random name in the form of a plugin
(p_<random_ascii_chars>) or function (f_<random_ascii_chars>) is used.
<!-- 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:
---------
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-01 13:57:00 -05:00
|
|
|
"""
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
args = {
|
|
|
|
|
"services": services,
|
Python: Updated plugins (#5827)
### 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 loading of plugins and functions into Kernel was not fully
consistent and clear, this is now simplified.
Adding a plugin can now be done in two ways:
- multiple: `kernel.add_plugins[plugin1, plugin2]`
- single plugin: `kernel.add_plugin(plugin)`
- if the plugin here is a class, with methods that have the
kernel_function decorator then that is parsed into a plugin
Adding a function can now be done in three ways:
- multiple: `kernel.add_functions([func1, func2]` or
`kernel.add_function({'func1': func1})`
- single: `kernel.add_function('plugin_name', func1)`
- from a prompt: `kernel.add_function(function_name='func1',
prompt='test prompt', ...)`
In other words, all the different methods that were available have been
simplified, these methods also return the created plugin (or updated
plugin for the `add_functions` method. The `add_function` (singular)
method has a parameter `return_plugin` to control whether you get the
created or updated plugin, instead of the created function.
**An important callout:**
One big internal change that this introduces is that a function that
gets added to a plugin (new or existing) is automatically copied, the
metadata is deep-copied and the plugin_name in the metadata is updated,
so this sequence is valid now:
```python
@kernel_function(name='test')
def f(...):
etc
func = KernelFunctionFromMethod(method=f)
func2 = kernel.add_function('plugin', func)
assert func != func2
```
Also closes: #5855
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Removed KernelPluginCollection
Updated KernelPlugin
added from_... method to load from different sources
Updated KernelFunctionFromPrompt
added from_yaml and from_directory methods.
Added and updated tests to 99% coverage of KernelPlugin and
KernelFunctionFromPrompt
### 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-04-15 15:51:59 +02:00
|
|
|
"plugins": plugins,
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
**kwargs,
|
|
|
|
|
}
|
|
|
|
|
if ai_service_selector:
|
|
|
|
|
args["ai_service_selector"] = ai_service_selector
|
|
|
|
|
super().__init__(**args)
|
|
|
|
|
|
|
|
|
|
async def invoke_stream(
|
2023-03-16 19:54:34 -07:00
|
|
|
self,
|
2024-05-21 18:33:08 +02:00
|
|
|
function: "KernelFunction | None" = None,
|
Python: Enhanced pre commit and tasks (#5512)
### 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.
-->
To further make the SK python robust and make it easier to get
high-quality PRs, some work on the pre-commit-config.yaml and adding a
mypy settings ini.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added two steps to the pre-commit-config, the first for pypy, the second
for tests with coverage.
Over time we want to mandate these checks to run against a PR before it
goes to github, that will also reduce the number of ruff and black fix
commits, and non-passing unit tests.
Added a mypy.ini, currently all but the root folder is excluded, so that
we can gradually introduce mypy coverage, did the first pieces in
kernel.py, including switch to new style annotations (using from
`__future__ import annotations`) in Kernel.
### 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-19 21:20:09 +01:00
|
|
|
arguments: KernelArguments | None = None,
|
|
|
|
|
function_name: str | None = None,
|
|
|
|
|
plugin_name: str | None = None,
|
Python: Replace mutable default arguments and prevent unintended side effects (#11849)
### Motivation and Context
Some functions and methods were using mutable objects (typically `dict`)
as default argument values.
In Python, default arguments are evaluated only once at the time of
function definition.
This can lead to unexpected behavior when the default value is modified
within the function, as subsequent calls to the function may reuse the
modified object.
As a result, calling such functions or methods multiple times can lead
to unintended behavior.
Reference:
[8.7. Function definitions (Official Python
Documentation)](https://docs.python.org/3.12/reference/compound_stmts.html#function-definitions)
> Default parameter values are evaluated from left to right when the
function definition is executed. This means that the expression is
evaluated once, when the function is defined, and that the same
“pre-computed” value is used for each call. This is especially important
to understand when a default parameter value is a mutable object, such
as a list or a dictionary: if the function modifies the object (e.g. by
appending an item to a list), the default parameter value is in effect
modified. This is generally not what was intended. A way around this is
to use None as the default, and explicitly test for it in the body of
the function, e.g.:
### Description
- Replaced mutable types (e.g., `dict`, `list`) used as default argument
values with `None`, and properly initialized them within the function
body.
- Reviewed and updated the parts of the code that performed destructive
modifications on the arguments, to avoid unintentionally modifying
variables from the caller’s scope.
- ~Added the `flake8-bugbear` rule in Ruff to detect this kind of
issue.~
- ~For now, errors in `tests` and `samples` directories are ignored due
to the large number of violations.~
### 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
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone :smile:
2025-05-07 04:03:05 +09:00
|
|
|
metadata: dict[str, Any] | None = None,
|
2024-05-17 23:05:56 +02:00
|
|
|
return_function_results: bool = False,
|
2024-03-08 22:10:43 +01:00
|
|
|
**kwargs: Any,
|
2024-04-16 16:21:29 +02:00
|
|
|
) -> AsyncGenerator[list["StreamingContentMixin"] | FunctionResult | list[FunctionResult], Any]:
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
"""Execute one or more stream functions.
|
2023-03-16 19:54:34 -07:00
|
|
|
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
This will execute the functions in the order they are provided, if a list of functions is provided.
|
|
|
|
|
When multiple functions are provided only the last one is streamed, the rest is executed as a pipeline.
|
Python: Add kernel plugin collection and remove old plugin collection classes (#4764)
### Motivation and Context
This PR solves two works items: #4560 and #4667. The Python SDK had
different ways of handling plugins and collections. The work in the PR
helps align the SDK with dotnet nomenclature and structure.
<!-- 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
A `KernelPluginCollection` is introduced with an underlying
`DefaultKernelPlugin` and a `KernelPlugin` base class. Other code is
refactored to use this new `KernelPluginCollection` model which should
simplify how plugins are handled. Unit tests are added to test the new
functionality. Existing unit tests and integration tests, along with
kernel examples and notebooks were updated based on these changes.
Unlike before, we're requiring plugins to be defined with a name, which
removes the GLOBAL_PLUGIN constant name of "__GLOBAL_PLUGIN__". In some
senses, while creating a plugin or a function, a name may not be
provided and so a random name in the form of a plugin
(p_<random_ascii_chars>) or function (f_<random_ascii_chars>) is used.
<!-- 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:
---------
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-01 13:57:00 -05:00
|
|
|
|
2024-05-28 17:28:04 +02:00
|
|
|
Args:
|
|
|
|
|
function (KernelFunction): The function to execute,
|
|
|
|
|
this value has precedence when supplying both this and using function_name and plugin_name,
|
|
|
|
|
if this is none, function_name and plugin_name are used and cannot be None.
|
|
|
|
|
arguments (KernelArguments | None): The arguments to pass to the function(s), optional
|
Python: Enhanced pre commit and tasks (#5512)
### 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.
-->
To further make the SK python robust and make it easier to get
high-quality PRs, some work on the pre-commit-config.yaml and adding a
mypy settings ini.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added two steps to the pre-commit-config, the first for pypy, the second
for tests with coverage.
Over time we want to mandate these checks to run against a PR before it
goes to github, that will also reduce the number of ruff and black fix
commits, and non-passing unit tests.
Added a mypy.ini, currently all but the root folder is excluded, so that
we can gradually introduce mypy coverage, did the first pieces in
kernel.py, including switch to new style annotations (using from
`__future__ import annotations`) in Kernel.
### 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-19 21:20:09 +01:00
|
|
|
function_name (str | None): The name of the function to execute
|
|
|
|
|
plugin_name (str | None): The name of the plugin to execute
|
2024-05-17 23:05:56 +02:00
|
|
|
metadata (dict[str, Any]): The metadata to pass to the function(s)
|
|
|
|
|
return_function_results (bool): If True, the function results are yielded as a list[FunctionResult]
|
|
|
|
|
in addition to the streaming content, otherwise only the streaming content is yielded.
|
Python: Enhanced pre commit and tasks (#5512)
### 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.
-->
To further make the SK python robust and make it easier to get
high-quality PRs, some work on the pre-commit-config.yaml and adding a
mypy settings ini.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added two steps to the pre-commit-config, the first for pypy, the second
for tests with coverage.
Over time we want to mandate these checks to run against a PR before it
goes to github, that will also reduce the number of ruff and black fix
commits, and non-passing unit tests.
Added a mypy.ini, currently all but the root folder is excluded, so that
we can gradually introduce mypy coverage, did the first pieces in
kernel.py, including switch to new style annotations (using from
`__future__ import annotations`) in Kernel.
### 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-19 21:20:09 +01:00
|
|
|
kwargs (dict[str, Any]): arguments that can be used instead of supplying KernelArguments
|
Python: Add kernel plugin collection and remove old plugin collection classes (#4764)
### Motivation and Context
This PR solves two works items: #4560 and #4667. The Python SDK had
different ways of handling plugins and collections. The work in the PR
helps align the SDK with dotnet nomenclature and structure.
<!-- 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
A `KernelPluginCollection` is introduced with an underlying
`DefaultKernelPlugin` and a `KernelPlugin` base class. Other code is
refactored to use this new `KernelPluginCollection` model which should
simplify how plugins are handled. Unit tests are added to test the new
functionality. Existing unit tests and integration tests, along with
kernel examples and notebooks were updated based on these changes.
Unlike before, we're requiring plugins to be defined with a name, which
removes the GLOBAL_PLUGIN constant name of "__GLOBAL_PLUGIN__". In some
senses, while creating a plugin or a function, a name may not be
provided and so a random name in the form of a plugin
(p_<random_ascii_chars>) or function (f_<random_ascii_chars>) is used.
<!-- 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:
---------
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-01 13:57:00 -05:00
|
|
|
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
Yields:
|
Python: Enhance Chat Message Content creation and parsing (#5520)
### 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.
-->
Introduces a polymorphic approach to creating and rendering Chat Message
Contents and it's subclasses, leverages Pydantic Rootmodel for that.
Fixes #5496
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds ChatMessageContentBase that can create all types of CMC's, based on
a type field (str)
Redoes some of the rendering and parsing
also retooled the hierarchy structure for Contents, including
introducing a StreamingContentMixin instead of the
StreamingKernelContent (which was never used), now all content classes
(TextContent, CMC, etc) determine the fields used, while the Mixin adds
one additional field and two additional methods for streaming messages,
especially __add__
everything else runs the same way.
### 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-28 13:45:56 +01:00
|
|
|
StreamingContentMixin: The content of the stream of the last function provided.
|
Python: Add kernel plugin collection and remove old plugin collection classes (#4764)
### Motivation and Context
This PR solves two works items: #4560 and #4667. The Python SDK had
different ways of handling plugins and collections. The work in the PR
helps align the SDK with dotnet nomenclature and structure.
<!-- 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
A `KernelPluginCollection` is introduced with an underlying
`DefaultKernelPlugin` and a `KernelPlugin` base class. Other code is
refactored to use this new `KernelPluginCollection` model which should
simplify how plugins are handled. Unit tests are added to test the new
functionality. Existing unit tests and integration tests, along with
kernel examples and notebooks were updated based on these changes.
Unlike before, we're requiring plugins to be defined with a name, which
removes the GLOBAL_PLUGIN constant name of "__GLOBAL_PLUGIN__". In some
senses, while creating a plugin or a function, a name may not be
provided and so a random name in the form of a plugin
(p_<random_ascii_chars>) or function (f_<random_ascii_chars>) is used.
<!-- 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:
---------
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-01 13:57:00 -05:00
|
|
|
"""
|
2024-02-26 21:20:25 +01:00
|
|
|
if arguments is None:
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
arguments = KernelArguments(**kwargs)
|
Python: slight improvements in kernel and function result content, sample of local function calling with Nexus Raven (#6982)
### 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 introduces a sample that uses function calling with a local model,
Nexus Raven V2, in this case using the API, but the same model can be
loaded using the Hugging Face Connection, the class here is highly
adapted and specific to Nexus including parsing the returned function
call value, into FunctionCallContent and then running that.
It then returns one or more ChatMessageContents with the function call
content and function result content which can then be shared with a
OpenAI call.
Error handling improvements:
*
[`python/semantic_kernel/contents/chat_history.py`](diffhunk://#diff-b57a30f97fec1a91bbf6f7d15584473a8f0cf409797bdcd90d8c80bedc670892L266-R267):
Improved error logging when parsing prompts as XML fails.
*
[`python/semantic_kernel/contents/function_result_content.py`](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10):
Removed field validator for `result` and allow result to be any value,
instead of casting everything to string.
[[1]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10)
[[2]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L50-R51)
[[3]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L63-L69)
[[4]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87R92-R115)
Minor changes:
*
[`python/semantic_kernel/core_plugins/math_plugin.py`](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38):
Removed some docstrings for clarity.
[[1]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38)
[[2]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L55-R47)
*
[`python/semantic_kernel/functions/kernel_function_from_prompt.py`](diffhunk://#diff-eabd53da2ec241cce6f92de85283065e7b478d62cefb52817c5b29ff07e28189L173-R174):
Changed the order of condition checks for better readability.
*
[`python/semantic_kernel/kernel.py`](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10):
Added functionality to update kernel arguments in `invoke_stream` method
and made `function_name` and `plugin_name` optional in `invoke_prompt`
method.
[[1]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10)
[[2]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR29)
[[3]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR119-R120)
[[4]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL195-R202)
[[5]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL209-R216)
Closes #6804
Closes #6944
### Description
<!-- 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-06-28 22:17:05 +02:00
|
|
|
else:
|
|
|
|
|
arguments.update(kwargs)
|
2024-04-04 17:54:19 +02:00
|
|
|
if not function:
|
2024-03-07 08:38:04 +01:00
|
|
|
if not function_name or not plugin_name:
|
|
|
|
|
raise KernelFunctionNotFoundError("No function(s) or function- and plugin-name provided")
|
Python: Updated plugins (#5827)
### 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 loading of plugins and functions into Kernel was not fully
consistent and clear, this is now simplified.
Adding a plugin can now be done in two ways:
- multiple: `kernel.add_plugins[plugin1, plugin2]`
- single plugin: `kernel.add_plugin(plugin)`
- if the plugin here is a class, with methods that have the
kernel_function decorator then that is parsed into a plugin
Adding a function can now be done in three ways:
- multiple: `kernel.add_functions([func1, func2]` or
`kernel.add_function({'func1': func1})`
- single: `kernel.add_function('plugin_name', func1)`
- from a prompt: `kernel.add_function(function_name='func1',
prompt='test prompt', ...)`
In other words, all the different methods that were available have been
simplified, these methods also return the created plugin (or updated
plugin for the `add_functions` method. The `add_function` (singular)
method has a parameter `return_plugin` to control whether you get the
created or updated plugin, instead of the created function.
**An important callout:**
One big internal change that this introduces is that a function that
gets added to a plugin (new or existing) is automatically copied, the
metadata is deep-copied and the plugin_name in the metadata is updated,
so this sequence is valid now:
```python
@kernel_function(name='test')
def f(...):
etc
func = KernelFunctionFromMethod(method=f)
func2 = kernel.add_function('plugin', func)
assert func != func2
```
Also closes: #5855
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Removed KernelPluginCollection
Updated KernelPlugin
added from_... method to load from different sources
Updated KernelFunctionFromPrompt
added from_yaml and from_directory methods.
Added and updated tests to 99% coverage of KernelPlugin and
KernelFunctionFromPrompt
### 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-04-15 15:51:59 +02:00
|
|
|
function = self.get_function(plugin_name, function_name)
|
2024-04-04 17:54:19 +02:00
|
|
|
|
|
|
|
|
function_result: list[list["StreamingContentMixin"] | Any] = []
|
|
|
|
|
|
|
|
|
|
async for stream_message in function.invoke_stream(self, arguments):
|
|
|
|
|
if isinstance(stream_message, FunctionResult) and (
|
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
|
|
|
exception := stream_message.metadata.get(METADATA_EXCEPTION_KEY, None)
|
2024-04-04 17:54:19 +02:00
|
|
|
):
|
|
|
|
|
raise KernelInvokeException(
|
|
|
|
|
f"Error occurred while invoking function: '{function.fully_qualified_name}'"
|
|
|
|
|
) from exception
|
|
|
|
|
function_result.append(stream_message)
|
|
|
|
|
yield stream_message
|
|
|
|
|
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
if return_function_results:
|
2024-04-04 17:54:19 +02:00
|
|
|
output_function_result: list["StreamingContentMixin"] = []
|
|
|
|
|
for result in function_result:
|
|
|
|
|
for choice in result:
|
|
|
|
|
if not isinstance(choice, StreamingContentMixin):
|
|
|
|
|
continue
|
|
|
|
|
if len(output_function_result) <= choice.choice_index:
|
|
|
|
|
output_function_result.append(copy(choice))
|
|
|
|
|
else:
|
|
|
|
|
output_function_result[choice.choice_index] += choice
|
|
|
|
|
yield FunctionResult(function=function.metadata, value=output_function_result)
|
2023-07-18 03:05:39 +09:00
|
|
|
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
async def invoke(
|
2023-04-22 04:47:37 +09:00
|
|
|
self,
|
2024-05-21 18:33:08 +02:00
|
|
|
function: "KernelFunction | None" = None,
|
Python: Enhanced pre commit and tasks (#5512)
### 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.
-->
To further make the SK python robust and make it easier to get
high-quality PRs, some work on the pre-commit-config.yaml and adding a
mypy settings ini.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added two steps to the pre-commit-config, the first for pypy, the second
for tests with coverage.
Over time we want to mandate these checks to run against a PR before it
goes to github, that will also reduce the number of ruff and black fix
commits, and non-passing unit tests.
Added a mypy.ini, currently all but the root folder is excluded, so that
we can gradually introduce mypy coverage, did the first pieces in
kernel.py, including switch to new style annotations (using from
`__future__ import annotations`) in Kernel.
### 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-19 21:20:09 +01:00
|
|
|
arguments: KernelArguments | None = None,
|
|
|
|
|
function_name: str | None = None,
|
|
|
|
|
plugin_name: str | None = None,
|
Python: Replace mutable default arguments and prevent unintended side effects (#11849)
### Motivation and Context
Some functions and methods were using mutable objects (typically `dict`)
as default argument values.
In Python, default arguments are evaluated only once at the time of
function definition.
This can lead to unexpected behavior when the default value is modified
within the function, as subsequent calls to the function may reuse the
modified object.
As a result, calling such functions or methods multiple times can lead
to unintended behavior.
Reference:
[8.7. Function definitions (Official Python
Documentation)](https://docs.python.org/3.12/reference/compound_stmts.html#function-definitions)
> Default parameter values are evaluated from left to right when the
function definition is executed. This means that the expression is
evaluated once, when the function is defined, and that the same
“pre-computed” value is used for each call. This is especially important
to understand when a default parameter value is a mutable object, such
as a list or a dictionary: if the function modifies the object (e.g. by
appending an item to a list), the default parameter value is in effect
modified. This is generally not what was intended. A way around this is
to use None as the default, and explicitly test for it in the body of
the function, e.g.:
### Description
- Replaced mutable types (e.g., `dict`, `list`) used as default argument
values with `None`, and properly initialized them within the function
body.
- Reviewed and updated the parts of the code that performed destructive
modifications on the arguments, to avoid unintentionally modifying
variables from the caller’s scope.
- ~Added the `flake8-bugbear` rule in Ruff to detect this kind of
issue.~
- ~For now, errors in `tests` and `samples` directories are ignored due
to the large number of violations.~
### 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
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone :smile:
2025-05-07 04:03:05 +09:00
|
|
|
metadata: dict[str, Any] | None = None,
|
2024-03-08 22:10:43 +01:00
|
|
|
**kwargs: Any,
|
2024-04-04 17:54:19 +02:00
|
|
|
) -> FunctionResult | None:
|
2024-05-31 15:06:41 +02:00
|
|
|
"""Execute a function and return the FunctionResult.
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
|
2024-05-28 17:28:04 +02:00
|
|
|
Args:
|
2024-04-04 17:54:19 +02:00
|
|
|
function (KernelFunction): The function or functions to execute,
|
2024-05-31 15:06:41 +02:00
|
|
|
this value has precedence when supplying both this and using function_name and plugin_name,
|
|
|
|
|
if this is none, function_name and plugin_name are used and cannot be None.
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
arguments (KernelArguments): The arguments to pass to the function(s), optional
|
Python: Enhanced pre commit and tasks (#5512)
### 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.
-->
To further make the SK python robust and make it easier to get
high-quality PRs, some work on the pre-commit-config.yaml and adding a
mypy settings ini.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added two steps to the pre-commit-config, the first for pypy, the second
for tests with coverage.
Over time we want to mandate these checks to run against a PR before it
goes to github, that will also reduce the number of ruff and black fix
commits, and non-passing unit tests.
Added a mypy.ini, currently all but the root folder is excluded, so that
we can gradually introduce mypy coverage, did the first pieces in
kernel.py, including switch to new style annotations (using from
`__future__ import annotations`) in Kernel.
### 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-19 21:20:09 +01:00
|
|
|
function_name (str | None): The name of the function to execute
|
|
|
|
|
plugin_name (str | None): The name of the plugin to execute
|
2024-05-17 23:05:56 +02:00
|
|
|
metadata (dict[str, Any]): The metadata to pass to the function(s)
|
Python: Enhanced pre commit and tasks (#5512)
### 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.
-->
To further make the SK python robust and make it easier to get
high-quality PRs, some work on the pre-commit-config.yaml and adding a
mypy settings ini.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added two steps to the pre-commit-config, the first for pypy, the second
for tests with coverage.
Over time we want to mandate these checks to run against a PR before it
goes to github, that will also reduce the number of ruff and black fix
commits, and non-passing unit tests.
Added a mypy.ini, currently all but the root folder is excluded, so that
we can gradually introduce mypy coverage, did the first pieces in
kernel.py, including switch to new style annotations (using from
`__future__ import annotations`) in Kernel.
### 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-19 21:20:09 +01:00
|
|
|
kwargs (dict[str, Any]): arguments that can be used instead of supplying KernelArguments
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
|
2024-05-31 15:06:41 +02:00
|
|
|
Raises:
|
|
|
|
|
KernelInvokeException: If an error occurs during function invocation
|
2023-04-22 04:47:37 +09:00
|
|
|
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
"""
|
2024-02-26 21:20:25 +01:00
|
|
|
if arguments is None:
|
Python: major features for the Kernel Arguments, Function Result and new Prompt Templating (#5077)
### 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. -->
Major work to replace the context with Kernel Arguments on the front-end
and Function Results on the back.
Updated the function decorator to a new approach, in line with dotnet.
Revamps the way function are called, allowing native functions to be
completely ignorant of SK, other then the decorator.
This also moves things into the new folder structure in sync with
dotnet.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Adds:
- KernelArguments, a dict-like class that replaces the variables in
KernelContext. Closes #4565
- FunctionResult, a class to hold the results of function executions,
includes the function, the value and metadata, as well as two convenient
function to get the value out of it (str and get_inner_content) the
first is generic, the second specifically for KernelContent responses
(from AI services).
- AI Service Selector class, has a default, which is based on the order
in the arguments followed by the order in the functions, can be
overridden to implement your own strategy. Closes #4631
- Introduces ChatHistory and refactors the PromptTemplateConfig. Closes
#4856, #4630
- Improves parsing of templates, will now all validate during creation
and throw an error then, instead of some that do not check for validaty
until used.
- Introduces named_args block and thereby the ability to have multiple
arguments for a function call in a template. Closes #5003
Updates:
- kernel_function decorators, the parameter decorator was removed and
instead we now use Annotated to add a description of a field and we get
the type and required from the function definition.
- core plugins, use the new approach to kernel_function decorators.
- planners, template engines have all been updated to use the kernel and
kernelarguments instead of Context.
- Events have been updated, now use kernelarguments and function_result
- Tokenizers support for named_args and improvements on parsing and
checking.
- Kernel examples and notebooks to use the latest code.
- All unit and integration tests. There is more code coverage now than
before.
Removed:
- kernelContext
- kernel_function_parameter_decorator
- delegate handling code for native functions
- file_io_plugin and tests
- SemanticFunctionConfig
- ChatPromptTemplate
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
2024-02-24 21:05:40 +01:00
|
|
|
arguments = KernelArguments(**kwargs)
|
Python: update ToolCallBehavior and rename to FunctionCallBehavior to match dotnet and extended capabilities (#5919)
### 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.
-->
Updated ToolCallBehavior to match the approach in dotnet
Closes #5727
Closes #5447
Closes #5414
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Extends ToolCallBehavior class with fields:
- enable_kernel_functions
- max_use_attempts
- allow_any_request_kernel_function
Added subclasses of TCB:
- KernelFunctions, the configure_options set tools to all functions in
the kernel and tool_choice to auto
- EnabledFunctions, created with a filter dict, sets tool_choice to auto
and tools to the filtered list
- RequiredFunction, created with a function fully qualified name
(plugin-function), sets tool_choice to that name and adds the definition
of just that tool to tools.
Methods:
- configure_options(kernel, exection_settings)
- This sets the execution settings depending on the field in
toolcallbehavior
- Does nothing in the default ToolCallBehavior class, so there you have
to manually set tools and tool_choice
ClassMethods:
- AutoInvokeKernelFunctions, returns KernelFunctions class with
max_auto_invoke_attempts set to 5 (default)
- EnabelKernelFunctions, return KernelFunctions but with
max_auto_invoke_attempts set to 0, disabling auto invoke, but it might
return toolcalls from the model
- EnableFunctions, takes the filter and a auto_invoke params and returns
a EnabledFunctions class, if auto_invoke == True then it will auto
invoke, otherwise it wont.
- RequiredFunctions, returns RequiredFunction class with either
max_invoke_attempts 0 or 1 depending on auto_invoke param
Changed OpenAIChatPromptExecutionSettings to have a field called
tool_call_behavior instead of max_invoke_attempts and
auto_invoke_tool_calls fields.
Some changes in openai_chat_completion_base to handle this.
### 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 <evmattso@microsoft.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
2024-05-07 20:33:16 +02:00
|
|
|
else:
|
|
|
|
|
arguments.update(kwargs)
|
2024-04-04 17:54:19 +02:00
|
|
|
if not function:
|
2024-03-07 08:38:04 +01:00
|
|
|
if not function_name or not plugin_name:
|
2024-05-17 23:05:56 +02:00
|
|
|
raise KernelFunctionNotFoundError("No function, or function name and plugin name provided")
|
Python: Updated plugins (#5827)
### 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 loading of plugins and functions into Kernel was not fully
consistent and clear, this is now simplified.
Adding a plugin can now be done in two ways:
- multiple: `kernel.add_plugins[plugin1, plugin2]`
- single plugin: `kernel.add_plugin(plugin)`
- if the plugin here is a class, with methods that have the
kernel_function decorator then that is parsed into a plugin
Adding a function can now be done in three ways:
- multiple: `kernel.add_functions([func1, func2]` or
`kernel.add_function({'func1': func1})`
- single: `kernel.add_function('plugin_name', func1)`
- from a prompt: `kernel.add_function(function_name='func1',
prompt='test prompt', ...)`
In other words, all the different methods that were available have been
simplified, these methods also return the created plugin (or updated
plugin for the `add_functions` method. The `add_function` (singular)
method has a parameter `return_plugin` to control whether you get the
created or updated plugin, instead of the created function.
**An important callout:**
One big internal change that this introduces is that a function that
gets added to a plugin (new or existing) is automatically copied, the
metadata is deep-copied and the plugin_name in the metadata is updated,
so this sequence is valid now:
```python
@kernel_function(name='test')
def f(...):
etc
func = KernelFunctionFromMethod(method=f)
func2 = kernel.add_function('plugin', func)
assert func != func2
```
Also closes: #5855
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Removed KernelPluginCollection
Updated KernelPlugin
added from_... method to load from different sources
Updated KernelFunctionFromPrompt
added from_yaml and from_directory methods.
Added and updated tests to 99% coverage of KernelPlugin and
KernelFunctionFromPrompt
### 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-04-15 15:51:59 +02:00
|
|
|
function = self.get_function(plugin_name, function_name)
|
2024-05-17 23:05:56 +02:00
|
|
|
|
2024-04-04 17:54:19 +02:00
|
|
|
try:
|
2024-05-17 23:05:56 +02:00
|
|
|
return await function.invoke(kernel=self, arguments=arguments, metadata=metadata)
|
|
|
|
|
except OperationCancelledException as exc:
|
|
|
|
|
logger.info(f"Operation cancelled during function invocation. Message: {exc}")
|
|
|
|
|
return None
|
2024-04-04 17:54:19 +02:00
|
|
|
except Exception as exc:
|
|
|
|
|
logger.error(
|
|
|
|
|
"Something went wrong in function invocation. During function invocation:"
|
2024-05-31 15:06:41 +02:00
|
|
|
f" '{function.fully_qualified_name}'. Error description: '{exc!s}'"
|
2024-04-04 17:54:19 +02:00
|
|
|
)
|
|
|
|
|
raise KernelInvokeException(
|
|
|
|
|
f"Error occurred while invoking function: '{function.fully_qualified_name}'"
|
2024-05-17 23:05:56 +02:00
|
|
|
) from exc
|
2023-03-16 19:54:34 -07:00
|
|
|
|
Python: Remove the memory tied to the kernel. Update kernel examples and notebooks. (#5143)
### Motivation and Context
As we move toward v1, it is necessary to remove the single kernel memory
attribute so that memory connectors can be handled via plugins. This
change also refactors the `import_plugin` method name to be similar with
dotnet as `import_plugin_from_object`, and introduces a simple
`kernel.invoke_prompt` method.
<!-- 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
Updates are:
- The memory connector is now passed to the `SemanticTextMemory` class,
along with the embedding generator. This allows the user to add memory
to multiple plugins/services as needed. Closes #4632
- Kernel Examples and Notebooks were updated to reflect these changes.
Now using Service Enum for all notebooks. Closes #4557
- There were planners using memory related to the kernel, to store the
functions, and many of these planners are non-existent in dotnet's v1
SDK. Python has pending work to update the planner/remove deprecated
planners before getting to v1.
- A Kernel method called `invoke_prompt` was introduced to invoke a
simple prompt, and return the `FunctionResult` when complete.
- All unit/integration tests are 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-02-26 12:50:23 -05:00
|
|
|
async def invoke_prompt(
|
|
|
|
|
self,
|
|
|
|
|
prompt: str,
|
Python: slight improvements in kernel and function result content, sample of local function calling with Nexus Raven (#6982)
### 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 introduces a sample that uses function calling with a local model,
Nexus Raven V2, in this case using the API, but the same model can be
loaded using the Hugging Face Connection, the class here is highly
adapted and specific to Nexus including parsing the returned function
call value, into FunctionCallContent and then running that.
It then returns one or more ChatMessageContents with the function call
content and function result content which can then be shared with a
OpenAI call.
Error handling improvements:
*
[`python/semantic_kernel/contents/chat_history.py`](diffhunk://#diff-b57a30f97fec1a91bbf6f7d15584473a8f0cf409797bdcd90d8c80bedc670892L266-R267):
Improved error logging when parsing prompts as XML fails.
*
[`python/semantic_kernel/contents/function_result_content.py`](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10):
Removed field validator for `result` and allow result to be any value,
instead of casting everything to string.
[[1]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10)
[[2]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L50-R51)
[[3]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L63-L69)
[[4]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87R92-R115)
Minor changes:
*
[`python/semantic_kernel/core_plugins/math_plugin.py`](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38):
Removed some docstrings for clarity.
[[1]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38)
[[2]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L55-R47)
*
[`python/semantic_kernel/functions/kernel_function_from_prompt.py`](diffhunk://#diff-eabd53da2ec241cce6f92de85283065e7b478d62cefb52817c5b29ff07e28189L173-R174):
Changed the order of condition checks for better readability.
*
[`python/semantic_kernel/kernel.py`](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10):
Added functionality to update kernel arguments in `invoke_stream` method
and made `function_name` and `plugin_name` optional in `invoke_prompt`
method.
[[1]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10)
[[2]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR29)
[[3]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR119-R120)
[[4]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL195-R202)
[[5]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL209-R216)
Closes #6804
Closes #6944
### Description
<!-- 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-06-28 22:17:05 +02:00
|
|
|
function_name: str | None = None,
|
|
|
|
|
plugin_name: str | None = None,
|
Python: Enhanced pre commit and tasks (#5512)
### 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.
-->
To further make the SK python robust and make it easier to get
high-quality PRs, some work on the pre-commit-config.yaml and adding a
mypy settings ini.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added two steps to the pre-commit-config, the first for pypy, the second
for tests with coverage.
Over time we want to mandate these checks to run against a PR before it
goes to github, that will also reduce the number of ruff and black fix
commits, and non-passing unit tests.
Added a mypy.ini, currently all but the root folder is excluded, so that
we can gradually introduce mypy coverage, did the first pieces in
kernel.py, including switch to new style annotations (using from
`__future__ import annotations`) in Kernel.
### 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-19 21:20:09 +01:00
|
|
|
arguments: KernelArguments | None = None,
|
2024-03-14 15:00:05 +01:00
|
|
|
template_format: Literal[
|
2024-03-15 09:23:25 -04:00
|
|
|
"semantic-kernel",
|
|
|
|
|
"handlebars",
|
|
|
|
|
"jinja2",
|
2024-03-14 15:00:05 +01:00
|
|
|
] = KERNEL_TEMPLATE_FORMAT_NAME,
|
Python: .Net: Updated encoding logic in prompt templates (#12983)
### 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.
-->
Resolves: https://github.com/microsoft/semantic-kernel/issues/11821
Today, the encoding of template arguments is performed only if argument
type is `string`. In case of custom type, anonymous type or collection -
the encoding is not performed.
This PR contains changes to throw an exception in case if encoding is
enabled but complex type is used. In case of complex type, the encoding
should be performed manually according to business logic and automatic
encoding should be explicitly disabled.
This enforces stricter, but more secure template rendering rules.
**Note**: this is a breaking change for customers who use Handlebars or
Liquid template with complex type arguments. Code changes are required
when initializing template arguments:
```diff
var arguments = new KernelArguments()
{
{ "customer", new
{
- firstName = userInput.FirstName,
- lastName = userInput.LastName,
+ firstName = HttpUtility.HtmlEncode(userInput.FirstName),
+ lastName = HttpUtility.HtmlEncode(userInput.LastName),
}
}
};
var templateFactory = new LiquidPromptTemplateFactory();
var promptTemplateConfig = new PromptTemplateConfig()
{
TemplateFormat = "liquid"
+ InputVariables = new()
+ {
+ // We set AllowDangerouslySetContent to 'true' because each property of this argument is encoded manually.
+ new() { Name = "customer", AllowDangerouslySetContent = true },
+ }
};
var promptTemplate = templateFactory.Create(promptTemplateConfig);
var renderedPrompt = await promptTemplate.RenderAsync(kernel, arguments);
```
### 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:
2025-08-26 22:09:00 -07:00
|
|
|
prompt_template_config: PromptTemplateConfig | None = None,
|
2024-03-08 22:10:43 +01:00
|
|
|
**kwargs: Any,
|
2024-04-04 17:54:19 +02:00
|
|
|
) -> FunctionResult | None:
|
2024-05-28 17:28:04 +02:00
|
|
|
"""Invoke a function from the provided prompt.
|
Python: Remove the memory tied to the kernel. Update kernel examples and notebooks. (#5143)
### Motivation and Context
As we move toward v1, it is necessary to remove the single kernel memory
attribute so that memory connectors can be handled via plugins. This
change also refactors the `import_plugin` method name to be similar with
dotnet as `import_plugin_from_object`, and introduces a simple
`kernel.invoke_prompt` method.
<!-- 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
Updates are:
- The memory connector is now passed to the `SemanticTextMemory` class,
along with the embedding generator. This allows the user to add memory
to multiple plugins/services as needed. Closes #4632
- Kernel Examples and Notebooks were updated to reflect these changes.
Now using Service Enum for all notebooks. Closes #4557
- There were planners using memory related to the kernel, to store the
functions, and many of these planners are non-existent in dotnet's v1
SDK. Python has pending work to update the planner/remove deprecated
planners before getting to v1.
- A Kernel method called `invoke_prompt` was introduced to invoke a
simple prompt, and return the `FunctionResult` when complete.
- All unit/integration tests are 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-02-26 12:50:23 -05:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
prompt (str): The prompt to use
|
Python: slight improvements in kernel and function result content, sample of local function calling with Nexus Raven (#6982)
### 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 introduces a sample that uses function calling with a local model,
Nexus Raven V2, in this case using the API, but the same model can be
loaded using the Hugging Face Connection, the class here is highly
adapted and specific to Nexus including parsing the returned function
call value, into FunctionCallContent and then running that.
It then returns one or more ChatMessageContents with the function call
content and function result content which can then be shared with a
OpenAI call.
Error handling improvements:
*
[`python/semantic_kernel/contents/chat_history.py`](diffhunk://#diff-b57a30f97fec1a91bbf6f7d15584473a8f0cf409797bdcd90d8c80bedc670892L266-R267):
Improved error logging when parsing prompts as XML fails.
*
[`python/semantic_kernel/contents/function_result_content.py`](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10):
Removed field validator for `result` and allow result to be any value,
instead of casting everything to string.
[[1]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10)
[[2]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L50-R51)
[[3]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L63-L69)
[[4]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87R92-R115)
Minor changes:
*
[`python/semantic_kernel/core_plugins/math_plugin.py`](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38):
Removed some docstrings for clarity.
[[1]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38)
[[2]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L55-R47)
*
[`python/semantic_kernel/functions/kernel_function_from_prompt.py`](diffhunk://#diff-eabd53da2ec241cce6f92de85283065e7b478d62cefb52817c5b29ff07e28189L173-R174):
Changed the order of condition checks for better readability.
*
[`python/semantic_kernel/kernel.py`](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10):
Added functionality to update kernel arguments in `invoke_stream` method
and made `function_name` and `plugin_name` optional in `invoke_prompt`
method.
[[1]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10)
[[2]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR29)
[[3]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR119-R120)
[[4]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL195-R202)
[[5]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL209-R216)
Closes #6804
Closes #6944
### Description
<!-- 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-06-28 22:17:05 +02:00
|
|
|
function_name (str): The name of the function, optional
|
|
|
|
|
plugin_name (str): The name of the plugin, optional
|
Python: Enhanced pre commit and tasks (#5512)
### 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.
-->
To further make the SK python robust and make it easier to get
high-quality PRs, some work on the pre-commit-config.yaml and adding a
mypy settings ini.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added two steps to the pre-commit-config, the first for pypy, the second
for tests with coverage.
Over time we want to mandate these checks to run against a PR before it
goes to github, that will also reduce the number of ruff and black fix
commits, and non-passing unit tests.
Added a mypy.ini, currently all but the root folder is excluded, so that
we can gradually introduce mypy coverage, did the first pieces in
kernel.py, including switch to new style annotations (using from
`__future__ import annotations`) in Kernel.
### 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-19 21:20:09 +01:00
|
|
|
arguments (KernelArguments | None): The arguments to pass to the function(s), optional
|
|
|
|
|
template_format (str | None): The format of the prompt template
|
Python: .Net: Updated encoding logic in prompt templates (#12983)
### 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.
-->
Resolves: https://github.com/microsoft/semantic-kernel/issues/11821
Today, the encoding of template arguments is performed only if argument
type is `string`. In case of custom type, anonymous type or collection -
the encoding is not performed.
This PR contains changes to throw an exception in case if encoding is
enabled but complex type is used. In case of complex type, the encoding
should be performed manually according to business logic and automatic
encoding should be explicitly disabled.
This enforces stricter, but more secure template rendering rules.
**Note**: this is a breaking change for customers who use Handlebars or
Liquid template with complex type arguments. Code changes are required
when initializing template arguments:
```diff
var arguments = new KernelArguments()
{
{ "customer", new
{
- firstName = userInput.FirstName,
- lastName = userInput.LastName,
+ firstName = HttpUtility.HtmlEncode(userInput.FirstName),
+ lastName = HttpUtility.HtmlEncode(userInput.LastName),
}
}
};
var templateFactory = new LiquidPromptTemplateFactory();
var promptTemplateConfig = new PromptTemplateConfig()
{
TemplateFormat = "liquid"
+ InputVariables = new()
+ {
+ // We set AllowDangerouslySetContent to 'true' because each property of this argument is encoded manually.
+ new() { Name = "customer", AllowDangerouslySetContent = true },
+ }
};
var promptTemplate = templateFactory.Create(promptTemplateConfig);
var renderedPrompt = await promptTemplate.RenderAsync(kernel, arguments);
```
### 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:
2025-08-26 22:09:00 -07:00
|
|
|
prompt_template_config (PromptTemplateConfig | None): The prompt template configuration
|
Python: Enhanced pre commit and tasks (#5512)
### 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.
-->
To further make the SK python robust and make it easier to get
high-quality PRs, some work on the pre-commit-config.yaml and adding a
mypy settings ini.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added two steps to the pre-commit-config, the first for pypy, the second
for tests with coverage.
Over time we want to mandate these checks to run against a PR before it
goes to github, that will also reduce the number of ruff and black fix
commits, and non-passing unit tests.
Added a mypy.ini, currently all but the root folder is excluded, so that
we can gradually introduce mypy coverage, did the first pieces in
kernel.py, including switch to new style annotations (using from
`__future__ import annotations`) in Kernel.
### 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-19 21:20:09 +01:00
|
|
|
kwargs (dict[str, Any]): arguments that can be used instead of supplying KernelArguments
|
Python: Remove the memory tied to the kernel. Update kernel examples and notebooks. (#5143)
### Motivation and Context
As we move toward v1, it is necessary to remove the single kernel memory
attribute so that memory connectors can be handled via plugins. This
change also refactors the `import_plugin` method name to be similar with
dotnet as `import_plugin_from_object`, and introduces a simple
`kernel.invoke_prompt` method.
<!-- 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
Updates are:
- The memory connector is now passed to the `SemanticTextMemory` class,
along with the embedding generator. This allows the user to add memory
to multiple plugins/services as needed. Closes #4632
- Kernel Examples and Notebooks were updated to reflect these changes.
Now using Service Enum for all notebooks. Closes #4557
- There were planners using memory related to the kernel, to store the
functions, and many of these planners are non-existent in dotnet's v1
SDK. Python has pending work to update the planner/remove deprecated
planners before getting to v1.
- A Kernel method called `invoke_prompt` was introduced to invoke a
simple prompt, and return the `FunctionResult` when complete.
- All unit/integration tests are 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-02-26 12:50:23 -05:00
|
|
|
|
|
|
|
|
Returns:
|
Python: Enhanced pre commit and tasks (#5512)
### 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.
-->
To further make the SK python robust and make it easier to get
high-quality PRs, some work on the pre-commit-config.yaml and adding a
mypy settings ini.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added two steps to the pre-commit-config, the first for pypy, the second
for tests with coverage.
Over time we want to mandate these checks to run against a PR before it
goes to github, that will also reduce the number of ruff and black fix
commits, and non-passing unit tests.
Added a mypy.ini, currently all but the root folder is excluded, so that
we can gradually introduce mypy coverage, did the first pieces in
kernel.py, including switch to new style annotations (using from
`__future__ import annotations`) in Kernel.
### 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-19 21:20:09 +01:00
|
|
|
FunctionResult | list[FunctionResult] | None: The result of the function(s)
|
Python: Remove the memory tied to the kernel. Update kernel examples and notebooks. (#5143)
### Motivation and Context
As we move toward v1, it is necessary to remove the single kernel memory
attribute so that memory connectors can be handled via plugins. This
change also refactors the `import_plugin` method name to be similar with
dotnet as `import_plugin_from_object`, and introduces a simple
`kernel.invoke_prompt` method.
<!-- 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
Updates are:
- The memory connector is now passed to the `SemanticTextMemory` class,
along with the embedding generator. This allows the user to add memory
to multiple plugins/services as needed. Closes #4632
- Kernel Examples and Notebooks were updated to reflect these changes.
Now using Service Enum for all notebooks. Closes #4557
- There were planners using memory related to the kernel, to store the
functions, and many of these planners are non-existent in dotnet's v1
SDK. Python has pending work to update the planner/remove deprecated
planners before getting to v1.
- A Kernel method called `invoke_prompt` was introduced to invoke a
simple prompt, and return the `FunctionResult` when complete.
- All unit/integration tests are 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-02-26 12:50:23 -05:00
|
|
|
"""
|
Python: Fix KernelArgument bug in invoke_prompt functions (#8414)
### 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.
-->
In `invoke_prompt(...)` and `invoke_prompt_async(...)`, the `arguments`
parameter is tested to see if it's empty before proceeding. The type of
the `arguments` parameter is `KernelArgument`, which derives from a
`dict` type, with an additional field for execution settings. When
`arguments` contains only the execution settings, it will be considered
as an empty dictionary. This will result in the `arguments` parameter
being overridden completely, invalidating all execution settings passed
to the APIs.
To reproduce (assuming we have a plugin/function that will return the
weather):
```
result = await kernel.invoke_prompt(
"What is the weather like in my location?",
arguments=KernelArguments(
settings={
service_id: PromptExecutionSettings(
service_id=service_id,
function_choice_behavior=FunctionChoiceBehavior.Auto(),
),
}
),
)
```
In the example above, the settings will be overridden to None.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Fix the bug.
2. Fix mypy in the pre-commit hook in order to commit.
### 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-08-29 08:40:53 -07:00
|
|
|
if arguments is None:
|
Python: Remove the memory tied to the kernel. Update kernel examples and notebooks. (#5143)
### Motivation and Context
As we move toward v1, it is necessary to remove the single kernel memory
attribute so that memory connectors can be handled via plugins. This
change also refactors the `import_plugin` method name to be similar with
dotnet as `import_plugin_from_object`, and introduces a simple
`kernel.invoke_prompt` method.
<!-- 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
Updates are:
- The memory connector is now passed to the `SemanticTextMemory` class,
along with the embedding generator. This allows the user to add memory
to multiple plugins/services as needed. Closes #4632
- Kernel Examples and Notebooks were updated to reflect these changes.
Now using Service Enum for all notebooks. Closes #4557
- There were planners using memory related to the kernel, to store the
functions, and many of these planners are non-existent in dotnet's v1
SDK. Python has pending work to update the planner/remove deprecated
planners before getting to v1.
- A Kernel method called `invoke_prompt` was introduced to invoke a
simple prompt, and return the `FunctionResult` when complete.
- All unit/integration tests are 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-02-26 12:50:23 -05:00
|
|
|
arguments = KernelArguments(**kwargs)
|
|
|
|
|
if not prompt:
|
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
|
|
|
raise TemplateSyntaxError("The prompt is either null or empty.")
|
2024-03-07 08:38:04 +01:00
|
|
|
|
|
|
|
|
function = KernelFunctionFromPrompt(
|
Python: slight improvements in kernel and function result content, sample of local function calling with Nexus Raven (#6982)
### 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 introduces a sample that uses function calling with a local model,
Nexus Raven V2, in this case using the API, but the same model can be
loaded using the Hugging Face Connection, the class here is highly
adapted and specific to Nexus including parsing the returned function
call value, into FunctionCallContent and then running that.
It then returns one or more ChatMessageContents with the function call
content and function result content which can then be shared with a
OpenAI call.
Error handling improvements:
*
[`python/semantic_kernel/contents/chat_history.py`](diffhunk://#diff-b57a30f97fec1a91bbf6f7d15584473a8f0cf409797bdcd90d8c80bedc670892L266-R267):
Improved error logging when parsing prompts as XML fails.
*
[`python/semantic_kernel/contents/function_result_content.py`](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10):
Removed field validator for `result` and allow result to be any value,
instead of casting everything to string.
[[1]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10)
[[2]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L50-R51)
[[3]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L63-L69)
[[4]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87R92-R115)
Minor changes:
*
[`python/semantic_kernel/core_plugins/math_plugin.py`](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38):
Removed some docstrings for clarity.
[[1]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38)
[[2]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L55-R47)
*
[`python/semantic_kernel/functions/kernel_function_from_prompt.py`](diffhunk://#diff-eabd53da2ec241cce6f92de85283065e7b478d62cefb52817c5b29ff07e28189L173-R174):
Changed the order of condition checks for better readability.
*
[`python/semantic_kernel/kernel.py`](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10):
Added functionality to update kernel arguments in `invoke_stream` method
and made `function_name` and `plugin_name` optional in `invoke_prompt`
method.
[[1]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10)
[[2]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR29)
[[3]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR119-R120)
[[4]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL195-R202)
[[5]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL209-R216)
Closes #6804
Closes #6944
### Description
<!-- 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-06-28 22:17:05 +02:00
|
|
|
function_name=function_name or generate_random_ascii_name(),
|
2024-03-06 03:38:47 -05:00
|
|
|
plugin_name=plugin_name,
|
Python: Remove the memory tied to the kernel. Update kernel examples and notebooks. (#5143)
### Motivation and Context
As we move toward v1, it is necessary to remove the single kernel memory
attribute so that memory connectors can be handled via plugins. This
change also refactors the `import_plugin` method name to be similar with
dotnet as `import_plugin_from_object`, and introduces a simple
`kernel.invoke_prompt` method.
<!-- 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
Updates are:
- The memory connector is now passed to the `SemanticTextMemory` class,
along with the embedding generator. This allows the user to add memory
to multiple plugins/services as needed. Closes #4632
- Kernel Examples and Notebooks were updated to reflect these changes.
Now using Service Enum for all notebooks. Closes #4557
- There were planners using memory related to the kernel, to store the
functions, and many of these planners are non-existent in dotnet's v1
SDK. Python has pending work to update the planner/remove deprecated
planners before getting to v1.
- A Kernel method called `invoke_prompt` was introduced to invoke a
simple prompt, and return the `FunctionResult` when complete.
- All unit/integration tests are 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-02-26 12:50:23 -05:00
|
|
|
prompt=prompt,
|
|
|
|
|
template_format=template_format,
|
Python: .Net: Updated encoding logic in prompt templates (#12983)
### 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.
-->
Resolves: https://github.com/microsoft/semantic-kernel/issues/11821
Today, the encoding of template arguments is performed only if argument
type is `string`. In case of custom type, anonymous type or collection -
the encoding is not performed.
This PR contains changes to throw an exception in case if encoding is
enabled but complex type is used. In case of complex type, the encoding
should be performed manually according to business logic and automatic
encoding should be explicitly disabled.
This enforces stricter, but more secure template rendering rules.
**Note**: this is a breaking change for customers who use Handlebars or
Liquid template with complex type arguments. Code changes are required
when initializing template arguments:
```diff
var arguments = new KernelArguments()
{
{ "customer", new
{
- firstName = userInput.FirstName,
- lastName = userInput.LastName,
+ firstName = HttpUtility.HtmlEncode(userInput.FirstName),
+ lastName = HttpUtility.HtmlEncode(userInput.LastName),
}
}
};
var templateFactory = new LiquidPromptTemplateFactory();
var promptTemplateConfig = new PromptTemplateConfig()
{
TemplateFormat = "liquid"
+ InputVariables = new()
+ {
+ // We set AllowDangerouslySetContent to 'true' because each property of this argument is encoded manually.
+ new() { Name = "customer", AllowDangerouslySetContent = true },
+ }
};
var promptTemplate = templateFactory.Create(promptTemplateConfig);
var renderedPrompt = await promptTemplate.RenderAsync(kernel, arguments);
```
### 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:
2025-08-26 22:09:00 -07:00
|
|
|
prompt_template_config=prompt_template_config,
|
Python: Remove the memory tied to the kernel. Update kernel examples and notebooks. (#5143)
### Motivation and Context
As we move toward v1, it is necessary to remove the single kernel memory
attribute so that memory connectors can be handled via plugins. This
change also refactors the `import_plugin` method name to be similar with
dotnet as `import_plugin_from_object`, and introduces a simple
`kernel.invoke_prompt` method.
<!-- 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
Updates are:
- The memory connector is now passed to the `SemanticTextMemory` class,
along with the embedding generator. This allows the user to add memory
to multiple plugins/services as needed. Closes #4632
- Kernel Examples and Notebooks were updated to reflect these changes.
Now using Service Enum for all notebooks. Closes #4557
- There were planners using memory related to the kernel, to store the
functions, and many of these planners are non-existent in dotnet's v1
SDK. Python has pending work to update the planner/remove deprecated
planners before getting to v1.
- A Kernel method called `invoke_prompt` was introduced to invoke a
simple prompt, and return the `FunctionResult` when complete.
- All unit/integration tests are 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-02-26 12:50:23 -05:00
|
|
|
)
|
2024-04-04 17:54:19 +02:00
|
|
|
return await self.invoke(function=function, arguments=arguments)
|
Python: Remove the memory tied to the kernel. Update kernel examples and notebooks. (#5143)
### Motivation and Context
As we move toward v1, it is necessary to remove the single kernel memory
attribute so that memory connectors can be handled via plugins. This
change also refactors the `import_plugin` method name to be similar with
dotnet as `import_plugin_from_object`, and introduces a simple
`kernel.invoke_prompt` method.
<!-- 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
Updates are:
- The memory connector is now passed to the `SemanticTextMemory` class,
along with the embedding generator. This allows the user to add memory
to multiple plugins/services as needed. Closes #4632
- Kernel Examples and Notebooks were updated to reflect these changes.
Now using Service Enum for all notebooks. Closes #4557
- There were planners using memory related to the kernel, to store the
functions, and many of these planners are non-existent in dotnet's v1
SDK. Python has pending work to update the planner/remove deprecated
planners before getting to v1.
- A Kernel method called `invoke_prompt` was introduced to invoke a
simple prompt, and return the `FunctionResult` when complete.
- All unit/integration tests are 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-02-26 12:50:23 -05:00
|
|
|
|
2024-05-02 18:15:07 -04:00
|
|
|
async def invoke_prompt_stream(
|
|
|
|
|
self,
|
|
|
|
|
prompt: str,
|
Python: slight improvements in kernel and function result content, sample of local function calling with Nexus Raven (#6982)
### 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 introduces a sample that uses function calling with a local model,
Nexus Raven V2, in this case using the API, but the same model can be
loaded using the Hugging Face Connection, the class here is highly
adapted and specific to Nexus including parsing the returned function
call value, into FunctionCallContent and then running that.
It then returns one or more ChatMessageContents with the function call
content and function result content which can then be shared with a
OpenAI call.
Error handling improvements:
*
[`python/semantic_kernel/contents/chat_history.py`](diffhunk://#diff-b57a30f97fec1a91bbf6f7d15584473a8f0cf409797bdcd90d8c80bedc670892L266-R267):
Improved error logging when parsing prompts as XML fails.
*
[`python/semantic_kernel/contents/function_result_content.py`](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10):
Removed field validator for `result` and allow result to be any value,
instead of casting everything to string.
[[1]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10)
[[2]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L50-R51)
[[3]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L63-L69)
[[4]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87R92-R115)
Minor changes:
*
[`python/semantic_kernel/core_plugins/math_plugin.py`](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38):
Removed some docstrings for clarity.
[[1]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38)
[[2]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L55-R47)
*
[`python/semantic_kernel/functions/kernel_function_from_prompt.py`](diffhunk://#diff-eabd53da2ec241cce6f92de85283065e7b478d62cefb52817c5b29ff07e28189L173-R174):
Changed the order of condition checks for better readability.
*
[`python/semantic_kernel/kernel.py`](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10):
Added functionality to update kernel arguments in `invoke_stream` method
and made `function_name` and `plugin_name` optional in `invoke_prompt`
method.
[[1]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10)
[[2]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR29)
[[3]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR119-R120)
[[4]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL195-R202)
[[5]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL209-R216)
Closes #6804
Closes #6944
### Description
<!-- 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-06-28 22:17:05 +02:00
|
|
|
function_name: str | None = None,
|
|
|
|
|
plugin_name: str | None = None,
|
2024-05-02 18:15:07 -04:00
|
|
|
arguments: KernelArguments | None = None,
|
|
|
|
|
template_format: Literal[
|
|
|
|
|
"semantic-kernel",
|
|
|
|
|
"handlebars",
|
|
|
|
|
"jinja2",
|
|
|
|
|
] = KERNEL_TEMPLATE_FORMAT_NAME,
|
|
|
|
|
return_function_results: bool | None = False,
|
Python: .Net: Updated encoding logic in prompt templates (#12983)
### 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.
-->
Resolves: https://github.com/microsoft/semantic-kernel/issues/11821
Today, the encoding of template arguments is performed only if argument
type is `string`. In case of custom type, anonymous type or collection -
the encoding is not performed.
This PR contains changes to throw an exception in case if encoding is
enabled but complex type is used. In case of complex type, the encoding
should be performed manually according to business logic and automatic
encoding should be explicitly disabled.
This enforces stricter, but more secure template rendering rules.
**Note**: this is a breaking change for customers who use Handlebars or
Liquid template with complex type arguments. Code changes are required
when initializing template arguments:
```diff
var arguments = new KernelArguments()
{
{ "customer", new
{
- firstName = userInput.FirstName,
- lastName = userInput.LastName,
+ firstName = HttpUtility.HtmlEncode(userInput.FirstName),
+ lastName = HttpUtility.HtmlEncode(userInput.LastName),
}
}
};
var templateFactory = new LiquidPromptTemplateFactory();
var promptTemplateConfig = new PromptTemplateConfig()
{
TemplateFormat = "liquid"
+ InputVariables = new()
+ {
+ // We set AllowDangerouslySetContent to 'true' because each property of this argument is encoded manually.
+ new() { Name = "customer", AllowDangerouslySetContent = true },
+ }
};
var promptTemplate = templateFactory.Create(promptTemplateConfig);
var renderedPrompt = await promptTemplate.RenderAsync(kernel, arguments);
```
### 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:
2025-08-26 22:09:00 -07:00
|
|
|
prompt_template_config: PromptTemplateConfig | None = None,
|
2024-05-02 18:15:07 -04:00
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> AsyncIterable[list["StreamingContentMixin"] | FunctionResult | list[FunctionResult]]:
|
2024-05-28 17:28:04 +02:00
|
|
|
"""Invoke a function from the provided prompt and stream the results.
|
2024-05-02 18:15:07 -04:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
prompt (str): The prompt to use
|
Python: slight improvements in kernel and function result content, sample of local function calling with Nexus Raven (#6982)
### 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 introduces a sample that uses function calling with a local model,
Nexus Raven V2, in this case using the API, but the same model can be
loaded using the Hugging Face Connection, the class here is highly
adapted and specific to Nexus including parsing the returned function
call value, into FunctionCallContent and then running that.
It then returns one or more ChatMessageContents with the function call
content and function result content which can then be shared with a
OpenAI call.
Error handling improvements:
*
[`python/semantic_kernel/contents/chat_history.py`](diffhunk://#diff-b57a30f97fec1a91bbf6f7d15584473a8f0cf409797bdcd90d8c80bedc670892L266-R267):
Improved error logging when parsing prompts as XML fails.
*
[`python/semantic_kernel/contents/function_result_content.py`](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10):
Removed field validator for `result` and allow result to be any value,
instead of casting everything to string.
[[1]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10)
[[2]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L50-R51)
[[3]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L63-L69)
[[4]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87R92-R115)
Minor changes:
*
[`python/semantic_kernel/core_plugins/math_plugin.py`](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38):
Removed some docstrings for clarity.
[[1]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38)
[[2]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L55-R47)
*
[`python/semantic_kernel/functions/kernel_function_from_prompt.py`](diffhunk://#diff-eabd53da2ec241cce6f92de85283065e7b478d62cefb52817c5b29ff07e28189L173-R174):
Changed the order of condition checks for better readability.
*
[`python/semantic_kernel/kernel.py`](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10):
Added functionality to update kernel arguments in `invoke_stream` method
and made `function_name` and `plugin_name` optional in `invoke_prompt`
method.
[[1]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10)
[[2]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR29)
[[3]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR119-R120)
[[4]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL195-R202)
[[5]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL209-R216)
Closes #6804
Closes #6944
### Description
<!-- 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-06-28 22:17:05 +02:00
|
|
|
function_name (str): The name of the function, optional
|
|
|
|
|
plugin_name (str): The name of the plugin, optional
|
2024-05-02 18:15:07 -04:00
|
|
|
arguments (KernelArguments | None): The arguments to pass to the function(s), optional
|
|
|
|
|
template_format (str | None): The format of the prompt template
|
2024-05-28 17:28:04 +02:00
|
|
|
return_function_results (bool): If True, the function results are yielded as a list[FunctionResult]
|
Python: .Net: Updated encoding logic in prompt templates (#12983)
### 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.
-->
Resolves: https://github.com/microsoft/semantic-kernel/issues/11821
Today, the encoding of template arguments is performed only if argument
type is `string`. In case of custom type, anonymous type or collection -
the encoding is not performed.
This PR contains changes to throw an exception in case if encoding is
enabled but complex type is used. In case of complex type, the encoding
should be performed manually according to business logic and automatic
encoding should be explicitly disabled.
This enforces stricter, but more secure template rendering rules.
**Note**: this is a breaking change for customers who use Handlebars or
Liquid template with complex type arguments. Code changes are required
when initializing template arguments:
```diff
var arguments = new KernelArguments()
{
{ "customer", new
{
- firstName = userInput.FirstName,
- lastName = userInput.LastName,
+ firstName = HttpUtility.HtmlEncode(userInput.FirstName),
+ lastName = HttpUtility.HtmlEncode(userInput.LastName),
}
}
};
var templateFactory = new LiquidPromptTemplateFactory();
var promptTemplateConfig = new PromptTemplateConfig()
{
TemplateFormat = "liquid"
+ InputVariables = new()
+ {
+ // We set AllowDangerouslySetContent to 'true' because each property of this argument is encoded manually.
+ new() { Name = "customer", AllowDangerouslySetContent = true },
+ }
};
var promptTemplate = templateFactory.Create(promptTemplateConfig);
var renderedPrompt = await promptTemplate.RenderAsync(kernel, arguments);
```
### 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:
2025-08-26 22:09:00 -07:00
|
|
|
prompt_template_config (PromptTemplateConfig | None): The prompt template configuration
|
2024-05-02 18:15:07 -04:00
|
|
|
kwargs (dict[str, Any]): arguments that can be used instead of supplying KernelArguments
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
AsyncIterable[StreamingContentMixin]: The content of the stream of the last function provided.
|
|
|
|
|
"""
|
Python: Fix KernelArgument bug in invoke_prompt functions (#8414)
### 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.
-->
In `invoke_prompt(...)` and `invoke_prompt_async(...)`, the `arguments`
parameter is tested to see if it's empty before proceeding. The type of
the `arguments` parameter is `KernelArgument`, which derives from a
`dict` type, with an additional field for execution settings. When
`arguments` contains only the execution settings, it will be considered
as an empty dictionary. This will result in the `arguments` parameter
being overridden completely, invalidating all execution settings passed
to the APIs.
To reproduce (assuming we have a plugin/function that will return the
weather):
```
result = await kernel.invoke_prompt(
"What is the weather like in my location?",
arguments=KernelArguments(
settings={
service_id: PromptExecutionSettings(
service_id=service_id,
function_choice_behavior=FunctionChoiceBehavior.Auto(),
),
}
),
)
```
In the example above, the settings will be overridden to None.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Fix the bug.
2. Fix mypy in the pre-commit hook in order to commit.
### 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-08-29 08:40:53 -07:00
|
|
|
if arguments is None:
|
2024-05-02 18:15:07 -04:00
|
|
|
arguments = KernelArguments(**kwargs)
|
|
|
|
|
if not prompt:
|
|
|
|
|
raise TemplateSyntaxError("The prompt is either null or empty.")
|
|
|
|
|
|
|
|
|
|
from semantic_kernel.functions.kernel_function_from_prompt import KernelFunctionFromPrompt
|
|
|
|
|
|
|
|
|
|
function = KernelFunctionFromPrompt(
|
Python: slight improvements in kernel and function result content, sample of local function calling with Nexus Raven (#6982)
### 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 introduces a sample that uses function calling with a local model,
Nexus Raven V2, in this case using the API, but the same model can be
loaded using the Hugging Face Connection, the class here is highly
adapted and specific to Nexus including parsing the returned function
call value, into FunctionCallContent and then running that.
It then returns one or more ChatMessageContents with the function call
content and function result content which can then be shared with a
OpenAI call.
Error handling improvements:
*
[`python/semantic_kernel/contents/chat_history.py`](diffhunk://#diff-b57a30f97fec1a91bbf6f7d15584473a8f0cf409797bdcd90d8c80bedc670892L266-R267):
Improved error logging when parsing prompts as XML fails.
*
[`python/semantic_kernel/contents/function_result_content.py`](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10):
Removed field validator for `result` and allow result to be any value,
instead of casting everything to string.
[[1]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10)
[[2]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L50-R51)
[[3]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L63-L69)
[[4]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87R92-R115)
Minor changes:
*
[`python/semantic_kernel/core_plugins/math_plugin.py`](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38):
Removed some docstrings for clarity.
[[1]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38)
[[2]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L55-R47)
*
[`python/semantic_kernel/functions/kernel_function_from_prompt.py`](diffhunk://#diff-eabd53da2ec241cce6f92de85283065e7b478d62cefb52817c5b29ff07e28189L173-R174):
Changed the order of condition checks for better readability.
*
[`python/semantic_kernel/kernel.py`](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10):
Added functionality to update kernel arguments in `invoke_stream` method
and made `function_name` and `plugin_name` optional in `invoke_prompt`
method.
[[1]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10)
[[2]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR29)
[[3]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR119-R120)
[[4]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL195-R202)
[[5]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL209-R216)
Closes #6804
Closes #6944
### Description
<!-- 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-06-28 22:17:05 +02:00
|
|
|
function_name=function_name or generate_random_ascii_name(),
|
2024-05-02 18:15:07 -04:00
|
|
|
plugin_name=plugin_name,
|
|
|
|
|
prompt=prompt,
|
|
|
|
|
template_format=template_format,
|
Python: .Net: Updated encoding logic in prompt templates (#12983)
### 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.
-->
Resolves: https://github.com/microsoft/semantic-kernel/issues/11821
Today, the encoding of template arguments is performed only if argument
type is `string`. In case of custom type, anonymous type or collection -
the encoding is not performed.
This PR contains changes to throw an exception in case if encoding is
enabled but complex type is used. In case of complex type, the encoding
should be performed manually according to business logic and automatic
encoding should be explicitly disabled.
This enforces stricter, but more secure template rendering rules.
**Note**: this is a breaking change for customers who use Handlebars or
Liquid template with complex type arguments. Code changes are required
when initializing template arguments:
```diff
var arguments = new KernelArguments()
{
{ "customer", new
{
- firstName = userInput.FirstName,
- lastName = userInput.LastName,
+ firstName = HttpUtility.HtmlEncode(userInput.FirstName),
+ lastName = HttpUtility.HtmlEncode(userInput.LastName),
}
}
};
var templateFactory = new LiquidPromptTemplateFactory();
var promptTemplateConfig = new PromptTemplateConfig()
{
TemplateFormat = "liquid"
+ InputVariables = new()
+ {
+ // We set AllowDangerouslySetContent to 'true' because each property of this argument is encoded manually.
+ new() { Name = "customer", AllowDangerouslySetContent = true },
+ }
};
var promptTemplate = templateFactory.Create(promptTemplateConfig);
var renderedPrompt = await promptTemplate.RenderAsync(kernel, arguments);
```
### 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:
2025-08-26 22:09:00 -07:00
|
|
|
prompt_template_config=prompt_template_config,
|
2024-05-02 18:15:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
function_result: list[list["StreamingContentMixin"] | Any] = []
|
|
|
|
|
|
|
|
|
|
async for stream_message in self.invoke_stream(function=function, arguments=arguments):
|
|
|
|
|
if isinstance(stream_message, FunctionResult) and (
|
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
|
|
|
exception := stream_message.metadata.get(METADATA_EXCEPTION_KEY, None)
|
2024-05-02 18:15:07 -04:00
|
|
|
):
|
|
|
|
|
raise KernelInvokeException(
|
|
|
|
|
f"Error occurred while invoking function: '{function.fully_qualified_name}'"
|
|
|
|
|
) from exception
|
|
|
|
|
function_result.append(stream_message)
|
|
|
|
|
yield stream_message
|
|
|
|
|
|
|
|
|
|
if return_function_results:
|
|
|
|
|
output_function_result: list["StreamingContentMixin"] = []
|
|
|
|
|
for result in function_result:
|
|
|
|
|
for choice in result:
|
|
|
|
|
if not isinstance(choice, StreamingContentMixin):
|
|
|
|
|
continue
|
|
|
|
|
if len(output_function_result) <= choice.choice_index:
|
|
|
|
|
output_function_result.append(copy(choice))
|
|
|
|
|
else:
|
|
|
|
|
output_function_result[choice.choice_index] += choice
|
|
|
|
|
yield FunctionResult(function=function.metadata, value=output_function_result)
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
|
|
|
|
|
async def invoke_function_call(
|
|
|
|
|
self,
|
|
|
|
|
function_call: FunctionCallContent,
|
|
|
|
|
chat_history: ChatHistory,
|
2025-02-19 01:40:51 +01:00
|
|
|
*,
|
Python: slight improvements in kernel and function result content, sample of local function calling with Nexus Raven (#6982)
### 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 introduces a sample that uses function calling with a local model,
Nexus Raven V2, in this case using the API, but the same model can be
loaded using the Hugging Face Connection, the class here is highly
adapted and specific to Nexus including parsing the returned function
call value, into FunctionCallContent and then running that.
It then returns one or more ChatMessageContents with the function call
content and function result content which can then be shared with a
OpenAI call.
Error handling improvements:
*
[`python/semantic_kernel/contents/chat_history.py`](diffhunk://#diff-b57a30f97fec1a91bbf6f7d15584473a8f0cf409797bdcd90d8c80bedc670892L266-R267):
Improved error logging when parsing prompts as XML fails.
*
[`python/semantic_kernel/contents/function_result_content.py`](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10):
Removed field validator for `result` and allow result to be any value,
instead of casting everything to string.
[[1]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10)
[[2]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L50-R51)
[[3]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L63-L69)
[[4]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87R92-R115)
Minor changes:
*
[`python/semantic_kernel/core_plugins/math_plugin.py`](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38):
Removed some docstrings for clarity.
[[1]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38)
[[2]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L55-R47)
*
[`python/semantic_kernel/functions/kernel_function_from_prompt.py`](diffhunk://#diff-eabd53da2ec241cce6f92de85283065e7b478d62cefb52817c5b29ff07e28189L173-R174):
Changed the order of condition checks for better readability.
*
[`python/semantic_kernel/kernel.py`](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10):
Added functionality to update kernel arguments in `invoke_stream` method
and made `function_name` and `plugin_name` optional in `invoke_prompt`
method.
[[1]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10)
[[2]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR29)
[[3]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR119-R120)
[[4]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL195-R202)
[[5]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL209-R216)
Closes #6804
Closes #6944
### Description
<!-- 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-06-28 22:17:05 +02:00
|
|
|
arguments: "KernelArguments | None" = None,
|
2025-02-19 01:40:51 +01:00
|
|
|
execution_settings: "PromptExecutionSettings | None" = None,
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
function_call_count: int | None = None,
|
|
|
|
|
request_index: int | None = None,
|
2025-02-19 01:40:51 +01:00
|
|
|
is_streaming: bool = False,
|
2025-03-04 08:44:10 +01:00
|
|
|
function_behavior: "FunctionChoiceBehavior | None" = None,
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
) -> "AutoFunctionInvocationContext | None":
|
|
|
|
|
"""Processes the provided FunctionCallContent and updates the chat history."""
|
|
|
|
|
try:
|
|
|
|
|
if function_call.name is None:
|
|
|
|
|
raise FunctionExecutionException("The function name is required.")
|
|
|
|
|
if function_behavior is not None and function_behavior.filters:
|
|
|
|
|
allowed_functions = [
|
|
|
|
|
func.fully_qualified_name for func in self.get_list_of_function_metadata(function_behavior.filters)
|
|
|
|
|
]
|
|
|
|
|
if function_call.name not in allowed_functions:
|
|
|
|
|
raise FunctionExecutionException(
|
|
|
|
|
f"Only functions: {allowed_functions} are allowed, {function_call.name} is not allowed."
|
|
|
|
|
)
|
|
|
|
|
function_to_call = self.get_function(function_call.plugin_name, function_call.function_name)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.exception(f"The function `{function_call.name}` is not part of the provided functions: {exc}.")
|
|
|
|
|
frc = FunctionResultContent.from_function_call_content_and_result(
|
|
|
|
|
function_call_content=function_call,
|
|
|
|
|
result=(
|
|
|
|
|
f"The tool call with name `{function_call.name}` is not part of the provided tools, "
|
|
|
|
|
"please try again with a supplied tool call name and make sure to validate the name."
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
chat_history.add_message(message=frc.to_chat_message_content())
|
|
|
|
|
return None
|
|
|
|
|
|
2025-05-19 14:13:27 +09:00
|
|
|
args_cloned = copy(arguments) if arguments else KernelArguments()
|
|
|
|
|
try:
|
|
|
|
|
parsed_args = function_call.to_kernel_arguments()
|
|
|
|
|
|
|
|
|
|
# Check for missing or unexpected parameters
|
|
|
|
|
required_param_names = {
|
|
|
|
|
param.name for param in function_to_call.parameters if param.name is not None and param.is_required
|
|
|
|
|
}
|
|
|
|
|
received_param_names = set(parsed_args or {})
|
|
|
|
|
|
|
|
|
|
missing_params = required_param_names - received_param_names
|
|
|
|
|
unexpected_params = received_param_names - {param.name for param in function_to_call.parameters}
|
|
|
|
|
|
|
|
|
|
if missing_params or unexpected_params:
|
|
|
|
|
msg_parts = []
|
|
|
|
|
if missing_params:
|
|
|
|
|
msg_parts.append(f"Missing required argument(s): {sorted(missing_params)}.")
|
|
|
|
|
if unexpected_params:
|
|
|
|
|
msg_parts.append(f"Received unexpected argument(s): {sorted(unexpected_params)}.")
|
|
|
|
|
msg = " ".join(msg_parts) + " Please revise the arguments to match the function signature."
|
|
|
|
|
|
|
|
|
|
logger.info(msg)
|
|
|
|
|
frc = FunctionResultContent.from_function_call_content_and_result(
|
|
|
|
|
function_call_content=function_call,
|
|
|
|
|
result=msg,
|
|
|
|
|
)
|
|
|
|
|
chat_history.add_message(message=frc.to_chat_message_content())
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
if parsed_args:
|
|
|
|
|
args_cloned.update(parsed_args)
|
|
|
|
|
except (FunctionCallInvalidArgumentsException, TypeError) as exc:
|
|
|
|
|
logger.info(f"Received invalid arguments for function {function_call.name}: {exc}. Trying tool call again.")
|
|
|
|
|
frc = FunctionResultContent.from_function_call_content_and_result(
|
|
|
|
|
function_call_content=function_call,
|
|
|
|
|
result="The tool call arguments are malformed. Arguments must be in JSON format. Please try again.",
|
|
|
|
|
)
|
|
|
|
|
chat_history.add_message(message=frc.to_chat_message_content())
|
|
|
|
|
return None
|
|
|
|
|
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
num_required_func_params = len([param for param in function_to_call.parameters if param.is_required])
|
|
|
|
|
if parsed_args is None or len(parsed_args) < num_required_func_params:
|
|
|
|
|
msg = (
|
|
|
|
|
f"There are `{num_required_func_params}` tool call arguments required and "
|
|
|
|
|
f"only `{len(parsed_args) if parsed_args is not None else 0}` received. The required arguments are: "
|
|
|
|
|
f"{[param.name for param in function_to_call.parameters if param.is_required]}. "
|
|
|
|
|
"Please provide the required arguments and try again."
|
|
|
|
|
)
|
|
|
|
|
logger.info(msg)
|
|
|
|
|
frc = FunctionResultContent.from_function_call_content_and_result(
|
|
|
|
|
function_call_content=function_call,
|
|
|
|
|
result=msg,
|
|
|
|
|
)
|
|
|
|
|
chat_history.add_message(message=frc.to_chat_message_content())
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
logger.info(f"Calling {function_call.name} function with args: {function_call.arguments}")
|
|
|
|
|
|
|
|
|
|
_rebuild_auto_function_invocation_context()
|
|
|
|
|
invocation_context = AutoFunctionInvocationContext(
|
|
|
|
|
function=function_to_call,
|
|
|
|
|
kernel=self,
|
|
|
|
|
arguments=args_cloned,
|
2025-02-19 01:40:51 +01:00
|
|
|
is_streaming=is_streaming,
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
chat_history=chat_history,
|
2025-05-27 16:41:44 -07:00
|
|
|
function_call_content=function_call,
|
2025-02-19 01:40:51 +01:00
|
|
|
execution_settings=execution_settings,
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
function_result=FunctionResult(function=function_to_call.metadata, value=None),
|
Python: slight improvements in kernel and function result content, sample of local function calling with Nexus Raven (#6982)
### 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 introduces a sample that uses function calling with a local model,
Nexus Raven V2, in this case using the API, but the same model can be
loaded using the Hugging Face Connection, the class here is highly
adapted and specific to Nexus including parsing the returned function
call value, into FunctionCallContent and then running that.
It then returns one or more ChatMessageContents with the function call
content and function result content which can then be shared with a
OpenAI call.
Error handling improvements:
*
[`python/semantic_kernel/contents/chat_history.py`](diffhunk://#diff-b57a30f97fec1a91bbf6f7d15584473a8f0cf409797bdcd90d8c80bedc670892L266-R267):
Improved error logging when parsing prompts as XML fails.
*
[`python/semantic_kernel/contents/function_result_content.py`](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10):
Removed field validator for `result` and allow result to be any value,
instead of casting everything to string.
[[1]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L7-R10)
[[2]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L50-R51)
[[3]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87L63-L69)
[[4]](diffhunk://#diff-3b69e6aedf4c29038cdff0ac99602b70583ab6c85f7bd777b08ad96b7fa48d87R92-R115)
Minor changes:
*
[`python/semantic_kernel/core_plugins/math_plugin.py`](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38):
Removed some docstrings for clarity.
[[1]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L32-R38)
[[2]](diffhunk://#diff-632444474bd3ea991b4409dfd0ab194e0789c73617bef4a3dcae4db5c4b0b6c0L55-R47)
*
[`python/semantic_kernel/functions/kernel_function_from_prompt.py`](diffhunk://#diff-eabd53da2ec241cce6f92de85283065e7b478d62cefb52817c5b29ff07e28189L173-R174):
Changed the order of condition checks for better readability.
*
[`python/semantic_kernel/kernel.py`](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10):
Added functionality to update kernel arguments in `invoke_stream` method
and made `function_name` and `plugin_name` optional in `invoke_prompt`
method.
[[1]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR9-R10)
[[2]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR29)
[[3]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aR119-R120)
[[4]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL195-R202)
[[5]](diffhunk://#diff-3db6fe98818eda0b824788901c718d35eac8b13d0ff428d3dfd417866d922d3aL209-R216)
Closes #6804
Closes #6944
### Description
<!-- 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-06-28 22:17:05 +02:00
|
|
|
function_count=function_call_count or 0,
|
|
|
|
|
request_sequence_index=request_index or 0,
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
)
|
|
|
|
|
if function_call.index is not None:
|
|
|
|
|
invocation_context.function_sequence_index = function_call.index
|
|
|
|
|
|
|
|
|
|
stack = self.construct_call_stack(
|
|
|
|
|
filter_type=FilterTypes.AUTO_FUNCTION_INVOCATION,
|
|
|
|
|
inner_function=self._inner_auto_function_invoke_handler,
|
|
|
|
|
)
|
|
|
|
|
await stack(invocation_context)
|
|
|
|
|
|
Python: Deep-copy `function_result.value` in `invoke_function_call` to prevent state mutation (#11826)
### Motivation and Context
The existing implementation of `kernel.invoke_function_call(...)` stores
mutable tool return values by reference. Subsequent mutations to the
same in-memory state bleed into earlier chat messages, which corrupts
conversation history and breaks reproducibility. By deep-copying the
function result immediately after execution, we can guarantee an
immutable snapshot of each tool's output at the time of invocation,
which preserves previous tool invocations and removes unwanted side
effects.
<!-- 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
After `await stack(invocation_context)` in
`kernel.invoke_function_call(...)`, we now have:
```python
if invocation_context.function_result and invocation_context.function_result.value is not None:
invocation_context.function_result.value = copy.deepcopy(
invocation_context.function_result.value
)
```
This has no change to our public API or user-facing behavior; internal
state handling is now robust against undesired mutations.
- Adds a unit test to exercise the new behavior and assert on the proper
mutations/states.
<!-- 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-05-01 09:19:34 +09:00
|
|
|
# Snapshot the tool's return value so later mutations don't leak back
|
|
|
|
|
if invocation_context.function_result and invocation_context.function_result.value is not None:
|
|
|
|
|
invocation_context.function_result.value = deepcopy(invocation_context.function_result.value)
|
|
|
|
|
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
frc = FunctionResultContent.from_function_call_content_and_result(
|
2025-04-03 08:18:57 +09:00
|
|
|
function_call_content=function_call,
|
|
|
|
|
result=invocation_context.function_result,
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
)
|
Python: Yield FunctionResultContent in streaming chat completion path. Update tests. (#9974)
### Motivation and Context
Currently, if using SK's Python streaming chat completion path, we only
yield the following content types: `StreamingChatMessageContent` and
`FunctionCallContent`. We are not yielding `FunctionResultContent` which
is valuable for some use cases.
<!-- 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 updates the code to yield `FunctionResultContent` if it exists
in the streaming chat completion path. When we merge the function call
result content together into a `StreamingChatMessageContent` type, we
check if that message has items (which are of type
`FunctionResultContent`) and if so, we yield them. The filter path still
works because once they're yielded, we break out of the function calling
loop.
We need to include the `ai_model_id` if it exists for the current
PromptExecutionSettings because if performing a `reduce` operation to
add two streaming chat message chunks together, the
`StreamingChatMessageContent` that has the function results will break
if the `ai_model_id` is not set (the error is thrown in the `__add__`
override for the `StreamingChatMessageContent`.
Some unit tests that cover function calling were also updated -- during
the test, the test JSON function args were breaking in the `json.loads`
call because they contained single quotes and not double. We're now
sanitizing the args, just in case, so we don't break there.
This PR fixes:
- #9408
- #9968
<!-- 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-12-18 10:09:25 +09:00
|
|
|
is_streaming = any(isinstance(message, StreamingChatMessageContent) for message in chat_history.messages)
|
|
|
|
|
message = frc.to_streaming_chat_message_content() if is_streaming else frc.to_chat_message_content()
|
|
|
|
|
|
|
|
|
|
chat_history.add_message(message=message)
|
2024-08-12 11:37:08 -04:00
|
|
|
|
|
|
|
|
return invocation_context if invocation_context.terminate else None
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
|
|
|
|
|
async def _inner_auto_function_invoke_handler(self, context: AutoFunctionInvocationContext):
|
|
|
|
|
"""Inner auto function invocation handler."""
|
|
|
|
|
try:
|
2025-05-27 16:41:44 -07:00
|
|
|
result = await context.function.invoke(
|
|
|
|
|
context.kernel,
|
|
|
|
|
context.arguments,
|
|
|
|
|
metadata=context.function_call_content.metadata | context.function_call_content.to_dict()
|
|
|
|
|
if context.function_call_content
|
|
|
|
|
else {},
|
|
|
|
|
)
|
Python: Introduce the new function calling abstraction, FunctionChoiceBehavior (#6910)
### Motivation and Context
The current `FunctionCallBehavior` has allowed us to utilize auto
function calling for OpenAI type models. As we proceed to support more
AI connectors, that differ from OpenAI models, we need to be able to
handle functions for all models that support function calling.
<!-- 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
In this PR:
- We introduce a new function calling abstraction called
`FunctionChoiceBehavior` which has three types: Auto, Required, and
None.
- One is able to configure the `function_choice_behavior` along with
fully qualified function names (e.g., plugin1.function1),
`maximum_auto_invoke_attempts` or `auto_invoke_kernel_functions` in yaml
and JSON prompts. We have a new concept example showing how to do this
for for yaml and JSON prompts.
- If the fully qualified names are specified in the config file, they
take precedence over the filters, if specified at a later point.
- To make sure this isn't a breaking change, we still handle the
previous `FunctionCallBehavior`, but so we can make decisions on the new
`FunctionChoiceBehavior` we map the `FunctionCallBehavior` to
`FunctionChoiceBehavior`. Each time `FunctionCallBehavior` is updated,
`FunctionChoiceBehavior` will be updated, too.
- The `_process_tool_call()` method in the
`open_ai_chat_completion_base` needs to maintain the argument name as
`function_call_behavior` as we cannot introduce a breaking change.
- All concept samples have been converted to use
`FunctionChoiceBehavior`.
- New unit tests have been added for `FunctionChoiceBehavior` and we
still currently support `FunctionCallBehavior` tests to make sure we
haven't broken the backwards compatibility.
- Added `deprecated` typing decorators to classes/methods to alert users
that it would be best to transition to `FunctionChoiceBehavior` even
though `FunctionCallBehavior` is still supported.
- The `FunctionCallingStepwisePlanner` was updated to use the new
`FunctionChoiceBehavior`.
- Closes #6496, #6458
- Addresses #6626
<!-- 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-06-27 16:34:59 -04:00
|
|
|
if result:
|
|
|
|
|
context.function_result = result
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.exception(f"Error invoking function {context.function.fully_qualified_name}: {exc}.")
|
|
|
|
|
value = f"An error occurred while invoking the function {context.function.fully_qualified_name}: {exc}"
|
|
|
|
|
if context.function_result is not None:
|
|
|
|
|
context.function_result.value = value
|
|
|
|
|
else:
|
|
|
|
|
context.function_result = FunctionResult(function=context.function.metadata, value=value)
|
|
|
|
|
return
|
Python: Feature new memory stores and collections (#7614)
### 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 PR adds new vector store and vector store record collections as
well as implementations for:
- Azure AI Search
- Redis
- Qdrant
- Volatile (in-memory)
It also adds samples, tests, and unit-tests for these.
Next it adds the vector store record fields, definition and decorator,
with tests and samples.
All marked experimental. Existing Redis, Azure AI Search, Qdrant and
Volatile will be marked as deprecated in the future, once the new
collections are feature complete.
### Description
<!-- 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-08-06 22:30:26 +02:00
|
|
|
|
|
|
|
|
async def add_embedding_to_object(
|
|
|
|
|
self,
|
|
|
|
|
inputs: OneOrMany[TDataModel],
|
|
|
|
|
field_to_embed: str,
|
|
|
|
|
field_to_store: str,
|
|
|
|
|
execution_settings: dict[str, "PromptExecutionSettings"],
|
|
|
|
|
container_mode: bool = False,
|
|
|
|
|
cast_function: Callable[[list[float]], Any] | None = None,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
):
|
|
|
|
|
"""Gather all fields to embed, batch the embedding generation and store."""
|
|
|
|
|
contents: list[Any] = []
|
|
|
|
|
dict_like = (getter := getattr(inputs, "get", False)) and callable(getter)
|
|
|
|
|
list_of_dicts: bool = False
|
|
|
|
|
if container_mode:
|
|
|
|
|
contents = inputs[field_to_embed].tolist() # type: ignore
|
|
|
|
|
elif isinstance(inputs, list):
|
|
|
|
|
list_of_dicts = (getter := getattr(inputs[0], "get", False)) and callable(getter)
|
|
|
|
|
for record in inputs:
|
|
|
|
|
if list_of_dicts:
|
|
|
|
|
contents.append(record.get(field_to_embed)) # type: ignore
|
|
|
|
|
else:
|
|
|
|
|
contents.append(getattr(record, field_to_embed))
|
|
|
|
|
else:
|
|
|
|
|
if dict_like:
|
|
|
|
|
contents.append(inputs.get(field_to_embed)) # type: ignore
|
|
|
|
|
else:
|
|
|
|
|
contents.append(getattr(inputs, field_to_embed))
|
|
|
|
|
vectors = None
|
|
|
|
|
service: EmbeddingGeneratorBase | None = None
|
|
|
|
|
for service_id, settings in execution_settings.items():
|
|
|
|
|
service = self.get_service(service_id, type=EmbeddingGeneratorBase) # type: ignore
|
|
|
|
|
if service:
|
|
|
|
|
vectors = await service.generate_raw_embeddings(texts=contents, settings=settings, **kwargs) # type: ignore
|
|
|
|
|
break
|
|
|
|
|
if not service:
|
|
|
|
|
raise KernelServiceNotFoundError("No service found to generate embeddings.")
|
|
|
|
|
if vectors is None:
|
|
|
|
|
raise KernelInvokeException("No vectors were generated.")
|
|
|
|
|
if cast_function:
|
|
|
|
|
vectors = [cast_function(vector) for vector in vectors]
|
|
|
|
|
if container_mode:
|
|
|
|
|
inputs[field_to_store] = vectors # type: ignore
|
|
|
|
|
return
|
|
|
|
|
if isinstance(inputs, list):
|
|
|
|
|
for record, vector in zip(inputs, vectors):
|
|
|
|
|
if list_of_dicts:
|
|
|
|
|
record[field_to_store] = vector # type: ignore
|
|
|
|
|
else:
|
|
|
|
|
setattr(record, field_to_store, vector)
|
|
|
|
|
return
|
|
|
|
|
if dict_like:
|
|
|
|
|
inputs[field_to_store] = vectors[0] # type: ignore
|
|
|
|
|
return
|
|
|
|
|
setattr(inputs, field_to_store, vectors[0])
|
2025-04-23 17:26:51 -07:00
|
|
|
|
|
|
|
|
def clone(self) -> "Kernel":
|
|
|
|
|
"""Clone the kernel instance to create a new one that may be mutated without affecting the current instance.
|
|
|
|
|
|
|
|
|
|
The current instance is not mutated by this operation.
|
|
|
|
|
|
|
|
|
|
Note: The same service clients are used in the new instance, so if you mutate the service clients
|
|
|
|
|
in the new instance, the original instance will be affected as well.
|
|
|
|
|
|
|
|
|
|
New lists of plugins and filters are created. It will not affect the original lists when the new instance
|
|
|
|
|
is mutated. A new `ai_service_selector` is created. It will not affect the original instance when the new
|
|
|
|
|
instance is mutated.
|
2025-09-12 09:25:07 +09:00
|
|
|
|
|
|
|
|
Important: Plugins are cloned without deep-copying their underlying callable methods. This avoids attempting
|
|
|
|
|
to pickle/clone unpickleable objects (e.g., async generators), which can be present when plugins wrap async
|
|
|
|
|
context managers such as MCP client sessions. Function metadata is deep-copied while callables are shared.
|
2025-04-23 17:26:51 -07:00
|
|
|
"""
|
2025-09-12 09:25:07 +09:00
|
|
|
# Safely clone plugins by copying function metadata while retaining callable references.
|
|
|
|
|
# This avoids deepcopying bound methods that may reference unpickleable async components.
|
|
|
|
|
new_plugins: dict[str, KernelPlugin] = {}
|
|
|
|
|
for plugin_name, plugin in self.plugins.items():
|
|
|
|
|
cloned_plugin = KernelPlugin(name=plugin.name, description=plugin.description)
|
|
|
|
|
# Using KernelPlugin.add will copy functions via KernelFunction.function_copy(),
|
|
|
|
|
# which deep-copies metadata but keeps callables shallow.
|
|
|
|
|
cloned_plugin.add(plugin.functions)
|
|
|
|
|
new_plugins[plugin_name] = cloned_plugin
|
|
|
|
|
|
2025-04-23 17:26:51 -07:00
|
|
|
return Kernel(
|
2025-09-12 09:25:07 +09:00
|
|
|
plugins=new_plugins,
|
2025-04-23 17:26:51 -07:00
|
|
|
# Shallow copy of the services, as they are not serializable
|
|
|
|
|
services={k: v for k, v in self.services.items()},
|
|
|
|
|
ai_service_selector=deepcopy(self.ai_service_selector),
|
|
|
|
|
function_invocation_filters=deepcopy(self.function_invocation_filters),
|
|
|
|
|
prompt_rendering_filters=deepcopy(self.prompt_rendering_filters),
|
|
|
|
|
auto_function_invocation_filters=deepcopy(self.auto_function_invocation_filters),
|
|
|
|
|
)
|
2025-04-11 07:22:07 +02:00
|
|
|
|
|
|
|
|
@experimental
|
|
|
|
|
def as_mcp_server(
|
|
|
|
|
self,
|
2025-04-15 10:25:35 +02:00
|
|
|
prompts: list[PromptTemplateBase] | None = None,
|
2025-04-11 07:22:07 +02:00
|
|
|
server_name: str = "Semantic Kernel MCP Server",
|
|
|
|
|
version: str | None = None,
|
|
|
|
|
instructions: str | None = None,
|
|
|
|
|
lifespan: Callable[["Server[LifespanResultT]"], AbstractAsyncContextManager["LifespanResultT"]] | None = None,
|
|
|
|
|
excluded_functions: OptionalOneOrMany[str] = None,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> "Server":
|
|
|
|
|
"""Create a MCP server from this kernel.
|
|
|
|
|
|
|
|
|
|
This function automatically creates a MCP server from a kernel instance, it uses the provided arguments to
|
|
|
|
|
configure the server and expose functions as tools and prompts, see the mcp documentation for more details.
|
|
|
|
|
|
2025-04-15 10:25:35 +02:00
|
|
|
By default, all functions are exposed as Tools, you can control this by
|
|
|
|
|
using use the `excluded_functions` argument.
|
|
|
|
|
These need to be set to the function name, without the plugin_name.
|
2025-04-11 07:22:07 +02:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
kernel: The kernel instance to use.
|
2025-04-15 10:25:35 +02:00
|
|
|
prompts: A list of prompt templates to expose as prompts.
|
2025-04-11 07:22:07 +02:00
|
|
|
server_name: The name of the server.
|
|
|
|
|
version: The version of the server.
|
|
|
|
|
instructions: The instructions to use for the server.
|
|
|
|
|
lifespan: The lifespan of the server.
|
2025-04-15 10:25:35 +02:00
|
|
|
excluded_functions: The list of function names to exclude from the server.
|
2025-04-11 07:22:07 +02:00
|
|
|
if None, no functions will be excluded.
|
|
|
|
|
kwargs: Any extra arguments to pass to the server creation.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The MCP server instance, it is a instance of
|
|
|
|
|
mcp.server.lowlevel.Server
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
from semantic_kernel.connectors.mcp import create_mcp_server_from_kernel
|
|
|
|
|
|
|
|
|
|
return create_mcp_server_from_kernel(
|
2025-04-15 10:25:35 +02:00
|
|
|
kernel=self,
|
|
|
|
|
prompts=prompts,
|
2025-04-11 07:22:07 +02:00
|
|
|
server_name=server_name,
|
|
|
|
|
version=version,
|
|
|
|
|
instructions=instructions,
|
|
|
|
|
lifespan=lifespan,
|
|
|
|
|
excluded_functions=excluded_functions,
|
|
|
|
|
**kwargs,
|
|
|
|
|
)
|