Skip to content

Commit

Permalink
Deprecate the field instead of removing
Browse files Browse the repository at this point in the history
  • Loading branch information
TaoChenOSU committed Dec 13, 2024
1 parent c986616 commit 0d06bb6
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python/semantic_kernel/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from semantic_kernel.functions.kernel_plugin import KernelPlugin
from semantic_kernel.kernel_types import AI_SERVICE_CLIENT_TYPE, OneOrMany
from semantic_kernel.prompt_template.const import KERNEL_TEMPLATE_FORMAT_NAME
from semantic_kernel.reliability.kernel_reliability_extension import KernelReliabilityExtension
from semantic_kernel.services.ai_service_selector import AIServiceSelector
from semantic_kernel.services.kernel_services_extension import KernelServicesExtension
from semantic_kernel.utils.naming import generate_random_ascii_name
Expand All @@ -51,7 +52,7 @@
logger: logging.Logger = logging.getLogger(__name__)


class Kernel(KernelFilterExtension, KernelFunctionExtension, KernelServicesExtension):
class Kernel(KernelFilterExtension, KernelFunctionExtension, KernelServicesExtension, KernelReliabilityExtension):
"""The Kernel of Semantic Kernel.
This is the main entry point for Semantic Kernel. It provides the ability to run
Expand Down
Empty file.
22 changes: 22 additions & 0 deletions python/semantic_kernel/reliability/kernel_reliability_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) Microsoft. All rights reserved.

import logging
from abc import ABC

from pydantic import Field
from typing_extensions import deprecated

from semantic_kernel.kernel_pydantic import KernelBaseModel
from semantic_kernel.reliability.pass_through_without_retry import PassThroughWithoutRetry
from semantic_kernel.reliability.retry_mechanism_base import RetryMechanismBase

logger: logging.Logger = logging.getLogger(__name__)


class KernelReliabilityExtension(KernelBaseModel, ABC):
"""Kernel reliability extension."""

retry_mechanism: RetryMechanismBase = Field(
default_factory=PassThroughWithoutRetry,
deprecated=deprecated("retry_mechanism is deprecated; This property doesn't have any effect on the kernel."),
)
31 changes: 31 additions & 0 deletions python/semantic_kernel/reliability/pass_through_without_retry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) Microsoft. All rights reserved.

import logging
from collections.abc import Awaitable, Callable
from typing import TypeVar

from semantic_kernel.kernel_pydantic import KernelBaseModel
from semantic_kernel.reliability.retry_mechanism_base import RetryMechanismBase

T = TypeVar("T")

logger: logging.Logger = logging.getLogger(__name__)


class PassThroughWithoutRetry(RetryMechanismBase, KernelBaseModel):
"""A retry mechanism that does not retry."""

async def execute_with_retry(self, action: Callable[[], Awaitable[T]]) -> Awaitable[T]:
"""Executes the given action with retry logic.
Args:
action (Callable[[], Awaitable[T]]): The action to retry on exception.
Returns:
Awaitable[T]: An awaitable that will return the result of the action.
"""
try:
return action()
except Exception as e:
logger.warning(e, "Error executing action, not retrying")
raise e
26 changes: 26 additions & 0 deletions python/semantic_kernel/reliability/retry_mechanism_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) Microsoft. All rights reserved.

import logging
from abc import ABC, abstractmethod
from collections.abc import Awaitable, Callable
from typing import TypeVar

T = TypeVar("T")

logger: logging.Logger = logging.getLogger(__name__)


class RetryMechanismBase(ABC):
"""Base class for retry mechanisms."""

@abstractmethod
async def execute_with_retry(self, action: Callable[[], Awaitable[T]]) -> Awaitable[T]:
"""Executes the given action with retry logic.
Args:
action (Callable[[], Awaitable[T]]): The action to retry on exception.
Returns:
Awaitable[T]: An awaitable that will return the result of the action.
"""
pass
1 change: 1 addition & 0 deletions python/tests/unit/kernel/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def test_init():
assert kernel.ai_service_selector is not None
assert kernel.plugins is not None
assert kernel.services is not None
assert kernel.retry_mechanism is not None
assert kernel.function_invocation_filters is not None
assert kernel.prompt_rendering_filters is not None

Expand Down

0 comments on commit 0d06bb6

Please sign in to comment.