Skip to content

Commit a9d7d5d

Browse files
stefan521moonbox3
andauthored
Python: Refactoring. Use get_function and get_plugin. (microsoft#6382)
### 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. --> Refactoring some of the code following [this advice](microsoft#6371 (comment)) from the maintainers. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Look up kernel functions and plugins with the `get_function` and `get_plugin` method. Removed the unused `DEFAULT_CHAT_SYSTEM_PROMPT` ### 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 😄 --------- Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
1 parent 76039ed commit a9d7d5d

File tree

11 files changed

+25
-24
lines changed

11 files changed

+25
-24
lines changed

python/samples/concepts/search/bing_plugin_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async def example1(kernel: Kernel, search_plugin_name: str):
1414
print("======== Bing and Google Search Plugins ========")
1515

1616
question = "What's the largest building in the world?"
17-
function = kernel.plugins[search_plugin_name]["search"]
17+
function = kernel.get_function(plugin_name=search_plugin_name, function_name="search")
1818
result = await kernel.invoke(function, query=question)
1919

2020
print(question)

python/samples/learn_resources/serializing_prompts.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ async def main():
5050
plugin_name="ConversationSummaryPlugin",
5151
)
5252

53-
summarize_function = kernel.plugins["ConversationSummaryPlugin"]["SummarizeConversation"]
53+
summarize_function = kernel.get_function(
54+
plugin_name="ConversationSummaryPlugin", function_name="SummarizeConversation"
55+
)
5456

5557
# Create the history
5658
history = ChatHistory()

python/semantic_kernel/connectors/ai/open_ai/const.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@
44

55
DEFAULT_AZURE_API_VERSION: Final[str] = "2024-02-01"
66
USER_AGENT: Final[str] = "User-Agent"
7-
DEFAULT_CHAT_SYSTEM_PROMPT: Final[str] = "Assistant is a large language model."

python/tests/unit/core_plugins/test_http_plugin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ async def test_it_can_be_imported():
2121
kernel = Kernel()
2222
plugin = HttpPlugin()
2323
kernel.add_plugin(plugin, "http")
24-
assert kernel.plugins["http"] is not None
25-
assert kernel.plugins["http"].name == "http"
26-
assert kernel.plugins["http"]["getAsync"] is not None
27-
assert kernel.plugins["http"]["postAsync"] is not None
24+
assert kernel.get_plugin(plugin_name="http") is not None
25+
assert kernel.get_plugin(plugin_name="http").name == "http"
26+
assert kernel.get_function(plugin_name="http", function_name="getAsync") is not None
27+
assert kernel.get_function(plugin_name="http", function_name="postAsync") is not None
2828

2929

3030
@patch("aiohttp.ClientSession.get")

python/tests/unit/core_plugins/test_math_plugin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ def test_can_be_instantiated():
1515
def test_can_be_imported():
1616
kernel = Kernel()
1717
kernel.add_plugin(MathPlugin(), "math")
18-
assert kernel.plugins["math"] is not None
19-
assert kernel.plugins["math"].name == "math"
20-
assert kernel.plugins["math"]["Add"] is not None
21-
assert kernel.plugins["math"]["Subtract"] is not None
18+
assert kernel.get_plugin(plugin_name="math") is not None
19+
assert kernel.get_plugin(plugin_name="math").name == "math"
20+
assert kernel.get_function(plugin_name="math", function_name="Add") is not None
21+
assert kernel.get_function(plugin_name="math", function_name="Subtract") is not None
2222

2323

