Spaces:
Sleeping
Sleeping
from smolagents import Tool | |
from typing import Any, Optional | |
class SimpleTool(Tool): | |
name = "format_github_repos" | |
description = "Formats GitHub repository information into a structured format." | |
inputs = {"repos":{"type":"object","description":"A dictionary containing a list of repositories under the key \"repos\". Each dictionary inside should have the following keys: - \"name\": The repository name. - \"url\": The GitHub URL. - \"stars\": The number of stars (as a string). - \"description\": A brief description of the repository."}} | |
output_type = "object" | |
def forward(self, repos: object) -> object: | |
""" | |
Formats GitHub repository information into a structured format. | |
Args: | |
repos: A dictionary containing a list of repositories under the key "repos". | |
Each dictionary inside should have the following keys: | |
- "name": The repository name. | |
- "url": The GitHub URL. | |
- "stars": The number of stars (as a string). | |
- "description": A brief description of the repository. | |
Returns: | |
A structured dictionary containing formatted repository details. | |
""" | |
# π₯ Fix: Unpack if "repos" is nested | |
if isinstance(repos, dict) and "repos" in repos: | |
repos = repos["repos"] | |
if not isinstance(repos, list): | |
raise ValueError("Expected a list of dictionaries containing repository details.") | |
required_keys = {"name", "url", "stars", "description"} | |
formatted_repos = [] | |
for repo in repos: | |
if not isinstance(repo, dict) or not required_keys.issubset(repo.keys()): | |
raise ValueError(f"Each repository dictionary must contain {required_keys}.") | |
formatted_repos.append({ | |
"Repository Name": repo["name"], | |
"URL": repo["url"], | |
"Stars": repo["stars"], | |
"Description": repo["description"] | |
}) | |
return {"GitHub Repositories": formatted_repos} |