Skip to content

Commit 22e190a

Browse files
author
Xiaodi Bi
committed
move chat group constants to orchestrator
1 parent 6dd4ca3 commit 22e190a

File tree

6 files changed

+37
-53
lines changed

6 files changed

+37
-53
lines changed

src/promptflow-core/promptflow/_constants.py

-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@
3030

3131
CHAT_HISTORY = "chat_history"
3232

33-
# chat group reserved
34-
CONVERSATION_HISTORY_EXPRESSION_KEY = "parent.conversation_history"
35-
CONVERSATION_HISTORY_EXPRESSION = f"${{{CONVERSATION_HISTORY_EXPRESSION_KEY}}}"
36-
CONVERSATION_HISTORY_OUTPUT_KEY = "conversation_history"
37-
CHAT_ROLE_KEY = "role"
38-
3933
# Tool meta info
4034
ICON_DARK = "icon_dark"
4135
ICON_LIGHT = "icon_light"

src/promptflow-devkit/promptflow/_orchestrator/_chat_group_orchestrator.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
from promptflow._utils.execution_utils import apply_default_value_for_input
1212
from promptflow._proxy._proxy_factory import ProxyFactory
1313
from promptflow._utils.logger_utils import bulk_logger
14-
from promptflow._constants import CONVERSATION_HISTORY_EXPRESSION, CONVERSATION_HISTORY_OUTPUT_KEY, CHAT_ROLE_KEY
14+
from promptflow._orchestrator._constants import (
15+
CONVERSATION_HISTORY_EXPRESSION,
16+
CONVERSATION_HISTORY_OUTPUT_KEY,
17+
CHAT_ROLE_KEY
18+
)
1519
from promptflow._orchestrator._errors import (
1620
InvalidChatRoleCount,
1721
MissingConversationHistoryExpression,
@@ -84,7 +88,7 @@ def _create_executor_proxy(self, **kwargs) -> List[AbstractExecutorProxy]:
8488
for chat_role in self._chat_group_roles:
8589
executor_proxy = executor_proxy_factory.create_executor_proxy(
8690
flow_file=chat_role._flow_file,
87-
working_dir=chat_role.working_dir,
91+
working_dir=chat_role._working_dir,
8892
connections=chat_role._connections,
8993
storage=self._storage,
9094
language=chat_role.check_language_from_yaml(),
@@ -97,7 +101,7 @@ def _create_executor_proxy(self, **kwargs) -> List[AbstractExecutorProxy]:
97101
init_kwargs=chat_role._init_kwargs,
98102
**kwargs
99103
)
100-
bulk_logger.info(f"Created executor proxy for role:{chat_role.role}. name: {chat_role.name}")
104+
bulk_logger.info(f"Created executor proxy for role:{chat_role.role}. name: {chat_role._name}")
101105
executor_proxy_list.append(executor_proxy)
102106
return executor_proxy_list
103107

@@ -145,17 +149,17 @@ async def _schedule_line_runs(
145149
if conversation_history_key is None:
146150
bulk_logger.error(
147151
f"Cannot find conversation expression mapping for "
148-
f"chat role: {chat_role.role}. name: {chat_role.name}"
152+
f"chat role: {chat_role.role}. name: {chat_role._name}"
149153
)
150154
message = (
151155
f"Cannot find conversation expression mapping for "
152-
f"chat role: {chat_role.role}. name: {chat_role.name} "
156+
f"chat role: {chat_role.role}. name: {chat_role._name} "
153157
f"Please use define {CONVERSATION_HISTORY_EXPRESSION} for a flow input."
154158
)
155159
raise MissingConversationHistoryExpression(message=message)
156160
chat_role_input[conversation_history_key] = conversation_history
157161
bulk_logger.info(
158-
f"Start to execute turn {turn}. role: {chat_role.role}. name: {chat_role.name}"
162+
f"Start to execute turn {turn}. role: {chat_role.role}. name: {chat_role._name}"
159163
)
160164

161165
current_line_result = await executor_proxy.exec_line_async(chat_role_input, line_index, run_id)
@@ -168,13 +172,13 @@ async def _schedule_line_runs(
168172
aggregation_inputs)
169173
bulk_logger.info(
170174
f"Finish process line result for "
171-
f"line number: {line_index}, turn:{turn}. role:{chat_role.role}, name: {chat_role.name}"
175+
f"line number: {line_index}, turn:{turn}. role:{chat_role.role}, name: {chat_role._name}"
172176
)
173177

174-
if any(value == chat_role.stop_signal for value in current_line_result.output.values()):
178+
if any(value == chat_role._stop_signal for value in current_line_result.output.values()):
175179
bulk_logger.info(
176180
f"Stop chat since current turn align with stop signal. "
177-
f"line number: {line_index}, turn:{turn}. role:{chat_role.role}, name: {chat_role.name}"
181+
f"line number: {line_index}, turn:{turn}. role:{chat_role.role}, name: {chat_role._name}"
178182
)
179183
break
180184

@@ -222,19 +226,19 @@ def _process_batch_inputs(self, inputs: Dict[str, Any]):
222226
if len(conversation_history_mapping) == 0:
223227
bulk_logger.error(
224228
f"Cannot find conversation expression mapping for "
225-
f"chat role: {chat_role.role}. name: {chat_role.name}"
229+
f"chat role: {chat_role.role}. name: {chat_role._name}"
226230
)
227231
message = (
228232
f"Cannot find conversation expression mapping for "
229-
f"chat role: {chat_role.role}. name: {chat_role.name} "
233+
f"chat role: {chat_role.role}. name: {chat_role._name} "
230234
f"Please mapping {CONVERSATION_HISTORY_EXPRESSION} for a flow input."
231235
)
232236
raise MissingConversationHistoryExpression(message=message)
233237

234238
if len(conversation_history_mapping) > 1:
235239
bulk_logger.error(f"Multiple inputs mapping of {CONVERSATION_HISTORY_EXPRESSION}")
236240
message = (
237-
f"chat role: {chat_role.role}. name: {chat_role.name} "
241+
f"chat role: {chat_role.role}. name: {chat_role._name} "
238242
f"only accepts 1 inputs mapping for {CONVERSATION_HISTORY_EXPRESSION}"
239243
)
240244
raise MultipleConversationHistoryInputsMapping(message=message)
@@ -245,7 +249,7 @@ def _process_batch_inputs(self, inputs: Dict[str, Any]):
245249
}
246250

247251
batch_input_processor = BatchInputsProcessor(
248-
chat_role.working_dir,
252+
chat_role._working_dir,
249253
chat_role._flow_definition.inputs,
250254
self._max_lines_count)
251255
batch_input = batch_input_processor._process_batch_inputs_line(inputs, cleaned_inputs_mapping)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
5+
# chat group reserved
6+
CONVERSATION_HISTORY_EXPRESSION_KEY = "parent.conversation_history"
7+
CONVERSATION_HISTORY_EXPRESSION = f"${{{CONVERSATION_HISTORY_EXPRESSION_KEY}}}"
8+
CONVERSATION_HISTORY_OUTPUT_KEY = "conversation_history"
9+
CHAT_ROLE_KEY = "role"

src/promptflow-devkit/promptflow/_sdk/entities/_chat_group/_chat_role.py

+10-33
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,8 @@ def __init__(self,
4848
init_kwargs: Optional[Dict[str, Any]] = None,
4949
**kwargs):
5050
self._role = role
51-
if working_dir is None:
52-
flow_path = Path(flow).resolve()
53-
else:
54-
flow_path = (working_dir / Path(flow)).resolve()
55-
self._flow, self._flow_object = self._validate_flow(flow_path)
56-
self._inputs, self._outputs = self._build_role_io(flow_path, inputs)
51+
self._flow, self._flow_object = self._validate_flow(flow, working_dir)
52+
self._inputs, self._outputs = self._build_role_io(self._flow, inputs)
5753

5854
# Below properties are used for cloud chat group. It may have some duplicate with above ones
5955
# Will evaluate and refine in the second step.
@@ -75,7 +71,7 @@ def __init__(self,
7571
self._line_timeout_sec = line_timeout_sec
7672
self._init_kwargs = init_kwargs
7773

78-
logger.info(f"Created chat role {self.role!r} with flow {flow_path.as_posix()!r}")
74+
logger.info(f"Created chat role {self.role!r} with flow {self._flow.as_posix()!r}")
7975

8076
@property
8177
def role(self):
@@ -92,32 +88,13 @@ def outputs(self):
9288
"""Outputs of the chat role"""
9389
return self._outputs
9490

95-
@property
96-
def name(self):
97-
"""Name of the chat role"""
98-
return self._name
99-
100-
@property
101-
def stop_signal(self):
102-
"""Stop signal of the role"""
103-
return self._stop_signal
104-
105-
@property
106-
def flow(self):
107-
"""Flow definition of the chat role"""
108-
return self._flow_definition
109-
110-
@property
111-
def working_dir(self):
112-
return self._working_dir
113-
114-
@property
115-
def output_dir(self):
116-
return self._output_dir
117-
118-
def _validate_flow(self, flow_path: Path):
91+
def _validate_flow(self, flow: Union[str, PathLike], working_dir: Optional[Path] = None):
11992
"""Validate flow"""
120-
logger.debug(f"Validating chat role flow source {flow_path!r}")
93+
logger.debug(f"Validating chat role flow source {flow!r}")
94+
if working_dir is None:
95+
flow_path = Path(flow).resolve()
96+
else:
97+
flow_path = (working_dir / Path(flow)).resolve()
12198
try:
12299
flow_object = load_flow(flow_path)
123100
except Exception as e:
@@ -188,7 +165,7 @@ def invoke(self, *args, **kwargs):
188165
return result
189166

190167
def check_language_from_yaml(self):
191-
flow_file = self._working_dir / self._flow_file if self.working_dir else self._flow_file
168+
flow_file = self._working_dir / self._flow_file if self._working_dir else self._flow_file
192169
if flow_file.suffix.lower() == ".dll":
193170
return FlowLanguage.CSharp
194171
with open(flow_file, "r", encoding="utf-8") as fin:

src/promptflow/tests/sdk_cli_test/unittests/test_chat_group.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ def test_chat_role_flow_dag_file(self):
6868
name="copilot",
6969
working_dir=FLOWS_DIR / "chat_group_copilot",
7070
)
71-
assert copilot.flow is not None
71+
assert copilot._flow_definition is not None

0 commit comments

Comments
 (0)