2424
@pytest.mark.parametrize(

python/tests/unit/core_plugins/test_sessions_python_plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def test_validate_endpoint(aca_python_sessions_unit_test_env):
3030
def test_it_can_be_imported(kernel: Kernel, aca_python_sessions_unit_test_env):
3131
plugin = SessionsPythonTool(auth_callback=test_auth_callback)
3232
assert kernel.add_plugin(plugin=plugin, plugin_name="PythonCodeInterpreter")
33-
assert kernel.plugins["PythonCodeInterpreter"] is not None
34-
assert kernel.plugins["PythonCodeInterpreter"].name == "PythonCodeInterpreter"
33+
assert kernel.get_plugin(plugin_name="PythonCodeInterpreter") is not None
34+
assert kernel.get_plugin(plugin_name="PythonCodeInterpreter").name == "PythonCodeInterpreter"
3535

3636

3737
@pytest.mark.asyncio

python/tests/unit/core_plugins/test_text_memory_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_can_be_instantiated(memory: SemanticTextMemory):
3636

3737
def test_can_be_imported(kernel: Kernel, memory: SemanticTextMemory):
3838
kernel.add_plugin(TextMemoryPlugin(memory), "memory_plugin")
39-
assert not kernel.plugins["memory_plugin"]["recall"].is_prompt
39+
assert not kernel.get_function(plugin_name="memory_plugin", function_name="recall").is_prompt
4040

4141

4242
@mark.asyncio

python/tests/unit/core_plugins/test_text_plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ def test_can_be_instantiated():
1010

1111
def test_can_be_imported(kernel: Kernel):
1212
kernel.add_plugin(TextPlugin(), "text_plugin")
13-
assert not kernel.plugins["text_plugin"]["trim"].is_prompt
13+
assert not kernel.get_function(plugin_name="text_plugin", function_name="trim").is_prompt
1414

1515

1616
def test_can_be_imported_with_name(kernel: Kernel):
1717
kernel.add_plugin(TextPlugin(), "text")
18-
assert not kernel.plugins["text"]["trim"].is_prompt
18+
assert not kernel.get_function(plugin_name="text", function_name="trim").is_prompt
1919

2020

2121
def test_can_trim():

python/tests/unit/core_plugins/test_time_plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ def test_can_be_instantiated():
1515
def test_can_be_imported():
1616
kernel = sk.Kernel()
1717
kernel.add_plugin(TimePlugin(), "time")
18-
assert kernel.plugins["time"] is not None
19-
assert kernel.plugins["time"].name == "time"
20-
assert kernel.plugins["time"]["now"] is not None
18+
assert kernel.get_plugin(plugin_name="time") is not None
19+
assert kernel.get_plugin(plugin_name="time").name == "time"
20+
assert kernel.get_function(plugin_name="time", function_name="now") is not None
2121

2222

2323
def test_date():

python/tests/unit/kernel/test_kernel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ async def test_add_plugin_from_openai(mock_parse_openai_manifest, kernel: Kernel
279279
enable_dynamic_payload=True,
280280
),
281281
)
282-
plugin = kernel.plugins["TestOpenAIPlugin"]
282+
plugin = kernel.get_plugin(plugin_name="TestOpenAIPlugin")
283283
assert plugin is not None
284284
assert plugin.name == "TestOpenAIPlugin"
285285
assert plugin.functions.get("GetSecret") is not None
@@ -295,7 +295,7 @@ def test_import_plugin_from_openapi(kernel: Kernel):
295295
plugin_name="TestOpenAPIPlugin",
296296
openapi_document_path=openapi_spec_file,
297297
)
298-
plugin = kernel.plugins["TestOpenAPIPlugin"]
298+
plugin = kernel.get_plugin(plugin_name="TestOpenAPIPlugin")
299299
assert plugin is not None
300300
assert plugin.name == "TestOpenAPIPlugin"
301301
assert plugin.functions.get("GetSecret") is not None

python/tests/unit/kernel/test_register_functions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
@pytest.mark.asyncio
1616
async def test_register_valid_native_function(kernel: Kernel, decorated_native_function: Callable):
17-
kernel.add_function("TestPlugin", function=decorated_native_function)
18-
registered_func = kernel.get_function("TestPlugin", "getLightStatus")
17+
kernel.add_function(plugin_name="TestPlugin", function=decorated_native_function)
18+
registered_func = kernel.get_function(plugin_name="TestPlugin", function_name="getLightStatus")
1919

2020
assert isinstance(registered_func, KernelFunction)
21-
assert kernel.plugins["TestPlugin"]["getLightStatus"] == registered_func
21+
assert kernel.get_function(plugin_name="TestPlugin", function_name="getLightStatus") == registered_func
2222
func_result = await registered_func.invoke(kernel, KernelArguments(arg1="testtest"))
2323
assert str(func_result) == "test"
2424

0 commit comments

Comments
 (0)