Create, Evaluate, and Connect AI Skills
Python ai-agentsai-skillsartificial-intelligenceknowledge-graphlarge-language-modelsnatural-language-processingopenkgskillnetskills
update examples
Jiang Chen40e59218afaf
diff --git a/skillnet-ai/examples/create_example.py b/skillnet-ai/examples/create_example.pyindex 17d0d51..4022396 100644--- a/skillnet-ai/examples/create_example.py+++ b/skillnet-ai/examples/create_example.py@@ -1,10 +1,10 @@+"""+Skill Creation Example - Using SkillNetClient+"""import os-import logging-from skillnet_ai import SkillCreator+from skillnet_ai import SkillNetClient-logging.basicConfig(level=logging.INFO)--# Sample trajectory for demonstration+# Sample trajectorySAMPLE_TRAJECTORY = """User: Please analyze this invoice.pdf and extract the total amount and date.Agent: I will use the PDF tool. Opening invoice.pdf...@@ -12,70 +12,35 @@ Agent: Text extracted. Found 'Total: $500' and 'Date: 2023-10-12'.Agent: The total is $500 and the date is Oct 12, 2023."""--def create_from_trajectory():- """Create skill from execution trajectory."""- api_key = os.getenv("API_KEY")- if not api_key:- print("Please set API_KEY environment variable.")- return-- base_url = os.getenv("BASE_URL") or "https://api.openai.com/v1"- creator = SkillCreator(api_key=api_key, base_url=base_url, model="gpt-4o")-- print("π Creating skill from trajectory...")-- try:- paths = creator.create_from_trajectory(- trajectory=SAMPLE_TRAJECTORY,- output_dir="./created_skills"- )- print("\nβ¨ Creation Complete!")- for p in paths:- print(f" - Created: {p}")- except Exception as e:- print(f"β Error: {e}")---def create_from_github():- """Create skill from GitHub repository."""- api_key = os.getenv("API_KEY")- if not api_key:- print("Please set API_KEY environment variable.")- return-- base_url = os.getenv("BASE_URL") or "https://api.openai.com/v1"- github_token = os.getenv("GITHUB_TOKEN")-- creator = SkillCreator(api_key=api_key, base_url=base_url, model="gpt-4o")-- # Example: DeepKE - Knowledge Extraction Toolkit- github_url = "https://github.com/zjunlp/DeepKE"-- print(f"π Creating skill from GitHub: {github_url}")-- try:- paths = creator.create_from_github(- github_url=github_url,- output_dir="./created_skills",- api_token=github_token,- max_files=20- )- print("\nβ¨ Creation Complete!")- for p in paths:- print(f" - Created: {p}")- except Exception as e:- print(f"β Error: {e}")+# Sample GitHub repository URL+SAMPLE_GITHUB_URL = "https://github.com/zjunlp/DeepKE"def main():- import sys-- if len(sys.argv) > 1 and sys.argv[1] == "--github":- create_from_github()- else:- create_from_trajectory()- print("\nπ‘ Tip: Use --github flag to create from GitHub repository")+ # Initialize client+ client = SkillNetClient(+ api_key=os.getenv("API_KEY"),+ base_url=os.getenv("BASE_URL", "https://api.openai.com/v1"),+ github_token=os.getenv("GITHUB_TOKEN"),+ )++ # Example 1: Create from trajectory+ print("π Creating skill from trajectory...")+ paths = client.create(+ trajectory_content=SAMPLE_TRAJECTORY,+ output_dir="./created_skills"+ )+ for p in paths:+ print(f" - {p}")++ # Example 2: Create from GitHub repository+ print("\nπ Creating skill from GitHub...")+ paths = client.create(+ github_url=SAMPLE_GITHUB_URL,+ output_dir="./created_skills"+ )+ for p in paths:+ print(f" - {p}")if __name__ == "__main__":diff --git a/skillnet-ai/examples/evaluate_example.py b/skillnet-ai/examples/evaluate_example.pyindex 7e48af4..21333b5 100644--- a/skillnet-ai/examples/evaluate_example.py+++ b/skillnet-ai/examples/evaluate_example.py@@ -1,69 +1,29 @@+"""+Skill Evaluation Example - Using SkillNetClient+"""import os-import json-import logging-import shutil-from typing import Dict, Any-from skillnet_ai import SkillEvaluator, EvaluatorConfig+from skillnet_ai import SkillNetClient-# Setup logging configuration-logging.basicConfig(level=logging.INFO)def main():- # 1. Configure Evaluator- api_key = os.getenv("API_KEY")- if not api_key:- print("Please set API_KEY environment variable to run this demo.")- return- base_url = os.getenv("BASE_URL") or "https://api.openai.com/v1" # Optional, custom base URL or default+ # Initialize client+ client = SkillNetClient(+ api_key=os.getenv("API_KEY"),+ base_url=os.getenv("BASE_URL", "https://api.openai.com/v1"),+ )- config = EvaluatorConfig(- api_key=api_key,- base_url=base_url,- model="gpt-4o",- temperature=0.2,+ # Evaluate a remote skill from GitHub+ print("π Evaluating remote skill...")+ result = client.evaluate(+ target="https://github.com/microsoft/autogen/tree/main/samples/tools/web_search")-- try:- evaluator = SkillEvaluator(config)- print("Evaluator initialized successfully.")- except Exception as e:- print(f"Failed to init evaluator: {e}")- return+ print("Result:", result)- # =================================================================- # A: Evaluate Local Skill Path- # =================================================================- local_skill_path = "/zjunlp/liangyuan/code/skills-resource/skillnet-ai/created_skills/pdf-invoice-data-extractor" # replace with your local skill path-- try:- print(f"Starting evaluation for local path: {local_skill_path}")-- result_local = evaluator.evaluate_from_path(- local_skill_path- )-- print("Local Skill Evaluation", result_local)-- except Exception as e:- print(f"Local evaluation failed: {e}")+ # Evaluate a local skill directory+ print("\nπ Evaluating local skill...")+ result = client.evaluate(target="./my_skills/example-skill")+ print("Result:", result)- # =================================================================- # B: Evaluate Skill from URL- # =================================================================- target_url = "https://github.com/anthropics/skills/tree/main/skills/algorithmic-art"-- try:- print(f"Starting evaluation for URL: {target_url}")-- result_url = evaluator.evaluate_from_url(- target_url,- description="Auto-downloaded skill for web searching"- )-- print("URL Skill Evaluation", result_url)-- except Exception as e:- print(f"URL evaluation failed (Create mock Downloader to fix): {e}")if __name__ == "__main__":main()\ No newline at end of filediff --git a/skillnet-ai/examples/search_demo.py b/skillnet-ai/examples/search_demo.pyindex 00cae8f..010265b 100644--- a/skillnet-ai/examples/search_demo.py+++ b/skillnet-ai/examples/search_demo.py@@ -1,52 +1,32 @@-import logging-from skillnet_ai import SkillNetSearcher, SkillDownloader+"""+Skill Search & Download Example - Using SkillNetClient+"""+from skillnet_ai import SkillNetClient-# Setup logging configuration-logging.basicConfig(level=logging.INFO)def main():- # Initialize the searcher- searcher = SkillNetSearcher()-- query = "python"- print(f"π Searching for: {query}")-- # Perform AI Search (Vector mode)- # Note: We now use the unified 'search' method with mode="vector"- results = searcher.search(- q=query,- limit=10,- category="Development",- )-+ # Initialize client (api_key optional for search/download)+ client = SkillNetClient()++ # Search for skills+ print("π Searching for: pdf tool")+ results = client.search(q="pdf tool", limit=5)+if not results:print("β No skills found.")return- print(f"β Found {len(results)} skills.")-- # Display the top result+ print(f"β Found {len(results)} skills.\n")+ for skill in results:+ print(f" - {skill.skill_name} (β {skill.stars})")++ # Download top skilltop_skill = results[0]- print(f"Top Skill: {top_skill.skill_name} (Stars: {top_skill.stars})")- print(f"Description: {top_skill.skill_description}")-- # Install the skill if a valid URL existsif top_skill.skill_url:- print("β¬οΈ Installing skill...")- skilldownloader = SkillDownloader()-- # Download the skill to a local directory- local_path = skilldownloader.download(- top_skill.skill_url,- target_dir="./my_skills"- )-- if local_path:- print(f"π Skill ready at: {local_path}")- else:- print("β οΈ Download failed or returned no path.")- else:- print("βΉοΈ No download URL available for this skill.")+ print(f"\nβ¬οΈ Downloading: {top_skill.skill_name}")+ local_path = client.download(url=top_skill.skill_url, target_dir="./my_skills")+ print(f"π Installed at: {local_path}")+if __name__ == "__main__":main()\ No newline at end of filediff --git a/skillnet-ai/src/skillnet_ai/client.py b/skillnet-ai/src/skillnet_ai/client.pyindex 39968bd..ae4a651 100644--- a/skillnet-ai/src/skillnet_ai/client.py+++ b/skillnet-ai/src/skillnet_ai/client.py@@ -115,26 +115,36 @@ class SkillNetClient:def create(self,- trajectory_content: str,+ trajectory_content: Optional[str] = None,+ github_url: Optional[str] = None,output_dir: Union[str, Path] = "./generated_skills",- model: str = "gpt-4o"+ model: str = "gpt-4o",+ max_files: int = 20) -> List[str]:"""- Generate executable skills from a trajectory log.+ Generate executable skills from a trajectory log or GitHub repository.Args:trajectory_content: The text content of the execution log/trajectory.+ github_url: Full URL to GitHub repository (e.g., https://github.com/owner/repo).output_dir: Directory where new skills will be saved.model: The LLM model to use.+ max_files: Maximum number of Python files to analyze (GitHub mode only).Returns:List[str]: A list of paths to the generated skill folders.++ Note:+ Provide either trajectory_content OR github_url, not both."""if not self.api_key:raise SkillNetError("API_KEY is required for skill creation.")- if not trajectory_content.strip():- raise SkillNetError("Trajectory content is empty.")+ if trajectory_content and github_url:+ raise SkillNetError("Provide either trajectory_content OR github_url, not both.")++ if not trajectory_content and not github_url:+ raise SkillNetError("Must provide trajectory_content or github_url.")try:creator = SkillCreator(@@ -143,58 +153,23 @@ class SkillNetClient:model=model)- created_paths = creator.create_from_trajectory(- trajectory=trajectory_content,- output_dir=str(output_dir)- )+ if github_url:+ created_paths = creator.create_from_github(+ github_url=github_url,+ output_dir=str(output_dir),+ api_token=self.github_token,+ max_files=max_files+ )+ else:+ created_paths = creator.create_from_trajectory(+ trajectory=trajectory_content,+ output_dir=str(output_dir)+ )return created_paths if created_paths else []except Exception as e:raise SkillNetError(f"Creation failed: {str(e)}") from e- def create_from_github(- self,- github_url: str,- output_dir: Union[str, Path] = "./generated_skills",- model: str = "gpt-4o",- max_files: int = 20- ) -> List[str]:- """- Generate a skill package from a GitHub repository.-- Args:- github_url: Full URL to GitHub repository (e.g., https://github.com/owner/repo).- output_dir: Directory where new skills will be saved.- model: The LLM model to use.- max_files: Maximum number of Python files to analyze for code signatures.-- Returns:- List[str]: A list of paths to the generated skill folders.- """- if not self.api_key:- raise SkillNetError("API_KEY is required for skill creation from GitHub.")-- if not github_url or not github_url.strip():- raise SkillNetError("GitHub URL is empty.")-- try:- creator = SkillCreator(- api_key=self.api_key,- base_url=self.base_url,- model=model- )-- created_paths = creator.create_from_github(- github_url=github_url,- output_dir=str(output_dir),- api_token=self.github_token,- max_files=max_files- )-- return created_paths if created_paths else []- except Exception as e:- raise SkillNetError(f"GitHub skill creation failed: {str(e)}") from e-def evaluate(self,target: str,diff --git a/skillnet-ai/src/skillnet_ai/creator.py b/skillnet-ai/src/skillnet_ai/creator.pyindex d280637..3a759c8 100644--- a/skillnet-ai/src/skillnet_ai/creator.py+++ b/skillnet-ai/src/skillnet_ai/creator.py@@ -304,7 +304,7 @@ class SkillCreator:for attempt in range(max_retries + 1):try:# Use higher max_tokens for complete response- response = self._get_llm_response(messages, max_tokens=8192)+ response = self._get_llm_response(messages, max_tokens=16384)# Validate response has required contentif self._validate_skill_content(response):