Files
semantic-kernel/python/samples/kernel-syntax-examples/sequential_planner.py
484b39d144 Python: Add sequential planner to python (#1916)
### 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.
-->
fixes #1915 

### Description
<!-- 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: -->
- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows SK Contribution Guidelines
(https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
- [ ] The code follows the .NET coding conventions
(https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions)
verified with `dotnet format`
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄

---------

Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Shawn Callegari <[email protected]>
2023-07-28 00:08:31 +00:00

48 lines
1.4 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_skills import FileIOSkill, MathSkill, TextSkill, TimeSkill
from semantic_kernel.planning import SequentialPlanner
async def main():
kernel = sk.Kernel()
api_key, org_id = sk.openai_settings_from_dot_env()
kernel.add_chat_service(
"gpt-3.5", OpenAIChatCompletion("gpt-3.5-turbo", api_key, org_id)
)
kernel.import_skill(MathSkill(), "math")
kernel.import_skill(FileIOSkill(), "fileIO")
kernel.import_skill(TimeSkill(), "time")
kernel.import_skill(TextSkill(), "text")
# create an instance of sequential planner.
planner = SequentialPlanner(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_async(goal=ask)
# ask the sequential planner to execute the identified function.
result = await plan.invoke_async()
for step in plan._steps:
print(step.description, ":", step._state.__dict__)
print("Expected Answer:")
print(result)
"""
Output:
1100
"""
if __name__ == "__main__":
import asyncio
asyncio.run(main())