mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### 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]>
119 lines
3.6 KiB
Python
119 lines
3.6 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import typing as t
|
|
import warnings
|
|
|
|
import pytest
|
|
|
|
import semantic_kernel as sk
|
|
from semantic_kernel.memory.null_memory import NullMemory
|
|
from semantic_kernel.orchestration.context_variables import ContextVariables
|
|
from semantic_kernel.orchestration.kernel_context import KernelContext
|
|
from semantic_kernel.orchestration.kernel_function import KernelFunction
|
|
from semantic_kernel.plugin_definition.kernel_plugin import KernelPlugin
|
|
from semantic_kernel.plugin_definition.kernel_plugin_collection import KernelPluginCollection
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def enable_debug_mode():
|
|
"""Set `autouse=True` to enable easy debugging for tests.
|
|
|
|
How to debug:
|
|
1. Ensure [snoop](https://github.com/alexmojaki/snoop) is installed
|
|
(`pip install snoop`).
|
|
2. If you're doing print based debugging, use `pr` instead of `print`.
|
|
That is, convert `print(some_var)` to `pr(some_var)`.
|
|
3. If you want a trace of a particular functions calls, just add `ss()` as the first
|
|
line of the function.
|
|
|
|
NOTE:
|
|
----
|
|
It's completely fine to leave `autouse=True` in the fixture. It doesn't affect
|
|
the tests unless you use `pr` or `ss` in any test.
|
|
|
|
NOTE:
|
|
----
|
|
When you use `ss` or `pr` in a test, pylance or mypy will complain. This is
|
|
because they don't know that we're adding these functions to the builtins. The
|
|
tests will run fine though.
|
|
"""
|
|
import builtins
|
|
|
|
try:
|
|
import snoop
|
|
except ImportError:
|
|
warnings.warn(
|
|
"Install snoop to enable trace debugging. `pip install snoop`",
|
|
ImportWarning,
|
|
)
|
|
return
|
|
|
|
builtins.ss = snoop.snoop(depth=4).__enter__
|
|
builtins.pr = snoop.pp
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def create_kernel():
|
|
kernel = sk.Kernel()
|
|
return kernel
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def get_aoai_config():
|
|
if "Python_Integration_Tests" in os.environ:
|
|
deployment_name = os.environ["AzureOpenAIEmbeddings__DeploymentName"]
|
|
api_key = os.environ["AzureOpenAI__ApiKey"]
|
|
endpoint = os.environ["AzureOpenAI__Endpoint"]
|
|
else:
|
|
# Load credentials from .env file
|
|
deployment_name, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
|
|
deployment_name = "text-embedding-ada-002"
|
|
|
|
return deployment_name, api_key, endpoint
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def get_oai_config():
|
|
if "Python_Integration_Tests" in os.environ:
|
|
api_key = os.environ["OpenAI__ApiKey"]
|
|
org_id = None
|
|
else:
|
|
# Load credentials from .env file
|
|
api_key, org_id = sk.openai_settings_from_dot_env()
|
|
|
|
return api_key, org_id
|
|
|
|
|
|
@pytest.fixture()
|
|
def context_factory() -> t.Callable[[ContextVariables], KernelContext]:
|
|
"""Return a factory for KernelContext objects."""
|
|
|
|
def create_context(context_variables: ContextVariables, *functions: KernelFunction) -> KernelContext:
|
|
"""Return a KernelContext object."""
|
|
|
|
plugin = KernelPlugin(name="test_plugin", functions=functions)
|
|
|
|
return KernelContext(
|
|
context_variables,
|
|
NullMemory(),
|
|
plugins=KernelPluginCollection(
|
|
plugins=[plugin],
|
|
),
|
|
)
|
|
|
|
return create_context
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def get_gp_config():
|
|
if "Python_Integration_Tests" in os.environ:
|
|
api_key = os.environ["GOOGLE_PALM_API_KEY"]
|
|
else:
|
|
# Load credentials from .env file
|
|
api_key = sk.google_palm_settings_from_dot_env()
|
|
|
|
return api_key
|