@@ -356,6 +356,11 @@ def filter_config(config_list, filter_dict):
356
356
filter_dict (dict): A dictionary representing the filter criteria, where each key is a
357
357
field name to check within the configuration dictionaries, and the
358
358
corresponding value is a list of acceptable values for that field.
359
+ If the configuration's field's value is not a list, then a match occurs
360
+ when it is found in the list of acceptable values. If the configuration's
361
+ field's value is a list, then a match occurs if there is a non-empty
362
+ intersection with the acceptable values.
363
+
359
364
360
365
Returns:
361
366
list of dict: A list of configuration dictionaries that meet all the criteria specified
@@ -368,6 +373,7 @@ def filter_config(config_list, filter_dict):
368
373
{'model': 'gpt-3.5-turbo'},
369
374
{'model': 'gpt-4'},
370
375
{'model': 'gpt-3.5-turbo', 'api_type': 'azure'},
376
+ {'model': 'gpt-3.5-turbo', 'tags': ['gpt35_turbo', 'gpt-35-turbo']},
371
377
]
372
378
373
379
# Define filter criteria to select configurations for the 'gpt-3.5-turbo' model
@@ -382,6 +388,19 @@ def filter_config(config_list, filter_dict):
382
388
383
389
# The resulting `filtered_configs` will be:
384
390
# [{'model': 'gpt-3.5-turbo', 'api_type': 'azure', ...}]
391
+
392
+
393
+ # Define a filter to select a given tag
394
+ filter_criteria = {
395
+ 'tags': ['gpt35_turbo'],
396
+ }
397
+
398
+ # Apply the filter to the configuration list
399
+ filtered_configs = filter_config(configs, filter_criteria)
400
+
401
+ # The resulting `filtered_configs` will be:
402
+ # [{'model': 'gpt-3.5-turbo', 'tags': ['gpt35_turbo', 'gpt-35-turbo']}]
403
+
385
404
```
386
405
387
406
Note:
@@ -391,9 +410,18 @@ def filter_config(config_list, filter_dict):
391
410
- If the list of acceptable values for a key in `filter_dict` includes None, then configuration
392
411
dictionaries that do not have that key will also be considered a match.
393
412
"""
413
+
414
+ def _satisfies (config_value , acceptable_values ):
415
+ if isinstance (config_value , list ):
416
+ return bool (set (config_value ) & set (acceptable_values )) # Non-empty intersection
417
+ else :
418
+ return config_value in acceptable_values
419
+
394
420
if filter_dict :
395
421
config_list = [
396
- config for config in config_list if all (config .get (key ) in value for key , value in filter_dict .items ())
422
+ config
423
+ for config in config_list
424
+ if all (_satisfies (config .get (key ), value ) for key , value in filter_dict .items ())
397
425
]
398
426
return config_list
399
427
0 commit comments