Create, Evaluate, and Connect AI Skills
Python ai-agentsai-skillsartificial-intelligenceknowledge-graphlarge-language-modelsnatural-language-processingopenkgskillnetskills
update analyze output format
LiamLYYY1890a4eb1a9a
diff --git a/skillnet-ai/src/skillnet_ai/analyzer.py b/skillnet-ai/src/skillnet_ai/analyzer.pyindex ba7c6db..468548c 100644--- a/skillnet-ai/src/skillnet_ai/analyzer.py+++ b/skillnet-ai/src/skillnet_ai/analyzer.py@@ -137,6 +137,18 @@ class SkillRelationshipAnalyzer:return lines[0]return "No description available."++ def _extract_json_from_tags(self, content: str, tag_name: str) -> str:+ """Helper to extract content between XML-style tags."""+ start_tag = f"<{tag_name}>"+ end_tag = f"</{tag_name}>"++ if start_tag in content and end_tag in content:+ return content.split(start_tag)[1].split(end_tag)[0].strip()++ clean_content = content.replace("```json", "").replace("```", "").strip()++ return clean_contentdef _generate_relationship_graph(self, skills: List[Dict]) -> List[Dict]:"""Calls LLM to infer edges between nodes."""@@ -156,23 +168,55 @@ class SkillRelationshipAnalyzer:messages=messages,)content = response.choices[0].message.content++ # 1. Extract JSON from tags+ json_str = self._extract_json_from_tags(content, "Skill_Relationships")++ # 2. Parse JSON+ parsed_data = json.loads(json_str)- parsed = json.loads(content)- edges = parsed.get("relationships", [])+ # 3. Extract edges+ edges = []+ if isinstance(parsed_data, list):+ edges = parsed_data+ elif isinstance(parsed_data, dict) and "relationships" in parsed_data:+ edges = parsed_data["relationships"]- # Validate edges structure+ # 4. Validate edges structurevalid_edges = []valid_names = {s['name'] for s in skills}+ valid_types = {'similar_to', 'belong_to', 'compose_with', 'depend_on'}+for edge in edges:- if (edge.get('source') in valid_names and- edge.get('target') in valid_names and- edge.get('type') in ['similar_to', 'belong_to', 'compose_with', 'depend_on']):- valid_edges.append(edge)+ # Basic type check+ if not isinstance(edge, dict):+ continue++ s_name = edge.get('source')+ t_name = edge.get('target')+ r_type = edge.get('type')++ # Validate names and type+ if (s_name in valid_names and+ t_name in valid_names and+ r_type in valid_types and+ s_name != t_name):++ valid_edges.append({+ "source": s_name,+ "target": t_name,+ "type": r_type,+ "reason": edge.get("reason", "No reason provided")+ })logger.info(f"Identified {len(valid_edges)} valid relationships.")return valid_edges+ except json.JSONDecodeError as e:+ logger.error(f"Failed to parse JSON content: {e}")+ logger.debug(f"Raw content was: {content}")+ return []except Exception as e:logger.error(f"Failed to analyze relationships: {e}")return []\ No newline at end of filediff --git a/skillnet-ai/src/skillnet_ai/client.py b/skillnet-ai/src/skillnet_ai/client.pyindex 46b3f64..030298f 100644--- a/skillnet-ai/src/skillnet_ai/client.py+++ b/skillnet-ai/src/skillnet_ai/client.py@@ -286,7 +286,7 @@ class SkillNetClient:Analyze a local directory containing multiple skills to infer relationships between them.This builds a knowledge graph (edges) between skills based on their names and descriptions.- Relationships detected: similar_to, belong_to, pairs_with (compose_with), depend_on.+ Relationships detected: similar_to, belong_to, compose_with, depend_on.Args:skills_dir: Path to the directory containing skill folders.diff --git a/skillnet-ai/src/skillnet_ai/prompts.py b/skillnet-ai/src/skillnet_ai/prompts.pyindex e57398a..8512d5e 100644--- a/skillnet-ai/src/skillnet_ai/prompts.py+++ b/skillnet-ai/src/skillnet_ai/prompts.py@@ -816,27 +816,25 @@ Remember:- Do not hallucinate skills not in the list.Output Format:-Return a JSON object with a single key "relationships", which is a list of objects.-Each object must have: "source", "target", "type", and "reason" (short explanation).+Return a JSON array where each element represents a relationship with the following keys:+- source: (string) The name of the source skill (the one initiating the relationship)+- target: (string) The name of the target skill (the one receiving the relationship)+- type: (string) One of the 4 relationship types: "similar_to", "belong_to", "compose_with", "depend_on"+- reason: (string) A brief explanation of why this relationship exists based on the skill descriptions.Output Example:-{{- "relationships": [+[{{"source": "google_search_tool","target": "bing_search_tool","type": "similar_to","reason": "Both provide web search capabilities and are interchangeable."}},- {{- "source": "analyze_stock_chart",- "target": "get_stock_data",- "type": "depend_on",- "reason": "Analysis requires the raw data fetcher to run first."- }},...- ]-}}+]-Now, analyze the skills and provide the relationships in the valid JSON format.+Keep your output in the format below:+<Skill_Relationships>+your generated JSON array here+</Skill_Relationships>"""\ No newline at end of file