Load agent skills from various sources. Args: skills: Single skill directory, the root path of skill directories, list of skill directories, list of SkillSchema, or skill IDs on the ModelScope hub. Returns: Dictionary
(
self, skills: Union[str, List[str], List[SkillSchema]]
)
| 22 | self.parser = SkillSchemaParser() |
| 23 | |
| 24 | def load_skills( |
| 25 | self, skills: Union[str, List[str], List[SkillSchema]] |
| 26 | ) -> Dict[str, SkillSchema]: |
| 27 | """ |
| 28 | Load agent skills from various sources. |
| 29 | |
| 30 | Args: |
| 31 | skills: Single skill directory, |
| 32 | the root path of skill directories, list of skill directories, list of SkillSchema, |
| 33 | or skill IDs on the ModelScope hub. |
| 34 | |
| 35 | Returns: |
| 36 | Dictionary mapping skill_id@version to SkillSchema objects |
| 37 | """ |
| 38 | all_skills = {} |
| 39 | |
| 40 | if not skills: |
| 41 | logger.warning('No skills provided to load.') |
| 42 | return all_skills |
| 43 | |
| 44 | def is_skill_id(s: str) -> bool: |
| 45 | return '/' in s and len(s.split('/')) == 2 and all( |
| 46 | s.split('/')) and not os.path.exists(s) |
| 47 | |
| 48 | if isinstance(skills, str): |
| 49 | # Could be a single skill path, root path of skills, or skill ID on ModelScope hub |
| 50 | skill_list = [skills] |
| 51 | elif all(isinstance(s, str) for s in skills) or all( |
| 52 | isinstance(s, SkillSchema) for s in skills): |
| 53 | skill_list = skills |
| 54 | else: |
| 55 | raise ValueError('Invalid skills input type.') |
| 56 | |
| 57 | for skill in skill_list: |
| 58 | |
| 59 | if is_skill_id(skill): |
| 60 | from modelscope import snapshot_download |
| 61 | skill_path: str = snapshot_download(repo_id=skill) |
| 62 | skill = skill_path |
| 63 | |
| 64 | if isinstance(skill, SkillSchema): |
| 65 | skill_key = self._get_skill_key(skill=skill) |
| 66 | all_skills[skill_key] = skill |
| 67 | logger.info( |
| 68 | f'Loaded skill from SkillSchema object: {skill_key}') |
| 69 | continue |
| 70 | |
| 71 | skill_dir: Path = Path(skill) |
| 72 | |
| 73 | if not skill_dir.exists(): |
| 74 | logger.warning(f'Path does not exist: {skill_dir} - Skipping.') |
| 75 | continue |
| 76 | |
| 77 | if self._is_skill_directory(skill_dir): |
| 78 | skill_schema = self._load_single_skill(skill_dir=skill_dir) |
| 79 | if skill_schema: |
| 80 | skill_key = f'{skill_schema.skill_id}@{skill_schema.version}' |
| 81 | all_skills[skill_key] = skill_schema |
no test coverage detected