🐫 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: set craw4ai and markitdown as toolkit (#2717)
Wendong-Fan3775213def24
diff --git a/camel/toolkits/__init__.py b/camel/toolkits/__init__.pyindex ab929931c..486023d57 100644--- a/camel/toolkits/__init__.py+++ b/camel/toolkits/__init__.py@@ -79,6 +79,8 @@ from .wolfram_alpha_toolkit import WolframAlphaToolkitfrom .task_planning_toolkit import TaskPlanningToolkitfrom .non_visual_browser_toolkit import BrowserNonVisualToolkitfrom .edgeone_pages_mcp_toolkit import EdgeOnePagesMCPToolkit+from .craw4ai_toolkit import Crawl4AIToolkit+from .markitdown_toolkit import MarkItDownToolkit__all__ = [@@ -146,4 +148,6 @@ __all__ = ['TaskPlanningToolkit','BrowserNonVisualToolkit','EdgeOnePagesMCPToolkit',+ 'Crawl4AIToolkit',+ 'MarkItDownToolkit',]diff --git a/camel/toolkits/craw4ai_toolkit.py b/camel/toolkits/craw4ai_toolkit.pynew file mode 100644index 000000000..ab7ec841f--- /dev/null+++ b/camel/toolkits/craw4ai_toolkit.py@@ -0,0 +1,73 @@+# ========= 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. =========++from typing import List, Optional++from camel.logger import get_logger+from camel.toolkits.base import BaseToolkit+from camel.toolkits.function_tool import FunctionTool+from camel.utils import MCPServer++logger = get_logger(__name__)+++@MCPServer()+class Crawl4AIToolkit(BaseToolkit):+ r"""A class representing a toolkit for Crawl4AI."""++ def __init__(+ self,+ timeout: Optional[float] = None,+ ):+ super().__init__(timeout=timeout)++ async def scrape(self, url: str) -> str:+ r"""Scrapes a webpage and returns its content.++ This function is designed to fetch the content of a given URL and+ return it as a single string. It's particularly useful for extracting+ text from web pages for further processing. The function+ will try to get the most meaningful content from the page.++ Args:+ url (str): The URL of the webpage to scrape.++ Returns:+ str: The scraped content of the webpage as a string. If the+ scraping fails, it will return an error message.+ """+ from crawl4ai import AsyncWebCrawler, CrawlerRunConfig++ try:+ async with AsyncWebCrawler() as client:+ config = CrawlerRunConfig(+ only_text=True,+ )+ content = await client.arun(url, crawler_config=config)+ return str(content.markdown) if content.markdown else ""+ except Exception as e:+ logger.error(f"Error scraping {url}: {e}")+ return f"Error scraping {url}: {e}"++ def get_tools(self) -> List[FunctionTool]:+ r"""Returns a list of FunctionTool objects representing the+ functions in the toolkit.++ Returns:+ List[FunctionTool]: A list of FunctionTool objects+ representing the functions in the toolkit.+ """+ return [+ FunctionTool(self.scrape),+ ]diff --git a/camel/toolkits/markitdown_toolkit.py b/camel/toolkits/markitdown_toolkit.pynew file mode 100644index 000000000..6f6557a05--- /dev/null+++ b/camel/toolkits/markitdown_toolkit.py@@ -0,0 +1,78 @@+# ========= 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. =========++from typing import Dict, List, Optional++from camel.loaders.markitdown import MarkItDownLoader+from camel.logger import get_logger+from camel.toolkits.base import BaseToolkit+from camel.toolkits.function_tool import FunctionTool+from camel.utils import MCPServer++logger = get_logger(__name__)+++@MCPServer()+class MarkItDownToolkit(BaseToolkit):+ r"""A class representing a toolkit for MarkItDown."""++ def __init__(+ self,+ timeout: Optional[float] = None,+ ):+ super().__init__(timeout=timeout)++ def load_files(self, file_paths: List[str]) -> Dict[str, str]:+ r"""Scrapes content from a list of files and converts it to Markdown.++ This function takes a list of local file paths, attempts to convert+ each file into Markdown format, and returns the converted content.+ The conversion is performed in parallel for efficiency.++ Supported file formats include:+ - PDF (.pdf)+ - Microsoft Office: Word (.doc, .docx), Excel (.xls, .xlsx),+ PowerPoint (.ppt, .pptx)+ - EPUB (.epub)+ - HTML (.html, .htm)+ - Images (.jpg, .jpeg, .png) for OCR+ - Audio (.mp3, .wav) for transcription+ - Text-based formats (.csv, .json, .xml, .txt)+ - ZIP archives (.zip)++ Args:+ file_paths (List[str]): A list of local file paths to be+ converted.++ Returns:+ Dict[str, str]: A dictionary where keys are the input file paths+ and values are the corresponding content in Markdown format.+ If conversion of a file fails, the value will contain an+ error message.+ """+ return MarkItDownLoader().convert_files(+ file_paths=file_paths, parallel=True+ )++ def get_tools(self) -> List[FunctionTool]:+ r"""Returns a list of FunctionTool objects representing the+ functions in the toolkit.++ Returns:+ List[FunctionTool]: A list of FunctionTool objects+ representing the functions in the toolkit.+ """+ return [+ FunctionTool(self.load_files),+ ]diff --git a/examples/workforce/eigent.py b/examples/workforce/eigent.pyindex 27cb52710..3a8553d0c 100644--- a/examples/workforce/eigent.py+++ b/examples/workforce/eigent.py@@ -15,7 +15,6 @@import asynciofrom camel.agents.chat_agent import ChatAgent-from camel.loaders import Crawl4AIfrom camel.messages.base import BaseMessagefrom camel.models import BaseModelBackend, ModelFactoryfrom camel.societies.workforce import Workforce@@ -24,6 +23,7 @@ from camel.toolkits import (AudioAnalysisToolkit,BrowserNonVisualToolkit,CodeExecutionToolkit,+ Crawl4AIToolkit,DalleToolkit,EdgeOnePagesMCPToolkit,FileWriteToolkit,@@ -31,6 +31,7 @@ from camel.toolkits import (HumanToolkit,ImageAnalysisToolkit,LinkedInToolkit,+ MarkItDownToolkit,NotionToolkit,PPTXToolkit,RedditToolkit,@@ -42,6 +43,7 @@ from camel.toolkits import (WhatsAppToolkit,)from camel.types import ModelPlatformType, ModelType+from camel.utils.commons import api_keys_requireddef developer_agent_factory(@@ -67,14 +69,15 @@ def developer_agent_factory(needed for efficient solutions- IMPLEMENT complete, production-ready code rather than theoreticalexamples- - USE edgeone pages mcp toolkit to create and edit web pages, after you- have created the web page, you can use the browser toolkit to view the- web page,you can let search agent to visit the web page and verify the- web page.+ - USE 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.- DEMONSTRATE results with proper error handling and practicalimplementation- If there's dependency issues when you try to execute code, you should- use the terminal toolkit to install the dependencies."""+ use the terminal toolkit to install the dependencies.+ - ASK for human input via the console if you are stuck or need+ clarification."""return ChatAgent(system_message=BaseMessage.make_assistant_message(@@ -86,27 +89,22 @@ def developer_agent_factory()+@api_keys_required([(None, 'EXA_API_KEY')])def search_agent_factory(model: BaseModelBackend,task_id: str,):r"""Factory for creating a search agent, based on user-provided codestructure."""- search_toolkits = SearchToolkit()- # browser_toolkits = AsyncBrowserToolkit()- browser_toolkit = BrowserNonVisualToolkit(headless=False)- terminal_toolkits = TerminalToolkit()- human_toolkits = HumanToolkit()tools = [- # FunctionTool(search_toolkits.search_wiki),- FunctionTool(search_toolkits.search_exa),- # FunctionTool(search_toolkits.search_bing),- # FunctionTool(search_toolkits.search_baidu),- *browser_toolkit.get_tools(),- # *browser_toolkits.get_tools(),- *terminal_toolkits.get_tools(),- human_toolkits.ask_human_via_console,- FunctionTool(Crawl4AI().scrape),+ # FunctionTool(SearchToolkit().search_wiki),+ FunctionTool(SearchToolkit().search_exa),+ # FunctionTool(SearchToolkit().search_bing),+ # FunctionTool(SearchToolkit().search_baidu),+ *BrowserNonVisualToolkit(headless=False).get_tools(),+ *TerminalToolkit().get_tools(),+ HumanToolkit().ask_human_via_console,+ *Crawl4AIToolkit().get_tools(),]system_message = """You are a helpful assistant that can search the web,@@ -162,7 +160,7 @@ Here are some tips that help you perform web search:return ChatAgent(system_message=BaseMessage.make_assistant_message(- role_name="Document Agent",+ role_name="Search Agent",content=system_message,),model=model,@@ -178,6 +176,7 @@ def document_agent_factory(model: BaseModelBackend, task_id: str):*PPTXToolkit().get_tools(),# *RetrievalToolkit().get_tools(),HumanToolkit().ask_human_via_console,+ *MarkItDownToolkit().get_tools(),]system_message = """You are a Document Processing Assistant specialized in@@ -241,7 +240,8 @@ def multi_modal_agent_factory(model: BaseModelBackend, task_id: str):in analyzing and generating various types of media content. Yourcapabilities include:- 1. Audio Analysis & Processing:+ 1. Video & Audio Analysis:+ - Download videos from URLs for analysis.- Transcribe speech from audio files to text with high accuracy- Answer specific questions about audio content- Process audio from both local files and URLs@@ -292,47 +292,45 @@ def social_medium_agent_factory(model: BaseModelBackend, task_id: str):integrated toolkits enable you to:1. WhatsApp Business Management (WhatsAppToolkit):- - Send text messages to customers via the WhatsApp Business API- - Send template messages for standardized communications- - Retrieve business profile information+ - Send text and template messages to customers via the WhatsApp Business+ API.+ - Retrieve business profile information.2. Twitter Account Management (TwitterToolkit):- - Create tweets with text content (respecting character limits)- - Create tweets with polls or as quote tweets- - Delete existing tweets- - Retrieve user profile information+ - Create tweets with text content, polls, or as quote tweets.+ - Delete existing tweets.+ - Retrieve user profile information.3. LinkedIn Professional Networking (LinkedInToolkit):- - Create posts on LinkedIn (respecting character limits)- - Delete existing posts (with user confirmation)- - Retrieve authenticated user's profile information+ - Create posts on LinkedIn.+ - Delete existing posts.+ - Retrieve authenticated user's profile information.4. Reddit Content Analysis (RedditToolkit):- - Collect top posts and comments from specified subreddits- - Perform sentiment analysis on Reddit comments- - Track keyword discussions across multiple subreddits+ - Collect top posts and comments from specified subreddits.+ - Perform sentiment analysis on Reddit comments.+ - Track keyword discussions across multiple subreddits.5. Notion Workspace Management (NotionToolkit):- - List all pages in a Notion workspace- - List all users with access to the workspace- - Retrieve and extract text content from Notion blocks+ - List all pages and users in a Notion workspace.+ - Retrieve and extract text content from Notion blocks.6. Slack Workspace Interaction (SlackToolkit):- - Create new Slack channels (public or private)- - Join or leave existing channels- - Send and delete messages in channels- - Retrieve channel information and message history+ - Create new Slack channels (public or private).+ - Join or leave existing channels.+ - Send and delete messages in channels.+ - Retrieve channel information and message history.7. Human Interaction (HumanToolkit):- - Ask questions to users via console- - Send messages to users via console+ - Ask questions to users and send messages via console.When assisting users, always:-1. Identify which platform's functionality is needed for the task-2. Check if required API credentials are available before attempting operations-3. Provide clear explanations of what actions you're taking-4. Handle rate limits and API restrictions appropriately-5. Ask clarifying questions when user requests are ambiguous""",+- Identify which platform's functionality is needed for the task.+- Check if required API credentials are available before attempting+operations.+- Provide clear explanations of what actions you're taking.+- Handle rate limits and API restrictions appropriately.+- Ask clarifying questions when user requests are ambiguous.""",),model=model,tools=[@@ -424,8 +422,11 @@ async def main():human_task = Task(content=("""- Gather today's headline news from BBC, Washington Post, Google News and- CNN. Each headline can be processed in parallel by the SearchAgent pool.+I want to read papers about GUI Agent. Please help me find+ten papers, check the detailed content of the papers and help+me write a comparison report, then create a nice slides(pptx)+to introduce the latest research progress of GUI Agent. The+slides should be very comprehensive and professional."""),id='0',