Skip to content

Commit

Permalink
Python wait core skill (microsoft#1373)
Browse files Browse the repository at this point in the history
### 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? Improved parity between .Net and Python
implementations
2. What problem does it solve? Allow for waiting in Python
implementation
  3. What scenario does it contribute to? Parity
  4. If it fixes an open issue, please link to the issue here.
-->


### Description
Pretty simple allows for waiting X seconds in a thread


### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [x] The code builds clean without any errors or warnings
- [x] The PR follows SK Contribution Guidelines
(https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
- [x] The code follows the .NET coding conventions
(https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions)
verified with `dotnet format`
- [x] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
  • Loading branch information
cschadewitz authored and name committed Jul 5, 2023
1 parent 276b651 commit 9f0ed87
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
23 changes: 23 additions & 0 deletions python/semantic_kernel/core_skills/wait_skill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import asyncio

from semantic_kernel.skill_definition import sk_function


class WaitSkill:
"""
WaitSkill provides a set of functions to wait for a certain amount of time.
Usage:
kernel.import_skill("wait", WaitSkill());
Examples:
{{wait.seconds 5}} => Wait for 5 seconds
"""

@sk_function(description="Wait for a certain number of seconds.")
async def wait(self, seconds_text: str):
try:
seconds = max(float(seconds_text), 0)
except ValueError:
raise ValueError("seconds text must be a number")
await asyncio.sleep(seconds)
55 changes: 55 additions & 0 deletions python/tests/unit/core_skills/test_wait_skill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest

from semantic_kernel.core_skills.wait_skill import WaitSkill

test_data_good = [
"0",
"1",
"2.1",
"0.1",
"0.01",
"0.001",
"0.0001",
"-0.0001",
"-10000",
]

test_data_bad = [
"$0",
"one hundred",
"20..,,2,1",
".2,2.1",
"0.1.0",
"00-099",
"¹²¹",
"2²",
"zero",
"-100 seconds",
"1 second",
]


def test_can_be_instantiated():
skill = WaitSkill()
assert skill is not None


@pytest.mark.asyncio
@pytest.mark.parametrize("wait_time", test_data_good)
async def test_wait_valid_params(wait_time):
skill = WaitSkill()

await skill.wait(wait_time)

assert True


@pytest.mark.asyncio
@pytest.mark.parametrize("wait_time", test_data_bad)
async def test_wait_invalid_params(wait_time):
skill = WaitSkill()

with pytest.raises(ValueError) as exc_info:
await skill.wait("wait_time")

assert exc_info.value.args[0] == "seconds text must be a number"

0 comments on commit 9f0ed87

Please sign in to comment.