Files
semantic-kernel/python/semantic_kernel/kernel_exception.py
T
Evan MattsonandEvan Mattson 2b6bd03b03 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 😄

---------

Co-authored-by: Evan Mattson <[email protected]>
2024-02-01 18:57:00 +00:00

61 lines
1.7 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
from enum import Enum
from typing import Optional
class KernelException(Exception):
class ErrorCodes(Enum):
# Unknown error.
UnknownError = -1
# Invalid function description.
InvalidFunctionDescription = 0
# Function overload not supported.
FunctionOverloadNotSupported = 1
# Function not available.
FunctionNotAvailable = 2
# Function type not supported.
FunctionTypeNotSupported = 3
# Invalid function type.
InvalidFunctionType = 4
# Invalid service configuration.
InvalidServiceConfiguration = 5
# Service not found.
ServiceNotFound = 6
# Plugin collection not set.
PluginCollectionNotSet = 7
# Represents an error that occurs when invoking a function.
FunctionInvokeError = 8
# Ambiguous implementation.
AmbiguousImplementation = 9
# Invalid plugin name
InvalidPluginName = 10
# The error code.
_error_code: ErrorCodes
def __init__(
self,
error_code: ErrorCodes,
message: str,
inner_exception: Optional[Exception] = None,
) -> None:
"""Initializes a new instance of the KernelError class.
Arguments:
error_code {ErrorCodes} -- The error code.
message {str} -- The error message.
inner_exception {Exception} -- The inner exception.
"""
super().__init__(error_code, message, inner_exception)
self._error_code = error_code
@property
def error_code(self) -> ErrorCodes:
"""Gets the error code.
Returns:
ErrorCodes -- The error code.
"""
return self._error_code