Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: Workforce support structured_output_handler for models can't do… (#2812)

Wendong-Fan955aa19c3817
diff --git a/camel/societies/workforce/role_playing_worker.py b/camel/societies/workforce/role_playing_worker.py
index 5e3175050..832d54ed2 100644
--- a/camel/societies/workforce/role_playing_worker.py
+++ b/camel/societies/workforce/role_playing_worker.py
@@ -13,7 +13,6 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
from __future__ import annotations
-import json
from typing import Dict, List, Optional
from colorama import Fore
@@ -25,6 +24,9 @@ from camel.societies.workforce.prompts import (
ROLEPLAY_PROCESS_TASK_PROMPT,
ROLEPLAY_SUMMARIZE_PROMPT,
)
+from camel.societies.workforce.structured_output_handler import (
+ StructuredOutputHandler,
+)
from camel.societies.workforce.utils import TaskResult
from camel.societies.workforce.worker import Worker
from camel.tasks.task import Task, TaskState, is_task_result_insufficient
@@ -48,6 +50,14 @@ class RolePlayingWorker(Worker):
(default: :obj:`None`)
chat_turn_limit (int): The maximum number of chat turns in the role
playing. (default: :obj:`20`)
+ use_structured_output_handler (bool, optional): Whether to use the
+ structured output handler instead of native structured output.
+ When enabled, the workforce will use prompts with structured
+ output instructions and regex extraction to parse responses.
+ This ensures compatibility with agents that don't reliably
+ support native structured output. When disabled, the workforce
+ uses the native response_format parameter.
+ (default: :obj:`True`)
"""
def __init__(
@@ -59,8 +69,15 @@ class RolePlayingWorker(Worker):
user_agent_kwargs: Optional[Dict] = None,
summarize_agent_kwargs: Optional[Dict] = None,
chat_turn_limit: int = 20,
+ use_structured_output_handler: bool = True,
) -> None:
super().__init__(description)
+ self.use_structured_output_handler = use_structured_output_handler
+ self.structured_handler = (
+ StructuredOutputHandler()
+ if use_structured_output_handler
+ else None
+ )
self.summarize_agent_kwargs = summarize_agent_kwargs
summ_sys_msg = BaseMessage.make_assistant_message(
role_name="Summarizer",
@@ -173,13 +190,53 @@ class RolePlayingWorker(Worker):
chat_history=chat_history_str,
additional_info=task.additional_info,
)
- response = self.summarize_agent.step(
- prompt, response_format=TaskResult
- )
- result_dict = json.loads(response.msg.content)
- task_result = TaskResult(**result_dict)
+ if self.use_structured_output_handler and self.structured_handler:
+ # Use structured output handler for prompt-based extraction
+ enhanced_prompt = (
+ self.structured_handler.generate_structured_prompt(
+ base_prompt=prompt,
+ schema=TaskResult,
+ examples=[
+ {
+ "content": "The assistant successfully completed "
+ "the task by...",
+ "failed": False,
+ }
+ ],
+ additional_instructions=(
+ "Summarize the task execution based "
+ "on the chat history, clearly indicating whether "
+ "the task succeeded or failed."
+ ),
+ )
+ )
+ response = self.summarize_agent.step(enhanced_prompt)
+ task_result = self.structured_handler.parse_structured_response(
+ response_text=response.msg.content if response.msg else "",
+ schema=TaskResult,
+ fallback_values={
+ "content": "Task summarization failed",
+ "failed": True,
+ },
+ )
+ else:
+ # Use native structured output if supported
+ response = self.summarize_agent.step(
+ prompt, response_format=TaskResult
+ )
+ if response.msg.parsed is None:
+ print(
+ f"{Fore.RED}Error in summarization: Invalid "
+ f"task result{Fore.RESET}"
+ )
+ task_result = TaskResult(
+ content="Failed to generate valid task summary.",
+ failed=True,
+ )
+ else:
+ task_result = response.msg.parsed
- task.result = task_result.content
+ task.result = task_result.content # type: ignore[union-attr]
if is_task_result_insufficient(task):
print(
diff --git a/camel/societies/workforce/single_agent_worker.py b/camel/societies/workforce/single_agent_worker.py
index 23453398c..188afb459 100644
--- a/camel/societies/workforce/single_agent_worker.py
+++ b/camel/societies/workforce/single_agent_worker.py
@@ -15,7 +15,6 @@ from __future__ import annotations
import asyncio
import datetime
-import json
import time
from collections import deque
from typing import Any, List, Optional
@@ -24,6 +23,9 @@ from colorama import Fore
from camel.agents import ChatAgent
from camel.societies.workforce.prompts import PROCESS_TASK_PROMPT
+from camel.societies.workforce.structured_output_handler import (
+ StructuredOutputHandler,
+)
from camel.societies.workforce.utils import TaskResult
from camel.societies.workforce.worker import Worker
from camel.tasks.task import Task, TaskState, is_task_result_insufficient
@@ -186,6 +188,14 @@ class SingleAgentWorker(Worker):
(default: :obj:`10`)
auto_scale_pool (bool): Whether to auto-scale the agent pool.
(default: :obj:`True`)
+ use_structured_output_handler (bool, optional): Whether to use the
+ structured output handler instead of native structured output.
+ When enabled, the workforce will use prompts with structured
+ output instructions and regex extraction to parse responses.
+ This ensures compatibility with agents that don't reliably
+ support native structured output. When disabled, the workforce
+ uses the native response_format parameter.
+ (default: :obj:`True`)
"""
def __init__(
@@ -196,12 +206,19 @@ class SingleAgentWorker(Worker):
pool_initial_size: int = 1,
pool_max_size: int = 10,
auto_scale_pool: bool = True,
+ use_structured_output_handler: bool = True,
) -> None:
node_id = worker.agent_id
super().__init__(
description,
node_id=node_id,
)
+ self.use_structured_output_handler = use_structured_output_handler
+ self.structured_handler = (
+ StructuredOutputHandler()
+ if use_structured_output_handler
+ else None
+ )
self.worker = worker
self.use_agent_pool = use_agent_pool
@@ -278,9 +295,43 @@ class SingleAgentWorker(Worker):
additional_info=task.additional_info,
)
- response = await worker_agent.astep(
- prompt, response_format=TaskResult
- )
+ if self.use_structured_output_handler and self.structured_handler:
+ # Use structured output handler for prompt-based extraction
+ enhanced_prompt = (
+ self.structured_handler.generate_structured_prompt(
+ base_prompt=prompt,
+ schema=TaskResult,
+ examples=[
+ {
+ "content": "I have successfully completed the "
+ "task...",
+ "failed": False,
+ }
+ ],
+ additional_instructions="Ensure you provide a clear "
+ "description of what was done and whether the task "
+ "succeeded or failed.",
+ )
+ )
+ response = await worker_agent.astep(enhanced_prompt)
+ task_result = (
+ self.structured_handler.parse_structured_response(
+ response_text=response.msg.content
+ if response.msg
+ else "",
+ schema=TaskResult,
+ fallback_values={
+ "content": "Task processing failed",
+ "failed": True,
+ },
+ )
+ )
+ else:
+ # Use native structured output if supported
+ response = await worker_agent.astep(
+ prompt, response_format=TaskResult
+ )
+ task_result = response.msg.parsed
# Get token usage from the response
usage_info = response.info.get("usage") or response.info.get(
@@ -331,25 +382,27 @@ class SingleAgentWorker(Worker):
print(f"======\n{Fore.GREEN}Response from {self}:{Fore.RESET}")
- try:
- result_dict = json.loads(response.msg.content)
- task_result = TaskResult(**result_dict)
- except json.JSONDecodeError as e:
- print(
- f"{Fore.RED}JSON parsing error for task {task.id}: "
- f"Invalid response format - {e}{Fore.RESET}"
- )
- return TaskState.FAILED
-
- color = Fore.RED if task_result.failed else Fore.GREEN
+ if not self.use_structured_output_handler:
+ # Handle native structured output parsing
+ if task_result is None:
+ print(
+ f"{Fore.RED}Error in worker step execution: Invalid "
+ f"task result{Fore.RESET}"
+ )
+ task_result = TaskResult(
+ content="Failed to generate valid task result.",
+ failed=True,
+ )
+
+ color = Fore.RED if task_result.failed else Fore.GREEN # type: ignore[union-attr]
print(
- f"\n{color}{task_result.content}{Fore.RESET}\n======",
+ f"\n{color}{task_result.content}{Fore.RESET}\n======", # type: ignore[union-attr]
)
- if task_result.failed:
+ if task_result.failed: # type: ignore[union-attr]
return TaskState.FAILED
- task.result = task_result.content
+ task.result = task_result.content # type: ignore[union-attr]
if is_task_result_insufficient(task):
print(
diff --git a/camel/societies/workforce/structured_output_handler.py b/camel/societies/workforce/structured_output_handler.py
new file mode 100644
index 000000000..914970ea7
--- /dev/null
+++ b/camel/societies/workforce/structured_output_handler.py
@@ -0,0 +1,500 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+import json
+import re
+from typing import Any, ClassVar, Dict, List, Optional, Type, Union
+
+from pydantic import BaseModel, ValidationError
+
+from camel.logger import get_logger
+from camel.societies.workforce.utils import (
+ RecoveryDecision,
+ RecoveryStrategy,
+ TaskAssignResult,
+ WorkerConf,
+)
+
+logger = get_logger(__name__)
+
+
+class StructuredOutputHandler:
+ r"""Handler for generating prompts and extracting structured output from
+ agent responses.
+
+ This handler provides functionality to:
+ - Generate prompts that guide agents to produce structured output
+ - Extract structured data from agent responses using regex patterns
+ - Provide fallback mechanisms when extraction fails
+ - Support the existing structured output schemas used in workforce.py
+ """
+
+ # Common JSON extraction patterns
+ JSON_PATTERNS: ClassVar[List[str]] = [
+ # Pattern 1: Standard JSON block
+ r'```json\s*\n(.*?)\n```',
+ # Pattern 2: JSON without code block
+ r'(\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\})',
+ # Pattern 3: JSON with potential nested objects
+ r'(\{(?:[^{}]|(?:\{[^{}]*\}))*\})',
+ ]
+
+ # Schema-specific patterns for more targeted extraction
+ SCHEMA_PATTERNS: ClassVar[Dict[str, List[str]]] = {
+ 'TaskAssignResult': [
+ r'"assignments"\s*:\s*\[(.*?)\]',
+ r'assignments.*?:\s*\[(.*?)\]',
+ ],
+ 'WorkerConf': [
+ (
+ r'"role"\s*:\s*"([^"]+)".*?"sys_msg"\s*:\s*"([^"]+)".*?'
+ r'"description"\s*:\s*"([^"]+)"'
+ ),
+ (
+ r'role.*?:\s*"([^"]+)".*?sys_msg.*?:\s*"([^"]+)".*?'
+ r'description.*?:\s*"([^"]+)"'
+ ),
+ ],
+ 'RecoveryDecision': [
+ r'"strategy"\s*:\s*"([^"]+)".*?"reasoning"\s*:\s*"([^"]+)"',
+ r'strategy.*?:\s*"([^"]+)".*?reasoning.*?:\s*"([^"]+)"',
+ ],
+ }
+
+ @staticmethod
+ def generate_structured_prompt(
+ base_prompt: str,
+ schema: Type[BaseModel],
+ examples: Optional[List[Dict[str, Any]]] = None,
+ additional_instructions: Optional[str] = None,
+ ) -> str:
+ r"""Generate a prompt that guides agents to produce structured output.
+
+ Args:
+ base_prompt (str): The base prompt content.
+ schema (Type[BaseModel]): The Pydantic model schema for the
+ expected output.
+ examples (Optional[List[Dict[str, Any]]]): Optional examples of
+ valid output.
+ additional_instructions (Optional[str]): Additional instructions
+ for output formatting.
+
+ Returns:
+ str: The enhanced prompt with structured output instructions.
+ """
+ # Get schema information
+ schema_name = schema.__name__
+ schema_json = schema.model_json_schema()
+ required_fields = schema_json.get('required', [])
+ properties = schema_json.get('properties', {})
+
+ # Build field descriptions
+ field_descriptions = []
+ for field_name, field_info in properties.items():
+ description = field_info.get('description', 'No description')
+ field_type = field_info.get('type', 'unknown')
+ required = field_name in required_fields
+ field_descriptions.append(
+ f"- {field_name} ({field_type}{'*' if required else ''}): "
+ f"{description}"
+ )
+
+ # Build structured output section
+ structured_section = f"""
+**STRUCTURED OUTPUT REQUIREMENTS:**
+
+You must return a valid JSON object that conforms to the {schema_name} schema.
+
+Required fields:
+{chr(10).join(field_descriptions)}
+
+Fields marked with * are required and must be present in your response.
+
+**FORMAT YOUR RESPONSE AS:**
+```json
+{{
+ // Your JSON response here following the schema
+}}
+```
+"""
+
+ # Add examples if provided
+ if examples:
+ examples_section = "\n**VALID EXAMPLES:**\n"
+ for i, example in enumerate(examples, 1):
+ examples_section += f"\nExample {i}:\n```json\n"
+ examples_section += json.dumps(example, indent=2)
+ examples_section += "\n```\n"
+ structured_section += examples_section
+
+ # Add additional instructions if provided
+ if additional_instructions:
+ structured_section += "\n**ADDITIONAL INSTRUCTIONS:**\n"
+ structured_section += additional_instructions + "\n"
+
+ # Add critical reminder
+ structured_section += """
+**CRITICAL**: Your response must contain ONLY the JSON object within the code
+block.
+Do not include any explanatory text, comments, or content outside the JSON
+structure.
+Ensure the JSON is valid and properly formatted.
+"""
+
+ # Combine with base prompt
+ return base_prompt + "\n\n" + structured_section
+
+ @staticmethod
+ def extract_json(
+ text: str,
+ schema: Optional[Type[BaseModel]] = None,
+ ) -> Optional[Dict[str, Any]]:
+ r"""Extract JSON data from text using multiple patterns.
+
+ Args:
+ text (str): The text containing JSON data.
+ schema (Optional[Type[BaseModel]]): Optional schema for targeted
+ extraction.
+
+ Returns:
+ Optional[Dict[str, Any]]: Extracted JSON data or None if extraction
+ fails.
+ """
+ if not text:
+ return None
+
+ # Try standard JSON patterns first
+ for pattern in StructuredOutputHandler.JSON_PATTERNS:
+ matches = re.findall(pattern, text, re.DOTALL | re.IGNORECASE)
+ for match in matches:
+ try:
+ # Clean up the match
+ json_str = match.strip()
+ # Remove any trailing commas
+ json_str = re.sub(r',\s*}', '}', json_str)
+ json_str = re.sub(r',\s*]', ']', json_str)
+
+ data = json.loads(json_str)
+ if isinstance(data, dict):
+ return data
+ except json.JSONDecodeError:
+ continue
+
+ # Try direct JSON parsing of the entire text
+ try:
+ data = json.loads(text.strip())
+ if isinstance(data, dict):
+ return data
+ except json.JSONDecodeError:
+ pass
+
+ # Try schema-specific patterns if schema is provided
+ if (
+ schema
+ and schema.__name__ in StructuredOutputHandler.SCHEMA_PATTERNS
+ ):
+ return StructuredOutputHandler._extract_with_schema_patterns(
+ text, schema
+ )
+
+ return None
+
+ @staticmethod
+ def _extract_with_schema_patterns(
+ text: str,
+ schema: Type[BaseModel],
+ ) -> Optional[Dict[str, Any]]:
+ r"""Extract data using schema-specific patterns.
+
+ Args:
+ text (str): The text to extract from.
+ schema (Type[BaseModel]): The schema to use for extraction.
+
+ Returns:
+ Optional[Dict[str, Any]]: Extracted data or None.
+ """
+ schema_name = schema.__name__
+ patterns = StructuredOutputHandler.SCHEMA_PATTERNS.get(schema_name, [])
+
+ if schema_name == 'WorkerConf':
+ for pattern in patterns:
+ match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
+ if match:
+ try:
+ return {
+ 'role': match.group(1),
+ 'sys_msg': match.group(2),
+ 'description': match.group(3),
+ }
+ except (IndexError, AttributeError):
+ continue
+
+ elif schema_name == 'RecoveryDecision':
+ for pattern in patterns:
+ match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
+ if match:
+ try:
+ strategy = match.group(1)
+ reasoning = match.group(2)
+ # Look for modified_task_content
+ content_match = re.search(
+ r'"modified_task_content"\s*:\s*"([^"]*)"',
+ text,
+ re.IGNORECASE,
+ )
+ return {
+ 'strategy': strategy,
+ 'reasoning': reasoning,
+ 'modified_task_content': (
+ content_match.group(1)
+ if content_match
+ else None
+ ),
+ }
+ except (IndexError, AttributeError):
+ continue
+
+ return None
+
+ @staticmethod
+ def parse_structured_response(
+ response_text: str,
+ schema: Type[BaseModel],
+ fallback_values: Optional[Dict[str, Any]] = None,
+ ) -> Union[BaseModel, Dict[str, Any]]:
+ r"""Parse agent response into structured data with fallback support.
+
+ Args:
+ response_text (str): The agent's response text.
+ schema (Type[BaseModel]): The expected schema.
+ fallback_values (Optional[Dict[str, Any]]): Fallback values to use
+ if parsing fails.
+
+ Returns:
+ Union[BaseModel, Dict[str, Any]]: Parsed data as schema instance
+ or fallback dictionary.
+ """
+ # Try to extract JSON
+ extracted_data = StructuredOutputHandler.extract_json(
+ response_text, schema
+ )
+
+ if extracted_data:
+ try:
+ # Validate against schema
+ return schema(**extracted_data)
+ except ValidationError as e:
+ logger.warning(
+ f"Validation error for {schema.__name__}: {e}. "
+ f"Attempting to fix common issues."
+ )
+
+ # Try to fix common validation issues
+ fixed_data = StructuredOutputHandler._fix_common_issues(
+ extracted_data, schema
+ )
+ if fixed_data:
+ try:
+ return schema(**fixed_data)
+ except ValidationError:
+ pass
+
+ # Use fallback values if provided
+ if fallback_values:
+ logger.warning(
+ f"Failed to parse {schema.__name__} from response. "
+ f"Using fallback values."
+ )
+ try:
+ return schema(**fallback_values)
+ except ValidationError:
+ return fallback_values
+
+ # Return default instance if possible
+ try:
+ logger.warning(f"Creating default {schema.__name__} instance.")
+ return StructuredOutputHandler._create_default_instance(schema)
+ except Exception:
+ logger.error(
+ f"Failed to create default instance for {schema.__name__}"
+ )
+ return {}
+
+ @staticmethod
+ def _fix_common_issues(
+ data: Dict[str, Any],
+ schema: Type[BaseModel],
+ ) -> Optional[Dict[str, Any]]:
+ r"""Attempt to fix common validation issues in extracted data.
+
+ Args:
+ data (Dict[str, Any]): The extracted data.
+ schema (Type[BaseModel]): The target schema.
+
+ Returns:
+ Optional[Dict[str, Any]]: Fixed data or None if unfixable.
+ """
+ fixed_data = data.copy()
+ schema_name = schema.__name__
+
+ if schema_name == 'TaskAssignResult':
+ # Ensure assignments is a list
+ if 'assignments' not in fixed_data:
+ fixed_data['assignments'] = []
+ elif not isinstance(fixed_data['assignments'], list):
+ fixed_data['assignments'] = [fixed_data['assignments']]
+
+ # Fix each assignment
+ for _i, assignment in enumerate(fixed_data['assignments']):
+ if isinstance(assignment, dict):
+ # Ensure dependencies is a list
+ if 'dependencies' not in assignment:
+ assignment['dependencies'] = []
+ elif isinstance(assignment['dependencies'], str):
+ # Handle comma-separated string
+ deps = assignment['dependencies'].strip()
+ if deps:
+ assignment['dependencies'] = [
+ d.strip() for d in deps.split(',')
+ ]
+ else:
+ assignment['dependencies'] = []
+
+ elif schema_name == 'RecoveryDecision':
+ # Ensure strategy is valid
+ if 'strategy' in fixed_data:
+ strategy = fixed_data['strategy'].lower()
+ valid_strategies = [
+ 'retry',
+ 'replan',
+ 'decompose',
+ 'create_worker',
+ ]
+ if strategy not in valid_strategies:
+ # Try to match partial
+ for valid in valid_strategies:
+ if valid.startswith(strategy) or strategy in valid:
+ fixed_data['strategy'] = valid
+ break
+
+ return fixed_data
+
+ @staticmethod
+ def _create_default_instance(schema: Type[BaseModel]) -> BaseModel:
+ r"""Create a default instance of the schema with minimal required
+ fields.
+
+ Args:
+ schema (Type[BaseModel]): The schema to instantiate.
+
+ Returns:
+ BaseModel: Default instance of the schema.
+ """
+ schema_name = schema.__name__
+
+ if schema_name == 'TaskAssignResult':
+ return TaskAssignResult(assignments=[])
+ elif schema_name == 'WorkerConf':
+ return WorkerConf(
+ role="General Assistant",
+ sys_msg="You are a helpful assistant.",
+ description="A general-purpose worker",
+ )
+ elif schema_name == 'RecoveryDecision':
+ return RecoveryDecision(
+ strategy=RecoveryStrategy.RETRY,
+ reasoning="Unable to parse response, defaulting to retry",
+ modified_task_content=None,
+ )
+ else:
+ # Try to create with empty dict and let defaults handle it
+ return schema()
+
+ @staticmethod
+ def validate_response(
+ response: Union[BaseModel, Dict[str, Any]],
+ schema: Type[BaseModel],
+ ) -> bool:
+ r"""Validate that a response conforms to the expected schema.
+
+ Args:
+ response: The response to validate.
+ schema (Type[BaseModel]): The expected schema.
+
+ Returns:
+ bool: True if valid, False otherwise.
+ """
+ if isinstance(response, schema):
+ return True
+
+ if isinstance(response, dict):
+ try:
+ schema(**response)
+ return True
+ except ValidationError:
+ return False
+
+ return False
+
+ @staticmethod
+ def create_fallback_response(
+ schema: Type[BaseModel],
+ error_message: str,
+ context: Optional[Dict[str, Any]] = None,
+ ) -> BaseModel:
+ r"""Create a fallback response for a given schema with error context.
+
+ Args:
+ schema (Type[BaseModel]): The schema to create a response for.
+ error_message (str): The error message to include.
+ context (Optional[Dict[str, Any]]): Additional context for the
+ fallback.
+
+ Returns:
+ BaseModel: A valid instance of the schema with fallback values.
+ """
+ schema_name = schema.__name__
+
+ if schema_name == 'TaskAssignResult':
+ # Return empty assignments
+ return TaskAssignResult(assignments=[])
+
+ elif schema_name == 'WorkerConf':
+ # Create a generic worker config
+ task_content = ""
+ if context and 'task_content' in context:
+ task_content = context['task_content'][:50]
+
+ return WorkerConf(
+ role="General Assistant",
+ sys_msg=f"You are a general assistant. Error during "
+ f"configuration: {error_message}",
+ description=f"Fallback worker for task: {task_content}...",
+ )
+
+ elif schema_name == 'RecoveryDecision':
+ # Default to retry strategy
+ return RecoveryDecision(
+ strategy=RecoveryStrategy.RETRY,
+ reasoning=f"Fallback decision due to: {error_message}",
+ modified_task_content=None,
+ )
+
+ else:
+ # Generic fallback
+ try:
+ return schema()
+ except Exception as e:
+ raise ValueError(
+ f"Cannot create fallback for unknown schema: {schema_name}"
+ ) from e
diff --git a/camel/societies/workforce/workforce.py b/camel/societies/workforce/workforce.py
index f581b374a..ce40513ae 100644
--- a/camel/societies/workforce/workforce.py
+++ b/camel/societies/workforce/workforce.py
@@ -47,6 +47,9 @@ from camel.societies.workforce.prompts import (
)
from camel.societies.workforce.role_playing_worker import RolePlayingWorker
from camel.societies.workforce.single_agent_worker import SingleAgentWorker
+from camel.societies.workforce.structured_output_handler import (
+ StructuredOutputHandler,
+)
from camel.societies.workforce.task_channel import TaskChannel
from camel.societies.workforce.utils import (
FailureContext,
@@ -168,6 +171,14 @@ class Workforce(BaseNode):
SingleAgentWorker instances; RolePlayingWorker and nested
Workforce instances do not participate in memory sharing.
(default: :obj:`False`)
+ use_structured_output_handler (bool, optional): Whether to use the
+ structured output handler instead of native structured output.
+ When enabled, the workforce will use prompts with structured
+ output instructions and regex extraction to parse responses.
+ This ensures compatibility with agents that don't reliably
+ support native structured output. When disabled, the workforce
+ uses the native response_format parameter.
+ (default: :obj:`True`)
Example:
>>> import asyncio
@@ -218,6 +229,7 @@ class Workforce(BaseNode):
new_worker_agent: Optional[ChatAgent] = None,
graceful_shutdown_timeout: float = 15.0,
share_memory: bool = False,
+ use_structured_output_handler: bool = True,
) -> None:
super().__init__(description)
self._child_listening_tasks: Deque[
@@ -227,6 +239,9 @@ class Workforce(BaseNode):
self.new_worker_agent = new_worker_agent
self.graceful_shutdown_timeout = graceful_shutdown_timeout
self.share_memory = share_memory
+ self.use_structured_output_handler = use_structured_output_handler
+ if self.use_structured_output_handler:
+ self.structured_handler = StructuredOutputHandler()
self.metrics_logger = WorkforceLogger(workforce_id=self.node_id)
self._task: Optional[Task] = None
self._pending_tasks: Deque[Task] = deque()
@@ -729,12 +744,55 @@ class Workforce(BaseNode):
)
try:
- # Get decision from task agent
- self.task_agent.reset()
- response = self.task_agent.step(
- analysis_prompt, response_format=RecoveryDecision
- )
- return response.msg.parsed
+ # Check if we should use structured handler
+ if self.use_structured_output_handler:
+ # Use structured handler
+ enhanced_prompt = (
+ self.structured_handler.generate_structured_prompt(
+ base_prompt=analysis_prompt,
+ schema=RecoveryDecision,
+ examples=[
+ {
+ "strategy": "RETRY",
+ "reasoning": "Temporary network error, "
+ "worth retrying",
+ "modified_task_content": None,
+ }
+ ],
+ )
+ )
+
+ self.task_agent.reset()
+ response = self.task_agent.step(enhanced_prompt)
+
+ result = self.structured_handler.parse_structured_response(
+ response.msg.content if response.msg else "",
+ schema=RecoveryDecision,
+ fallback_values={
+ "strategy": RecoveryStrategy.RETRY,
+ "reasoning": "Defaulting to retry due to parsing "
+ "issues",
+ "modified_task_content": None,
+ },
+ )
+ # Ensure we return a RecoveryDecision instance
+ if isinstance(result, RecoveryDecision):
+ return result
+ elif isinstance(result, dict):
+ return RecoveryDecision(**result)
+ else:
+ return RecoveryDecision(
+ strategy=RecoveryStrategy.RETRY,
+ reasoning="Failed to parse recovery decision",
+ modified_task_content=None,
+ )
+ else:
+ # Use existing native structured output code
+ self.task_agent.reset()
+ response = self.task_agent.step(
+ analysis_prompt, response_format=RecoveryDecision
+ )
+ return response.msg.parsed
except Exception as e:
logger.warning(
@@ -1338,6 +1396,9 @@ class Workforce(BaseNode):
start_coroutine, self._loop
)
self._child_listening_tasks.append(child_task)
+ else:
+ # Close the coroutine to prevent RuntimeWarning
+ start_coroutine.close()
def add_single_agent_worker(
self,
@@ -1372,6 +1433,7 @@ class Workforce(BaseNode):
description=description,
worker=worker,
pool_max_size=pool_max_size,
+ use_structured_output_handler=self.use_structured_output_handler,
)
self._children.append(worker_node)
@@ -1450,6 +1512,7 @@ class Workforce(BaseNode):
user_agent_kwargs=user_agent_kwargs,
summarize_agent_kwargs=summarize_agent_kwargs,
chat_turn_limit=chat_turn_limit,
+ use_structured_output_handler=self.use_structured_output_handler,
)
self._children.append(worker_node)
@@ -1623,26 +1686,73 @@ class Workforce(BaseNode):
)
prompt = prompt + f"\n\n{feedback}"
- response = self.coordinator_agent.step(
- prompt, response_format=TaskAssignResult
- )
-
- if response.msg is None or response.msg.content is None:
- logger.error(
- "Coordinator agent returned empty response for task assignment"
+ # Check if we should use structured handler
+ if self.use_structured_output_handler:
+ # Use structured handler for prompt-based extraction
+ enhanced_prompt = (
+ self.structured_handler.generate_structured_prompt(
+ base_prompt=prompt,
+ schema=TaskAssignResult,
+ examples=[
+ {
+ "assignments": [
+ {
+ "task_id": "task_1",
+ "assignee_id": "worker_123",
+ "dependencies": [],
+ }
+ ]
+ }
+ ],
+ )
)
- return TaskAssignResult(assignments=[])
- try:
- result_dict = json.loads(response.msg.content, parse_int=str)
- return TaskAssignResult(**result_dict)
- except json.JSONDecodeError as e:
- logger.error(
- f"JSON parsing error in task assignment: Invalid response "
- f"format - {e}. Response content: "
- f"{response.msg.content[:50]}..."
+ # Get response without structured format
+ response = self.coordinator_agent.step(enhanced_prompt)
+
+ if response.msg is None or response.msg.content is None:
+ logger.error(
+ "Coordinator agent returned empty response for "
+ "task assignment"
+ )
+ return TaskAssignResult(assignments=[])
+
+ # Parse with structured handler
+ result = self.structured_handler.parse_structured_response(
+ response.msg.content,
+ schema=TaskAssignResult,
+ fallback_values={"assignments": []},
+ )
+ # Ensure we return a TaskAssignResult instance
+ if isinstance(result, TaskAssignResult):
+ return result
+ elif isinstance(result, dict):
+ return TaskAssignResult(**result)
+ else:
+ return TaskAssignResult(assignments=[])
+ else:
+ # Use existing native structured output code
+ response = self.coordinator_agent.step(
+ prompt, response_format=TaskAssignResult
)
- return TaskAssignResult(assignments=[])
+
+ if response.msg is None or response.msg.content is None:
+ logger.error(
+ "Coordinator agent returned empty response for "
+ "task assignment"
+ )
+ return TaskAssignResult(assignments=[])
+
+ try:
+ result_dict = json.loads(response.msg.content, parse_int=str)
+ return TaskAssignResult(**result_dict)
+ except json.JSONDecodeError as e:
+ logger.error(
+ f"JSON parsing error in task assignment: Invalid response "
+ f"format - {e}. Response content: "
+ f"{response.msg.content[:50]}..."
+ )
+ return TaskAssignResult(assignments=[])
def _validate_assignments(
self, assignments: List[TaskAssignment], valid_ids: Set[str]
@@ -1844,6 +1954,10 @@ class Workforce(BaseNode):
logger.error(
f"Failed to post task {task.id} to {assignee_id}: {e}"
)
+ print(
+ f"{Fore.RED}Failed to post task {task.id} to {assignee_id}: "
+ f"{e}{Fore.RESET}"
+ )
async def _post_dependency(self, dependency: Task) -> None:
await self._channel.post_dependency(dependency, self.node_id)
@@ -1864,35 +1978,92 @@ class Workforce(BaseNode):
child_nodes_info=self._get_child_nodes_info(),
additional_info=task.additional_info,
)
- response = self.coordinator_agent.step(
- prompt, response_format=WorkerConf
- )
- if response.msg is None or response.msg.content is None:
- logger.error(
- "Coordinator agent returned empty response for worker creation"
- )
- # Create a fallback worker configuration
- new_node_conf = WorkerConf(
- description=f"Fallback worker for "
- f"task: {task.content[:50]}...",
- role="General Assistant",
- sys_msg="You are a general assistant that can help "
- "with various tasks.",
+ # Check if we should use structured handler
+ if self.use_structured_output_handler:
+ # Use structured handler
+ enhanced_prompt = (
+ self.structured_handler.generate_structured_prompt(
+ base_prompt=prompt,
+ schema=WorkerConf,
+ examples=[
+ {
+ "description": "Data analysis specialist",
+ "role": "Data Analyst",
+ "sys_msg": "You are an expert data analyst.",
+ }
+ ],
+ )
)
+
+ response = self.coordinator_agent.step(enhanced_prompt)
+
+ if response.msg is None or response.msg.content is None:
+ logger.error(
+ "Coordinator agent returned empty response for "
+ "worker creation"
+ )
+ new_node_conf = WorkerConf(
+ description=f"Fallback worker for task: "
+ f"{task.content[:50]}...",
+ role="General Assistant",
+ sys_msg="You are a general assistant that can help "
+ "with various tasks.",
+ )
+ else:
+ result = self.structured_handler.parse_structured_response(
+ response.msg.content,
+ schema=WorkerConf,
+ fallback_values={
+ "description": f"Worker for task: "
+ f"{task.content[:50]}...",
+ "role": "Task Specialist",
+ "sys_msg": f"You are a specialist for: {task.content}",
+ },
+ )
+ # Ensure we have a WorkerConf instance
+ if isinstance(result, WorkerConf):
+ new_node_conf = result
+ elif isinstance(result, dict):
+ new_node_conf = WorkerConf(**result)
+ else:
+ new_node_conf = WorkerConf(
+ description=f"Worker for task: {task.content[:50]}...",
+ role="Task Specialist",
+ sys_msg=f"You are a specialist for: {task.content}",
+ )
else:
- try:
- result_dict = json.loads(response.msg.content)
- new_node_conf = WorkerConf(**result_dict)
- except json.JSONDecodeError as e:
+ # Use existing native structured output code
+ response = self.coordinator_agent.step(
+ prompt, response_format=WorkerConf
+ )
+ if response.msg is None or response.msg.content is None:
logger.error(
- f"JSON parsing error in worker creation: Invalid response "
- f"format - {e}. Response content: "
- f"{response.msg.content[:100]}..."
+ "Coordinator agent returned empty response for "
+ "worker creation"
)
- raise RuntimeError(
- f"Failed to create worker for task {task.id}: "
- f"Coordinator agent returned malformed JSON response. "
+ # Create a fallback worker configuration
+ new_node_conf = WorkerConf(
+ description=f"Fallback worker for "
+ f"task: {task.content[:50]}...",
+ role="General Assistant",
+ sys_msg="You are a general assistant that can help "
+ "with various tasks.",
)
+ else:
+ try:
+ result_dict = json.loads(response.msg.content)
+ new_node_conf = WorkerConf(**result_dict)
+ except json.JSONDecodeError as e:
+ logger.error(
+ f"JSON parsing error in worker creation: Invalid "
+ f"response format - {e}. Response content: "
+ f"format - {e}. Response content: "
+ f"{response.msg.content[:100]}..."
+ )
+ raise RuntimeError(
+ f"Failed to create worker for task {task.id}: "
+ f"Coordinator agent returned malformed JSON response. "
+ ) from e
new_agent = await self._create_new_agent(
new_node_conf.role,
@@ -1903,6 +2074,7 @@ class Workforce(BaseNode):
description=new_node_conf.description,
worker=new_agent,
pool_max_size=DEFAULT_WORKER_POOL_SIZE,
+ use_structured_output_handler=self.use_structured_output_handler,
)
new_node.set_channel(self._channel)
@@ -2129,101 +2301,107 @@ class Workforce(BaseNode):
f"{recovery_decision.reasoning}"
)
- if recovery_decision.strategy == RecoveryStrategy.RETRY:
- # Simply retry the task by reposting it
- if task.id in self._assignees:
- assignee_id = self._assignees[task.id]
- await self._post_task(task, assignee_id)
- action_taken = f"retried with same worker {assignee_id}"
- else:
- # Find a new assignee and retry
- batch_result = await self._find_assignee([task])
- assignment = batch_result.assignments[0]
- self._assignees[task.id] = assignment.assignee_id
- await self._post_task(task, assignment.assignee_id)
- action_taken = (
- f"retried with new worker {assignment.assignee_id}"
- )
+ # Clean up tracking before attempting recovery
+ if task.id in self._assignees:
+ await self._channel.archive_task(task.id)
+ self._cleanup_task_tracking(task.id)
- elif recovery_decision.strategy == RecoveryStrategy.REPLAN:
- # Modify the task content and retry
- if recovery_decision.modified_task_content:
- task.content = recovery_decision.modified_task_content
- logger.info(f"Task {task.id} content modified for replan")
+ try:
+ if recovery_decision.strategy == RecoveryStrategy.RETRY:
+ # Simply retry the task by reposting it
+ if task.id in self._assignees:
+ assignee_id = self._assignees[task.id]
+ await self._post_task(task, assignee_id)
+ action_taken = f"retried with same worker {assignee_id}"
+ else:
+ # Find a new assignee and retry
+ batch_result = await self._find_assignee([task])
+ assignment = batch_result.assignments[0]
+ self._assignees[task.id] = assignment.assignee_id
+ await self._post_task(task, assignment.assignee_id)
+ action_taken = (
+ f"retried with new worker {assignment.assignee_id}"
+ )
- # Repost the modified task
- if task.id in self._assignees:
- assignee_id = self._assignees[task.id]
- await self._post_task(task, assignee_id)
- action_taken = (
- f"replanned and retried with worker {assignee_id}"
- )
- else:
- # Find a new assignee for the replanned task
- batch_result = await self._find_assignee([task])
- assignment = batch_result.assignments[0]
- self._assignees[task.id] = assignment.assignee_id
- await self._post_task(task, assignment.assignee_id)
- action_taken = (
- f"replanned and assigned to "
- f"worker {assignment.assignee_id}"
- )
+ elif recovery_decision.strategy == RecoveryStrategy.REPLAN:
+ # Modify the task content and retry
+ if recovery_decision.modified_task_content:
+ task.content = recovery_decision.modified_task_content
+ logger.info(f"Task {task.id} content modified for replan")
- elif recovery_decision.strategy == RecoveryStrategy.DECOMPOSE:
- # Decompose the task into subtasks
- subtasks = self._decompose_task(task)
- if self.metrics_logger and subtasks:
- self.metrics_logger.log_task_decomposed(
- parent_task_id=task.id,
- subtask_ids=[st.id for st in subtasks],
- )
- for subtask in subtasks:
- self.metrics_logger.log_task_created(
- task_id=subtask.id,
- description=subtask.content,
- parent_task_id=task.id,
- task_type=subtask.type,
- metadata=subtask.additional_info,
+ # Repost the modified task
+ if task.id in self._assignees:
+ assignee_id = self._assignees[task.id]
+ await self._post_task(task, assignee_id)
+ action_taken = (
+ f"replanned and retried with worker {assignee_id}"
+ )
+ else:
+ # Find a new assignee for the replanned task
+ batch_result = await self._find_assignee([task])
+ assignment = batch_result.assignments[0]
+ self._assignees[task.id] = assignment.assignee_id
+ await self._post_task(task, assignment.assignee_id)
+ action_taken = (
+ f"replanned and assigned to "
+ f"worker {assignment.assignee_id}"
)
- # Insert packets at the head of the queue
- self._pending_tasks.extendleft(reversed(subtasks))
-
- await self._post_ready_tasks()
- action_taken = f"decomposed into {len(subtasks)} subtasks"
- # Handle task completion differently for decomposed tasks
- if task.id in self._assignees:
- await self._channel.archive_task(task.id)
+ elif recovery_decision.strategy == RecoveryStrategy.DECOMPOSE:
+ # Decompose the task into subtasks
+ subtasks = self._decompose_task(task)
+ if self.metrics_logger and subtasks:
+ self.metrics_logger.log_task_decomposed(
+ parent_task_id=task.id,
+ subtask_ids=[st.id for st in subtasks],
+ )
+ for subtask in subtasks:
+ self.metrics_logger.log_task_created(
+ task_id=subtask.id,
+ description=subtask.content,
+ parent_task_id=task.id,
+ task_type=subtask.type,
+ metadata=subtask.additional_info,
+ )
+ # Insert packets at the head of the queue
+ self._pending_tasks.extendleft(reversed(subtasks))
- self._cleanup_task_tracking(task.id)
- logger.debug(
- f"Task {task.id} failed and was {action_taken}. "
- f"Dependencies updated for subtasks."
- )
+ await self._post_ready_tasks()
+ action_taken = f"decomposed into {len(subtasks)} subtasks"
- # Sync shared memory after task decomposition
- if self.share_memory:
- logger.info(
- f"Syncing shared memory after task {task.id} decomposition"
+ logger.debug(
+ f"Task {task.id} failed and was {action_taken}. "
+ f"Dependencies updated for subtasks."
)
- self._sync_shared_memory()
- # Check if any pending tasks are now ready to execute
- await self._post_ready_tasks()
- return False
+ # Sync shared memory after task decomposition
+ if self.share_memory:
+ logger.info(
+ f"Syncing shared memory after "
+ f"task {task.id} decomposition"
+ )
+ self._sync_shared_memory()
- elif recovery_decision.strategy == RecoveryStrategy.CREATE_WORKER:
- assignee = await self._create_worker_node_for_task(task)
- await self._post_task(task, assignee.node_id)
- action_taken = (
- f"created new worker {assignee.node_id} and assigned "
- f"task {task.id} to it"
- )
+ # Check if any pending tasks are now ready to execute
+ await self._post_ready_tasks()
+ return False
- if task.id in self._assignees:
- await self._channel.archive_task(task.id)
+ elif recovery_decision.strategy == RecoveryStrategy.CREATE_WORKER:
+ assignee = await self._create_worker_node_for_task(task)
+ await self._post_task(task, assignee.node_id)
+ action_taken = (
+ f"created new worker {assignee.node_id} and assigned "
+ f"task {task.id} to it"
+ )
+ except Exception as e:
+ logger.error(f"Recovery strategy failed for task {task.id}: {e}")
+ # If max retries reached, halt the workforce
+ if task.failure_count >= MAX_TASK_RETRIES:
+ self._completed_tasks.append(task)
+ return True
+ self._completed_tasks.append(task)
+ return False
- self._cleanup_task_tracking(task.id)
logger.debug(
f"Task {task.id} failed and was {action_taken}. "
f"Updating dependency state."
@@ -2716,6 +2894,7 @@ class Workforce(BaseNode):
else None,
graceful_shutdown_timeout=self.graceful_shutdown_timeout,
share_memory=self.share_memory,
+ use_structured_output_handler=self.use_structured_output_handler,
)
for child in self._children:
diff --git a/examples/workforce/eigent.py b/examples/workforce/eigent.py
index 6a52a853d..d1ea522bb 100644
--- a/examples/workforce/eigent.py
+++ b/examples/workforce/eigent.py
@@ -21,9 +21,9 @@ from camel.models import BaseModelBackend, ModelFactory
from camel.societies.workforce import Workforce
from camel.tasks.task import Task
from camel.toolkits import (
+ AgentCommunicationToolkit,
AudioAnalysisToolkit,
Crawl4AIToolkit,
- EdgeOnePagesMCPToolkit,
ExcelToolkit,
FileWriteToolkit,
# GoogleDriveMCPToolkit,
@@ -79,14 +79,12 @@ def send_message_to_user(message: str) -> None:
def developer_agent_factory(
model: BaseModelBackend,
task_id: str,
- edgeone_pages_mcp_toolkit: EdgeOnePagesMCPToolkit,
):
r"""Factory for creating a developer agent."""
tools = [
send_message_to_user,
HumanToolkit().ask_human_via_console,
*TerminalToolkit(clone_current_env=True).get_tools(),
- *edgeone_pages_mcp_toolkit.get_tools(),
]
system_message = """You are a skilled coding assistant. You can write and
@@ -109,13 +107,13 @@ def developer_agent_factory(
using `pip` in the terminal.
- Implementing complete, production-ready code rather than theoretical
examples.
- - Using the `edgeone_pages_mcp_toolkit` to create and edit web pages.
- After you create a web page, you can ask the search agent to visit it for
- verification.
- Demonstrating results with proper error handling and practical
implementation.
- Asking for human input via the console if you are stuck or need
clarification.
+ - Communicating with other agents using messaging tools. You can use
+ `list_available_agents` to see available team members and `send_message`
+ to coordinate with them for complex tasks requiring collaboration.
Remember to manage your terminal sessions. You can create new sessions
and run commands in them.
@@ -131,7 +129,7 @@ def developer_agent_factory(
)
-@api_keys_required([(None, 'EXA_API_KEY'), (None, 'GOOGLE_API_KEY')])
+@api_keys_required([(None, 'EXA_API_KEY')])
def search_agent_factory(
model: BaseModelBackend,
task_id: str,
@@ -140,10 +138,6 @@ def search_agent_factory(
structure.
"""
tools = [
- # FunctionTool(SearchToolkit().search_wiki),
- SearchToolkit().search_google,
- SearchToolkit().search_bing,
- # FunctionTool(SearchToolkit().search_baidu),
*HybridBrowserToolkit(headless=False).get_tools(),
*TerminalToolkit().get_tools(),
send_message_to_user,
@@ -162,15 +156,18 @@ def search_agent_factory(
workflow.
### Web Search Workflow
- 1. **Search First**: You MUST start by using a search engine to get
- initial URLs. Use `search_google` for English queries and
- `search_bing` for Chinese queries. Do not use browser tools before
- this step.
- 2. **Browse URLs**: After getting search results, use the `visit_page`
- tool from the browser toolkit to open promising URLs.
- 3. **Explore and Scrape**: Once on a page, use browser tools to read
- content, scrape information, and navigate to other linked pages to
- gather all necessary data.
+ 1. **Open Browser**: You MUST start by using the `open_browser` tool to
+ launch a browser. This will automatically open a search engine page.
+ 2. **Perform Search**: Use the `type` tool to enter your search query
+ into the search bar on the page. Then, use the `click` tool to
+ press the search button.
+ 3. **Analyze Search Results**: After the search results load, use
+ `get_page_snapshot` to see the available links.
+ 4. **Navigate and Explore**: Instead of using `visit_page` with a URL
+ you guess, you MUST `click` on a link from the snapshot to navigate
+ to a page. Once on a page, use browser tools to read content, scrape
+ information, and navigate to other linked pages to gather all
+ necessary data. This avoids errors from using incorrect URLs.
### Core Principles
- For each decision you make and action you take, you must send a message
@@ -179,6 +176,11 @@ def search_agent_factory(
a broader perspective and help validate existing knowledge.
- If one way fails to provide an answer, try other ways or methods. The
answer does exist.
+ - Communicate with other agents using messaging tools when needed. You can
+ use `list_available_agents` to see available team members and
+ `send_message` to coordinate with them, especially when you need
+ assistance from developers
+ or document processing capabilities.
### Note Taking
- As you find information, you MUST use the `take_note` tool to record
@@ -250,6 +252,10 @@ def document_agent_factory(
get all the information gathered by the Search Agent.
- The notes contain all the raw data, findings, and sources you need
to complete your work.
+ - You can communicate with other agents using messaging tools when you
+ need additional information. Use `list_available_agents` to see
+ available team members and `send_message` to request specific data or
+ clarifications.
2. Document Creation & Editing:
- Create and write to various file formats including Markdown (.md),
@@ -353,6 +359,13 @@ def multi_modal_agent_factory(model: BaseModelBackend, task_id: str):
- Ask questions to users and receive their responses
- Send informative messages to users without requiring responses
+ 5. Agent Communication:
+ - Communicate with other agents using messaging tools when
+ collaboration is needed. Use `list_available_agents` to see available
+ team members and `send_message` to coordinate with them, especially
+ when you need to share analysis results or request additional
+ processing capabilities.
+
When working with multi-modal content, you should:
- Provide detailed and accurate descriptions of media content
- Extract relevant information based on user queries
@@ -420,6 +433,12 @@ Your integrated toolkits enable you to:
7. Human Interaction (HumanToolkit):
- Ask questions to users and send messages via console.
+8. Agent Communication:
+ - Communicate with other agents using messaging tools when collaboration
+ is needed. Use `list_available_agents` to see available team members and
+ `send_message` to coordinate with them, especially when you need content
+ from document agents or research from search agents.
+
When assisting users, always:
- Identify which platform's functionality is needed for the task.
- Check if required API credentials are available before attempting
@@ -444,116 +463,151 @@ operations.
async def main():
- edgeone_pages_mcp_toolkit = EdgeOnePagesMCPToolkit()
# google_drive_mcp_toolkit = GoogleDriveMCPToolkit(
# credentials_path="path/to/credentials.json"
# )
- try:
- await edgeone_pages_mcp_toolkit.connect()
- # await google_drive_mcp_toolkit.connect()
-
- # Create a single model backend for all agents
- model_backend = ModelFactory.create(
- model_platform=ModelPlatformType.OPENAI,
- model_type=ModelType.GPT_4_1_MINI,
- # model_config_dict={
- # "max_tokens": 32768,
- # },
- )
-
- model_backend_reason = ModelFactory.create(
- model_platform=ModelPlatformType.OPENAI,
- model_type=ModelType.GPT_4_1_MINI,
- # model_config_dict={
- # "max_tokens": 32768,
- # },
- )
-
- task_id = 'workforce_task'
-
- # Create custom agents for the workforce
- coordinator_agent = ChatAgent(
- "You are a helpful coordinator. You MUST use the "
- "`send_message_to_user` tool to inform the user of every "
- "decision and action you take. Your message must include a short "
- "title and a one-sentence description. This is a mandatory part "
- "of your workflow.",
- model=model_backend_reason,
- tools=[
- send_message_to_user,
- ],
- )
- task_agent = ChatAgent(
- "You are a helpful task planner. You MUST use the "
- "`send_message_to_user` tool to inform the user of every decision "
- "and action you take. Your message must include a short title and "
- "a one-sentence description. This is a mandatory part of your "
- "workflow.",
- model=model_backend_reason,
- tools=[
- send_message_to_user,
- ],
- )
- new_worker_agent = ChatAgent(
- "You are a helpful worker. You MUST use the "
- "`send_message_to_user` tool to inform the user of every "
- "decision and action you take. Your message must include a short "
- "title and a one-sentence description. This is a mandatory part "
- "of your workflow.",
- model=model_backend,
- tools=[
- send_message_to_user,
- HumanToolkit().ask_human_via_console,
- ],
- )
-
- # Create agents using factory functions
- search_agent = search_agent_factory(model_backend, task_id)
- developer_agent = developer_agent_factory(
- model_backend, task_id, edgeone_pages_mcp_toolkit
- )
- document_agent = document_agent_factory(
- model_backend,
- task_id,
- # google_drive_mcp_toolkit
- )
- multi_modal_agent = multi_modal_agent_factory(model_backend, task_id)
-
- # Create workforce instance before adding workers
- workforce = Workforce(
- 'A workforce',
- graceful_shutdown_timeout=30.0, # 30 seconds for debugging
- share_memory=False,
- coordinator_agent=coordinator_agent,
- task_agent=task_agent,
- new_worker_agent=new_worker_agent,
- )
-
- workforce.add_single_agent_worker(
- "Search Agent: Can search the web, extract webpage content, "
- "simulate browser actions, and provide relevant information to "
- "solve the given task.",
- worker=search_agent,
- ).add_single_agent_worker(
- "Developer Agent: A skilled coding assistant that can write and "
- "execute code, run terminal commands, and verify solutions to "
- "complete tasks.",
- worker=developer_agent,
- ).add_single_agent_worker(
- "Document Agent: A document processing assistant for creating, "
- "modifying, and managing various document formats, including "
- "presentations.",
- worker=document_agent,
- ).add_single_agent_worker(
- "Multi-Modal Agent: A multi-modal processing assistant for "
- "analyzing, and generating media content like audio and images.",
- worker=multi_modal_agent,
- )
-
- # specify the task to be solved
- human_task = Task(
- content=(
- """
+
+ # Initialize the AgentCommunicationToolkit
+ msg_toolkit = AgentCommunicationToolkit(max_message_history=100)
+
+ # await google_drive_mcp_toolkit.connect()
+
+ # Create a single model backend for all agents
+ model_backend = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.GPT_4_1_MINI,
+ # model_config_dict={
+ # "max_tokens": 32768,
+ # },
+ )
+
+ model_backend_reason = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.GPT_4_1_MINI,
+ # model_config_dict={
+ # "max_tokens": 32768,
+ # },
+ )
+
+ task_id = 'workforce_task'
+
+ # Create custom agents for the workforce
+ coordinator_agent = ChatAgent(
+ "You are a helpful coordinator. You MUST use the "
+ "`send_message_to_user` tool to inform the user of every "
+ "decision and action you take. Your message must include a short "
+ "title and a one-sentence description. This is a mandatory part "
+ "of your workflow.",
+ model=model_backend_reason,
+ tools=[
+ send_message_to_user,
+ ],
+ )
+ task_agent = ChatAgent(
+ "You are a helpful task planner. You MUST use the "
+ "`send_message_to_user` tool to inform the user of every decision "
+ "and action you take. Your message must include a short title and "
+ "a one-sentence description. This is a mandatory part of your "
+ "workflow.",
+ model=model_backend_reason,
+ tools=[
+ send_message_to_user,
+ ],
+ )
+ new_worker_agent = ChatAgent(
+ "You are a helpful worker. You MUST use the "
+ "`send_message_to_user` tool to inform the user of every "
+ "decision and action you take. Your message must include a short "
+ "title and a one-sentence description. This is a mandatory part "
+ "of your workflow. You can also communicate with other agents "
+ "using messaging tools - use `list_available_agents` to see "
+ "available team members and `send_message` to coordinate work "
+ "and ask for help when needed.",
+ model=model_backend,
+ tools=[
+ send_message_to_user,
+ HumanToolkit().ask_human_via_console,
+ ],
+ )
+
+ # Create agents using factory functions
+ search_agent = search_agent_factory(model_backend, task_id)
+ developer_agent = developer_agent_factory(
+ model_backend,
+ task_id,
+ )
+ document_agent = document_agent_factory(
+ model_backend,
+ task_id,
+ # google_drive_mcp_toolkit
+ )
+ multi_modal_agent = multi_modal_agent_factory(model_backend, task_id)
+ # social_medium_agent = social_medium_agent_factory(
+ # model_backend, task_id
+ # )
+
+ # Register all agents with the communication toolkit
+ # msg_toolkit.register_agent("Coordinator", coordinator_agent)
+ # msg_toolkit.register_agent("Task_Planner", task_agent)
+ msg_toolkit.register_agent("Worker", new_worker_agent)
+ msg_toolkit.register_agent("Search_Agent", search_agent)
+ msg_toolkit.register_agent("Developer_Agent", developer_agent)
+ msg_toolkit.register_agent("Document_Agent", document_agent)
+ msg_toolkit.register_agent("Multi_Modal_Agent", multi_modal_agent)
+ # msg_toolkit.register_agent("Social_Medium_Agent",
+ # social_medium_agent)
+
+ # # Add communication tools to all agents
+ # communication_tools = msg_toolkit.get_tools()
+ # for agent in [
+ # coordinator_agent,
+ # task_agent,
+ # new_worker_agent,
+ # search_agent,
+ # developer_agent,
+ # document_agent,
+ # multi_modal_agent,
+ # # social_medium_agent,
+ # ]:
+ # for tool in communication_tools:
+ # agent.add_tool(tool)
+
+ # Create workforce instance before adding workers
+ workforce = Workforce(
+ 'A workforce',
+ graceful_shutdown_timeout=30.0, # 30 seconds for debugging
+ share_memory=False,
+ coordinator_agent=coordinator_agent,
+ task_agent=task_agent,
+ new_worker_agent=new_worker_agent,
+ use_structured_output_handler=False,
+ )
+
+ workforce.add_single_agent_worker(
+ "Search Agent: Can search the web, extract webpage content, "
+ "simulate browser actions, and provide relevant information to "
+ "solve the given task.",
+ worker=search_agent,
+ ).add_single_agent_worker(
+ "Developer Agent: A skilled coding assistant that can write and "
+ "execute code, run terminal commands, and verify solutions to "
+ "complete tasks.",
+ worker=developer_agent,
+ ).add_single_agent_worker(
+ "Document Agent: A document processing assistant for creating, "
+ "modifying, and managing various document formats, including "
+ "presentations.",
+ worker=document_agent,
+ ).add_single_agent_worker(
+ "Multi-Modal Agent: A multi-modal processing assistant for "
+ "analyzing, and generating media content like audio and images.",
+ worker=multi_modal_agent,
+ )
+
+ # specify the task to be solved
+ human_task = Task(
+ content=(
+ """
Analyze the UK healthcare industry to support the planning of my next company.
Provide a comprehensive market overview, including current trends, growth
projections, and relevant regulations. Identify the top 5-10 major competitors
@@ -561,31 +615,27 @@ in the space, including their names, website URLs, estimated market size or
share, core services or products, key strengths, and notable weaknesses. Also
highlight any significant opportunities, gaps, or underserved segments within
the market. Present all findings in a well-structured, professional PDF report.
- """
- ),
- id='0',
- )
-
- # Use the async version directly to avoid hanging with async tools
- await workforce.process_task_async(human_task)
-
- # Test WorkforceLogger features
- print("\n--- Workforce Log Tree ---")
- print(workforce.get_workforce_log_tree())
-
- print("\n--- Workforce KPIs ---")
- kpis = workforce.get_workforce_kpis()
- for key, value in kpis.items():
- print(f"{key}: {value}")
-
- log_file_path = "eigent_logs.json"
- print(f"\n--- Dumping Workforce Logs to {log_file_path} ---")
- workforce.dump_workforce_logs(log_file_path)
- print(f"Logs dumped. Please check the file: {log_file_path}")
-
- finally:
- await edgeone_pages_mcp_toolkit.disconnect()
- # await google_drive_mcp_toolkit.disconnect()
+ """
+ ),
+ id='0',
+ )
+
+ # Use the async version directly to avoid hanging with async tools
+ await workforce.process_task_async(human_task)
+
+ # Test WorkforceLogger features
+ print("\n--- Workforce Log Tree ---")
+ print(workforce.get_workforce_log_tree())
+
+ print("\n--- Workforce KPIs ---")
+ kpis = workforce.get_workforce_kpis()
+ for key, value in kpis.items():
+ print(f"{key}: {value}")
+
+ log_file_path = "eigent_logs.json"
+ print(f"\n--- Dumping Workforce Logs to {log_file_path} ---")
+ workforce.dump_workforce_logs(log_file_path)
+ print(f"Logs dumped. Please check the file: {log_file_path}")
if __name__ == "__main__":