🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: task_stream (#2829)
Tao Sun30d48a3dddcd
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.ymlindex b1bd4d981..e11599cd5 100644--- a/.github/ISSUE_TEMPLATE/bug_report.yml+++ b/.github/ISSUE_TEMPLATE/bug_report.yml@@ -26,7 +26,7 @@ body:attributes:label: What version of camel are you using?description: Run command `python3 -c 'print(__import__("camel").__version__)'` in your shell and paste the output here.- placeholder: E.g., 0.2.71a8+ placeholder: E.g., 0.2.71a9validations:required: truediff --git a/camel/__init__.py b/camel/__init__.pyindex 3f2754a3f..4bff3b408 100644--- a/camel/__init__.py+++ b/camel/__init__.py@@ -14,7 +14,7 @@from camel.logger import disable_logging, enable_logging, set_log_level-__version__ = '0.2.71a8'+__version__ = '0.2.71a9'__all__ = ['__version__',diff --git a/camel/societies/workforce/single_agent_worker.py b/camel/societies/workforce/single_agent_worker.pyindex a7e7a35e9..18d3e4549 100644--- a/camel/societies/workforce/single_agent_worker.py+++ b/camel/societies/workforce/single_agent_worker.py@@ -22,6 +22,7 @@ from typing import Any, List, Optionalfrom colorama import Forefrom camel.agents import ChatAgent+from camel.agents.chat_agent import AsyncStreamingChatAgentResponsefrom camel.societies.workforce.prompts import PROCESS_TASK_PROMPTfrom camel.societies.workforce.structured_output_handler import (StructuredOutputHandler,@@ -285,6 +286,7 @@ class SingleAgentWorker(Worker):"""# Get agent efficiently (from pool or by cloning)worker_agent = await self._get_worker_agent()+ response_content = ""try:dependency_tasks_info = self._get_dep_tasks_info(dependencies)@@ -314,11 +316,23 @@ class SingleAgentWorker(Worker):))response = await worker_agent.astep(enhanced_prompt)++ # Handle streaming response+ if isinstance(response, AsyncStreamingChatAgentResponse):+ content = ""+ async for chunk in response:+ if chunk.msg:+ content = chunk.msg.content+ response_content = content+ else:+ # Regular ChatAgentResponse+ response_content = (+ response.msg.content if response.msg else ""+ )+task_result = (self.structured_handler.parse_structured_response(- response_text=response.msg.content- if response.msg- else "",+ response_text=response_content,schema=TaskResult,fallback_values={"content": "Task processing failed",@@ -331,13 +345,41 @@ class SingleAgentWorker(Worker):response = await worker_agent.astep(prompt, response_format=TaskResult)- task_result = response.msg.parsed++ # Handle streaming response for native output+ if isinstance(response, AsyncStreamingChatAgentResponse):+ task_result = None+ async for chunk in response:+ if chunk.msg and chunk.msg.parsed:+ task_result = chunk.msg.parsed+ response_content = chunk.msg.content+ # If no parsed result found in streaming, create fallback+ if task_result is None:+ task_result = TaskResult(+ content="Failed to parse streaming response",+ failed=True,+ )+ else:+ # Regular ChatAgentResponse+ task_result = response.msg.parsed+ response_content = (+ response.msg.content if response.msg else ""+ )# Get token usage from the response- usage_info = response.info.get("usage") or response.info.get(- "token_usage"+ if isinstance(response, AsyncStreamingChatAgentResponse):+ # For streaming responses, get the final response info+ final_response = await response+ usage_info = final_response.info.get(+ "usage"+ ) or final_response.info.get("token_usage")+ else:+ usage_info = response.info.get("usage") or response.info.get(+ "token_usage"+ )+ total_tokens = (+ usage_info.get("total_tokens", 0) if usage_info else 0)- total_tokens = usage_info.get("total_tokens", 0)except Exception as e:print(@@ -369,8 +411,10 @@ class SingleAgentWorker(Worker):f"(from pool/clone of "f"{getattr(self.worker, 'agent_id', self.worker.role_name)}) "f"to process task {task.content}",- "response_content": response.msg.content,- "tool_calls": response.info.get("tool_calls"),+ "response_content": response_content,+ "tool_calls": final_response.info.get("tool_calls")+ if isinstance(response, AsyncStreamingChatAgentResponse)+ else response.info.get("tool_calls"),"total_tokens": total_tokens,}diff --git a/camel/societies/workforce/workforce.py b/camel/societies/workforce/workforce.pyindex 2619075d2..e7f76e255 100644--- a/camel/societies/workforce/workforce.py+++ b/camel/societies/workforce/workforce.py@@ -25,6 +25,7 @@ from typing import (Coroutine,Deque,Dict,+ Generator,List,Optional,Set,@@ -420,6 +421,11 @@ class Workforce(BaseNode):"CodeExecutionToolkit, and ThinkingToolkit. To customize ""runtime worker creation, pass a ChatAgent instance.")+ else:+ # Validate new_worker_agent if provided+ self._validate_agent_compatibility(+ new_worker_agent, "new_worker_agent"+ )if self.share_memory:logger.info(@@ -432,6 +438,42 @@ class Workforce(BaseNode):# Helper for propagating pause control to externally supplied agents# ------------------------------------------------------------------+ def _validate_agent_compatibility(+ self, agent: ChatAgent, agent_context: str = "agent"+ ) -> None:+ r"""Validate that agent configuration is compatible with workforce+ settings.++ Args:+ agent (ChatAgent): The agent to validate.+ agent_context (str): Context description for error messages.++ Raises:+ ValueError: If agent has tools and stream mode enabled but+ use_structured_output_handler is False.+ """+ agent_has_tools = (+ bool(agent.tool_dict) if hasattr(agent, 'tool_dict') else False+ )+ agent_stream_mode = (+ getattr(agent.model_backend, 'stream', False)+ if hasattr(agent, 'model_backend')+ else False+ )++ if (+ agent_has_tools+ and agent_stream_mode+ and not self.use_structured_output_handler+ ):+ raise ValueError(+ f"{agent_context} has tools and stream mode enabled, but "+ "use_structured_output_handler is False. Native structured "+ "output doesn't work with tool calls in stream mode. "+ "Please set use_structured_output_handler=True when creating "+ "the Workforce."+ )+def _attach_pause_event_to_agent(self, agent: ChatAgent) -> None:r"""Ensure the given ChatAgent shares this workforce's pause_event.@@ -678,12 +720,15 @@ class Workforce(BaseNode):if task_id in self._assignees:del self._assignees[task_id]- def _decompose_task(self, task: Task) -> List[Task]:+ def _decompose_task(+ self, task: Task+ ) -> Union[List[Task], Generator[List[Task], None, None]]:r"""Decompose the task into subtasks. This method will also set therelationship between the task and its subtasks.Returns:- List[Task]: The subtasks.+ Union[List[Task], Generator[List[Task], None, None]]:+ The subtasks or generator of subtasks."""decompose_prompt = WF_TASK_DECOMPOSE_PROMPT.format(content=task.content,@@ -691,13 +736,30 @@ class Workforce(BaseNode):additional_info=task.additional_info,)self.task_agent.reset()- subtasks = task.decompose(self.task_agent, decompose_prompt)-- # Update dependency tracking for decomposed task- if subtasks:- self._update_dependencies_for_decomposition(task, subtasks)+ result = task.decompose(self.task_agent, decompose_prompt)++ # Handle both streaming and non-streaming results+ if isinstance(result, Generator):+ # This is a generator (streaming mode)+ def streaming_with_dependencies():+ all_subtasks = []+ for new_tasks in result:+ all_subtasks.extend(new_tasks)+ # Update dependency tracking for each batch of new tasks+ if new_tasks:+ self._update_dependencies_for_decomposition(+ task, all_subtasks+ )+ yield new_tasks- return subtasks+ return streaming_with_dependencies()+ else:+ # This is a regular list (non-streaming mode)+ subtasks = result+ # Update dependency tracking for decomposed task+ if subtasks:+ self._update_dependencies_for_decomposition(task, subtasks)+ return subtasksdef _analyze_failure(self, task: Task, error_message: str@@ -1148,7 +1210,17 @@ class Workforce(BaseNode):task.state = TaskState.FAILED# The agent tend to be overconfident on the whole task, so we# decompose the task into subtasks first- subtasks = self._decompose_task(task)+ subtasks_result = self._decompose_task(task)++ # Handle both streaming and non-streaming results+ if isinstance(subtasks_result, Generator):+ # This is a generator (streaming mode)+ subtasks = []+ for new_tasks in subtasks_result:+ subtasks.extend(new_tasks)+ else:+ # This is a regular list (non-streaming mode)+ subtasks = subtasks_resultif self.metrics_logger and subtasks:self.metrics_logger.log_task_decomposed(parent_task_id=task.id, subtask_ids=[st.id for st in subtasks]@@ -1265,7 +1337,17 @@ class Workforce(BaseNode):task.state = TaskState.FAILED # TODO: Add logic for OPEN# Decompose the task into subtasks first- subtasks = self._decompose_task(task)+ subtasks_result = self._decompose_task(task)++ # Handle both streaming and non-streaming results+ if isinstance(subtasks_result, Generator):+ # This is a generator (streaming mode)+ subtasks = []+ for new_tasks in subtasks_result:+ subtasks.extend(new_tasks)+ else:+ # This is a regular list (non-streaming mode)+ subtasks = subtasks_resultif subtasks:# If decomposition happened, the original task becomes a container.# We only execute its subtasks.@@ -1435,12 +1517,18 @@ class Workforce(BaseNode):Raises:RuntimeError: If called while workforce is running (not paused).+ ValueError: If worker has tools and stream mode enabled but+ use_structured_output_handler is False."""if self._state == WorkforceState.RUNNING:raise RuntimeError("Cannot add workers while workforce is running. ""Pause the workforce first.")++ # Validate worker agent compatibility+ self._validate_agent_compatibility(worker, "Worker agent")+# Ensure the worker agent shares this workforce's pause controlself._attach_pause_event_to_agent(worker)@@ -2084,6 +2172,14 @@ class Workforce(BaseNode):new_node_conf.sys_msg,)+ # Validate the new agent compatibility before creating worker+ try:+ self._validate_agent_compatibility(+ new_agent, f"Agent for task {task.id}"+ )+ except ValueError as e:+ raise ValueError(f"Cannot create worker for task {task.id}: {e!s}")+new_node = SingleAgentWorker(description=new_node_conf.description,worker=new_agent,@@ -2349,7 +2445,17 @@ class Workforce(BaseNode):elif recovery_decision.strategy == RecoveryStrategy.DECOMPOSE:# Decompose the task into subtasks- subtasks = self._decompose_task(task)+ subtasks_result = self._decompose_task(task)++ # Handle both streaming and non-streaming results+ if isinstance(subtasks_result, Generator):+ # This is a generator (streaming mode)+ subtasks = []+ for new_tasks in subtasks_result:+ subtasks.extend(new_tasks)+ else:+ # This is a regular list (non-streaming mode)+ subtasks = subtasks_resultif self.metrics_logger and subtasks:self.metrics_logger.log_task_decomposed(parent_task_id=task.id,@@ -3203,6 +3309,18 @@ class Workforce(BaseNode):)agent = ChatAgent(sys_msg, **(agent_kwargs or {}))++ # Validate agent compatibility+ try:+ workforce_instance._validate_agent_compatibility(+ agent, "Worker agent"+ )+ except ValueError as e:+ return {+ "status": "error",+ "message": str(e),+ }+workforce_instance.add_single_agent_worker(description, agent)return {diff --git a/camel/tasks/task.py b/camel/tasks/task.pyindex da57fe88a..af89d5b67 100644--- a/camel/tasks/task.py+++ b/camel/tasks/task.py@@ -19,6 +19,7 @@ from typing import (Any,Callable,Dict,+ Generator,List,Literal,Optional,@@ -30,6 +31,7 @@ from pydantic import BaseModel, ConfigDict, Fieldif TYPE_CHECKING:from camel.agents import ChatAgent+ from camel.agents.chat_agent import StreamingChatAgentResponseimport uuidfrom camel.logger import get_logger@@ -402,9 +404,9 @@ class Task(BaseModel):agent: "ChatAgent",prompt: Optional[str] = None,task_parser: Callable[[str, str], List["Task"]] = parse_response,- ) -> List["Task"]:- r"""Decompose a task to a list of sub-tasks. It can be used for data- generation and planner of agent.+ ) -> Union[List["Task"], Generator[List["Task"], None, None]]:+ r"""Decompose a task to a list of sub-tasks. Automatically detects+ streaming or non-streaming based on agent configuration.Args:agent (ChatAgent): An agent that used to decompose the task.@@ -415,7 +417,10 @@ class Task(BaseModel):the default parse_response will be used.Returns:- List[Task]: A list of tasks which are :obj:`Task` instances.+ Union[List[Task], Generator[List[Task], None, None]]: If agent is+ configured for streaming, returns a generator that yields lists+ of new tasks as they are parsed. Otherwise returns a list of+ all tasks."""role_name = agent.role_name@@ -427,6 +432,72 @@ class Task(BaseModel):role_name=role_name, content=content)response = agent.step(msg)++ # Auto-detect streaming based on response type+ from camel.agents.chat_agent import StreamingChatAgentResponse++ if isinstance(response, StreamingChatAgentResponse):+ return self._decompose_streaming(response, task_parser)+ else:+ return self._decompose_non_streaming(response, task_parser)++ def _decompose_streaming(+ self,+ response: "StreamingChatAgentResponse",+ task_parser: Callable[[str, str], List["Task"]],+ ) -> Generator[List["Task"], None, None]:+ r"""Handle streaming response for task decomposition.++ Args:+ response: Streaming response from agent+ task_parser: Function to parse tasks from response++ Yields:+ List[Task]: New tasks as they are parsed from streaming response+ """+ accumulated_content = ""+ yielded_count = 0++ # Process streaming response+ for chunk in response:+ accumulated_content = chunk.msg.content++ # Try to parse partial tasks from accumulated content+ try:+ current_tasks = self._parse_partial_tasks(accumulated_content)++ # Yield new tasks if we have more than previously yielded+ if len(current_tasks) > yielded_count:+ new_tasks = current_tasks[yielded_count:]+ for task in new_tasks:+ task.additional_info = self.additional_info+ task.parent = self+ yield new_tasks+ yielded_count = len(current_tasks)++ except Exception:+ # If parsing fails, continue accumulating+ continue++ # Final complete parsing+ final_tasks = task_parser(accumulated_content, self.id)+ for task in final_tasks:+ task.additional_info = self.additional_info+ task.parent = self+ self.subtasks = final_tasks++ def _decompose_non_streaming(+ self, response, task_parser: Callable[[str, str], List["Task"]]+ ) -> List["Task"]:+ r"""Handle non-streaming response for task decomposition.++ Args:+ response: Regular response from agent+ task_parser: Function to parse tasks from response++ Returns:+ List[Task]: All parsed tasks+ """tasks = task_parser(response.msg.content, self.id)for task in tasks:task.additional_info = self.additional_info@@ -434,6 +505,35 @@ class Task(BaseModel):self.subtasks = tasksreturn tasks+ def _parse_partial_tasks(self, response: str) -> List["Task"]:+ r"""Parse tasks from potentially incomplete response.++ Args:+ response: Partial response content++ Returns:+ List[Task]: Tasks parsed from complete <task></task> blocks+ """+ pattern = r"<task>(.*?)</task>"+ tasks_content = re.findall(pattern, response, re.DOTALL)++ tasks = []+ task_id = self.id or "0"++ for i, content in enumerate(tasks_content, 1):+ stripped_content = content.strip()+ if validate_task_content(stripped_content, f"{task_id}.{i}"):+ tasks.append(+ Task(content=stripped_content, id=f"{task_id}.{i}")+ )+ else:+ logger.warning(+ f"Skipping invalid subtask {task_id}.{i} "+ f"during streaming decomposition: "+ f"Content '{stripped_content[:50]}...' failed validation"+ )+ return tasks+def compose(self,agent: "ChatAgent",diff --git a/docs/conf.py b/docs/conf.pyindex bf01c92de..2ee84fb3e 100644--- a/docs/conf.py+++ b/docs/conf.py@@ -27,7 +27,7 @@ sys.path.insert(0, os.path.abspath('..'))project = 'CAMEL'copyright = '2024, CAMEL-AI.org'author = 'CAMEL-AI.org'-release = '0.2.71a8'+release = '0.2.71a9'html_favicon = ('https://raw.githubusercontent.com/camel-ai/camel/master/misc/favicon.png'diff --git a/examples/workforce/eigent.py b/examples/workforce/eigent.pyindex 95140cee9..a48ef9602 100644--- a/examples/workforce/eigent.py+++ b/examples/workforce/eigent.py@@ -188,65 +188,53 @@ def search_agent_factory(extract webpage content, simulate browser actions, and provide relevantinformation to solve the given task.- 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 are now working in `{WORKING_DIRECTORY}`. All your workrelated to local operations should be done in that directory.- You MUST use the `append_note` tool to record your- findings, make sure the note is very detailed and include all the- information you have gathered.-+ ### Mandatory Instructions+ 1. **Inform the User**: 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.+ 2. **Take Detailed Notes**: You MUST use the `append_note` tool to+ record your findings. Ensure notes are detailed, well-organized,+ and include source URLs. Do not overwrite notes unless summarizing;+ append new information. Your notes are crucial for the Document+ Agent.+### Web Search Workflow- 1. **Initial Search**: Start by using `search_google` to get a list of- initial URLs for your research.- 2. **Browser Exploration**: Use the URLs from `search_google` with the- browser tools to investigate pages. You can `visit_page` to open a- webpage, `get_som_screenshot` to analyze its content, and `click` to- navigate.- 3. **Quick Information Gathering**: For quick questions or summaries,- use `search_exa` for fast and direct information retrieval.- 4. **Note Taking**: You MUST use `append_note` to record your findings,- ensuring your notes are detailed and include all gathered- information and sources.-- ### Core Principles- - For each decision you make and action you take, you must send a message- to the user to keep them informed.- - Do not be overly confident in your own knowledge. Searching can provide- 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 `append_note` tool to record- your findings in a structured way.- - Append new information to the notes. Do not overwrite the note file- unless you are summarizing or restructuring the content.- - Your notes will be used by the Document Agent to create the final- report, so make them clear, concise, and well-organized. Include- headings, details, and any relevant URLs or sources.-- ### Guidelines- - If a search query is complex, break it down. Start with broad terms to- find authoritative sources, then narrow your search.- - If a search snippet is unhelpful but the URL seems authoritative, visit- the page to investigate further.- - IMPORTANT: You MUST only use URLs from search results or scraped from- pages you have visited. NEVER invent or guess URLs.- - After visiting a page, check its subpages for more relevant information.- - Combine search, scraper, and browser tools for comprehensive information- gathering.- - In your response, you should mention the URLs you have visited and- processed.+ 1. **Initial Search**: Start with a search engine like `search_google` or+ `search_bing` to get a list of relevant URLs for your research if+ available.+ 2. **Browser-Based Exploration**: Use the rich browser toolset to+ investigate websites.+ - **Navigation**: Use `visit_page` to open a URL. Navigate with `click`,+ `back`, and `forward`. Manage multiple pages with `switch_tab`.+ - **Analysis**: Use `get_som_screenshot` to understand the page layout+ and identify interactive elements. Since this is a heavy operation,+ only use it when visual analysis is necessary.+ - **Interaction**: Use `type` to fill out forms and `enter` to submit.+ 3. **Detailed Content Extraction**: Prioritize using the scraping tools+ from `Crawl4AIToolkit` for in-depth information gathering from a+ webpage.+ 4. **Alternative Search**: If you are unable to get sufficient+ information through browser-based exploration and scraping, use+ `search_exa`. This tool is best used for getting quick summaries or+ finding specific answers when direct browsing is not effective.++ ### Guidelines and Best Practices+ - **URL Integrity**: You MUST only use URLs from trusted sources (e.g.,+ search engine results or links on visited pages). NEVER invent or+ guess URLs.+ - **Thoroughness**: If a search query is complex, break it down. If a+ snippet is unhelpful but the URL seems authoritative, visit the page.+ Check subpages for more information.+ - **Persistence**: If one method fails, try another. Combine search,+ scraper, and browser tools for comprehensive information gathering.+ - **Collaboration**: Communicate with other agents using `send_message`+ when you need help. Use `list_available_agents` to see who is+ available.+ - **Clarity**: In your response, you should mention the URLs you have+ visited and processed.### Handling Obstacles- When encountering verification challenges (like login, CAPTCHAs or@@ -538,17 +526,17 @@ async def main():model_backend = ModelFactory.create(model_platform=ModelPlatformType.OPENAI,model_type=ModelType.GPT_4_1_MINI,- # model_config_dict={- # "max_tokens": 32768,- # },+ model_config_dict={+ "stream": False,+ },)model_backend_reason = ModelFactory.create(model_platform=ModelPlatformType.OPENAI,model_type=ModelType.GPT_4_1_MINI,- # model_config_dict={- # "max_tokens": 32768,- # },+ model_config_dict={+ "stream": False,+ },)task_id = 'workforce_task'diff --git a/pyproject.toml b/pyproject.tomlindex aa41eef3b..5c7ea5b51 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "hatchling.build"[project]name = "camel-ai"-version = "0.2.71a8"+version = "0.2.71a9"description = "Communicative Agents for AI Society Study"authors = [{ name = "CAMEL-AI.org" }]requires-python = ">=3.10,<3.13"diff --git a/uv.lock b/uv.lockindex 853cd2622..3de413221 100644--- a/uv.lock+++ b/uv.lock@@ -647,7 +647,7 @@ wheels = [[[package]]name = "camel-ai"-version = "0.2.71a8"+version = "0.2.71a9"source = { editable = "." }dependencies = [{ name = "colorama" },@@ -1352,11 +1352,11 @@ provides-extras = ["dev", "docs", "rag", "web-tools", "document-tools", "media-t[[package]]name = "certifi"-version = "2025.6.15"+version = "2025.7.9"source = { registry = "https://pypi.org/simple" }-sdist = { url = "https://files.pythonhosted.org/packages/73/f7/f14b46d4bcd21092d7d3ccef689615220d8a08fb25e564b65d20738e672e/certifi-2025.6.15.tar.gz", hash = "sha256:d747aa5a8b9bbbb1bb8c22bb13e22bd1f18e9796defa16bab421f7f7a317323b", size = 158753, upload-time = "2025-06-15T02:45:51.329Z" }+sdist = { url = "https://files.pythonhosted.org/packages/de/8a/c729b6b60c66a38f590c4e774decc4b2ec7b0576be8f1aa984a53ffa812a/certifi-2025.7.9.tar.gz", hash = "sha256:c1d2ec05395148ee10cf672ffc28cd37ea0ab0d99f9cc74c43e588cbd111b079", size = 160386, upload-time = "2025-07-09T02:13:58.874Z" }wheels = [- { url = "https://files.pythonhosted.org/packages/84/ae/320161bd181fc06471eed047ecce67b693fd7515b16d495d8932db763426/certifi-2025.6.15-py3-none-any.whl", hash = "sha256:2e0c7ce7cb5d8f8634ca55d2ba7e6ec2689a2fd6537d8dec1296a477a4910057", size = 157650, upload-time = "2025-06-15T02:45:49.977Z" },+ { url = "https://files.pythonhosted.org/packages/66/f3/80a3f974c8b535d394ff960a11ac20368e06b736da395b551a49ce950cce/certifi-2025.7.9-py3-none-any.whl", hash = "sha256:d842783a14f8fdd646895ac26f719a061408834473cfc10203f6a575beb15d39", size = 159230, upload-time = "2025-07-09T02:13:57.007Z" },][[package]]