Files
semantic-kernel/python/samples/kernel-syntax-examples/sequential_planner.py
T
Evan Mattson 8b16af69c0 Python: Remove the memory tied to the kernel. Update kernel examples and notebooks. (#5143)
### Motivation and Context

As we move toward v1, it is necessary to remove the single kernel memory
attribute so that memory connectors can be handled via plugins. This
change also refactors the `import_plugin` method name to be similar with
dotnet as `import_plugin_from_object`, and introduces a simple
`kernel.invoke_prompt` method.
<!-- 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

Updates are:
- The memory connector is now passed to the `SemanticTextMemory` class,
along with the embedding generator. This allows the user to add memory
to multiple plugins/services as needed. Closes #4632
- Kernel Examples and Notebooks were updated to reflect these changes.
Now using Service Enum for all notebooks. Closes #4557
- There were planners using memory related to the kernel, to store the
functions, and many of these planners are non-existent in dotnet's v1
SDK. Python has pending work to update the planner/remove deprecated
planners before getting to v1.
- A Kernel method called `invoke_prompt` was introduced to invoke a
simple prompt, and return the `FunctionResult` when complete.
- All unit/integration tests are passing.

<!-- 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 😄
2024-02-26 17:50:23 +00:00

52 lines
1.5 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.core_plugins import (
MathPlugin,
TextPlugin,
TimePlugin,
)
from semantic_kernel.planners import SequentialPlanner
async def main():
kernel = sk.Kernel()
api_key, org_id = sk.openai_settings_from_dot_env()
service_id = "gpt-3.5"
kernel.add_service(
OpenAIChatCompletion(service_id=service_id, ai_model_id="gpt-3.5-turbo", api_key=api_key, org_id=org_id)
)
kernel.import_plugin_from_object(MathPlugin(), "math")
kernel.import_plugin_from_object(TimePlugin(), "time")
kernel.import_plugin_from_object(TextPlugin(), "text")
# create an instance of sequential planner.
planner = SequentialPlanner(service_id=service_id, kernel=kernel)
# the ask for which the sequential planner is going to find a relevant function.
ask = "What day of the week is today, all uppercase?"
# ask the sequential planner to identify a suitable function from the list of functions available.
plan = await planner.create_plan(goal=ask)
# ask the sequential planner to execute the identified function.
result = await plan.invoke()
for step in plan._steps:
print(step.description, ":", step._state.__dict__)
print("Expected Answer:")
print(result)
"""
Output:
SUNDAY
"""
if __name__ == "__main__":
import asyncio
asyncio.run(main())