33
44# Copyright (c) Alibaba, Inc. and its affiliates.
55
6- import yaml
7- from pathlib import Path
86from typing import Union , List , Optional , ClassVar , Dict , Any
97from typing_extensions import Self
108
2927from dashscope .finetune .reinforcement import (
3028 FunctionType ,
3129 DatasetsType ,
32- TrainingType ,
33- DataSourceType ,
3430)
3531from dashscope .finetune .reinforcement import (
3632 RewardInput ,
4743from dashscope .finetune .reinforcement .common .errors import (
4844 RegistrationError ,
4945 ValidationError ,
50- IOErrorWithCode ,
5146 RuntimeErrorWithCode ,
5247 ValueErrorWithCode ,
5348 DatasetsError ,
@@ -65,7 +60,8 @@ def __init__(self, api_key: str = None):
6560 except Exception as e :
6661 logger .error ("API key initialization failed" , exc_info = True )
6762 raise ValueErrorWithCode (
68- "Invalid API key configuration" , error_code = 3000
63+ "Invalid API key configuration" ,
64+ error_code = 3000 ,
6965 ) from e
7066
7167 def init (self , config_path : Optional [str ] = None , ** kwargs ) -> Self :
@@ -86,7 +82,14 @@ async def register_functions(
8682 ]
8783 ] = None ,
8884 lazy_load : Optional [bool ] = True ,
89- ) -> tuple [List [str ], List [str ], List [str ], List [str ], List [str ], List [str ]]:
85+ ) -> tuple [
86+ List [str ],
87+ List [str ],
88+ List [str ],
89+ List [str ],
90+ List [str ],
91+ List [str ],
92+ ]:
9093 """Register function components and return entity/instance IDs."""
9194 if functions :
9295 self .tuning .functions = functions
@@ -105,10 +108,12 @@ async def register_functions(
105108 logger .info ("Function components registered" )
106109 except Exception as e :
107110 logger .error (
108- "Function component registration failed" , exc_info = True
111+ "Function component registration failed" ,
112+ exc_info = True ,
109113 )
110114 raise RegistrationError (
111- "Function registration error" , error_code = 3200
115+ "Function registration error" ,
116+ error_code = 3200 ,
112117 ) from e
113118
114119 return (
@@ -130,17 +135,19 @@ async def upload_datasets(
130135 self .tuning .datasets = datasets
131136
132137 try :
133- uploaded_training_ids , uploaded_validation_ids = (
134- await self .tuning .upload_datasets (
135- training_files = training_files ,
136- validation_files = validation_files ,
137- )
138+ (
139+ uploaded_training_ids ,
140+ uploaded_validation_ids ,
141+ ) = await self .tuning .upload_datasets (
142+ training_files = training_files ,
143+ validation_files = validation_files ,
138144 )
139145 logger .info ("Datasets uploaded" )
140146 except Exception as e :
141147 logger .error ("Datasets upload failed" , exc_info = True )
142148 raise DatasetsError (
143- "Datasets upload error" , error_code = 3300
149+ "Datasets upload error" ,
150+ error_code = 3300 ,
144151 ) from e
145152
146153 return uploaded_training_ids , uploaded_validation_ids
@@ -187,16 +194,16 @@ def submit_job(
187194 self .tuning .functions = functions
188195 try :
189196 rollouts = self .tuning .combine_ids_runtimes (
190- functype = FunctionType .ROLLOUT
197+ functype = FunctionType .ROLLOUT ,
191198 )
192199 rewards = self .tuning .combine_ids_runtimes (
193- functype = FunctionType .REWARD
200+ functype = FunctionType .REWARD ,
194201 )
195202 rewards .extend (
196203 self .tuning .combine_ids_runtimes (
197204 functype = FunctionType .GROUP_REWARD ,
198205 id_str = get_func_type_id (FunctionType .REWARD ),
199- )
206+ ),
200207 )
201208 except Exception as e :
202209 logger .error (
@@ -207,7 +214,8 @@ def submit_job(
207214 # names of functions
208215 if not self .tuning .check_function_names ():
209216 raise ValueErrorWithCode (
210- "Duplicate function names detected. All function names must be unique." ,
217+ "Duplicate function names detected. All function names must "
218+ "be unique." ,
211219 error_code = 3401 ,
212220 )
213221
@@ -259,7 +267,8 @@ def submit_job(
259267 except Exception as e :
260268 logger .error ("Job submission failed" , exc_info = True )
261269 raise RuntimeErrorWithCode (
262- "Job submission error" , error_code = 3400
270+ "Job submission error" ,
271+ error_code = 3400 ,
263272 ) from e
264273
265274 return FineTune (** resp )
@@ -296,15 +305,16 @@ async def run(
296305 """
297306 try :
298307 logger .info (
299- "🟦 Path-Driven mode: Registering functions & uploading datasets..."
308+ "🟦 Path-Driven mode: Registering functions & uploading "
309+ "datasets..." ,
300310 )
301311 await self .register_functions (
302312 functions = functions ,
303313 lazy_load = True ,
304314 )
305315
306316 datasets = list (training_datasets or []) + list (
307- validation_datasets or []
317+ validation_datasets or [],
308318 )
309319 await self .upload_datasets (
310320 datasets = datasets ,
@@ -321,7 +331,8 @@ async def run(
321331 except Exception as e :
322332 logger .error ("RL tuning workflow failed" , exc_info = True )
323333 raise RuntimeErrorWithCode (
324- f"RL tuning workflow failed: { str (e )} " , error_code = 3500
334+ f"RL tuning workflow failed: { str (e )} " ,
335+ error_code = 3500 ,
325336 ) from e
326337
327338 @classmethod
@@ -439,7 +450,8 @@ async def test_functions(
439450 value = GroupRewardInput .model_validate (input_data )
440451 else :
441452 raise ValueErrorWithCode (
442- f"Unsupported function type: { functype } " , error_code = 3600
453+ f"Unsupported function type: { functype } " ,
454+ error_code = 3600 ,
443455 )
444456
445457 logger .info (
@@ -451,13 +463,16 @@ async def test_functions(
451463 )
452464
453465 return await AgenticRLFunctionComponent .verify_function (
454- value , instance_id
466+ value ,
467+ instance_id ,
455468 )
456469
457470 except Exception as e :
458471 logger .error (
459- f"Failure during { str (functype )} test: { str (e )} " , exc_info = True
472+ f"Failure during { str (functype )} test: { str (e )} " ,
473+ exc_info = True ,
460474 )
461475 raise ValidationError (
462- f"Function test failed: { str (e )} " , error_code = 3601
476+ f"Function test failed: { str (e )} " ,
477+ error_code = 3601 ,
463478 ) from e
0 commit comments