SIGN IN SIGN UP

Integrate cutting-edge LLM technology quickly and easily into your apps

0 0 26 C#
Python: Migrate and update docs samples (#5235) ### Motivation and Context As of the latest code in main, and the push towards a beta package, the Python sample code needed updates. We've migrated away from using the semantic-kernel-docs repo and the Python sample code is now in the main SK repo. The SK Python docs will be updated to reflect this latest code. <!-- 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 This PR: - Migrates the Python samples to the main SK repo - The naming of the files is about 1:1 with dotnet, based on python naming conventions/using underscores - Updates the Python samples to work with the latest code in main. Closes #5234 - Fixes some other small logging quirks related to parsing blocks and printing plan steps. <!-- 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 - [X] I didn't break anyone :smile:
2024-03-01 11:31:24 -05:00
# Copyright (c) Microsoft. All rights reserved.
# <NecessaryPackages>
Python: Migrate and update docs samples (#5235) ### Motivation and Context As of the latest code in main, and the push towards a beta package, the Python sample code needed updates. We've migrated away from using the semantic-kernel-docs repo and the Python sample code is now in the main SK repo. The SK Python docs will be updated to reflect this latest code. <!-- 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 This PR: - Migrates the Python samples to the main SK repo - The naming of the files is about 1:1 with dotnet, based on python naming conventions/using underscores - Updates the Python samples to work with the latest code in main. Closes #5234 - Fixes some other small logging quirks related to parsing blocks and printing plan steps. <!-- 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 - [X] I didn't break anyone :smile:
2024-03-01 11:31:24 -05:00
import asyncio
import os
from samples.sk_service_configurator import add_service
from semantic_kernel import Kernel
Python: Migrate and update docs samples (#5235) ### Motivation and Context As of the latest code in main, and the push towards a beta package, the Python sample code needed updates. We've migrated away from using the semantic-kernel-docs repo and the Python sample code is now in the main SK repo. The SK Python docs will be updated to reflect this latest code. <!-- 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 This PR: - Migrates the Python samples to the main SK repo - The naming of the files is about 1:1 with dotnet, based on python naming conventions/using underscores - Updates the Python samples to work with the latest code in main. Closes #5234 - Fixes some other small logging quirks related to parsing blocks and printing plan steps. <!-- 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 - [X] I didn't break anyone :smile:
2024-03-01 11:31:24 -05:00
# </NecessaryPackages>
Python: Migrate and update docs samples (#5235) ### Motivation and Context As of the latest code in main, and the push towards a beta package, the Python sample code needed updates. We've migrated away from using the semantic-kernel-docs repo and the Python sample code is now in the main SK repo. The SK Python docs will be updated to reflect this latest code. <!-- 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 This PR: - Migrates the Python samples to the main SK repo - The naming of the files is about 1:1 with dotnet, based on python naming conventions/using underscores - Updates the Python samples to work with the latest code in main. Closes #5234 - Fixes some other small logging quirks related to parsing blocks and printing plan steps. <!-- 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 - [X] I didn't break anyone :smile:
2024-03-01 11:31:24 -05:00
async def main():
# Initialize the kernel
# <KernelCreation>
kernel = Kernel()
Python: Migrate and update docs samples (#5235) ### Motivation and Context As of the latest code in main, and the push towards a beta package, the Python sample code needed updates. We've migrated away from using the semantic-kernel-docs repo and the Python sample code is now in the main SK repo. The SK Python docs will be updated to reflect this latest code. <!-- 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 This PR: - Migrates the Python samples to the main SK repo - The naming of the files is about 1:1 with dotnet, based on python naming conventions/using underscores - Updates the Python samples to work with the latest code in main. Closes #5234 - Fixes some other small logging quirks related to parsing blocks and printing plan steps. <!-- 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 - [X] I didn't break anyone :smile:
2024-03-01 11:31:24 -05:00
# Add the service to the kernel
# use_chat: True to use chat completion, False to use text completion
kernel = add_service(kernel, use_chat=True)
# </KernelCreation>
# <InvokeUtcNow>
# Import the TimePlugin and add it to the kernel
from semantic_kernel.core_plugins import TimePlugin
Python: Migrate and update docs samples (#5235) ### Motivation and Context As of the latest code in main, and the push towards a beta package, the Python sample code needed updates. We've migrated away from using the semantic-kernel-docs repo and the Python sample code is now in the main SK repo. The SK Python docs will be updated to reflect this latest code. <!-- 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 This PR: - Migrates the Python samples to the main SK repo - The naming of the files is about 1:1 with dotnet, based on python naming conventions/using underscores - Updates the Python samples to work with the latest code in main. Closes #5234 - Fixes some other small logging quirks related to parsing blocks and printing plan steps. <!-- 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 - [X] I didn't break anyone :smile:
2024-03-01 11:31:24 -05:00
Python: Updated plugins (#5827) ### 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. --> The loading of plugins and functions into Kernel was not fully consistent and clear, this is now simplified. Adding a plugin can now be done in two ways: - multiple: `kernel.add_plugins[plugin1, plugin2]` - single plugin: `kernel.add_plugin(plugin)` - if the plugin here is a class, with methods that have the kernel_function decorator then that is parsed into a plugin Adding a function can now be done in three ways: - multiple: `kernel.add_functions([func1, func2]` or `kernel.add_function({'func1': func1})` - single: `kernel.add_function('plugin_name', func1)` - from a prompt: `kernel.add_function(function_name='func1', prompt='test prompt', ...)` In other words, all the different methods that were available have been simplified, these methods also return the created plugin (or updated plugin for the `add_functions` method. The `add_function` (singular) method has a parameter `return_plugin` to control whether you get the created or updated plugin, instead of the created function. **An important callout:** One big internal change that this introduces is that a function that gets added to a plugin (new or existing) is automatically copied, the metadata is deep-copied and the plugin_name in the metadata is updated, so this sequence is valid now: ```python @kernel_function(name='test') def f(...): etc func = KernelFunctionFromMethod(method=f) func2 = kernel.add_function('plugin', func) assert func != func2 ``` Also closes: #5855 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Removed KernelPluginCollection Updated KernelPlugin added from_... method to load from different sources Updated KernelFunctionFromPrompt added from_yaml and from_directory methods. Added and updated tests to 99% coverage of KernelPlugin and KernelFunctionFromPrompt ### 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 :smile:
2024-04-15 15:51:59 +02:00
time = kernel.add_plugin(TimePlugin(), "TimePlugin")
Python: Migrate and update docs samples (#5235) ### Motivation and Context As of the latest code in main, and the push towards a beta package, the Python sample code needed updates. We've migrated away from using the semantic-kernel-docs repo and the Python sample code is now in the main SK repo. The SK Python docs will be updated to reflect this latest code. <!-- 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 This PR: - Migrates the Python samples to the main SK repo - The naming of the files is about 1:1 with dotnet, based on python naming conventions/using underscores - Updates the Python samples to work with the latest code in main. Closes #5234 - Fixes some other small logging quirks related to parsing blocks and printing plan steps. <!-- 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 - [X] I didn't break anyone :smile:
2024-03-01 11:31:24 -05:00
# Invoke the Today function
current_time = await kernel.invoke(time["today"])
print(f"The current date is: {current_time}\n")
# </InvokeUtcNow>
# <InvokeShortPoem>
Python: Migrate and update docs samples (#5235) ### Motivation and Context As of the latest code in main, and the push towards a beta package, the Python sample code needed updates. We've migrated away from using the semantic-kernel-docs repo and the Python sample code is now in the main SK repo. The SK Python docs will be updated to reflect this latest code. <!-- 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 This PR: - Migrates the Python samples to the main SK repo - The naming of the files is about 1:1 with dotnet, based on python naming conventions/using underscores - Updates the Python samples to work with the latest code in main. Closes #5234 - Fixes some other small logging quirks related to parsing blocks and printing plan steps. <!-- 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 - [X] I didn't break anyone :smile:
2024-03-01 11:31:24 -05:00
# Import the WriterPlugin from the plugins directory.
script_directory = os.path.dirname(__file__)
plugins_directory = os.path.join(script_directory, "plugins")
kernel.add_plugin(parent_directory=plugins_directory, plugin_name="WriterPlugin")
Python: Migrate and update docs samples (#5235) ### Motivation and Context As of the latest code in main, and the push towards a beta package, the Python sample code needed updates. We've migrated away from using the semantic-kernel-docs repo and the Python sample code is now in the main SK repo. The SK Python docs will be updated to reflect this latest code. <!-- 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 This PR: - Migrates the Python samples to the main SK repo - The naming of the files is about 1:1 with dotnet, based on python naming conventions/using underscores - Updates the Python samples to work with the latest code in main. Closes #5234 - Fixes some other small logging quirks related to parsing blocks and printing plan steps. <!-- 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 - [X] I didn't break anyone :smile:
2024-03-01 11:31:24 -05:00
# Run the short poem function with the Kernel Argument
poem_result = await kernel.invoke(function_name="ShortPoem", plugin_name="WriterPlugin", input=str(current_time))
print(f"The poem result:\n\n{poem_result}")
# </InvokeShortPoem>
Python: Migrate and update docs samples (#5235) ### Motivation and Context As of the latest code in main, and the push towards a beta package, the Python sample code needed updates. We've migrated away from using the semantic-kernel-docs repo and the Python sample code is now in the main SK repo. The SK Python docs will be updated to reflect this latest code. <!-- 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 This PR: - Migrates the Python samples to the main SK repo - The naming of the files is about 1:1 with dotnet, based on python naming conventions/using underscores - Updates the Python samples to work with the latest code in main. Closes #5234 - Fixes some other small logging quirks related to parsing blocks and printing plan steps. <!-- 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 - [X] I didn't break anyone :smile:
2024-03-01 11:31:24 -05:00
# Run the main function
if __name__ == "__main__":
asyncio.run(main())