Skip to content

Commit 304a3a5

Browse files
authored
[flow test] Add flow chat/test/streamlit_chat in telemetry (#975)
# Description 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.
1 parent e8861dd commit 304a3a5

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

src/promptflow/promptflow/_cli/_pf/_flow.py

+7-40
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import json
88
import logging
99
import os
10-
import sys
1110
import tempfile
1211
import webbrowser
1312
from pathlib import Path
@@ -39,8 +38,6 @@
3938
from promptflow._cli._utils import _copy_to_flow, activate_action, confirm, inject_sys_path, list_of_dict_to_dict
4039
from promptflow._sdk._constants import LOGGER_NAME, PROMPT_FLOW_DIR_NAME, ConnectionProvider
4140
from promptflow._sdk._pf_client import PFClient
42-
from promptflow._sdk._utils import dump_flow_result
43-
from promptflow.exceptions import UserErrorException
4441

4542
DEFAULT_CONNECTION = "open_ai_connection"
4643
DEFAULT_DEPLOYMENT = "gpt-35-turbo"
@@ -364,8 +361,6 @@ def _init_flow_by_template(flow_name, flow_type, overwrite=False, connection=Non
364361
@exception_handler("Flow test")
365362
def test_flow(args):
366363
from promptflow._sdk._load_functions import load_flow
367-
from promptflow._sdk._utils import parse_variant
368-
from promptflow._sdk.operations._test_submitter import TestSubmitter
369364

370365
config = list_of_dict_to_dict(args.config)
371366
pf_client = PFClient(config=config)
@@ -386,14 +381,6 @@ def test_flow(args):
386381

387382
if args.multi_modal or args.ui:
388383
with tempfile.TemporaryDirectory() as temp_dir:
389-
try:
390-
import bs4 # noqa: F401
391-
import streamlit_quill # noqa: F401
392-
from streamlit.web import cli as st_cli
393-
except ImportError as ex:
394-
raise UserErrorException(
395-
f"Please try 'pip install promptflow[executable]' to install dependency, {ex.msg}."
396-
)
397384
flow = load_flow(args.flow)
398385

399386
script_path = [
@@ -407,16 +394,8 @@ def test_flow(args):
407394
flow_dag_path=flow.flow_dag_path,
408395
connection_provider=pf_client._ensure_connection_provider(),
409396
).generate_to_file(script)
410-
411-
sys.argv = [
412-
"streamlit",
413-
"run",
414-
os.path.join(temp_dir, "main.py"),
415-
"--global.developmentMode=false",
416-
"--client.toolbarMode=viewer",
417-
"--browser.gatherUsageStats=false",
418-
]
419-
st_cli.main()
397+
main_script_path = os.path.join(temp_dir, "main.py")
398+
pf_client.flows._chat_with_ui(script=main_script_path)
420399
else:
421400
if args.interactive:
422401
pf_client.flows._chat(
@@ -427,33 +406,21 @@ def test_flow(args):
427406
show_step_output=args.verbose,
428407
)
429408
else:
430-
result = pf_client.flows._test(
409+
result = pf_client.flows.test(
431410
flow=args.flow,
432411
inputs=inputs,
433412
environment_variables=environment_variables,
434413
variant=args.variant,
435414
node=args.node,
436415
allow_generator_output=False,
437416
stream_output=False,
417+
dump_test_result=True,
438418
)
439-
# Dump flow/node test info
440-
flow = load_flow(args.flow)
441-
if args.node:
442-
dump_flow_result(flow_folder=flow.code, node_result=result, prefix=f"flow-{args.node}.node")
443-
else:
444-
if args.variant:
445-
tuning_node, node_variant = parse_variant(args.variant)
446-
prefix = f"flow-{tuning_node}-{node_variant}"
447-
else:
448-
prefix = "flow"
449-
dump_flow_result(flow_folder=flow.code, flow_result=result, prefix=prefix)
450-
451-
TestSubmitter._raise_error_when_test_failed(result, show_trace=args.node is not None)
452419
# Print flow/node test result
453-
if isinstance(result.output, dict):
454-
print(json.dumps(result.output, indent=4, ensure_ascii=False))
420+
if isinstance(result, dict):
421+
print(json.dumps(result, indent=4, ensure_ascii=False))
455422
else:
456-
print(result.output)
423+
print(result)
457424

458425

459426
def serve_flow(args):

src/promptflow/promptflow/_sdk/operations/_flow_operations.py

+38
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import os
88
import subprocess
9+
import sys
910
from importlib.metadata import version
1011
from os import PathLike
1112
from pathlib import Path
@@ -23,6 +24,7 @@
2324
generate_flow_tools_json,
2425
generate_random_string,
2526
parse_variant,
27+
dump_flow_result,
2628
)
2729
from promptflow._sdk.entities._validation import ValidationResult
2830
from promptflow._sdk.operations._run_submitter import remove_additional_includes, variant_overwrite_context
@@ -71,6 +73,21 @@ def test(
7173
result = self._test(
7274
flow=flow, inputs=inputs, variant=variant, node=node, environment_variables=environment_variables, **kwargs
7375
)
76+
77+
dump_test_result = kwargs.get("dump_test_result", False)
78+
if dump_test_result:
79+
# Dump flow/node test info
80+
flow = load_flow(flow)
81+
if node:
82+
dump_flow_result(flow_folder=flow.code, node_result=result, prefix=f"flow-{node}.node")
83+
else:
84+
if variant:
85+
tuning_node, node_variant = parse_variant(variant)
86+
prefix = f"flow-{tuning_node}-{node_variant}"
87+
else:
88+
prefix = "flow"
89+
dump_flow_result(flow_folder=flow.code, flow_result=result, prefix=prefix)
90+
7491
TestSubmitter._raise_error_when_test_failed(result, show_trace=node is not None)
7592
return result.output
7693

@@ -162,6 +179,7 @@ def _is_chat_flow(flow):
162179
error_msg = "chat_history is required in the inputs of chat flow"
163180
return is_chat_flow, chat_history_input_name, error_msg
164181

182+
@monitor_operation(activity_name="pf.flows._chat", activity_type=ActivityType.INTERNALCALL)
165183
def _chat(
166184
self,
167185
flow,
@@ -202,6 +220,26 @@ def _chat(
202220
show_step_output=kwargs.get("show_step_output", False),
203221
)
204222

223+
@monitor_operation(activity_name="pf.flows._chat_with_ui", activity_type=ActivityType.INTERNALCALL)
224+
def _chat_with_ui(self, script):
225+
try:
226+
import bs4 # noqa: F401
227+
import streamlit_quill # noqa: F401
228+
from streamlit.web import cli as st_cli
229+
except ImportError as ex:
230+
raise UserErrorException(
231+
f"Please try 'pip install promptflow[executable]' to install dependency, {ex.msg}."
232+
)
233+
sys.argv = [
234+
"streamlit",
235+
"run",
236+
script,
237+
"--global.developmentMode=false",
238+
"--client.toolbarMode=viewer",
239+
"--browser.gatherUsageStats=false",
240+
]
241+
st_cli.main()
242+
205243
def _build_environment_config(self, flow_dag_path: Path):
206244
flow_info = yaml.safe_load(flow_dag_path.read_text())
207245
# standard env object:

0 commit comments

Comments
 (0)