|
|
@ -9,10 +9,11 @@ All components are now in one file: hierarchical_structured_communication_framew
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
from typing import Dict, Any
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Add the project root to the Python path
|
|
|
|
# Add the project root to the Python path
|
|
|
|
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
|
|
|
|
project_root = os.path.abspath(
|
|
|
|
|
|
|
|
os.path.join(os.path.dirname(__file__), "..", "..")
|
|
|
|
|
|
|
|
)
|
|
|
|
sys.path.insert(0, project_root)
|
|
|
|
sys.path.insert(0, project_root)
|
|
|
|
|
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
from dotenv import load_dotenv
|
|
|
@ -20,11 +21,6 @@ from dotenv import load_dotenv
|
|
|
|
# Import everything from the single file
|
|
|
|
# Import everything from the single file
|
|
|
|
from swarms.structs.hierarchical_structured_communication_framework import (
|
|
|
|
from swarms.structs.hierarchical_structured_communication_framework import (
|
|
|
|
HierarchicalStructuredCommunicationFramework,
|
|
|
|
HierarchicalStructuredCommunicationFramework,
|
|
|
|
HierarchicalStructuredCommunicationGenerator,
|
|
|
|
|
|
|
|
HierarchicalStructuredCommunicationEvaluator,
|
|
|
|
|
|
|
|
HierarchicalStructuredCommunicationRefiner,
|
|
|
|
|
|
|
|
HierarchicalStructuredCommunicationSupervisor,
|
|
|
|
|
|
|
|
# Convenience aliases
|
|
|
|
|
|
|
|
TalkHierarchicalGenerator,
|
|
|
|
TalkHierarchicalGenerator,
|
|
|
|
TalkHierarchicalEvaluator,
|
|
|
|
TalkHierarchicalEvaluator,
|
|
|
|
TalkHierarchicalRefiner,
|
|
|
|
TalkHierarchicalRefiner,
|
|
|
@ -45,9 +41,7 @@ def example_basic_usage():
|
|
|
|
|
|
|
|
|
|
|
|
# Create framework with default configuration
|
|
|
|
# Create framework with default configuration
|
|
|
|
framework = HierarchicalStructuredCommunicationFramework(
|
|
|
|
framework = HierarchicalStructuredCommunicationFramework(
|
|
|
|
name="BasicFramework",
|
|
|
|
name="BasicFramework", max_loops=2, verbose=True
|
|
|
|
max_loops=2,
|
|
|
|
|
|
|
|
verbose=True
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Run a simple task
|
|
|
|
# Run a simple task
|
|
|
@ -64,7 +58,9 @@ def example_basic_usage():
|
|
|
|
print(result["final_result"])
|
|
|
|
print(result["final_result"])
|
|
|
|
|
|
|
|
|
|
|
|
print(f"\nTotal loops: {result['total_loops']}")
|
|
|
|
print(f"\nTotal loops: {result['total_loops']}")
|
|
|
|
print(f"Conversation history entries: {len(result['conversation_history'])}")
|
|
|
|
print(
|
|
|
|
|
|
|
|
f"Conversation history entries: {len(result['conversation_history'])}"
|
|
|
|
|
|
|
|
)
|
|
|
|
print(f"Evaluation results: {len(result['evaluation_results'])}")
|
|
|
|
print(f"Evaluation results: {len(result['evaluation_results'])}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -80,33 +76,33 @@ def example_custom_agents():
|
|
|
|
generator = TalkHierarchicalGenerator(
|
|
|
|
generator = TalkHierarchicalGenerator(
|
|
|
|
agent_name="ContentCreator",
|
|
|
|
agent_name="ContentCreator",
|
|
|
|
model_name="gpt-4o-mini",
|
|
|
|
model_name="gpt-4o-mini",
|
|
|
|
verbose=True
|
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
evaluator1 = TalkHierarchicalEvaluator(
|
|
|
|
evaluator1 = TalkHierarchicalEvaluator(
|
|
|
|
agent_name="AccuracyChecker",
|
|
|
|
agent_name="AccuracyChecker",
|
|
|
|
evaluation_criteria=["accuracy", "technical_correctness"],
|
|
|
|
evaluation_criteria=["accuracy", "technical_correctness"],
|
|
|
|
model_name="gpt-4o-mini",
|
|
|
|
model_name="gpt-4o-mini",
|
|
|
|
verbose=True
|
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
evaluator2 = TalkHierarchicalEvaluator(
|
|
|
|
evaluator2 = TalkHierarchicalEvaluator(
|
|
|
|
agent_name="ClarityChecker",
|
|
|
|
agent_name="ClarityChecker",
|
|
|
|
evaluation_criteria=["clarity", "readability", "coherence"],
|
|
|
|
evaluation_criteria=["clarity", "readability", "coherence"],
|
|
|
|
model_name="gpt-4o-mini",
|
|
|
|
model_name="gpt-4o-mini",
|
|
|
|
verbose=True
|
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
refiner = TalkHierarchicalRefiner(
|
|
|
|
refiner = TalkHierarchicalRefiner(
|
|
|
|
agent_name="ContentImprover",
|
|
|
|
agent_name="ContentImprover",
|
|
|
|
model_name="gpt-4o-mini",
|
|
|
|
model_name="gpt-4o-mini",
|
|
|
|
verbose=True
|
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
supervisor = TalkHierarchicalSupervisor(
|
|
|
|
supervisor = TalkHierarchicalSupervisor(
|
|
|
|
agent_name="WorkflowManager",
|
|
|
|
agent_name="WorkflowManager",
|
|
|
|
model_name="gpt-4o-mini",
|
|
|
|
model_name="gpt-4o-mini",
|
|
|
|
verbose=True
|
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Create framework with custom agents
|
|
|
|
# Create framework with custom agents
|
|
|
@ -117,7 +113,7 @@ def example_custom_agents():
|
|
|
|
evaluators=[evaluator1, evaluator2],
|
|
|
|
evaluators=[evaluator1, evaluator2],
|
|
|
|
refiners=[refiner],
|
|
|
|
refiners=[refiner],
|
|
|
|
max_loops=3,
|
|
|
|
max_loops=3,
|
|
|
|
verbose=True
|
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Run a complex task
|
|
|
|
# Run a complex task
|
|
|
@ -134,7 +130,9 @@ def example_custom_agents():
|
|
|
|
print(result["final_result"])
|
|
|
|
print(result["final_result"])
|
|
|
|
|
|
|
|
|
|
|
|
print(f"\nTotal loops: {result['total_loops']}")
|
|
|
|
print(f"\nTotal loops: {result['total_loops']}")
|
|
|
|
print(f"Conversation history entries: {len(result['conversation_history'])}")
|
|
|
|
print(
|
|
|
|
|
|
|
|
f"Conversation history entries: {len(result['conversation_history'])}"
|
|
|
|
|
|
|
|
)
|
|
|
|
print(f"Evaluation results: {len(result['evaluation_results'])}")
|
|
|
|
print(f"Evaluation results: {len(result['evaluation_results'])}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -154,7 +152,7 @@ def example_ollama_integration():
|
|
|
|
model_name="llama3:latest",
|
|
|
|
model_name="llama3:latest",
|
|
|
|
use_ollama=True,
|
|
|
|
use_ollama=True,
|
|
|
|
ollama_base_url="http://localhost:11434/v1",
|
|
|
|
ollama_base_url="http://localhost:11434/v1",
|
|
|
|
ollama_api_key="ollama"
|
|
|
|
ollama_api_key="ollama",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Run a task with local model
|
|
|
|
# Run a task with local model
|
|
|
@ -172,8 +170,12 @@ def example_ollama_integration():
|
|
|
|
print(result["final_result"])
|
|
|
|
print(result["final_result"])
|
|
|
|
|
|
|
|
|
|
|
|
print(f"\nTotal loops: {result['total_loops']}")
|
|
|
|
print(f"\nTotal loops: {result['total_loops']}")
|
|
|
|
print(f"Conversation history entries: {len(result['conversation_history'])}")
|
|
|
|
print(
|
|
|
|
print(f"Evaluation results: {len(result['evaluation_results'])}")
|
|
|
|
f"Conversation history entries: {len(result['conversation_history'])}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
print(
|
|
|
|
|
|
|
|
f"Evaluation results: {len(result['evaluation_results'])}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error with Ollama: {e}")
|
|
|
|
print(f"Error with Ollama: {e}")
|
|
|
@ -190,8 +192,7 @@ def example_structured_communication():
|
|
|
|
|
|
|
|
|
|
|
|
# Create framework
|
|
|
|
# Create framework
|
|
|
|
framework = HierarchicalStructuredCommunicationFramework(
|
|
|
|
framework = HierarchicalStructuredCommunicationFramework(
|
|
|
|
name="CommunicationDemo",
|
|
|
|
name="CommunicationDemo", verbose=True
|
|
|
|
verbose=True
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Demonstrate structured message sending
|
|
|
|
# Demonstrate structured message sending
|
|
|
@ -202,13 +203,17 @@ def example_structured_communication():
|
|
|
|
recipient="Generator",
|
|
|
|
recipient="Generator",
|
|
|
|
message="Create a technical documentation outline",
|
|
|
|
message="Create a technical documentation outline",
|
|
|
|
background="For a Python library focused on data processing",
|
|
|
|
background="For a Python library focused on data processing",
|
|
|
|
intermediate_output="Previous research on similar libraries"
|
|
|
|
intermediate_output="Previous research on similar libraries",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
print(f"Message sent: {structured_msg.message}")
|
|
|
|
print(f"Message sent: {structured_msg.message}")
|
|
|
|
print(f"Background: {structured_msg.background}")
|
|
|
|
print(f"Background: {structured_msg.background}")
|
|
|
|
print(f"Intermediate output: {structured_msg.intermediate_output}")
|
|
|
|
print(
|
|
|
|
print(f"From: {structured_msg.sender} -> To: {structured_msg.recipient}")
|
|
|
|
f"Intermediate output: {structured_msg.intermediate_output}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
print(
|
|
|
|
|
|
|
|
f"From: {structured_msg.sender} -> To: {structured_msg.recipient}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def example_agent_interaction():
|
|
|
|
def example_agent_interaction():
|
|
|
@ -221,19 +226,17 @@ def example_agent_interaction():
|
|
|
|
|
|
|
|
|
|
|
|
# Create agents
|
|
|
|
# Create agents
|
|
|
|
generator = TalkHierarchicalGenerator(
|
|
|
|
generator = TalkHierarchicalGenerator(
|
|
|
|
agent_name="ContentGenerator",
|
|
|
|
agent_name="ContentGenerator", verbose=True
|
|
|
|
verbose=True
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
evaluator = TalkHierarchicalEvaluator(
|
|
|
|
evaluator = TalkHierarchicalEvaluator(
|
|
|
|
agent_name="QualityEvaluator",
|
|
|
|
agent_name="QualityEvaluator",
|
|
|
|
evaluation_criteria=["accuracy", "clarity"],
|
|
|
|
evaluation_criteria=["accuracy", "clarity"],
|
|
|
|
verbose=True
|
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
refiner = TalkHierarchicalRefiner(
|
|
|
|
refiner = TalkHierarchicalRefiner(
|
|
|
|
agent_name="ContentRefiner",
|
|
|
|
agent_name="ContentRefiner", verbose=True
|
|
|
|
verbose=True
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Generate content
|
|
|
|
# Generate content
|
|
|
@ -241,7 +244,7 @@ def example_agent_interaction():
|
|
|
|
gen_result = generator.generate_with_structure(
|
|
|
|
gen_result = generator.generate_with_structure(
|
|
|
|
message="Create a brief explanation of machine learning",
|
|
|
|
message="Create a brief explanation of machine learning",
|
|
|
|
background="For beginners with no technical background",
|
|
|
|
background="For beginners with no technical background",
|
|
|
|
intermediate_output=""
|
|
|
|
intermediate_output="",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
print(f"Generated content: {gen_result.content[:200]}...")
|
|
|
|
print(f"Generated content: {gen_result.content[:200]}...")
|
|
|
@ -249,8 +252,7 @@ def example_agent_interaction():
|
|
|
|
# Evaluate content
|
|
|
|
# Evaluate content
|
|
|
|
print("\n2. Evaluating content...")
|
|
|
|
print("\n2. Evaluating content...")
|
|
|
|
eval_result = evaluator.evaluate_with_criterion(
|
|
|
|
eval_result = evaluator.evaluate_with_criterion(
|
|
|
|
content=gen_result.content,
|
|
|
|
content=gen_result.content, criterion="clarity"
|
|
|
|
criterion="clarity"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
print(f"Evaluation score: {eval_result.score}/10")
|
|
|
|
print(f"Evaluation score: {eval_result.score}/10")
|
|
|
@ -260,10 +262,12 @@ def example_agent_interaction():
|
|
|
|
print("\n3. Refining content...")
|
|
|
|
print("\n3. Refining content...")
|
|
|
|
refine_result = refiner.refine_with_feedback(
|
|
|
|
refine_result = refiner.refine_with_feedback(
|
|
|
|
original_content=gen_result.content,
|
|
|
|
original_content=gen_result.content,
|
|
|
|
evaluation_results=[eval_result]
|
|
|
|
evaluation_results=[eval_result],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
print(f"Refined content: {refine_result.refined_content[:200]}...")
|
|
|
|
print(
|
|
|
|
|
|
|
|
f"Refined content: {refine_result.refined_content[:200]}..."
|
|
|
|
|
|
|
|
)
|
|
|
|
print(f"Changes made: {refine_result.changes_made}")
|
|
|
|
print(f"Changes made: {refine_result.changes_made}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -271,9 +275,13 @@ def main():
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Main function to run all examples
|
|
|
|
Main function to run all examples
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
print("SINGLE-FILE HIERARCHICAL STRUCTURED COMMUNICATION FRAMEWORK")
|
|
|
|
print(
|
|
|
|
|
|
|
|
"SINGLE-FILE HIERARCHICAL STRUCTURED COMMUNICATION FRAMEWORK"
|
|
|
|
|
|
|
|
)
|
|
|
|
print("=" * 80)
|
|
|
|
print("=" * 80)
|
|
|
|
print("This demonstrates the consolidated single-file implementation")
|
|
|
|
print(
|
|
|
|
|
|
|
|
"This demonstrates the consolidated single-file implementation"
|
|
|
|
|
|
|
|
)
|
|
|
|
print("based on the research paper: arXiv:2502.11098")
|
|
|
|
print("based on the research paper: arXiv:2502.11098")
|
|
|
|
print("=" * 80)
|
|
|
|
print("=" * 80)
|
|
|
|
|
|
|
|
|
|
|
@ -290,7 +298,9 @@ def main():
|
|
|
|
print("=" * 80)
|
|
|
|
print("=" * 80)
|
|
|
|
print("Framework Features Demonstrated:")
|
|
|
|
print("Framework Features Demonstrated:")
|
|
|
|
print("✓ Single-file implementation")
|
|
|
|
print("✓ Single-file implementation")
|
|
|
|
print("✓ Structured Communication Protocol (M_ij, B_ij, I_ij)")
|
|
|
|
print(
|
|
|
|
|
|
|
|
"✓ Structured Communication Protocol (M_ij, B_ij, I_ij)"
|
|
|
|
|
|
|
|
)
|
|
|
|
print("✓ Hierarchical Evaluation System")
|
|
|
|
print("✓ Hierarchical Evaluation System")
|
|
|
|
print("✓ Iterative Refinement Process")
|
|
|
|
print("✓ Iterative Refinement Process")
|
|
|
|
print("✓ Flexible Model Configuration (OpenAI/Ollama)")
|
|
|
|
print("✓ Flexible Model Configuration (OpenAI/Ollama)")
|
|
|
@ -303,6 +313,7 @@ def main():
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error during execution: {e}")
|
|
|
|
print(f"Error during execution: {e}")
|
|
|
|
import traceback
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
traceback.print_exc()
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|