Repository navigation
Expand file tree
/
Copy pathtest_functions.py
More file actions
221 lines (197 loc) · 7.34 KB
/
Copy pathtest_functions.py
File metadata and controls
221 lines (197 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# -*- coding: utf-8 -*-
"""
Environment Variables Configuration:
--------------------------------------------------------------
1. DASHSCOPE_API_KEY (Required)
- Sets the authentication key for DashScope API access
- Format: API key string (e.g., "ds-1234567890abcdef")
2. LOG_LEVEL (Optional, default: 'info')
- Controls logging verbosity
- Allowed values: 'debug', 'info', 'warning', 'error', 'critical'
3. FC_PYPI_LIB (Required)
- Specifies local/alternative dashscope package installations
- Accepts space-separated list of wheel paths
- Default: Uses official PyPI 'dashscope' package
- Example: "dashscope-1.25.16-py3-none-any.whl"
Functions Workflow (main_functions):
--------------------------------------------------------------
register_functions():
- Registers and initializes function components for RL workflow
- Parameters:
- functions: List of AgenticRLFunctionComponent configurations
- lazy_load: If False, immediately loads components (default: False)
- Returns tuple of registered component IDs (rollout/reward/group_reward
entities and instances)
test_functions():
- Validates registered function components
- Parameters:
- instance_id: Target component instance ID
- type: FunctionType (ROLLOUT/REWARD/GROUP_REWARD)
- input_data: Test input payload
- Verifies component outputs against expected results
"""
import json
from dashscope.finetune.reinforcement import (
logger,
TaskStatus,
FunctionType,
AgenticRLFunctionComponent,
FunctionComponentModel,
)
from dashscope.finetune.agentic_rl import AgenticRL
async def main_functions():
"""Main execution workflow"""
try:
logger.info("Starting main tests functions")
client = AgenticRL()
functions = [
# rollout-only
AgenticRLFunctionComponent(
type=FunctionType.ROLLOUT,
fcmodel=FunctionComponentModel(
classpath="functions.rollout.rollout_only"
".DemoRolloutProcessor", # noqa: E501
),
),
# reward
AgenticRLFunctionComponent(
type=FunctionType.REWARD,
fcmodel=FunctionComponentModel(
classpath="functions.reward.reward.DemoRewardProcessor",
),
),
# reward-decorator
AgenticRLFunctionComponent(
type=FunctionType.REWARD,
fcmodel=FunctionComponentModel(
classpath="functions/reward/reward_decorator.py:SafetyProcessor", # noqa: E501 # pylint: disable=line-too-long
),
),
# group-reward
AgenticRLFunctionComponent(
type=FunctionType.GROUP_REWARD,
fcmodel=FunctionComponentModel(
classpath="functions.reward.group_reward.DemoGroupRewardProcessor", # noqa: E501 # pylint: disable=line-too-long
),
),
]
# Register functions
(
rollout_entity_ids,
reward_entity_ids,
group_reward_entity_ids,
rollout_instance_ids,
reward_instance_ids,
group_reward_instance_ids,
) = await client.register_functions(
functions=functions,
lazy_load=False,
) # register & load functions
logger.info(
f"agentic rl register functions: {rollout_entity_ids=},"
f" {reward_entity_ids=}, {group_reward_entity_ids=},"
f" {rollout_instance_ids=}, {reward_instance_ids=},"
f" {group_reward_instance_ids=}",
)
with open(
"./resources/rollout_input.json",
"r",
encoding="utf-8",
) as file:
json_data = json.load(file)
rollout_input = json_data
with open(
"./resources/reward_input.json",
"r",
encoding="utf-8",
) as file:
json_data = json.load(file)
reward_input = json_data
with open(
"./resources/reward_decorator_input.json",
"r",
encoding="utf-8",
) as file:
json_data = json.load(file)
reward_decorator_input = json_data
with open(
"./resources/group_reward_input.json",
"r",
encoding="utf-8",
) as file:
json_data = json.load(file)
group_reward_input = json_data
# Testing rollout-only functions
if rollout_instance_ids and rollout_instance_ids[0]:
result = await AgenticRL.test_functions(
instance_id=rollout_instance_ids[0],
functype=FunctionType.ROLLOUT,
input_data=rollout_input,
)
logger.info(
f"agentic rl test rollout: {rollout_instance_ids[0]=},"
f" {result=}",
)
status = result.get("status", None)
assert (
status == TaskStatus.SUCCESS
), f"Expected status, got {status}"
# Testing reward functions
if reward_instance_ids and reward_instance_ids[0]:
result = await AgenticRL.test_functions(
instance_id=reward_instance_ids[0],
functype=FunctionType.REWARD,
input_data=reward_input,
)
logger.info(
f"agentic rl test rewards: {reward_instance_ids[0]=},"
f" {result=}",
)
status = result.get("status", None)
assert (
status == TaskStatus.SUCCESS
), f"Expected status, got {status}"
# Testing reward-decorator functions
if (
reward_instance_ids
and len(
reward_instance_ids,
)
> 1
and reward_instance_ids[1]
):
result = await AgenticRL.test_functions(
instance_id=reward_instance_ids[1],
functype=FunctionType.REWARD,
input_data=reward_decorator_input,
)
logger.info(
f"agentic rl test rewards-decorator: "
f"{reward_instance_ids[1]=}, {result=}",
)
reward_score = result.get("reward", {}).get("reward_score", 0.0)
assert (
reward_score == 0.85
), f"Expected reward_score 0.85, got {reward_score}"
# Testing group-reward functions
if group_reward_instance_ids and group_reward_instance_ids[0]:
result = await AgenticRL.test_functions(
instance_id=group_reward_instance_ids[0],
functype=FunctionType.GROUP_REWARD,
input_data=group_reward_input,
)
logger.info(
f"agentic rl test group-rewards: "
f"{group_reward_instance_ids[0]=}, {result=}",
)
status = result.get("status", None)
assert (
status == TaskStatus.SUCCESS
), f"Expected status, got {status}"
logger.info("All tests functions completed successfully")
except Exception as e:
root = e.root_cause if hasattr(e, "root_cause") else e
logger.error(f"Main execution flow terminated: {root}")
if __name__ == "__main__":
import asyncio
asyncio.run(main_functions())