Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Refactoring. Use get_function and get_plugin. #6382

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/samples/concepts/search/bing_plugin_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(plugin_name=search_plugin_name, function_name="search")
result = await kernel.invoke(function, query=question)

print(question)
Expand Down
4 changes: 3 additions & 1 deletion python/samples/learn_resources/serializing_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ async def main():
plugin_name="ConversationSummaryPlugin",
)

summarize_function = kernel.plugins["ConversationSummaryPlugin"]["SummarizeConversation"]
summarize_function = kernel.get_function(
plugin_name="ConversationSummaryPlugin", function_name="SummarizeConversation"
)

# Create the history
history = ChatHistory()
Expand Down
1 change: 0 additions & 1 deletion python/semantic_kernel/connectors/ai/open_ai/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
8 changes: 4 additions & 4 deletions python/tests/unit/core_plugins/test_http_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(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")
Expand Down
8 changes: 4 additions & 4 deletions python/tests/unit/core_plugins/test_math_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(plugin_name="PythonCodeInterpreter") is not None
assert kernel.get_plugin(plugin_name="PythonCodeInterpreter").name == "PythonCodeInterpreter"


@pytest.mark.asyncio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(plugin_name="memory_plugin", function_name="recall").is_prompt


@mark.asyncio
Expand Down
4 changes: 2 additions & 2 deletions python/tests/unit/core_plugins/test_text_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(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.plugins["text"]["trim"].is_prompt
assert not kernel.get_function(plugin_name="text", function_name="trim").is_prompt


def test_can_trim():
Expand Down
6 changes: 3 additions & 3 deletions python/tests/unit/core_plugins/test_time_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(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():
Expand Down
4 changes: 2 additions & 2 deletions python/tests/unit/kernel/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(plugin_name="TestOpenAIPlugin")
assert plugin is not None
assert plugin.name == "TestOpenAIPlugin"
assert plugin.functions.get("GetSecret") is not None
Expand All @@ -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(plugin_name="TestOpenAPIPlugin")
assert plugin is not None
assert plugin.name == "TestOpenAPIPlugin"
assert plugin.functions.get("GetSecret") is not None
Expand Down
6 changes: 3 additions & 3 deletions python/tests/unit/kernel/test_register_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.plugins["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"

Expand Down
Loading