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: update workforce and anthropic model (#2682)

Wendong-Fan7d97f6de7a1b
diff --git a/camel/loaders/firecrawl_reader.py b/camel/loaders/firecrawl_reader.py
index 645df3f78..49920f5ee 100644
--- a/camel/loaders/firecrawl_reader.py
+++ b/camel/loaders/firecrawl_reader.py
@@ -98,8 +98,8 @@ class Firecrawl:
def scrape(
self,
url: str,
- params: Optional[Dict[str, Any]] = None,
- ) -> Dict:
+ params: Optional[Dict[str, str]] = None,
+ ) -> Dict[str, str]:
r"""To scrape a single URL. This function supports advanced scraping
by setting different parameters and returns the full scraped data as a
dictionary.
@@ -108,11 +108,11 @@ class Firecrawl:
Args:
url (str): The URL to read.
- params (Optional[Dict[str, Any]]): Additional parameters for the
+ params (Optional[Dict[str, str]]): Additional parameters for the
scrape request.
Returns:
- Dict: The scraped data.
+ Dict[str, str]: The scraped data.
Raises:
RuntimeError: If the scrape process fails.
diff --git a/camel/models/anthropic_model.py b/camel/models/anthropic_model.py
index 1d6896f21..7c18c12db 100644
--- a/camel/models/anthropic_model.py
+++ b/camel/models/anthropic_model.py
@@ -12,11 +12,14 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import os
-from typing import Any, Dict, Optional, Union
+from typing import Any, Dict, List, Optional, Union
+
+from openai import AsyncStream, Stream
from camel.configs import ANTHROPIC_API_PARAMS, AnthropicConfig
+from camel.messages import OpenAIMessage
from camel.models.openai_compatible_model import OpenAICompatibleModel
-from camel.types import ModelType
+from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
from camel.utils import (
AnthropicTokenCounter,
BaseTokenCounter,
@@ -25,6 +28,47 @@ from camel.utils import (
)
+def strip_trailing_whitespace_from_messages(
+ messages: List[OpenAIMessage],
+) -> List[OpenAIMessage]:
+ r"""Strip trailing whitespace from all message contents in a list of
+ messages. This is necessary because the Anthropic API doesn't allow
+ trailing whitespace in message content.
+
+ Args:
+ messages (List[OpenAIMessage]): List of messages to process
+
+ Returns:
+ List[OpenAIMessage]: The processed messages with trailing whitespace
+ removed
+ """
+ if not messages:
+ return messages
+
+ # Create a deep copy to avoid modifying the original messages
+ processed_messages = [dict(msg) for msg in messages]
+
+ # Process each message
+ for msg in processed_messages:
+ if "content" in msg and msg["content"] is not None:
+ if isinstance(msg["content"], str):
+ msg["content"] = msg["content"].rstrip()
+ elif isinstance(msg["content"], list):
+ # Handle content that's a list of content parts (e.g., for
+ # multimodal content)
+ for i, part in enumerate(msg["content"]):
+ if (
+ isinstance(part, dict)
+ and "text" in part
+ and isinstance(part["text"], str)
+ ):
+ part["text"] = part["text"].rstrip()
+ elif isinstance(part, str):
+ msg["content"][i] = part.rstrip()
+
+ return processed_messages # type: ignore[return-value]
+
+
class AnthropicModel(OpenAICompatibleModel):
r"""Anthropic API in a unified OpenAICompatibleModel interface.
@@ -89,6 +133,9 @@ class AnthropicModel(OpenAICompatibleModel):
**kwargs,
)
+ # Monkey patch the AnthropicTokenCounter to handle trailing whitespace
+ self._patch_anthropic_token_counter()
+
@property
def token_counter(self) -> BaseTokenCounter:
r"""Initialize the token counter for the model backend.
@@ -115,3 +162,68 @@ class AnthropicModel(OpenAICompatibleModel):
f"Unexpected argument `{param}` is "
"input into Anthropic model backend."
)
+
+ def _request_chat_completion(
+ self,
+ messages: List[OpenAIMessage],
+ tools: Optional[List[Dict[str, Any]]] = None,
+ ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
+ # Strip trailing whitespace from all message contents to prevent
+ # Anthropic API errors
+ processed_messages = strip_trailing_whitespace_from_messages(messages)
+
+ # Call the parent class method
+ return super()._request_chat_completion(processed_messages, tools)
+
+ async def _arequest_chat_completion(
+ self,
+ messages: List[OpenAIMessage],
+ tools: Optional[List[Dict[str, Any]]] = None,
+ ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
+ # Strip trailing whitespace from all message contents to prevent
+ # Anthropic API errors
+ processed_messages = strip_trailing_whitespace_from_messages(messages)
+
+ # Call the parent class method
+ return await super()._arequest_chat_completion(
+ processed_messages, tools
+ )
+
+ def _patch_anthropic_token_counter(self):
+ r"""Monkey patch the AnthropicTokenCounter class to handle trailing
+ whitespace.
+
+ This patches the count_tokens_from_messages method to strip trailing
+ whitespace from message content before sending to the Anthropic API.
+ """
+ import functools
+
+ from anthropic.types import MessageParam
+
+ from camel.utils import AnthropicTokenCounter
+
+ original_count_tokens = (
+ AnthropicTokenCounter.count_tokens_from_messages
+ )
+
+ @functools.wraps(original_count_tokens)
+ def patched_count_tokens(self, messages):
+ # Process messages to remove trailing whitespace
+ processed_messages = strip_trailing_whitespace_from_messages(
+ messages
+ )
+
+ # Use the processed messages with the original method
+ return self.client.messages.count_tokens(
+ messages=[
+ MessageParam(
+ content=str(msg["content"]),
+ role="user" if msg["role"] == "user" else "assistant",
+ )
+ for msg in processed_messages
+ ],
+ model=self.model,
+ ).input_tokens
+
+ # Apply the monkey patch
+ AnthropicTokenCounter.count_tokens_from_messages = patched_count_tokens
diff --git a/camel/societies/workforce/prompts.py b/camel/societies/workforce/prompts.py
index f4c794009..53942c320 100644
--- a/camel/societies/workforce/prompts.py
+++ b/camel/societies/workforce/prompts.py
@@ -88,16 +88,17 @@ Do not include any other text, explanations, justifications, or conversational f
PROCESS_TASK_PROMPT = TextPrompt(
"""You need to process one given task.
-Here are results of some prerequisite tasks that you can refer to:
+
+Please keep in mind the task you are going to process, the content of the task that you need to do is:
==============================
-{dependency_tasks_info}
+{content}
==============================
-The content of the task that you need to do is:
+Here are results of some prerequisite tasks that you can refer to:
==============================
-{content}
+{dependency_tasks_info}
==============================
Here are some additional information about the task:
diff --git a/camel/societies/workforce/workforce.py b/camel/societies/workforce/workforce.py
index ce252b800..ee099ed62 100644
--- a/camel/societies/workforce/workforce.py
+++ b/camel/societies/workforce/workforce.py
@@ -274,10 +274,13 @@ class Workforce(BaseNode):
"of agents. This ensures efficient execution by minimizing "
"context switching between agents.",
)
- _kwargs = dict(task_agent_kwargs or {})
+ _task_agent_kwargs = dict(task_agent_kwargs or {})
extra_tools = TaskPlanningToolkit().get_tools()
- _kwargs["tools"] = [*_kwargs.get("tools", []), *extra_tools]
- self.task_agent = ChatAgent(task_sys_msg, **_kwargs)
+ _task_agent_kwargs["tools"] = [
+ *_task_agent_kwargs.get("tools", []),
+ *extra_tools,
+ ]
+ self.task_agent = ChatAgent(task_sys_msg, **_task_agent_kwargs)
def __repr__(self):
return (
@@ -823,9 +826,17 @@ class Workforce(BaseNode):
# Check if we're already in an event loop
try:
- asyncio.get_running_loop()
+ current_loop = asyncio.get_running_loop()
+ # Store the current loop for potential reuse by async tools
+ self._loop = current_loop
+
+ logger.info(
+ "Running in active event loop context. "
+ "Consider using process_task_async() directly for better "
+ "async tool compatibility."
+ )
- # If we're in an event loop, we need to run in a thread
+ # Create a new thread with a fresh event loop
def run_in_thread():
# Create new event loop for this thread
new_loop = asyncio.new_event_loop()
@@ -836,6 +847,8 @@ class Workforce(BaseNode):
)
finally:
new_loop.close()
+ # Restore original loop reference
+ self._loop = current_loop
with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(run_in_thread)
@@ -1273,12 +1286,15 @@ class Workforce(BaseNode):
# Add timeout to prevent indefinite waiting
return await asyncio.wait_for(
self._channel.get_returned_task_by_publisher(self.node_id),
- timeout=300.0, # 5 minute timeout
+ timeout=180.0, # 3 minute timeout
)
except asyncio.TimeoutError:
logger.warning(
f"Timeout waiting for returned task in "
- f"workforce {self.node_id}"
+ f"workforce {self.node_id}. "
+ f"This may indicate an issue with async tool execution. "
+ f"Current pending tasks: {len(self._pending_tasks)}, "
+ f"In-flight tasks: {self._in_flight_tasks}"
)
raise
@@ -1771,10 +1787,10 @@ class Workforce(BaseNode):
child.description,
child.assistant_role_name,
child.user_role_name,
- child.chat_turn_limit,
child.assistant_agent_kwargs,
child.user_agent_kwargs,
child.summarize_agent_kwargs,
+ child.chat_turn_limit,
)
elif isinstance(child, Workforce):
new_instance.add_workforce(child.clone(with_memory))
diff --git a/examples/workforce/eigent.py b/examples/workforce/eigent.py
index eace7c4b6..85cd51a8f 100644
--- a/examples/workforce/eigent.py
+++ b/examples/workforce/eigent.py
@@ -15,6 +15,7 @@
import asyncio
from camel.agents.chat_agent import ChatAgent
+from camel.loaders import Firecrawl
from camel.messages.base import BaseMessage
from camel.models import BaseModelBackend, ModelFactory
from camel.societies.workforce import Workforce
@@ -24,6 +25,7 @@ from camel.toolkits import (
CodeExecutionToolkit,
DalleToolkit,
FileWriteToolkit,
+ FunctionTool,
HumanToolkit,
ImageAnalysisToolkit,
LinkedInToolkit,
@@ -31,6 +33,7 @@ from camel.toolkits import (
PlaywrightMCPToolkit,
PPTXToolkit,
RedditToolkit,
+ SearchToolkit,
SlackToolkit,
TerminalToolkit,
TwitterToolkit,
@@ -80,19 +83,20 @@ async def search_agent_factory(
):
r"""Factory for creating a search agent, based on user-provided code
structure."""
- # search_toolkits = SearchToolkit()
+ search_toolkits = SearchToolkit()
# browser_toolkits = AsyncBrowserToolkit()
terminal_toolkits = TerminalToolkit()
human_toolkits = HumanToolkit()
tools = [
- # FunctionTool(search_toolkits.search_wiki),
- # # FunctionTool(search_toolkits.search_duckduckgo),
- # # FunctionTool(search_toolkits.search_bing),
- # # FunctionTool(search_toolkits.search_baidu),
+ FunctionTool(search_toolkits.search_wiki),
+ FunctionTool(search_toolkits.search_google),
+ FunctionTool(search_toolkits.search_bing),
+ FunctionTool(search_toolkits.search_baidu),
*playwright_toolkit.get_tools(),
# *browser_toolkits.get_tools(),
*terminal_toolkits.get_tools(),
*human_toolkits.get_tools(),
+ Firecrawl().scrape,
]
system_message = """You are a helpful assistant that can search the web,
@@ -121,10 +125,10 @@ async def search_agent_factory(
difficult to find through simple web searches). You can find the answer to
the question by performing subsequent operations on the URL, such as
extracting the content of the webpage.
- - Do not solely rely on document tools or browser simulation to find the
- answer, you should combine document tools and browser simulation to
- comprehensively process web page information. Some content may need to do
- browser simulation to get, or some content is rendered by javascript.
+ - Do not solely rely on browser simulation to find the
+ answer, you should combine search tools, scraper tools and browser
+ simulation to comprehensively process web page information. Some content
+ may need to do browser simulation to get.
- In your response, you should mention the urls you have visited and
processed.
@@ -334,14 +338,25 @@ When assisting users, always:
async def main():
- playwright_toolkit = PlaywrightMCPToolkit() # Create instance
+ playwright_toolkit = PlaywrightMCPToolkit(timeout=60) # Create instance
try:
await playwright_toolkit.connect() # Connect the instance
# Create a single model backend for all agents
model_backend = ModelFactory.create(
- model_platform=ModelPlatformType.DEFAULT,
- model_type=ModelType.DEFAULT,
+ model_platform=ModelPlatformType.ANTHROPIC,
+ model_type=ModelType.CLAUDE_SONNET_4,
+ # model_config_dict={
+ # "max_tokens": 64000,
+ # }
+ )
+
+ model_backend_reason = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.O3_MINI,
+ # model_config_dict={
+ # "max_tokens": 64000,
+ # }
)
task_id = 'workforce_task'
@@ -356,10 +371,18 @@ async def main():
# social_medium_agent = social_medium_agent_factory(model_backend,
# task_id)
+ # Configure kwargs for all agents to use the same model_backend
+ coordinator_agent_kwargs = {"model": model_backend_reason}
+ task_agent_kwargs = {"model": model_backend_reason}
+ new_worker_agent_kwargs = {"model": model_backend}
+
workforce = Workforce(
'A workforce',
graceful_shutdown_timeout=30.0, # 30 seconds for debugging
share_memory=False,
+ coordinator_agent_kwargs=coordinator_agent_kwargs,
+ task_agent_kwargs=task_agent_kwargs,
+ new_worker_agent_kwargs=new_worker_agent_kwargs,
)
workforce.add_single_agent_worker(
@@ -387,7 +410,8 @@ async def main():
id='0',
)
- workforce.process_task(human_task)
+ # 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 ---")