mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### 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 `invoke_stream_async` method on a native `SKFunction` is not
functional. The following sample code should work:
```python
import asyncio
import semantic_kernel as sk
from semantic_kernel.skill_definition import sk_function
class StreamNativeSkill:
@sk_function(name="stream")
async def stream(self, input: str) -> None: # This should also work: -> AsyncGenerator
for i in range(5):
await asyncio.sleep(1)
yield input + str(i)
async def main():
kernel = sk.Kernel()
skill = kernel.import_skill(StreamNativeSkill(), "stream_skill")
native_stream_func = skill["stream"]
async for chunk in native_stream_func.invoke_stream_async("HELLO!"):
print(chunk)
if __name__ == "__main__":
asyncio.run(main())
```
And should output:
```
HELLO!0
HELLO!1
HELLO!2
HELLO!3
HELLO!4
```
But now it only shows:
```
HELLO!
```
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Add four new `DelegateTypes` for native asynchronous generator functions
for different types of parameters:
- OutAsyncGenerator: No parameter, has `yield`
- InStringOutAsyncGenerator: One `str` parameter, has `yield`
- InContextOutAsyncGenerator: One `SKContext` parameter, has `yield`
- InStringAndContextOutAsyncGenerator: Both `str` and `SKContext`
parameters, has `yield`
Along with these four new types, add four new `DelegateInference`
functions to detect the types:
- infer_out_async_generator
- infer_in_string_out_async_generator
- infer_in_context_out_async_generator
- infer_in_string_and_context_out_async_generator
And because the async generator functions needs to use
`inspect.isasyncgenfunction` function to check instead of
`inspect.iscoroutinefunction`, add another parameter `is_asyncgenfunc`
to all the infer functions.
And finally, fix `SKFunction._invoke_native_stream_async` so that it can
correctly iterating from an async generator function.
### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [ ] The code builds clean without any errors or warnings
- [ ] 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
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