Skip to content

Commit

Permalink
Fix stream test case failed due to modifying GeneratorProxy (#3366)
Browse files Browse the repository at this point in the history
# Description
GeneratorProxy -> IteratorProxy:
1f3d495

![image](https://github.com/microsoft/promptflow/assets/17938940/25fd31df-3ecc-4ab5-9c44-5155f963a58a)

Please add an informative description that covers that changes made by
the pull request and link all relevant issues.

# 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.
  • Loading branch information
lalala123123 authored Jun 6, 2024
1 parent ab3f142 commit 3f792e7
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/promptflow-devkit/promptflow/_sdk/_orchestrator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pathlib import Path
from time import sleep
from types import GeneratorType
from typing import Any, Dict, List
from typing import Any, Dict, List, Union

import psutil
import pydash
Expand Down Expand Up @@ -52,6 +52,7 @@
from promptflow.contracts.flow import Flow as ExecutableFlow
from promptflow.core._utils import get_used_connection_names_from_dict, update_dict_value_with_connections
from promptflow.exceptions import UserErrorException
from promptflow.tracing.contracts.iterator_proxy import IteratorProxy

logger = get_cli_sdk_logger()

Expand Down Expand Up @@ -433,7 +434,7 @@ def show_node_log_and_output(node_run_infos, show_node_output, generator_record)


def print_chat_output(output, generator_record, *, generator_key: str):
if isinstance(output, GeneratorType):
if isinstance(output, (IteratorProxy, GeneratorType)):
for event in resolve_generator_output_with_cache(output, generator_record, generator_key=generator_key):
print(event, end="")
# For better animation effects
Expand All @@ -445,34 +446,31 @@ def print_chat_output(output, generator_record, *, generator_key: str):


def resolve_generator_output_with_cache(
output: GeneratorType, generator_record: Dict[str, Any], *, generator_key: str
output: Union[GeneratorType, IteratorProxy], generator_record: Dict[str, Any], *, generator_key: str
) -> List[str]:
"""Get the output of a generator. If the generator has been recorded, return the recorded result. Otherwise, record
the result and return it.
We use a separate generator_key instead of the output itself as the key in the generator_record in case the output
is not a valid dict key in some cases.
:param output: The generator to get the output from.
:type output: GeneratorType
:type output: Union[GeneratorType, IteratorProxy]
:param generator_record: The record of the generator.
:type generator_record: dict
:param generator_key: The key of the generator in the record, need to be unique.
:type generator_key: str
:return: The output of the generator.
:rtype: str
"""
if isinstance(output, GeneratorType):
if isinstance(output, (GeneratorType, IteratorProxy)):
if generator_key in generator_record:
if hasattr(generator_record[generator_key], "items"):
output = iter(generator_record[generator_key].items)
else:
output = iter(generator_record[generator_key])
else:
if hasattr(output.gi_frame.f_locals, "proxy"):
generator_record[generator_key] = output.gi_frame.f_locals["proxy"]
else:
generator_record[generator_key] = list(output)
output = generator_record[generator_key]
generator_record[generator_key] = list(output)
output = generator_record[generator_key]
return output


Expand Down

0 comments on commit 3f792e7

Please sign in to comment.