Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Executor] Refine executor logic to support async generator in flow t…
…est (#3083) # Description Refine exec_line logic to support async generator in flow test. This pull request primarily focuses on improving the handling of generators and asynchronous generators in the `promptflow` package. The changes ensure that the code can handle both synchronous and asynchronous generators, improve the handling of generator outputs, and refactor the way nodes are executed in the flow. Here are the key changes: Improved handling of generators and asynchronous generators: * [`src/promptflow-core/promptflow/_core/run_tracker.py`](diffhunk://#diff-a5027d19a24cb28a68ead16dfe6c54492c78d6e0e7640e80533928808cdb3422R6-L9): The `inspect` module was imported to replace the use of `GeneratorType` for checking if a value is a generator. The method `update_and_persist_generator_node_runs` was introduced to replace `persist_selected_node_runs`, and it now updates the output of the node run with the output in the trace before persisting it. [[1]](diffhunk://#diff-a5027d19a24cb28a68ead16dfe6c54492c78d6e0e7640e80533928808cdb3422R6-L9) [[2]](diffhunk://#diff-a5027d19a24cb28a68ead16dfe6c54492c78d6e0e7640e80533928808cdb3422L299-R299) [[3]](diffhunk://#diff-a5027d19a24cb28a68ead16dfe6c54492c78d6e0e7640e80533928808cdb3422L433-R450) * [`src/promptflow-core/promptflow/_utils/run_tracker_utils.py`](diffhunk://#diff-cc2845177424c6393b16b91ff5a7753eaf73aa52c4c52c53c8f83eb68746815cR4-R7): The `inspect` module was imported, and the method `_deep_copy_and_extract_items_from_generator_proxy` was updated to handle `AsyncGeneratorProxy` and to convert generators to strings to avoid deepcopy errors. [[1]](diffhunk://#diff-cc2845177424c6393b16b91ff5a7753eaf73aa52c4c52c53c8f83eb68746815cR4-R7) [[2]](diffhunk://#diff-cc2845177424c6393b16b91ff5a7753eaf73aa52c4c52c53c8f83eb68746815cL21-R25) Refactoring of node execution: * [`src/promptflow-core/promptflow/executor/flow_executor.py`](diffhunk://#diff-bec06607cb28fd791b8ed11bb488979344ca342be5f1c67ba6dd663d5e12240fR63): The `ThreadPoolExecutorWithContext` was imported from `promptflow.tracing`. In the `exec_line` method, a check was added to use `exec_line_async` when the tools are async. The `exec_line_async` method was updated to include a `line_timeout_sec` parameter. The `_exec_inner_with_trace_async` method was updated to use `_stringify_generator_output_async` to handle async generator output. The `_exec_post_process` method was updated to use `update_and_persist_generator_node_runs` instead of `persist_selected_node_runs`. The `_should_use_async` method was updated to check if any tool is async. The `_traverse_nodes_async` method was updated to use an async scheduler. The methods `_merge_async_generator`, `_stringify_generator_output_async`, and `_merge_generator` were added to handle generator outputs. The `_submit_to_scheduler` method was updated to only use the thread pool mode. [[1]](diffhunk://#diff-bec06607cb28fd791b8ed11bb488979344ca342be5f1c67ba6dd663d5e12240fR63) [[2]](diffhunk://#diff-bec06607cb28fd791b8ed11bb488979344ca342be5f1c67ba6dd663d5e12240fR693-R699) [[3]](diffhunk://#diff-bec06607cb28fd791b8ed11bb488979344ca342be5f1c67ba6dd663d5e12240fR731) [[4]](diffhunk://#diff-bec06607cb28fd791b8ed11bb488979344ca342be5f1c67ba6dd663d5e12240fR751-R752) [[5]](diffhunk://#diff-bec06607cb28fd791b8ed11bb488979344ca342be5f1c67ba6dd663d5e12240fL869-R880) [[6]](diffhunk://#diff-bec06607cb28fd791b8ed11bb488979344ca342be5f1c67ba6dd663d5e12240fL913-R926) [[7]](diffhunk://#diff-bec06607cb28fd791b8ed11bb488979344ca342be5f1c67ba6dd663d5e12240fR1158-R1212) [[8]](diffhunk://#diff-bec06607cb28fd791b8ed11bb488979344ca342be5f1c67ba6dd663d5e12240fL1186-L1190) # All Promptflow Contribution checklist: - [ ] **The pull request does not introduce [breaking changes].** - [ ] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [ ] **I have read the [contribution guidelines](../CONTRIBUTING.md).** - [ ] **Create an issue and link to the pull request to get dedicated review from promptflow team. Learn more: [suggested workflow](../CONTRIBUTING.md#suggested-workflow).** ## General Guidelines and Best Practices - [ ] Title of the pull request is clear and informative. - [ ] There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, [see this page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md). ### Testing Guidelines - [ ] Pull request includes test coverage for the included changes. --------- Co-authored-by: Heyi <[email protected]>
- Loading branch information