mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Python: [BugFix] ReadOnlySkillCollection is not a Generic (#2364)
All (unit) tests are passing. ### Motivation and Context This change reduces the number of user facing changes and aligns the type of ReadOnlySkillCollection with the usage in SK. There is only one type of `SkillCollection` in SK, so it doesn't make sense to make `ReadOnlySkillCollection` a generic. If, in the future, another SkillCollection type is added, then we might want to change this to a generic type. <!-- 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 <!-- 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 😄
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
import typing as t
|
||||
|
||||
GLOBAL_SKILL: t.Final[str] = "_GLOBAL_FUNCTIONS_"
|
||||
@@ -1,39 +1,38 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from logging import Logger
|
||||
from typing import TYPE_CHECKING, Dict, Generic, Literal, Optional, Tuple, TypeVar
|
||||
from typing import TYPE_CHECKING, ClassVar, Dict, Optional, Tuple
|
||||
|
||||
import pydantic as pdt
|
||||
|
||||
from semantic_kernel.kernel_exception import KernelException
|
||||
from semantic_kernel.sk_pydantic import SKGenericModel
|
||||
from semantic_kernel.orchestration.sk_function import SKFunction
|
||||
from semantic_kernel.sk_pydantic import SKBaseModel
|
||||
from semantic_kernel.skill_definition import constants
|
||||
from semantic_kernel.skill_definition.functions_view import FunctionsView
|
||||
from semantic_kernel.skill_definition.read_only_skill_collection_base import (
|
||||
ReadOnlySkillCollectionBase,
|
||||
)
|
||||
from semantic_kernel.utils.null_logger import NullLogger
|
||||
from semantic_kernel.utils.static_property import static_property
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from semantic_kernel.orchestration.sk_function_base import SKFunctionBase
|
||||
|
||||
|
||||
SKFunctionT = TypeVar("SKFunctionT", bound="SKFunctionBase")
|
||||
|
||||
|
||||
class ReadOnlySkillCollection(
|
||||
SKGenericModel, ReadOnlySkillCollectionBase, Generic[SKFunctionT]
|
||||
):
|
||||
data: Dict[str, Dict[str, SKFunctionT]]
|
||||
class ReadOnlySkillCollection(SKBaseModel, ReadOnlySkillCollectionBase):
|
||||
GLOBAL_SKILL: ClassVar[str] = constants.GLOBAL_SKILL
|
||||
data: Dict[str, Dict[str, SKFunction]] = pdt.Field(default_factory=dict)
|
||||
_log: Logger = pdt.PrivateAttr()
|
||||
|
||||
class Config:
|
||||
allow_mutation = False
|
||||
|
||||
def __init__(
|
||||
self, data: Dict[str, Dict[str, SKFunctionT]], log: Optional[Logger] = None
|
||||
self,
|
||||
data: Dict[str, Dict[str, SKFunction]] = None,
|
||||
log: Optional[Logger] = None,
|
||||
) -> None:
|
||||
super().__init__(data=data)
|
||||
super().__init__(data=data or {})
|
||||
self._log = log or NullLogger()
|
||||
|
||||
def has_function(self, skill_name: Optional[str], function_name: str) -> bool:
|
||||
@@ -126,7 +125,3 @@ class ReadOnlySkillCollection(
|
||||
|
||||
s_name, f_name = s_name.lower(), f_name.lower()
|
||||
return s_name, f_name
|
||||
|
||||
@static_property
|
||||
def GLOBAL_SKILL() -> Literal["_GLOBAL_FUNCTIONS_"]:
|
||||
return "_GLOBAL_FUNCTIONS_"
|
||||
|
||||
@@ -3,16 +3,17 @@
|
||||
from logging import Logger
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
ClassVar,
|
||||
Dict,
|
||||
Generic,
|
||||
Optional,
|
||||
TypeVar,
|
||||
Union,
|
||||
)
|
||||
|
||||
import pydantic as pdt
|
||||
|
||||
from semantic_kernel.orchestration.sk_function import SKFunction
|
||||
from semantic_kernel.sk_pydantic import SKGenericModel
|
||||
from semantic_kernel.skill_definition import constants
|
||||
from semantic_kernel.skill_definition.functions_view import FunctionsView
|
||||
from semantic_kernel.skill_definition.read_only_skill_collection import (
|
||||
ReadOnlySkillCollection,
|
||||
@@ -22,15 +23,13 @@ from semantic_kernel.skill_definition.read_only_skill_collection_base import (
|
||||
)
|
||||
from semantic_kernel.skill_definition.skill_collection_base import SkillCollectionBase
|
||||
from semantic_kernel.utils.null_logger import NullLogger
|
||||
from semantic_kernel.utils.static_property import static_property
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from semantic_kernel.orchestration.sk_function_base import SKFunctionBase
|
||||
|
||||
SKFunctionT = TypeVar("SKFunctionT", bound="SKFunctionBase")
|
||||
|
||||
|
||||
class SkillCollection(SKGenericModel, SkillCollectionBase, Generic[SKFunctionT]):
|
||||
class SkillCollection(SKGenericModel, SkillCollectionBase):
|
||||
GLOBAL_SKILL: ClassVar[str] = constants.GLOBAL_SKILL
|
||||
read_only_skill_collection_: ReadOnlySkillCollection = pdt.Field(
|
||||
alias="read_only_skill_collection"
|
||||
)
|
||||
@@ -39,7 +38,7 @@ class SkillCollection(SKGenericModel, SkillCollectionBase, Generic[SKFunctionT])
|
||||
def __init__(
|
||||
self,
|
||||
log: Optional[Logger] = None,
|
||||
skill_collection: Union[Dict[str, Dict[str, SKFunctionT]], None] = None,
|
||||
skill_collection: Union[Dict[str, Dict[str, SKFunction]], None] = None,
|
||||
read_only_skill_collection_: Optional[ReadOnlySkillCollection] = None,
|
||||
) -> None:
|
||||
if skill_collection and read_only_skill_collection_:
|
||||
@@ -71,10 +70,7 @@ class SkillCollection(SKGenericModel, SkillCollectionBase, Generic[SKFunctionT])
|
||||
s_name, f_name = function.skill_name, function.name
|
||||
s_name, f_name = s_name.lower(), f_name.lower()
|
||||
|
||||
if s_name not in self.skill_collection:
|
||||
self.skill_collection[s_name] = {}
|
||||
|
||||
self.skill_collection[s_name][f_name] = function
|
||||
self.skill_collection.setdefault(s_name, {})[f_name] = function
|
||||
|
||||
def add_native_function(self, function: "SKFunctionBase") -> None:
|
||||
if function is None:
|
||||
@@ -84,10 +80,8 @@ class SkillCollection(SKGenericModel, SkillCollectionBase, Generic[SKFunctionT])
|
||||
s_name, f_name = self.read_only_skill_collection_._normalize_names(
|
||||
s_name, f_name, True
|
||||
)
|
||||
if s_name not in self.skill_collection:
|
||||
self.skill_collection[s_name] = {}
|
||||
|
||||
self.skill_collection[s_name][f_name] = function
|
||||
self.skill_collection.setdefault(s_name, {})[f_name] = function
|
||||
|
||||
def has_function(self, skill_name: Optional[str], function_name: str) -> bool:
|
||||
return self.read_only_skill_collection_.has_function(skill_name, function_name)
|
||||
@@ -131,7 +125,3 @@ class SkillCollection(SKGenericModel, SkillCollectionBase, Generic[SKFunctionT])
|
||||
self, skill_name: Optional[str], function_name: str
|
||||
) -> "SKFunctionBase":
|
||||
return self.read_only_skill_collection_.get_function(skill_name, function_name)
|
||||
|
||||
@static_property
|
||||
def GLOBAL_SKILL():
|
||||
return ReadOnlySkillCollection.GLOBAL_SKILL
|
||||
|
||||
@@ -115,6 +115,7 @@ def sk_factory() -> t.Callable[[t.Type[_Serializable]], _Serializable]:
|
||||
False,
|
||||
),
|
||||
FunctionsView: create_functions_view(),
|
||||
ReadOnlySkillCollection: create_skill_collection().read_only_skill_collection,
|
||||
SkillCollection: create_skill_collection(),
|
||||
}
|
||||
|
||||
@@ -171,6 +172,7 @@ PYDANTIC_MODELS = [
|
||||
ParameterView,
|
||||
FunctionView,
|
||||
FunctionsView,
|
||||
ReadOnlySkillCollection,
|
||||
SkillCollection,
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user