From dba99ca4a6210631a6ebad42b43b5f5d3e8600a7 Mon Sep 17 00:00:00 2001 From: stefan521 Date: Thu, 23 May 2024 20:35:28 +0100 Subject: [PATCH 1/3] Refactoring. Use get_function and get_plugin. --- python/samples/concepts/search/bing_plugin_examples.py | 2 +- python/samples/learn_resources/serializing_prompts.py | 2 +- python/semantic_kernel/connectors/ai/open_ai/const.py | 1 - python/tests/unit/core_plugins/test_http_plugin.py | 8 ++++---- python/tests/unit/core_plugins/test_math_plugin.py | 8 ++++---- .../unit/core_plugins/test_sessions_python_plugin.py | 4 ++-- python/tests/unit/core_plugins/test_text_memory_plugin.py | 2 +- python/tests/unit/core_plugins/test_text_plugin.py | 4 ++-- python/tests/unit/core_plugins/test_time_plugin.py | 6 +++--- python/tests/unit/kernel/test_kernel.py | 4 ++-- python/tests/unit/kernel/test_register_functions.py | 2 +- 11 files changed, 21 insertions(+), 22 deletions(-) diff --git a/python/samples/concepts/search/bing_plugin_examples.py b/python/samples/concepts/search/bing_plugin_examples.py index 6482a3a6d707..e2d00664e407 100644 --- a/python/samples/concepts/search/bing_plugin_examples.py +++ b/python/samples/concepts/search/bing_plugin_examples.py @@ -14,7 +14,7 @@ async def example1(kernel: Kernel, search_plugin_name: str): print("======== Bing and Google Search Plugins ========") question = "What's the largest building in the world?" - function = kernel.plugins[search_plugin_name]["search"] + function = kernel.get_function(search_plugin_name, "search") result = await kernel.invoke(function, query=question) print(question) diff --git a/python/samples/learn_resources/serializing_prompts.py b/python/samples/learn_resources/serializing_prompts.py index 4ced1ee36936..155466c1262c 100644 --- a/python/samples/learn_resources/serializing_prompts.py +++ b/python/samples/learn_resources/serializing_prompts.py @@ -50,7 +50,7 @@ async def main(): plugin_name="ConversationSummaryPlugin", ) - summarize_function = kernel.plugins["ConversationSummaryPlugin"]["SummarizeConversation"] + summarize_function = kernel.get_function("ConversationSummaryPlugin", "SummarizeConversation") # Create the history history = ChatHistory() diff --git a/python/semantic_kernel/connectors/ai/open_ai/const.py b/python/semantic_kernel/connectors/ai/open_ai/const.py index e8e89f0cc633..eaeaa0eddcec 100644 --- a/python/semantic_kernel/connectors/ai/open_ai/const.py +++ b/python/semantic_kernel/connectors/ai/open_ai/const.py @@ -4,4 +4,3 @@ DEFAULT_AZURE_API_VERSION: Final[str] = "2024-02-01" USER_AGENT: Final[str] = "User-Agent" -DEFAULT_CHAT_SYSTEM_PROMPT: Final[str] = "Assistant is a large language model." diff --git a/python/tests/unit/core_plugins/test_http_plugin.py b/python/tests/unit/core_plugins/test_http_plugin.py index 3c13eb38000e..233c9f0425aa 100644 --- a/python/tests/unit/core_plugins/test_http_plugin.py +++ b/python/tests/unit/core_plugins/test_http_plugin.py @@ -21,10 +21,10 @@ async def test_it_can_be_imported(): kernel = Kernel() plugin = HttpPlugin() kernel.add_plugin(plugin, "http") - assert kernel.plugins["http"] is not None - assert kernel.plugins["http"].name == "http" - assert kernel.plugins["http"]["getAsync"] is not None - assert kernel.plugins["http"]["postAsync"] is not None + assert kernel.get_plugin("http") is not None + assert kernel.get_plugin("http").name == "http" + assert kernel.get_function("http", "getAsync") is not None + assert kernel.get_function("http", "postAsync") is not None @patch("aiohttp.ClientSession.get") diff --git a/python/tests/unit/core_plugins/test_math_plugin.py b/python/tests/unit/core_plugins/test_math_plugin.py index 28687d6da3af..d3a2cbfac2dd 100644 --- a/python/tests/unit/core_plugins/test_math_plugin.py +++ b/python/tests/unit/core_plugins/test_math_plugin.py @@ -15,10 +15,10 @@ def test_can_be_instantiated(): def test_can_be_imported(): kernel = Kernel() kernel.add_plugin(MathPlugin(), "math") - assert kernel.plugins["math"] is not None - assert kernel.plugins["math"].name == "math" - assert kernel.plugins["math"]["Add"] is not None - assert kernel.plugins["math"]["Subtract"] is not None + assert kernel.get_plugin("math") is not None + assert kernel.get_plugin("math").name == "math" + assert kernel.get_function("math", "Add") is not None + assert kernel.get_function("math", "Subtract") is not None @pytest.mark.parametrize( diff --git a/python/tests/unit/core_plugins/test_sessions_python_plugin.py b/python/tests/unit/core_plugins/test_sessions_python_plugin.py index 86a867fa8d9e..71cc0ad81997 100644 --- a/python/tests/unit/core_plugins/test_sessions_python_plugin.py +++ b/python/tests/unit/core_plugins/test_sessions_python_plugin.py @@ -30,8 +30,8 @@ def test_validate_endpoint(aca_python_sessions_unit_test_env): def test_it_can_be_imported(kernel: Kernel, aca_python_sessions_unit_test_env): plugin = SessionsPythonTool(auth_callback=test_auth_callback) assert kernel.add_plugin(plugin=plugin, plugin_name="PythonCodeInterpreter") - assert kernel.plugins["PythonCodeInterpreter"] is not None - assert kernel.plugins["PythonCodeInterpreter"].name == "PythonCodeInterpreter" + assert kernel.get_plugin("PythonCodeInterpreter") is not None + assert kernel.get_plugin("PythonCodeInterpreter").name == "PythonCodeInterpreter" @pytest.mark.asyncio diff --git a/python/tests/unit/core_plugins/test_text_memory_plugin.py b/python/tests/unit/core_plugins/test_text_memory_plugin.py index 7f377c57a416..30ed63f22327 100644 --- a/python/tests/unit/core_plugins/test_text_memory_plugin.py +++ b/python/tests/unit/core_plugins/test_text_memory_plugin.py @@ -36,7 +36,7 @@ def test_can_be_instantiated(memory: SemanticTextMemory): def test_can_be_imported(kernel: Kernel, memory: SemanticTextMemory): kernel.add_plugin(TextMemoryPlugin(memory), "memory_plugin") - assert not kernel.plugins["memory_plugin"]["recall"].is_prompt + assert not kernel.get_function("memory_plugin", "recall").is_prompt @mark.asyncio diff --git a/python/tests/unit/core_plugins/test_text_plugin.py b/python/tests/unit/core_plugins/test_text_plugin.py index c7b67b5980b5..336495343577 100644 --- a/python/tests/unit/core_plugins/test_text_plugin.py +++ b/python/tests/unit/core_plugins/test_text_plugin.py @@ -10,12 +10,12 @@ def test_can_be_instantiated(): def test_can_be_imported(kernel: Kernel): kernel.add_plugin(TextPlugin(), "text_plugin") - assert not kernel.plugins["text_plugin"]["trim"].is_prompt + assert not kernel.get_function("text_plugin", "trim").is_prompt def test_can_be_imported_with_name(kernel: Kernel): kernel.add_plugin(TextPlugin(), "text") - assert not kernel.plugins["text"]["trim"].is_prompt + assert not kernel.get_function("text", "trim").is_prompt def test_can_trim(): diff --git a/python/tests/unit/core_plugins/test_time_plugin.py b/python/tests/unit/core_plugins/test_time_plugin.py index a92713aad2eb..6253fb002eda 100644 --- a/python/tests/unit/core_plugins/test_time_plugin.py +++ b/python/tests/unit/core_plugins/test_time_plugin.py @@ -15,9 +15,9 @@ def test_can_be_instantiated(): def test_can_be_imported(): kernel = sk.Kernel() kernel.add_plugin(TimePlugin(), "time") - assert kernel.plugins["time"] is not None - assert kernel.plugins["time"].name == "time" - assert kernel.plugins["time"]["now"] is not None + assert kernel.get_plugin("time") is not None + assert kernel.get_plugin("time").name == "time" + assert kernel.get_function("time", "now") is not None def test_date(): diff --git a/python/tests/unit/kernel/test_kernel.py b/python/tests/unit/kernel/test_kernel.py index 207bed0ba9e2..7c8c96af55ef 100644 --- a/python/tests/unit/kernel/test_kernel.py +++ b/python/tests/unit/kernel/test_kernel.py @@ -279,7 +279,7 @@ async def test_add_plugin_from_openai(mock_parse_openai_manifest, kernel: Kernel enable_dynamic_payload=True, ), ) - plugin = kernel.plugins["TestOpenAIPlugin"] + plugin = kernel.get_plugin("TestOpenAIPlugin") assert plugin is not None assert plugin.name == "TestOpenAIPlugin" assert plugin.functions.get("GetSecret") is not None @@ -295,7 +295,7 @@ def test_import_plugin_from_openapi(kernel: Kernel): plugin_name="TestOpenAPIPlugin", openapi_document_path=openapi_spec_file, ) - plugin = kernel.plugins["TestOpenAPIPlugin"] + plugin = kernel.get_plugin("TestOpenAPIPlugin") assert plugin is not None assert plugin.name == "TestOpenAPIPlugin" assert plugin.functions.get("GetSecret") is not None diff --git a/python/tests/unit/kernel/test_register_functions.py b/python/tests/unit/kernel/test_register_functions.py index 3207ca22c037..e638fec78440 100644 --- a/python/tests/unit/kernel/test_register_functions.py +++ b/python/tests/unit/kernel/test_register_functions.py @@ -18,7 +18,7 @@ async def test_register_valid_native_function(kernel: Kernel, decorated_native_f registered_func = kernel.get_function("TestPlugin", "getLightStatus") assert isinstance(registered_func, KernelFunction) - assert kernel.plugins["TestPlugin"]["getLightStatus"] == registered_func + assert kernel.get_function("TestPlugin", "getLightStatus") == registered_func func_result = await registered_func.invoke(kernel, KernelArguments(arg1="testtest")) assert str(func_result) == "test" From c9c1a3cda2bf123fcf01e2bcb8f2a90d0ab60e9d Mon Sep 17 00:00:00 2001 From: stefan521 Date: Thu, 23 May 2024 22:34:52 +0100 Subject: [PATCH 2/3] PR suggestion. --- python/samples/concepts/search/bing_plugin_examples.py | 2 +- python/samples/learn_resources/serializing_prompts.py | 5 ++++- python/tests/unit/core_plugins/test_http_plugin.py | 8 ++++---- python/tests/unit/core_plugins/test_math_plugin.py | 8 ++++---- .../unit/core_plugins/test_sessions_python_plugin.py | 4 ++-- python/tests/unit/core_plugins/test_text_memory_plugin.py | 2 +- python/tests/unit/core_plugins/test_text_plugin.py | 4 ++-- python/tests/unit/core_plugins/test_time_plugin.py | 6 +++--- python/tests/unit/kernel/test_kernel.py | 4 ++-- python/tests/unit/kernel/test_register_functions.py | 6 +++--- 10 files changed, 26 insertions(+), 23 deletions(-) diff --git a/python/samples/concepts/search/bing_plugin_examples.py b/python/samples/concepts/search/bing_plugin_examples.py index e2d00664e407..dbe6b91e09ec 100644 --- a/python/samples/concepts/search/bing_plugin_examples.py +++ b/python/samples/concepts/search/bing_plugin_examples.py @@ -14,7 +14,7 @@ async def example1(kernel: Kernel, search_plugin_name: str): print("======== Bing and Google Search Plugins ========") question = "What's the largest building in the world?" - function = kernel.get_function(search_plugin_name, "search") + function = kernel.get_function(plugin_name=search_plugin_name, function_name="search") result = await kernel.invoke(function, query=question) print(question) diff --git a/python/samples/learn_resources/serializing_prompts.py b/python/samples/learn_resources/serializing_prompts.py index 155466c1262c..a7943d96765d 100644 --- a/python/samples/learn_resources/serializing_prompts.py +++ b/python/samples/learn_resources/serializing_prompts.py @@ -50,7 +50,10 @@ async def main(): plugin_name="ConversationSummaryPlugin", ) - summarize_function = kernel.get_function("ConversationSummaryPlugin", "SummarizeConversation") + summarize_function = kernel.get_function( + plugin_name="ConversationSummaryPlugin", + function_name="SummarizeConversation" + ) # Create the history history = ChatHistory() diff --git a/python/tests/unit/core_plugins/test_http_plugin.py b/python/tests/unit/core_plugins/test_http_plugin.py index 233c9f0425aa..ef156bddba50 100644 --- a/python/tests/unit/core_plugins/test_http_plugin.py +++ b/python/tests/unit/core_plugins/test_http_plugin.py @@ -21,10 +21,10 @@ async def test_it_can_be_imported(): kernel = Kernel() plugin = HttpPlugin() kernel.add_plugin(plugin, "http") - assert kernel.get_plugin("http") is not None - assert kernel.get_plugin("http").name == "http" - assert kernel.get_function("http", "getAsync") is not None - assert kernel.get_function("http", "postAsync") is not None + assert kernel.get_plugin(plugin_name="http") is not None + assert kernel.get_plugin(plugin_name="http").name == "http" + assert kernel.get_function(plugin_name="http", function_name="getAsync") is not None + assert kernel.get_function(plugin_name="http", function_name="postAsync") is not None @patch("aiohttp.ClientSession.get") diff --git a/python/tests/unit/core_plugins/test_math_plugin.py b/python/tests/unit/core_plugins/test_math_plugin.py index d3a2cbfac2dd..d38b14da876f 100644 --- a/python/tests/unit/core_plugins/test_math_plugin.py +++ b/python/tests/unit/core_plugins/test_math_plugin.py @@ -15,10 +15,10 @@ def test_can_be_instantiated(): def test_can_be_imported(): kernel = Kernel() kernel.add_plugin(MathPlugin(), "math") - assert kernel.get_plugin("math") is not None - assert kernel.get_plugin("math").name == "math" - assert kernel.get_function("math", "Add") is not None - assert kernel.get_function("math", "Subtract") is not None + assert kernel.get_plugin(plugin_name="math") is not None + assert kernel.get_plugin(plugin_name="math").name == "math" + assert kernel.get_function(plugin_name="math", function_name="Add") is not None + assert kernel.get_function(plugin_name="math", function_name="Subtract") is not None @pytest.mark.parametrize( diff --git a/python/tests/unit/core_plugins/test_sessions_python_plugin.py b/python/tests/unit/core_plugins/test_sessions_python_plugin.py index 71cc0ad81997..0334bdc90f36 100644 --- a/python/tests/unit/core_plugins/test_sessions_python_plugin.py +++ b/python/tests/unit/core_plugins/test_sessions_python_plugin.py @@ -30,8 +30,8 @@ def test_validate_endpoint(aca_python_sessions_unit_test_env): def test_it_can_be_imported(kernel: Kernel, aca_python_sessions_unit_test_env): plugin = SessionsPythonTool(auth_callback=test_auth_callback) assert kernel.add_plugin(plugin=plugin, plugin_name="PythonCodeInterpreter") - assert kernel.get_plugin("PythonCodeInterpreter") is not None - assert kernel.get_plugin("PythonCodeInterpreter").name == "PythonCodeInterpreter" + assert kernel.get_plugin(plugin_name="PythonCodeInterpreter") is not None + assert kernel.get_plugin(plugin_name="PythonCodeInterpreter").name == "PythonCodeInterpreter" @pytest.mark.asyncio diff --git a/python/tests/unit/core_plugins/test_text_memory_plugin.py b/python/tests/unit/core_plugins/test_text_memory_plugin.py index 30ed63f22327..6d3b21674225 100644 --- a/python/tests/unit/core_plugins/test_text_memory_plugin.py +++ b/python/tests/unit/core_plugins/test_text_memory_plugin.py @@ -36,7 +36,7 @@ def test_can_be_instantiated(memory: SemanticTextMemory): def test_can_be_imported(kernel: Kernel, memory: SemanticTextMemory): kernel.add_plugin(TextMemoryPlugin(memory), "memory_plugin") - assert not kernel.get_function("memory_plugin", "recall").is_prompt + assert not kernel.get_function(plugin_name="memory_plugin", function_name="recall").is_prompt @mark.asyncio diff --git a/python/tests/unit/core_plugins/test_text_plugin.py b/python/tests/unit/core_plugins/test_text_plugin.py index 336495343577..02f95db1b1ff 100644 --- a/python/tests/unit/core_plugins/test_text_plugin.py +++ b/python/tests/unit/core_plugins/test_text_plugin.py @@ -10,12 +10,12 @@ def test_can_be_instantiated(): def test_can_be_imported(kernel: Kernel): kernel.add_plugin(TextPlugin(), "text_plugin") - assert not kernel.get_function("text_plugin", "trim").is_prompt + assert not kernel.get_function(plugin_name="text_plugin", function_name="trim").is_prompt def test_can_be_imported_with_name(kernel: Kernel): kernel.add_plugin(TextPlugin(), "text") - assert not kernel.get_function("text", "trim").is_prompt + assert not kernel.get_function(plugin_name="text", function_name="trim").is_prompt def test_can_trim(): diff --git a/python/tests/unit/core_plugins/test_time_plugin.py b/python/tests/unit/core_plugins/test_time_plugin.py index 6253fb002eda..7f5693df00f5 100644 --- a/python/tests/unit/core_plugins/test_time_plugin.py +++ b/python/tests/unit/core_plugins/test_time_plugin.py @@ -15,9 +15,9 @@ def test_can_be_instantiated(): def test_can_be_imported(): kernel = sk.Kernel() kernel.add_plugin(TimePlugin(), "time") - assert kernel.get_plugin("time") is not None - assert kernel.get_plugin("time").name == "time" - assert kernel.get_function("time", "now") is not None + assert kernel.get_plugin(plugin_name="time") is not None + assert kernel.get_plugin(plugin_name="time").name == "time" + assert kernel.get_function(plugin_name="time", function_name="now") is not None def test_date(): diff --git a/python/tests/unit/kernel/test_kernel.py b/python/tests/unit/kernel/test_kernel.py index 7c8c96af55ef..45f8fc9c46f7 100644 --- a/python/tests/unit/kernel/test_kernel.py +++ b/python/tests/unit/kernel/test_kernel.py @@ -279,7 +279,7 @@ async def test_add_plugin_from_openai(mock_parse_openai_manifest, kernel: Kernel enable_dynamic_payload=True, ), ) - plugin = kernel.get_plugin("TestOpenAIPlugin") + plugin = kernel.get_plugin(plugin_name="TestOpenAIPlugin") assert plugin is not None assert plugin.name == "TestOpenAIPlugin" assert plugin.functions.get("GetSecret") is not None @@ -295,7 +295,7 @@ def test_import_plugin_from_openapi(kernel: Kernel): plugin_name="TestOpenAPIPlugin", openapi_document_path=openapi_spec_file, ) - plugin = kernel.get_plugin("TestOpenAPIPlugin") + plugin = kernel.get_plugin(plugin_name="TestOpenAPIPlugin") assert plugin is not None assert plugin.name == "TestOpenAPIPlugin" assert plugin.functions.get("GetSecret") is not None diff --git a/python/tests/unit/kernel/test_register_functions.py b/python/tests/unit/kernel/test_register_functions.py index e638fec78440..3ea0a8881e4b 100644 --- a/python/tests/unit/kernel/test_register_functions.py +++ b/python/tests/unit/kernel/test_register_functions.py @@ -14,11 +14,11 @@ @pytest.mark.asyncio async def test_register_valid_native_function(kernel: Kernel, decorated_native_function: Callable): - kernel.add_function("TestPlugin", function=decorated_native_function) - registered_func = kernel.get_function("TestPlugin", "getLightStatus") + kernel.add_function(plugin_name="TestPlugin", function=decorated_native_function) + registered_func = kernel.get_function(plugin_name="TestPlugin", function_name="getLightStatus") assert isinstance(registered_func, KernelFunction) - assert kernel.get_function("TestPlugin", "getLightStatus") == registered_func + assert kernel.get_function(plugin_name="TestPlugin",function_name= "getLightStatus") == registered_func func_result = await registered_func.invoke(kernel, KernelArguments(arg1="testtest")) assert str(func_result) == "test" From 30dc3d3fd1ee332926d08dbf40cf7bc5e1a2d983 Mon Sep 17 00:00:00 2001 From: stefan521 Date: Thu, 23 May 2024 22:44:30 +0100 Subject: [PATCH 3/3] black linting --- python/samples/learn_resources/serializing_prompts.py | 3 +-- python/tests/unit/kernel/test_register_functions.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/python/samples/learn_resources/serializing_prompts.py b/python/samples/learn_resources/serializing_prompts.py index a7943d96765d..9ade73ac575c 100644 --- a/python/samples/learn_resources/serializing_prompts.py +++ b/python/samples/learn_resources/serializing_prompts.py @@ -51,8 +51,7 @@ async def main(): ) summarize_function = kernel.get_function( - plugin_name="ConversationSummaryPlugin", - function_name="SummarizeConversation" + plugin_name="ConversationSummaryPlugin", function_name="SummarizeConversation" ) # Create the history diff --git a/python/tests/unit/kernel/test_register_functions.py b/python/tests/unit/kernel/test_register_functions.py index 3ea0a8881e4b..fa04bc75af4c 100644 --- a/python/tests/unit/kernel/test_register_functions.py +++ b/python/tests/unit/kernel/test_register_functions.py @@ -18,7 +18,7 @@ async def test_register_valid_native_function(kernel: Kernel, decorated_native_f registered_func = kernel.get_function(plugin_name="TestPlugin", function_name="getLightStatus") assert isinstance(registered_func, KernelFunction) - assert kernel.get_function(plugin_name="TestPlugin",function_name= "getLightStatus") == registered_func + assert kernel.get_function(plugin_name="TestPlugin", function_name="getLightStatus") == registered_func func_result = await registered_func.invoke(kernel, KernelArguments(arg1="testtest")) assert str(func_result) == "test"