š« 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
refactor: move functions to toolkits directory (#677)
Andrei - Octavian Brabete17bafb92e6d1
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.pyindex db04856e4..2a41a86b9 100644--- a/camel/agents/chat_agent.py+++ b/camel/agents/chat_agent.py@@ -42,8 +42,8 @@ from camel.utils import get_model_encodingif TYPE_CHECKING:from openai import Stream- from camel.functions import OpenAIFunctionfrom camel.terminators import ResponseTerminator+ from camel.toolkits import OpenAIFunction@dataclass(frozen=True)diff --git a/camel/configs/litellm_config.py b/camel/configs/litellm_config.pyindex 975bee8ea..1203d33f0 100644--- a/camel/configs/litellm_config.py+++ b/camel/configs/litellm_config.py@@ -19,7 +19,7 @@ from typing import TYPE_CHECKING, List, Optional, Unionfrom camel.configs.base_config import BaseConfigif TYPE_CHECKING:- from camel.functions import OpenAIFunction+ from camel.toolkits import OpenAIFunction@dataclass(frozen=True)diff --git a/camel/configs/openai_config.py b/camel/configs/openai_config.pyindex f07900e33..88b6590e9 100644--- a/camel/configs/openai_config.py+++ b/camel/configs/openai_config.py@@ -21,7 +21,7 @@ from openai._types import NOT_GIVEN, NotGivenfrom camel.configs.base_config import BaseConfigif TYPE_CHECKING:- from camel.functions import OpenAIFunction+ from camel.toolkits import OpenAIFunction@dataclass(frozen=True)diff --git a/camel/configs/zhipuai_config.py b/camel/configs/zhipuai_config.pyindex a1ffed0c6..eab9a4ac0 100644--- a/camel/configs/zhipuai_config.py+++ b/camel/configs/zhipuai_config.py@@ -21,7 +21,7 @@ from openai._types import NOT_GIVEN, NotGivenfrom camel.configs.base_config import BaseConfigif TYPE_CHECKING:- from camel.functions import OpenAIFunction+ from camel.toolkits import OpenAIFunction@dataclass(frozen=True)diff --git a/camel/functions/__init__.py b/camel/functions/__init__.pydeleted file mode 100644index 8ef16f47d..000000000--- a/camel/functions/__init__.py+++ /dev/null@@ -1,51 +0,0 @@-# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========-# ruff: noqa: I001-from .openai_function import (- OpenAIFunction,- get_openai_function_schema,- get_openai_tool_schema,-)-from .open_api_specs.security_config import openapi_security_config--from .google_maps_function import MAP_FUNCS-from .math_functions import MATH_FUNCS-from .open_api_function import OPENAPI_FUNCS-from .retrieval_functions import RETRIEVAL_FUNCS-from .search_functions import SEARCH_FUNCS-from .twitter_function import TWITTER_FUNCS-from .weather_functions import WEATHER_FUNCS-from .slack_functions import SLACK_FUNCS--from .open_api_function import (- apinames_filepaths_to_funs_schemas,- generate_apinames_filepaths,-)--__all__ = [- 'OpenAIFunction',- 'get_openai_function_schema',- 'get_openai_tool_schema',- 'openapi_security_config',- 'apinames_filepaths_to_funs_schemas',- 'generate_apinames_filepaths',- 'MAP_FUNCS',- 'MATH_FUNCS',- 'OPENAPI_FUNCS',- 'RETRIEVAL_FUNCS',- 'SEARCH_FUNCS',- 'TWITTER_FUNCS',- 'WEATHER_FUNCS',- 'SLACK_FUNCS',-]diff --git a/camel/functions/google_maps_function.py b/camel/functions/google_maps_function.pydeleted file mode 100644index 942557f53..000000000--- a/camel/functions/google_maps_function.py+++ /dev/null@@ -1,335 +0,0 @@-# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========-import os-from functools import wraps-from typing import Any, Callable, List, Optional, Tuple, Union--from camel.functions.openai_function import OpenAIFunction---def import_googlemaps_or_raise() -> Any:- r"""Attempts to import the `googlemaps` library and returns it.-- Returns:- module: The `googlemaps` module if successfully imported.-- Raises:- ImportError: If the `googlemaps` library is not installed, this error- is raised with a message instructing how to install the- library using pip.- """- try:- import googlemaps-- return googlemaps- except ImportError:- raise ImportError(- "Please install `googlemaps` first. You can install "- "it by running `pip install googlemaps`."- )---def get_googlemap_api_key() -> str:- r"""Retrieve the Google Maps API key from environment variables.-- Returns:- str: The Google Maps API key.-- Raises:- ValueError: If the API key is not found in the environment variables.- """- # Get `GOOGLEMAPS_API_KEY` here:- # https://console.cloud.google.com/apis/credentials- GOOGLEMAPS_API_KEY = os.environ.get('GOOGLEMAPS_API_KEY')- if not GOOGLEMAPS_API_KEY:- raise ValueError(- "`GOOGLEMAPS_API_KEY` not found in environment "- "variables. `GOOGLEMAPS_API_KEY` API keys are "- "generated in the `Credentials` page of the "- "`APIs & Services` tab of "- "https://console.cloud.google.com/apis/credentials."- )- return GOOGLEMAPS_API_KEY---def get_address_description(- address: Union[str, List[str]],- region_code: Optional[str] = None,- locality: Optional[str] = None,-) -> str:- r"""Validates an address via Google Maps API, returns a descriptive- summary.-- Validates an address using Google Maps API, returning a summary that- includes information on address completion, formatted address, location- coordinates, and metadata types that are true for the given address.-- Args:- address (Union[str, List[str]]): The address or components to validate.- Can be a single string or a list representing different parts.- region_code (str, optional): Country code for regional restriction,- helps narrowing down results. (default: :obj:`None`)- locality (str, optional): Restricts validation to a specific locality,- e.g., "Mountain View". (default: :obj:`None`)-- Returns:- str: Summary of the address validation results, including information- on address completion, formatted address, geographical coordinates- (latitude and longitude), and metadata types true for the address.-- Raises:- ImportError: If the `googlemaps` library is not installed.- Exception: For unexpected errors during the address validation.- """- googlemaps = import_googlemaps_or_raise()- GOOGLEMAPS_API_KEY = get_googlemap_api_key()- try:- gmaps = googlemaps.Client(key=GOOGLEMAPS_API_KEY)- except Exception as e:- return f"Error: {e!s}"-- try:- addressvalidation_result = gmaps.addressvalidation(- [address],- regionCode=region_code,- locality=locality,- enableUspsCass=False,- ) # Always False as per requirements-- # Check if the result contains an error- if 'error' in addressvalidation_result:- error_info = addressvalidation_result['error']- error_message = error_info.get(- 'message', 'An unknown error occurred'- )- error_status = error_info.get('status', 'UNKNOWN_STATUS')- error_code = error_info.get('code', 'UNKNOWN_CODE')- return (- f"Address validation failed with error: {error_message} "- f"Status: {error_status}, Code: {error_code}"- )-- # Assuming the successful response structure includes a 'result' key- result = addressvalidation_result['result']- verdict = result.get('verdict', {})- address_info = result.get('address', {})- geocode = result.get('geocode', {})- metadata = result.get('metadata', {})-- # Construct the descriptive string- address_complete = (- "Yes" if verdict.get('addressComplete', False) else "No"- )- formatted_address = address_info.get(- 'formattedAddress', 'Not available'- )- location = geocode.get('location', {})- latitude = location.get('latitude', 'Not available')- longitude = location.get('longitude', 'Not available')- true_metadata_types = [key for key, value in metadata.items() if value]- true_metadata_types_str = (- ', '.join(true_metadata_types) if true_metadata_types else 'None'- )-- description = (- f"Address completion status: {address_complete}. "- f"Formatted address: {formatted_address}. "- f"Location (latitude, longitude): ({latitude}, {longitude}). "- f"Metadata indicating true types: {true_metadata_types_str}."- )-- return description- except Exception as e:- return f"An unexpected error occurred: {e!s}"---def handle_googlemaps_exceptions(- func: Callable[..., Any],-) -> Callable[..., Any]:- r"""Decorator to catch and handle exceptions raised by Google Maps API- calls.-- Args:- func (Callable): The function to be wrapped by the decorator.-- Returns:- Callable: A wrapper function that calls the wrapped function and- handles exceptions.- """-- @wraps(func)- def wrapper(*args: Any, **kwargs: Any) -> Any:- try:- from googlemaps.exceptions import ( # type: ignore[import-untyped] # isort: skip- ApiError,- HTTPError,- Timeout,- TransportError,- )- except ImportError:- raise ImportError(- "Please install `googlemaps` first. You can install "- "it by running `pip install googlemaps`."- )-- try:- return func(*args, **kwargs)- except ApiError as e:- return (- 'An exception returned by the remote API. '- f'Status: {e.status}, Message: {e.message}'- )- except HTTPError as e:- return (- 'An unexpected HTTP error occurred. '- f'Status Code: {e.status_code}'- )- except Timeout:- return 'The request timed out.'- except TransportError as e:- return (- 'Something went wrong while trying to execute the '- f'request. Details: {e.base_exception}'- )- except Exception as e:- return f'An unexpected error occurred: {e}'-- return wrapper---@handle_googlemaps_exceptions-def get_elevation(lat_lng: Tuple) -> str:- r"""Retrieves elevation data for a given latitude and longitude.-- Uses the Google Maps API to fetch elevation data for the specified latitude- and longitude. It handles exceptions gracefully and returns a description- of the elevation, including its value in meters and the data resolution.-- Args:- lat_lng (Tuple[float, float]): The latitude and longitude for- which to retrieve elevation data.-- Returns:- str: A description of the elevation at the specified location(s),- including the elevation in meters and the data resolution. If- elevation data is not available, a message indicating this is- returned.- """- googlemaps = import_googlemaps_or_raise()- GOOGLEMAPS_API_KEY = get_googlemap_api_key()- try:- gmaps = googlemaps.Client(key=GOOGLEMAPS_API_KEY)- except Exception as e:- return f"Error: {e!s}"-- # Assuming gmaps is a configured Google Maps client instance- elevation_result = gmaps.elevation(lat_lng)-- # Extract the elevation data from the first (and presumably only) result- if elevation_result:- elevation = elevation_result[0]['elevation']- location = elevation_result[0]['location']- resolution = elevation_result[0]['resolution']-- # Format the elevation data into a natural language description- description = (- f"The elevation at latitude {location['lat']}, "- f"longitude {location['lng']} "- f"is approximately {elevation:.2f} meters above sea level, "- f"with a data resolution of {resolution:.2f} meters."- )- else:- description = "Elevation data is not available for the given location."-- return description---def format_offset_to_natural_language(offset: int) -> str:- r"""Converts a time offset in seconds to a more natural language- description using hours as the unit, with decimal places to represent- minutes and seconds.-- Args:- offset (int): The time offset in seconds. Can be positive, negative,- or zero.-- Returns:- str: A string representing the offset in hours, such as "+2.50 hours"- or "-3.75 hours".- """- # Convert the offset to hours as a float- hours = offset / 3600.0- hours_str = f"{hours:+.2f} hour{'s' if abs(hours) != 1 else ''}"- return hours_str---@handle_googlemaps_exceptions-def get_timezone(lat_lng: Tuple) -> str:- r"""Retrieves timezone information for a given latitude and longitude.-- This function uses the Google Maps Timezone API to fetch timezone data for- the specified latitude and longitude. It returns a natural language- description of the timezone, including the timezone ID, name, standard- time offset, daylight saving time offset, and the total offset from- Coordinated Universal Time (UTC).-- Args:- lat_lng (Tuple[float, float]): The latitude and longitude for- which to retrieve elevation data.-- Returns:- str: A descriptive string of the timezone information, including the- timezone ID and name, standard time offset, daylight saving time- offset, and total offset from UTC.- """- googlemaps = import_googlemaps_or_raise()- GOOGLEMAPS_API_KEY = get_googlemap_api_key()- try:- gmaps = googlemaps.Client(key=GOOGLEMAPS_API_KEY)- except Exception as e:- return f"Error: {e!s}"-- # Get timezone information- timezone_dict = gmaps.timezone(lat_lng)-- # Extract necessary information- dst_offset = timezone_dict[- 'dstOffset'- ] # Daylight Saving Time offset in seconds- raw_offset = timezone_dict['rawOffset'] # Standard time offset in seconds- timezone_id = timezone_dict['timeZoneId']- timezone_name = timezone_dict['timeZoneName']-- raw_offset_str = format_offset_to_natural_language(raw_offset)- dst_offset_str = format_offset_to_natural_language(dst_offset)- total_offset_seconds = dst_offset + raw_offset- total_offset_str = format_offset_to_natural_language(total_offset_seconds)-- # Create a natural language description- description = (- f"Timezone ID is {timezone_id}, named {timezone_name}. "- f"The standard time offset is {raw_offset_str}. "- f"Daylight Saving Time offset is {dst_offset_str}. "- f"The total offset from Coordinated Universal Time (UTC) is "- f"{total_offset_str}, including any Daylight Saving Time adjustment "- f"if applicable. "- )-- return description---MAP_FUNCS: List[OpenAIFunction] = [- OpenAIFunction(func) # type: ignore[arg-type]- for func in [get_address_description, get_elevation, get_timezone]-]diff --git a/camel/functions/math_functions.py b/camel/functions/math_functions.pydeleted file mode 100644index 7de44f8b6..000000000--- a/camel/functions/math_functions.py+++ /dev/null@@ -1,61 +0,0 @@-# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========--from typing import List--from camel.functions.openai_function import OpenAIFunction---def add(a: int, b: int) -> int:- r"""Adds two numbers.-- Args:- a (int): The first number to be added.- b (int): The second number to be added.-- Returns:- integer: The sum of the two numbers.- """- return a + b---def sub(a: int, b: int) -> int:- r"""Do subtraction between two numbers.-- Args:- a (int): The minuend in subtraction.- b (int): The subtrahend in subtraction.-- Returns:- integer: The result of subtracting :obj:`b` from :obj:`a`.- """- return a - b---def mul(a: int, b: int) -> int:- r"""Multiplies two integers.-- Args:- a (int): The multiplier in the multiplication.- b (int): The multiplicand in the multiplication.-- Returns:- integer: The product of the two numbers.- """- return a * b---MATH_FUNCS: List[OpenAIFunction] = [- OpenAIFunction(func) for func in [add, sub, mul]-]diff --git a/camel/functions/open_api_function.py b/camel/functions/open_api_function.pydeleted file mode 100644index bf77829e2..000000000--- a/camel/functions/open_api_function.py+++ /dev/null@@ -1,508 +0,0 @@-# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========-import json-import os-from typing import Any, Callable, Dict, List, Optional, Tuple--import requests--from camel.functions import OpenAIFunction, openapi_security_config-from camel.types import OpenAPIName---def parse_openapi_file(openapi_spec_path: str) -> Optional[Dict[str, Any]]:- r"""Load and parse an OpenAPI specification file.-- This function utilizes the `prance.ResolvingParser` to parse and resolve- the given OpenAPI specification file, returning the parsed OpenAPI- specification as a dictionary.-- Args:- openapi_spec_path (str): The file path or URL to the OpenAPI- specification.-- Returns:- Optional[Dict[str, Any]]: The parsed OpenAPI specification- as a dictionary. :obj:`None` if the package is not installed.- """- try:- import prance- except Exception:- return None-- # Load the OpenAPI spec- parser = prance.ResolvingParser(- openapi_spec_path, backend="openapi-spec-validator", strict=False- )- openapi_spec = parser.specification- version = openapi_spec.get('openapi', {})- if not version:- raise ValueError(- "OpenAPI version not specified in the spec. "- "Only OPENAPI 3.0.x and 3.1.x are supported."- )- if not (version.startswith('3.0') or version.startswith('3.1')):- raise ValueError(- f"Unsupported OpenAPI version: {version}. "- f"Only OPENAPI 3.0.x and 3.1.x are supported."- )- return openapi_spec---def openapi_spec_to_openai_schemas(- api_name: str, openapi_spec: Dict[str, Any]-) -> List[Dict[str, Any]]:- r"""Convert OpenAPI specification to OpenAI schema format.-- This function iterates over the paths and operations defined in an- OpenAPI specification, filtering out deprecated operations. For each- operation, it constructs a schema in a format suitable for OpenAI,- including operation metadata such as function name, description,- parameters, and request bodies. It raises a ValueError if an operation- lacks a description or summary.-- Args:- api_name (str): The name of the API, used to prefix generated function- names.- openapi_spec (Dict[str, Any]): The OpenAPI specification as a- dictionary.-- Returns:- List[Dict[str, Any]]: A list of dictionaries, each representing a- function in the OpenAI schema format, including details about the- function's name, description, and parameters.-- Raises:- ValueError: If an operation in the OpenAPI specification does not have- a description or summary.-- Note:- This function assumes that the OpenAPI specification follows the 3.0+- format.-- Reference:- https://swagger.io/specification/- """- result = []-- for path, path_item in openapi_spec.get('paths', {}).items():- for method, op in path_item.items():- if op.get('deprecated') is True:- continue-- # Get the function name from the operationId- # or construct it from the API method, and path- function_name = f"{api_name}"- operation_id = op.get('operationId')- if operation_id:- function_name += f"_{operation_id}"- else:- function_name += f"{method}{path.replace('/', '_')}"-- description = op.get('description') or op.get('summary')- if not description:- raise ValueError(- f"{method} {path} Operation from {api_name} "- f"does not have a description or summary."- )- description += " " if description[-1] != " " else ""- description += f"This function is from {api_name} API. "-- # If the OpenAPI spec has a description,- # add it to the operation description- if 'description' in openapi_spec.get('info', {}):- description += f"{openapi_spec['info']['description']}"-- # Get the parameters for the operation, if any- params = op.get('parameters', [])- properties: Dict[str, Any] = {}- required = []-- for param in params:- if not param.get('deprecated', False):- param_name = param['name'] + '_in_' + param['in']- properties[param_name] = {}-- if 'description' in param:- properties[param_name]['description'] = param[- 'description'- ]-- if 'schema' in param:- if (- properties[param_name].get('description')- and 'description' in param['schema']- ):- param['schema'].pop('description')- properties[param_name].update(param['schema'])-- if param.get('required'):- required.append(param_name)-- # If the property dictionary does not have a description,- # use the parameter name as the description- if 'description' not in properties[param_name]:- properties[param_name]['description'] = param['name']-- if 'type' not in properties[param_name]:- properties[param_name]['type'] = 'Any'-- # Process requestBody if present- if 'requestBody' in op:- properties['requestBody'] = {}- requestBody = op['requestBody']- if requestBody.get('required') is True:- required.append('requestBody')-- content = requestBody.get('content', {})- json_content = content.get('application/json', {})- json_schema = json_content.get('schema', {})- if json_schema:- properties['requestBody'] = json_schema- if 'description' not in properties['requestBody']:- properties['requestBody']['description'] = (- "The request body, with parameters specifically "- "described under the `properties` key"- )-- function = {- "type": "function",- "function": {- "name": function_name,- "description": description,- "parameters": {- "type": "object",- "properties": properties,- "required": required,- },- },- }- result.append(function)-- return result # Return the result list---def openapi_function_decorator(- api_name: str,- base_url: str,- path: str,- method: str,- openapi_security: List[Dict[str, Any]],- sec_schemas: Dict[str, Dict[str, Any]],- operation: Dict[str, Any],-) -> Callable:- r"""Decorate a function to make HTTP requests based on OpenAPI- specification details.-- This decorator dynamically constructs and executes an API request based on- the provided OpenAPI operation specifications, security requirements, and- parameters. It supports operations secured with `apiKey` type security- schemes and automatically injects the necessary API keys from environment- variables. Parameters in `path`, `query`, `header`, and `cookie` are also- supported.-- Args:- api_name (str): The name of the API, used to retrieve API key names- and URLs from the configuration.- base_url (str): The base URL for the API.- path (str): The path for the API endpoint, relative to the base URL.- method (str): The HTTP method (e.g., 'get', 'post') for the request.- openapi_security (List[Dict[str, Any]]): The global security- definitions as specified in the OpenAPI specs.- sec_schemas (Dict[str, Dict[str, Any]]): Detailed security schemes.- operation (Dict[str, Any]): A dictionary containing the OpenAPI- operation details, including parameters and request body- definitions.-- Returns:- Callable: A decorator that, when applied to a function, enables the- function to make HTTP requests based on the provided OpenAPI- operation details.-- Raises:- TypeError: If the security requirements include unsupported types.- ValueError: If required API keys are missing from environment- variables or if the content type of the request body is- unsupported.- """-- def inner_decorator(openapi_function: Callable) -> Callable:- def wrapper(**kwargs):- request_url = f"{base_url.rstrip('/')}/{path.lstrip('/')}"- headers = {}- params = {}- cookies = {}-- # Security definition of operation overrides any declared- # top-level security.- sec_requirements = operation.get('security', openapi_security)- avail_sec_requirement = {}- # Write to avaliable_security_requirement only if all the- # security_type are "apiKey"- for security_requirement in sec_requirements:- have_unsupported_type = False- for sec_scheme_name, _ in security_requirement.items():- sec_type = sec_schemas.get(sec_scheme_name).get('type')- if sec_type != "apiKey":- have_unsupported_type = True- break- if have_unsupported_type is False:- avail_sec_requirement = security_requirement- break-- if sec_requirements and not avail_sec_requirement:- raise TypeError(- "Only security schemas of type `apiKey` are supported."- )-- for sec_scheme_name, _ in avail_sec_requirement.items():- try:- API_KEY_NAME = openapi_security_config.get(api_name).get(- sec_scheme_name- )- api_key_value = os.environ[API_KEY_NAME]- except Exception:- api_key_url = openapi_security_config.get(api_name).get(- 'get_api_key_url'- )- raise ValueError(- f"`{API_KEY_NAME}` not found in environment "- f"variables. Get `{API_KEY_NAME}` here: {api_key_url}"- )- request_key_name = sec_schemas.get(sec_scheme_name).get('name')- request_key_in = sec_schemas.get(sec_scheme_name).get('in')- if request_key_in == 'query':- params[request_key_name] = api_key_value- elif request_key_in == 'header':- headers[request_key_name] = api_key_value- elif request_key_in == 'coolie':- cookies[request_key_name] = api_key_value-- # Assign parameters to the correct position- for param in operation.get('parameters', []):- input_param_name = param['name'] + '_in_' + param['in']- # Irrelevant arguments does not affect function operation- if input_param_name in kwargs:- if param['in'] == 'path':- request_url = request_url.replace(- f"{{{param['name']}}}",- str(kwargs[input_param_name]),- )- elif param['in'] == 'query':- params[param['name']] = kwargs[input_param_name]- elif param['in'] == 'header':- headers[param['name']] = kwargs[input_param_name]- elif param['in'] == 'cookie':- cookies[param['name']] = kwargs[input_param_name]-- if 'requestBody' in operation:- request_body = kwargs.get('requestBody', {})- content_type_list = list(- operation.get('requestBody', {}).get('content', {}).keys()- )- if content_type_list:- content_type = content_type_list[0]- headers.update({"Content-Type": content_type})-- # send the request body based on the Content-Type- if content_type == "application/json":- response = requests.request(- method.upper(),- request_url,- params=params,- headers=headers,- cookies=cookies,- json=request_body,- )- else:- raise ValueError(- f"Unsupported content type: {content_type}"- )- else:- # If there is no requestBody, no request body is sent- response = requests.request(- method.upper(),- request_url,- params=params,- headers=headers,- cookies=cookies,- )-- try:- return response.json()- except json.JSONDecodeError:- raise ValueError(- "Response could not be decoded as JSON. "- "Please check the input parameters."- )-- return wrapper-- return inner_decorator---def generate_openapi_funcs(- api_name: str, openapi_spec: Dict[str, Any]-) -> List[Callable]:- r"""Generates a list of Python functions based on an OpenAPI specification.-- This function dynamically creates a list of callable functions that- represent the API operations defined in an OpenAPI specification document.- Each function is designed to perform an HTTP request corresponding to an- API operation (e.g., GET, POST) as defined in the specification. The- functions are decorated with `openapi_function_decorator`, which- configures them to construct and send the HTTP requests with appropriate- parameters, headers, and body content.-- Args:- api_name (str): The name of the API, used to prefix generated function- names.- openapi_spec (Dict[str, Any]): The OpenAPI specification as a- dictionary.-- Returns:- List[Callable]: A list containing the generated functions. Each- function, when called, will make an HTTP request according to its- corresponding API operation defined in the OpenAPI specification.-- Raises:- ValueError: If the OpenAPI specification does not contain server- information, which is necessary for determining the base URL for- the API requests.- """- # Check server information- servers = openapi_spec.get('servers', [])- if not servers:- raise ValueError("No server information found in OpenAPI spec.")- base_url = servers[0].get('url') # Use the first server URL-- # Security requirement objects for all methods- openapi_security = openapi_spec.get('security', {})- # Security schemas which can be reused by different methods- sec_schemas = openapi_spec.get('components', {}).get('securitySchemes', {})- functions = []-- # Traverse paths and methods- for path, methods in openapi_spec.get('paths', {}).items():- for method, operation in methods.items():- # Get the function name from the operationId- # or construct it from the API method, and path- operation_id = operation.get('operationId')- if operation_id:- function_name = f"{api_name}_{operation_id}"- else:- sanitized_path = path.replace('/', '_').strip('_')- function_name = f"{api_name}_{method}_{sanitized_path}"-- @openapi_function_decorator(- api_name,- base_url,- path,- method,- openapi_security,- sec_schemas,- operation,- )- def openapi_function(**kwargs):- pass-- openapi_function.__name__ = function_name-- functions.append(openapi_function)-- return functions---def apinames_filepaths_to_funs_schemas(- apinames_filepaths: List[Tuple[str, str]],-) -> Tuple[List[Callable], List[Dict[str, Any]]]:- r"""Combines functions and schemas from multiple OpenAPI specifications,- using API names as keys.-- This function iterates over tuples of API names and OpenAPI spec file- paths, parsing each spec to generate callable functions and schema- dictionaries, all organized by API name.-- Args:- apinames_filepaths (List[Tuple[str, str]]): A list of tuples, where each- tuple consists of:- - The API name (str) as the first element.- - The file path (str) to the API's OpenAPI specification file as the- second element.-- Returns:- Tuple[List[Callable], List[Dict[str, Any]]]:: one of callable- functions for API operations, and another of dictionaries- representing the schemas from the specifications.- """- combined_func_lst = []- combined_schemas_list = []- for api_name, file_path in apinames_filepaths:- # Parse the OpenAPI specification for each API- current_dir = os.path.dirname(__file__)- file_path = os.path.join(- current_dir, 'open_api_specs', f'{api_name}', 'openapi.yaml'- )-- openapi_spec = parse_openapi_file(file_path)- if openapi_spec is None:- return [], []-- # Generate and merge function schemas- openapi_functions_schemas = openapi_spec_to_openai_schemas(- api_name, openapi_spec- )- combined_schemas_list.extend(openapi_functions_schemas)-- # Generate and merge function lists- openapi_functions_list = generate_openapi_funcs(api_name, openapi_spec)- combined_func_lst.extend(openapi_functions_list)-- return combined_func_lst, combined_schemas_list---def generate_apinames_filepaths() -> List[Tuple[str, str]]:- """Generates a list of tuples containing API names and their corresponding- file paths.-- This function iterates over the OpenAPIName enum, constructs the file path- for each API's OpenAPI specification file, and appends a tuple of the API- name and its file path to the list. The file paths are relative to the- 'open_api_specs' directory located in the same directory as this script.-- Returns:- List[Tuple[str, str]]: A list of tuples where each tuple contains two- elements. The first element of each tuple is a string representing- the name of an API, and the second element is a string that- specifies the file path to that API's OpenAPI specification file.- """- apinames_filepaths = []- current_dir = os.path.dirname(__file__)- for api_name in OpenAPIName:- file_path = os.path.join(- current_dir, 'open_api_specs', f'{api_name.value}', 'openapi.yaml'- )- apinames_filepaths.append((api_name.value, file_path))- return apinames_filepaths---apinames_filepaths = generate_apinames_filepaths()-all_funcs_lst, all_schemas_lst = apinames_filepaths_to_funs_schemas(- apinames_filepaths-)--OPENAPI_FUNCS: List[OpenAIFunction] = [- OpenAIFunction(a_func, a_schema)- for a_func, a_schema in zip(all_funcs_lst, all_schemas_lst)-]diff --git a/camel/functions/retrieval_functions.py b/camel/functions/retrieval_functions.pydeleted file mode 100644index 53cc8fc7a..000000000--- a/camel/functions/retrieval_functions.py+++ /dev/null@@ -1,61 +0,0 @@-# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========-from typing import List, Union--from camel.functions import OpenAIFunction-from camel.retrievers import AutoRetriever-from camel.types import StorageType---def information_retrieval(- query: str, content_input_paths: Union[str, List[str]]-) -> str:- r"""Retrieves information from a local vector storage based on the- specified query. This function connects to a local vector storage system- and retrieves relevant information by processing the input query. It is- essential to use this function when the answer to a question requires- external knowledge sources.-- Args:- query (str): The question or query for which an answer is required.- content_input_paths (Union[str, List[str]]): Paths to local- files or remote URLs.-- Returns:- str: The information retrieved in response to the query, aggregated- and formatted as a string.-- Example:- # Retrieve information about CAMEL AI.- information_retrieval(query = "what is CAMEL AI?",- content_input_paths="https://www.camel-ai.org/")- """- auto_retriever = AutoRetriever(- vector_storage_local_path="camel/temp_storage",- storage_type=StorageType.QDRANT,- )-- retrieved_info = auto_retriever.run_vector_retriever(- query=query, content_input_paths=content_input_paths, top_k=3- )- return retrieved_info---# add the function to OpenAIFunction list-RETRIEVAL_FUNCS: List[OpenAIFunction] = [- OpenAIFunction(func)- for func in [- information_retrieval,- ]-]diff --git a/camel/functions/search_functions.py b/camel/functions/search_functions.pydeleted file mode 100644index 464ea0868..000000000--- a/camel/functions/search_functions.py+++ /dev/null@@ -1,298 +0,0 @@-# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========-import os-from typing import Any, Dict, List--from camel.functions.openai_function import OpenAIFunction-from camel.utils import dependencies_required---@dependencies_required('wikipedia')-def search_wiki(entity: str) -> str:- r"""Search the entity in WikiPedia and return the summary of the required- page, containing factual information about the given entity.-- Args:- entity (str): The entity to be searched.-- Returns:- str: The search result. If the page corresponding to the entity- exists, return the summary of this entity in a string.- """- import wikipedia-- result: str-- try:- result = wikipedia.summary(entity, sentences=5, auto_suggest=False)- except wikipedia.exceptions.DisambiguationError as e:- result = wikipedia.summary(- e.options[0], sentences=5, auto_suggest=False- )- except wikipedia.exceptions.PageError:- result = (- "There is no page in Wikipedia corresponding to entity "- f"{entity}, please specify another word to describe the"- " entity to be searched."- )- except wikipedia.exceptions.WikipediaException as e:- result = f"An exception occurred during the search: {e}"-- return result---def search_duckduckgo(- query: str, source: str = "text", max_results: int = 10-) -> List[Dict[str, Any]]:- r"""Use DuckDuckGo search engine to search information for the given query.-- This function queries the DuckDuckGo API for related topics to the given- search term. The results are formatted into a list of dictionaries, each- representing a search result.-- Args:- query (str): The query to be searched.- source (str): The type of information to query (e.g., "text",- "images", "videos"). Defaults to "text".- max_results (int): Max number of results, defaults to `10`.-- Returns:- List[Dict[str, Any]]: A list of dictionaries where each dictionary- represents a search result.- """- from duckduckgo_search import DDGS- from requests.exceptions import RequestException-- ddgs = DDGS()- responses: List[Dict[str, Any]] = []-- if source == "text":- try:- results = ddgs.text(keywords=query, max_results=max_results)- except RequestException as e:- # Handle specific exceptions or general request exceptions- responses.append({"error": f"duckduckgo search failed.{e}"})-- # Iterate over results found- for i, result in enumerate(results, start=1):- # Creating a response object with a similar structure- response = {- "result_id": i,- "title": result["title"],- "description": result["body"],- "url": result["href"],- }- responses.append(response)-- elif source == "images":- try:- results = ddgs.images(keywords=query, max_results=max_results)- except RequestException as e:- # Handle specific exceptions or general request exceptions- responses.append({"error": f"duckduckgo search failed.{e}"})-- # Iterate over results found- for i, result in enumerate(results, start=1):- # Creating a response object with a similar structure- response = {- "result_id": i,- "title": result["title"],- "image": result["image"],- "url": result["url"],- "source": result["source"],- }- responses.append(response)-- elif source == "videos":- try:- results = ddgs.videos(keywords=query, max_results=max_results)- except RequestException as e:- # Handle specific exceptions or general request exceptions- responses.append({"error": f"duckduckgo search failed.{e}"})-- # Iterate over results found- for i, result in enumerate(results, start=1):- # Creating a response object with a similar structure- response = {- "result_id": i,- "title": result["title"],- "description": result["description"],- "embed_url": result["embed_url"],- "publisher": result["publisher"],- "duration": result["duration"],- "published": result["published"],- }- responses.append(response)-- # If no answer found, return an empty list- return responses---def search_google(- query: str, num_result_pages: int = 10-) -> List[Dict[str, Any]]:- r"""Use Google search engine to search information for the given query.-- Args:- query (str): The query to be searched.- num_result_pages (int): The number of result pages to retrieve.-- Returns:- List[Dict[str, Any]]: A list of dictionaries where each dictionary- represents a website.- Each dictionary contains the following keys:- - 'result_id': A number in order.- - 'title': The title of the website.- - 'description': A brief description of the website.- - 'long_description': More detail of the website.- - 'url': The URL of the website.-- Example:- {- 'result_id': 1,- 'title': 'OpenAI',- 'description': 'An organization focused on ensuring that- artificial general intelligence benefits all of humanity.',- 'long_description': 'OpenAI is a non-profit artificial- intelligence research company. Our goal is to advance digital- intelligence in the way that is most likely to benefit humanity- as a whole',- 'url': 'https://www.openai.com'- }- title, description, url of a website.- """- import requests-- # https://developers.google.com/custom-search/v1/overview- GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")- # https://cse.google.com/cse/all- SEARCH_ENGINE_ID = os.getenv("SEARCH_ENGINE_ID")-- # Using the first page- start_page_idx = 1- # Different language may get different result- search_language = "en"- # How many pages to return- num_result_pages = 10- # Constructing the URL- # Doc: https://developers.google.com/custom-search/v1/using_rest- url = (- f"https://www.googleapis.com/customsearch/v1?"- f"key={GOOGLE_API_KEY}&cx={SEARCH_ENGINE_ID}&q={query}&start="- f"{start_page_idx}&lr={search_language}&num={num_result_pages}"- )-- responses = []- # Fetch the results given the URL- try:- # Make the get- result = requests.get(url)- data = result.json()-- # Get the result items- if "items" in data:- search_items = data.get("items")-- # Iterate over 10 results found- for i, search_item in enumerate(search_items, start=1):- if "og:description" in search_item["pagemap"]["metatags"][0]:- long_description = search_item["pagemap"]["metatags"][0][- "og:description"- ]- else:- long_description = "N/A"- # Get the page title- title = search_item.get("title")- # Page snippet- snippet = search_item.get("snippet")-- # Extract the page url- link = search_item.get("link")- response = {- "result_id": i,- "title": title,- "description": snippet,- "long_description": long_description,- "url": link,- }- responses.append(response)- else:- responses.append({"error": "google search failed."})-- except requests.RequestException:- # Handle specific exceptions or general request exceptions- responses.append({"error": "google search failed."})- # If no answer found, return an empty list- return responses---@dependencies_required('wolframalpha')-def query_wolfram_alpha(query: str, is_detailed: bool) -> str:- r"""Queries Wolfram|Alpha and returns the result. Wolfram|Alpha is an- answer engine developed by Wolfram Research. It is offered as an online- service that answers factual queries by computing answers from externally- sourced data.-- Args:- query (str): The query to send to Wolfram Alpha.- is_detailed (bool): Whether to include additional details in the- result.-- Returns:- str: The result from Wolfram Alpha, formatted as a string.- """- import wolframalpha-- WOLFRAMALPHA_APP_ID = os.environ.get('WOLFRAMALPHA_APP_ID')- if not WOLFRAMALPHA_APP_ID:- raise ValueError(- "`WOLFRAMALPHA_APP_ID` not found in environment "- "variables. Get `WOLFRAMALPHA_APP_ID` here: "- "`https://products.wolframalpha.com/api/`."- )-- try:- client = wolframalpha.Client(WOLFRAMALPHA_APP_ID)- res = client.query(query)- assumption = next(res.pods).text or "No assumption made."- answer = next(res.results).text or "No answer found."- except Exception as e:- if isinstance(e, StopIteration):- return "Wolfram Alpha wasn't able to answer it"- else:- error_message = f"Wolfram Alpha wasn't able to answer it" f"{e!s}."- return error_message-- result = f"Assumption:\n{assumption}\n\nAnswer:\n{answer}"-- # Add additional details in the result- if is_detailed:- result += '\n'- for pod in res.pods:- result += '\n' + pod['@title'] + ':\n'- for sub in pod.subpods:- result += (sub.plaintext or "None") + '\n'-- return result.rstrip() # Remove trailing whitespace---SEARCH_FUNCS: List[OpenAIFunction] = [- OpenAIFunction(func) # type: ignore[arg-type]- for func in [- search_wiki,- search_google,- search_duckduckgo,- query_wolfram_alpha,- ]-]diff --git a/camel/functions/slack_functions.py b/camel/functions/slack_functions.pydeleted file mode 100644index 60612de86..000000000--- a/camel/functions/slack_functions.py+++ /dev/null@@ -1,286 +0,0 @@-# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========--from __future__ import annotations--import json-import logging-import os-from typing import TYPE_CHECKING, List, Optional--from camel.utils import dependencies_required--if TYPE_CHECKING:- from ssl import SSLContext-- from slack_sdk import WebClient--from camel.functions import OpenAIFunction--logger = logging.getLogger(__name__)---@dependencies_required('slack_sdk')-def _login_slack(- slack_token: Optional[str] = None,- ssl: Optional[SSLContext] = None,-) -> WebClient:- r"""Authenticate using the Slack API.-- Args:- slack_token (str, optional): The Slack API token. If not provided, it- attempts to retrieve the token from the environment variable- SLACK_BOT_TOKEN or SLACK_USER_TOKEN.- ssl (SSLContext, optional): SSL context for secure connections.- Defaults to `None`.-- Returns:- WebClient: A WebClient object for interacting with Slack API.-- Raises:- ImportError: If slack_sdk package is not installed.- KeyError: If SLACK_BOT_TOKEN or SLACK_USER_TOKEN environment variables- are not set.- """- from slack_sdk import WebClient-- if not slack_token:- slack_token = os.environ.get("SLACK_BOT_TOKEN") or os.environ.get(- "SLACK_USER_TOKEN"- )- if not slack_token:- raise KeyError(- "SLACK_BOT_TOKEN or SLACK_USER_TOKEN environment variable not "- "set."- )-- client = WebClient(token=slack_token, ssl=ssl)- logger.info("Slack login successful.")- return client---def create_slack_channel(name: str, is_private: Optional[bool] = True) -> str:- r"""Creates a new slack channel, either public or private.-- Args:- name (str): Name of the public or private channel to create.- is_private (bool, optional): Whether to create a private channel- instead of a public one. Defaults to `True`.-- Returns:- str: JSON string containing information about Slack channel created.-- Raises:- SlackApiError: If there is an error during get slack channel- information.- """- from slack_sdk.errors import SlackApiError-- try:- slack_client = _login_slack()- response = slack_client.conversations_create(- name=name, is_private=is_private- )- channel_id = response["channel"]["id"]- response = slack_client.conversations_archive(channel=channel_id)- return str(response)- except SlackApiError as e:- return f"Error creating conversation: {e.response['error']}"---def join_slack_channel(channel_id: str) -> str:- r"""Joins an existing Slack channel.-- Args:- channel_id (str): The ID of the Slack channel to join.-- Returns:- str: A confirmation message indicating whether join successfully or an- error message.-- Raises:- SlackApiError: If there is an error during get slack channel- information.- """- from slack_sdk.errors import SlackApiError-- try:- slack_client = _login_slack()- response = slack_client.conversations_join(channel=channel_id)- return str(response)- except SlackApiError as e:- return f"Error creating conversation: {e.response['error']}"---def leave_slack_channel(channel_id: str) -> str:- r"""Leaves an existing Slack channel.-- Args:- channel_id (str): The ID of the Slack channel to leave.-- Returns:- str: A confirmation message indicating whether leave successfully or an- error message.-- Raises:- SlackApiError: If there is an error during get slack channel- information.- """- from slack_sdk.errors import SlackApiError-- try:- slack_client = _login_slack()- response = slack_client.conversations_leave(channel=channel_id)- return str(response)- except SlackApiError as e:- return f"Error creating conversation: {e.response['error']}"---def get_slack_channel_information() -> str:- r"""Retrieve Slack channels and return relevant information in JSON format.-- Returns:- str: JSON string containing information about Slack channels.-- Raises:- SlackApiError: If there is an error during get slack channel- information.- """- from slack_sdk.errors import SlackApiError-- try:- slack_client = _login_slack()- response = slack_client.conversations_list()- conversations = response["channels"]- # Filtering conversations and extracting required information- filtered_result = [- {- key: conversation[key]- for key in ("id", "name", "created", "num_members")- }- for conversation in conversations- if all(- key in conversation- for key in ("id", "name", "created", "num_members")- )- ]- return json.dumps(filtered_result, ensure_ascii=False)- except SlackApiError as e:- return f"Error creating conversation: {e.response['error']}"---def get_slack_channel_message(channel_id: str) -> str:- r"""Retrieve messages from a Slack channel.-- Args:- channel_id (str): The ID of the Slack channel to retrieve messages- from.-- Returns:- str: JSON string containing filtered message data.-- Raises:- SlackApiError: If there is an error during get slack channel message.- """- from slack_sdk.errors import SlackApiError-- try:- slack_client = _login_slack()- result = slack_client.conversations_history(channel=channel_id)- messages = result["messages"]- filtered_messages = [- {key: message[key] for key in ("user", "text", "ts")}- for message in messages- if all(key in message for key in ("user", "text", "ts"))- ]- return json.dumps(filtered_messages, ensure_ascii=False)- except SlackApiError as e:- return f"Error retrieving messages: {e.response['error']}"---def send_slack_message(- message: str,- channel_id: str,- user: Optional[str] = None,-) -> str:- r"""Send a message to a Slack channel.-- Args:- message (str): The message to send.- channel_id (str): The ID of the Slack channel to send message.- user (Optional[str]): The user ID of the recipient. Defaults to `None`.-- Returns:- str: A confirmation message indicating whether the message was sent- successfully or an error message.-- Raises:- SlackApiError: If an error occurs while sending the message.- """- from slack_sdk.errors import SlackApiError-- try:- slack_client = _login_slack()- if user:- response = slack_client.chat_postEphemeral(- channel=channel_id, text=message, user=user- )- else:- response = slack_client.chat_postMessage(- channel=channel_id, text=message- )- return str(response)- except SlackApiError as e:- return f"Error creating conversation: {e.response['error']}"---def delete_slack_message(- time_stamp: str,- channel_id: str,-) -> str:- r"""Delete a message to a Slack channel.-- Args:- time_stamp (str): Timestamp of the message to be deleted.- channel_id (str): The ID of the Slack channel to delete message.-- Returns:- str: A confirmation message indicating whether the message was delete- successfully or an error message.-- Raises:- SlackApiError: If an error occurs while sending the message.- """- from slack_sdk.errors import SlackApiError-- try:- slack_client = _login_slack()- response = slack_client.chat_delete(channel=channel_id, ts=time_stamp)- return str(response)- except SlackApiError as e:- return f"Error creating conversation: {e.response['error']}"---SLACK_FUNCS: List[OpenAIFunction] = [- OpenAIFunction(func) # type: ignore[arg-type]- for func in [- create_slack_channel,- join_slack_channel,- leave_slack_channel,- get_slack_channel_information,- get_slack_channel_message,- send_slack_message,- delete_slack_message,- ]-]diff --git a/camel/functions/twitter_function.py b/camel/functions/twitter_function.pydeleted file mode 100644index c6dfcf7fb..000000000--- a/camel/functions/twitter_function.py+++ /dev/null@@ -1,479 +0,0 @@-# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========-import datetime-import os-from http import HTTPStatus-from http.client import responses-from typing import List, Optional, Tuple, Union--import requests--from camel.functions import OpenAIFunction-from camel.utils import dependencies_required--TWEET_TEXT_LIMIT = 280---def get_twitter_api_key() -> Tuple[str, str]:- r"""Retrieve the Twitter API key and secret from environment variables.-- Returns:- Tuple[str, str]: A tuple containing the Twitter API key and secret.-- Raises:- ValueError: If the API key or secret is not found in the environment- variables.- """- # Get `TWITTER_CONSUMER_KEY` and `TWITTER_CONSUMER_SECRET` here:- # https://developer.twitter.com/en/portal/products/free- TWITTER_CONSUMER_KEY = os.environ.get("TWITTER_CONSUMER_KEY")- TWITTER_CONSUMER_SECRET = os.environ.get("TWITTER_CONSUMER_SECRET")-- if not TWITTER_CONSUMER_KEY or not TWITTER_CONSUMER_SECRET:- missing_keys = ", ".join(- [- "TWITTER_CONSUMER_KEY" if not TWITTER_CONSUMER_KEY else "",- "TWITTER_CONSUMER_SECRET"- if not TWITTER_CONSUMER_SECRET- else "",- ]- ).strip(", ")- raise ValueError(- f"{missing_keys} not found in environment variables. Get them "- "here: `https://developer.twitter.com/en/portal/products/free`."- )- return TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET---@dependencies_required('requests_oauthlib')-def get_oauth_session() -> requests.Session:- r'''Initiates an OAuth1Session with Twitter's API and returns it.-- The function first fetches a request token, then prompts the user to- authorize the application. After the user has authorized the application- and provided a verifier (PIN), the function fetches an access token.- Finally, a new OAuth1Session is created with the access token and returned.-- Raises:- RuntimeError: If an error occurs while fetching the OAuth access token- or the OAuth request token.-- Returns:- requests_oauthlib.OAuth1Session: An OAuth1Session object authenticated- with the user's access token.-- Reference:- https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/Manage-Tweets/create_tweet.py- https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/User-Lookup/get_users_me_user_context.py- '''- from requests_oauthlib import OAuth1Session-- consumer_key, consumer_secret = get_twitter_api_key()-- # Get request token- request_token_url = (- "https://api.twitter.com/oauth/request_token"- "?oauth_callback=oob&x_auth_access_type=write"- )- oauth = OAuth1Session(consumer_key, client_secret=consumer_secret)-- try:- fetch_response = oauth.fetch_request_token(request_token_url)- except Exception as e:- raise RuntimeError(- f"Error occurred while fetching the OAuth access token: {e}"- )-- resource_owner_key = fetch_response.get("oauth_token")- resource_owner_secret = fetch_response.get("oauth_token_secret")-- # Get authorization- base_authorization_url = "https://api.twitter.com/oauth/authorize"- authorization_url = oauth.authorization_url(base_authorization_url)- print("Please go here and authorize: %s" % authorization_url)- verifier = input("Paste the PIN here: ")-- # Get the access token- access_token_url = "https://api.twitter.com/oauth/access_token"- oauth = OAuth1Session(- consumer_key,- client_secret=consumer_secret,- resource_owner_key=resource_owner_key,- resource_owner_secret=resource_owner_secret,- verifier=verifier,- )-- try:- oauth_tokens = oauth.fetch_access_token(access_token_url)- except Exception as e:- raise RuntimeError(- f"Error occurred while fetching the OAuth request token: {e}"- )-- # Create a new OAuth1Session with the access token- oauth = OAuth1Session(- consumer_key,- client_secret=consumer_secret,- resource_owner_key=oauth_tokens["oauth_token"],- resource_owner_secret=oauth_tokens["oauth_token_secret"],- )- return oauth---def handle_http_error(response: requests.Response) -> str:- r"""Handles the HTTP response by checking the status code and returning an- appropriate message if there is an error.-- Args:- response (requests.Response): The HTTP response to handle.-- Returns:- str: A string describing the error, if any. If there is no error, the- function returns an "Unexpected Exception" message.-- Reference:- https://github.com/tweepy/tweepy/blob/master/tweepy/client.py#L64- """- if response.status_code in responses:- # For 5xx server errors, return "Twitter Server Error"- if 500 <= response.status_code < 600:- return "Twitter Server Error"- else:- error_message = responses[response.status_code] + " Error"- return error_message- elif not 200 <= response.status_code < 300:- return "HTTP Exception"- else:- return "Unexpected Exception"---def create_tweet(- *,- text: str,- poll_options: Optional[List[str]] = None,- poll_duration_minutes: Optional[int] = None,- quote_tweet_id: Optional[Union[int, str]] = None,-) -> str:- r"""Creates a new tweet, optionally including a poll or a quote tweet, or- simply a text-only tweet.-- This function sends a POST request to the Twitter API to create a new- tweet. The tweet can be a text-only tweet, or optionally include a poll or- be a quote tweet. A confirmation prompt is presented to the user before the- tweet is created.-- Args:- text (str): The text of the tweet. The Twitter character limit for a- single tweet is 280 characters.- poll_options (Optional[List[str]]): A list of poll options for a tweet- with a poll.- poll_duration_minutes (Optional[int]): Duration of the poll in minutes- for a tweet with a poll. This is only required if the request- includes poll_options.- quote_tweet_id (Optional[Union[int, str]]): Link to the tweet being- quoted.-- Note:- You can only provide either the `quote_tweet_id` parameter or the pair- of `poll_duration_minutes` and `poll_options` parameters, not both.-- Returns:- str: A message indicating the success of the tweet creation, including- the tweet ID and text. If the request to the Twitter API is not- successful, the return is an error message.-- Reference:- https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets- https://github.com/xdevplatform/Twitter-API-v2-sample-code/blob/main/Manage-Tweets/create_tweet.py- """- # validate text- if text is None:- return "Text cannot be None"- elif len(text) > TWEET_TEXT_LIMIT:- return "Text must not exceed 280 characters."-- # Validate poll options and duration- if (poll_options is None) != (poll_duration_minutes is None):- return (- "Error: Both `poll_options` and `poll_duration_minutes` must "- "be provided together or not at all."- )-- # Validate exclusive parameters- if quote_tweet_id is not None and (poll_options or poll_duration_minutes):- return (- "Error: Cannot provide both `quote_tweet_id` and "- "(`poll_options` or `poll_duration_minutes`)."- )-- # Print the parameters that are not None- params = {- "text": text,- "poll_options": poll_options,- "poll_duration_minutes": poll_duration_minutes,- "quote_tweet_id": quote_tweet_id,- }- print("You are going to create a tweet with following parameters:")- for key, value in params.items():- if value is not None:- print(f"{key}: {value}")-- # Add a confirmation prompt at the beginning of the function- confirm = input("Are you sure you want to create this tweet? (yes/no): ")- if confirm.lower() != "yes":- return "Execution cancelled by the user."-- oauth = get_oauth_session()- json_data = {}-- if poll_options is not None and poll_duration_minutes is not None:- json_data["poll"] = {- "options": poll_options,- "duration_minutes": poll_duration_minutes,- }-- if quote_tweet_id is not None:- json_data["quote_tweet_id"] = str(quote_tweet_id) # type: ignore[assignment]-- json_data["text"] = text # type: ignore[assignment]-- # Making the request- response = oauth.post(- "https://api.twitter.com/2/tweets",- json=json_data,- )-- if response.status_code != HTTPStatus.CREATED:- error_type = handle_http_error(response)- # use string concatenation to satisfy flake8- return (- "Request returned a(n) "- + str(error_type)- + ": "- + str(response.status_code)- + " "- + response.text- )-- # Saving the response as JSON- json_response = response.json()-- tweet_id = json_response["data"]["id"]- tweet_text = json_response["data"]["text"]-- response_str = (- f"Create tweet successful. "- f"The tweet ID is: {tweet_id}. "- f"The tweet text is: '{tweet_text}'."- )-- return response_str---def delete_tweet(tweet_id: str) -> str:- r"""Deletes a tweet with the specified ID for an authorized user.-- This function sends a DELETE request to the Twitter API to delete- a tweet with the specified ID. Before sending the request, it- prompts the user to confirm the deletion.-- Args:- tweet_id (str): The ID of the tweet to delete.-- Returns:- str: A message indicating the result of the deletion. If the- deletion was successful, the message includes the ID of the- deleted tweet. If the deletion was not successful, the message- includes an error message.-- Reference:- https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/delete-tweets-id- """- # Print the parameters that are not None- if tweet_id is not None:- print(- f"You are going to delete a tweet with the following "- f"ID: {tweet_id}"- )-- # Add a confirmation prompt at the beginning of the function- confirm = input("Are you sure you want to delete this tweet? (yes/no): ")- if confirm.lower() != "yes":- return "Execution cancelled by the user."-- oauth = get_oauth_session()-- # Making the request- response = oauth.delete(- f"https://api.twitter.com/2/tweets/{tweet_id}",- )-- if response.status_code != HTTPStatus.OK:- error_type = handle_http_error(response)- # use string concatenation to satisfy flake8- return (- "Request returned a(n) "- + str(error_type)- + ": "- + str(response.status_code)- + " "- + response.text- )-- # Saving the response as JSON- json_response = response.json()- # `deleted_status` may be True or False. Defaults to False if not found.- deleted_status = json_response.get("data", {}).get("deleted", False)- response_str = (- f"Delete tweet successful: {deleted_status}. "- f"The tweet ID is: {tweet_id}. "- )- return response_str---def get_my_user_profile() -> str:- r"""Retrieves and formats the authenticated user's Twitter profile info.-- This function sends a GET request to the Twitter API to retrieve the- authenticated user's profile information, including their pinned tweet.- It then formats this information into a readable report.-- Returns:- str: A formatted report of the authenticated user's Twitter profile- information. This includes their ID, name, username, description,- location, most recent tweet ID, profile image URL, account creation- date, protection status, verification type, public metrics, and- pinned tweet information. If the request to the Twitter API is not- successful, the return is an error message.-- Reference:- https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-me- """- oauth = get_oauth_session()-- tweet_fields = ["created_at", "text"]- user_fields = [- "created_at",- "description",- "id",- "location",- "most_recent_tweet_id",- "name",- "pinned_tweet_id",- "profile_image_url",- "protected",- "public_metrics",- "url",- "username",- "verified_type",- ]- params = {- "expansions": "pinned_tweet_id",- "tweet.fields": ",".join(tweet_fields),- "user.fields": ",".join(user_fields),- }-- response = oauth.get("https://api.twitter.com/2/users/me", params=params)-- if response.status_code != HTTPStatus.OK:- error_type = handle_http_error(response)- error_message = "Request returned a(n) {}: {} {}".format(- error_type, response.status_code, response.text- )- return error_message-- json_response = response.json()-- user_info = json_response.get('data', {})- tweets = json_response.get('includes', {}).get('tweets', [{}])[0]-- user_report = ""- user_report += f"ID: {user_info['id']}. "- user_report += f"Name: {user_info['name']}. "- user_report += f"Username: {user_info['username']}. "-- # Define the part of keys that need to be repeatedly processed- user_info_keys = [- 'description',- 'location',- 'most_recent_tweet_id',- 'profile_image_url',- ]- for key in user_info_keys:- value = user_info.get(key)- if user_info.get(key):- user_report += f"{key.replace('_', ' ').capitalize()}: {value}. "-- if 'created_at' in user_info:- created_at = datetime.datetime.strptime(- user_info['created_at'], "%Y-%m-%dT%H:%M:%S.%fZ"- )- date_str = created_at.strftime('%B %d, %Y at %H:%M:%S')- user_report += f"Account created at: {date_str}. "-- protection_status = "private" if user_info['protected'] else "public"- user_report += f"Protected: This user's Tweets are {protection_status}. "-- verification_messages = {- 'blue': (- "The user has a blue verification, typically reserved for "- "public figures, celebrities, or global brands. "- ),- 'business': (- "The user has a business verification, typically "- "reserved for businesses and corporations. "- ),- 'government': (- "The user has a government verification, typically "- "reserved for government officials or entities. "- ),- 'none': "The user is not verified. ",- }- verification_type = user_info.get('verified_type', 'none')- user_report += (- f"Verified type: {verification_messages.get(verification_type)}"- )-- if 'public_metrics' in user_info:- user_report += "Public metrics: "- metrics = user_info['public_metrics']- user_report += (- f"The user has {metrics.get('followers_count', 0)} followers, "- f"is following {metrics.get('following_count', 0)} users, "- f"has made {metrics.get('tweet_count', 0)} tweets, "- f"is listed in {metrics.get('listed_count', 0)} lists, "- f"and has received {metrics.get('like_count', 0)} likes. "- )-- if 'pinned_tweet_id' in user_info:- user_report += f"Pinned tweet ID: {user_info['pinned_tweet_id']}. "-- if 'created_at' in tweets and 'text' in tweets:- user_report += "\nPinned tweet information: "- tweet_created_at = datetime.datetime.strptime(- tweets['created_at'], "%Y-%m-%dT%H:%M:%S.%fZ"- )- user_report += (- f"Pinned tweet created at "- f"{tweet_created_at.strftime('%B %d, %Y at %H:%M:%S')} "- f"with text: '{tweets['text']}'."- )-- return user_report---TWITTER_FUNCS: List[OpenAIFunction] = [- OpenAIFunction(func) # type: ignore[arg-type]- for func in [create_tweet, delete_tweet, get_my_user_profile]-]diff --git a/camel/functions/weather_functions.py b/camel/functions/weather_functions.pydeleted file mode 100644index cf540c6ae..000000000--- a/camel/functions/weather_functions.py+++ /dev/null@@ -1,144 +0,0 @@-# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========-import os-from typing import List, Literal--from camel.functions.openai_function import OpenAIFunction-from camel.utils import dependencies_required---def get_openweathermap_api_key() -> str:- r"""Retrieve the OpenWeatherMap API key from environment variables.-- Returns:- str: The OpenWeatherMap API key.-- Raises:- ValueError: If the API key is not found in the environment variables.- """- # Get `OPENWEATHERMAP_API_KEY` here: https://openweathermap.org- OPENWEATHERMAP_API_KEY = os.environ.get('OPENWEATHERMAP_API_KEY')- if not OPENWEATHERMAP_API_KEY:- raise ValueError(- "`OPENWEATHERMAP_API_KEY` not found in environment "- "variables. Get `OPENWEATHERMAP_API_KEY` here: "- "`https://openweathermap.org`."- )- return OPENWEATHERMAP_API_KEY---@dependencies_required('pyowm')-def get_weather_data(- city: str,- temp_units: Literal['kelvin', 'celsius', 'fahrenheit'] = 'kelvin',- wind_units: Literal[- 'meters_sec', 'miles_hour', 'knots', 'beaufort'- ] = 'meters_sec',- visibility_units: Literal['meters', 'miles'] = 'meters',- time_units: Literal['unix', 'iso', 'date'] = 'unix',-) -> str:- r"""Fetch and return a comprehensive weather report for a given city as a- string. The report includes current weather conditions, temperature,- wind details, visibility, and sunrise/sunset times, all formatted as-- The function interacts with the OpenWeatherMap API to retrieve the data.-- Args:- city (str): The name of the city for which the weather information- is desired. Format "City, CountryCode" (e.g., "Paris, FR"- for Paris, France). If the country code is not provided,- the API will search for the city in all countries, which- may yield incorrect results if multiple cities with the- same name exist.- temp_units (Literal['kelvin', 'celsius', 'fahrenheit']): Units for- temperature. (default: :obj:`kelvin`)- wind_units (Literal['meters_sec', 'miles_hour', 'knots', 'beaufort']):- Units for wind speed. (default: :obj:`meters_sec`)- visibility_units (Literal['meters', 'miles']): Units for visibility- distance. (default: :obj:`meters`)- time_units (Literal['unix', 'iso', 'date']): Format for sunrise and- sunset times. (default: :obj:`unix`)-- Returns:- str: A string containing the fetched weather data, formatted in a- readable manner. If an error occurs, a message indicating the- error will be returned instead.-- Example of return string:- "Weather in Paris, FR: 15°C, feels like 13°C. Max temp: 17°C, Min temp- : 12°C.- Wind: 5 m/s at 270 degrees. Visibility: 10 kilometers.- Sunrise at 05:46:05 (UTC), Sunset at 18:42:20 (UTC)."-- Note:- Please ensure that the API key is valid and has permissions to access- the weather data.- """- # NOTE: This tool may not work as expected since the input arguments like- # `time_units` should be enum types which are not supported yet.- import pyowm-- OPENWEATHERMAP_API_KEY = get_openweathermap_api_key()- owm = pyowm.OWM(OPENWEATHERMAP_API_KEY)- mgr = owm.weather_manager()-- try:- observation = mgr.weather_at_place(city)- weather = observation.weather-- # Temperature- temperature = weather.temperature(temp_units)-- # Wind- wind_data = observation.weather.wind(unit=wind_units)- wind_speed = wind_data.get('speed')- # 'N/A' if the degree is not available- wind_deg = wind_data.get('deg', 'N/A')-- # Visibility- visibility_distance = observation.weather.visibility_distance- visibility = (- str(visibility_distance)- if visibility_units == 'meters'- else str(observation.weather.visibility(unit='miles'))- )-- # Sunrise and Sunset- sunrise_time = str(weather.sunrise_time(timeformat=time_units))- sunset_time = str(weather.sunset_time(timeformat=time_units))-- # Compile all the weather details into a report string- weather_report = (- f"Weather in {city}: {temperature['temp']}°{temp_units.title()}, "- f"feels like {temperature['feels_like']}°{temp_units.title()}. "- f"Max temp: {temperature['temp_max']}°{temp_units.title()}, "- f"Min temp: {temperature['temp_min']}°{temp_units.title()}. "- f"Wind: {wind_speed} {wind_units} at {wind_deg} degrees. "- f"Visibility: {visibility} {visibility_units}. "- f"Sunrise at {sunrise_time}, Sunset at {sunset_time}."- )-- return weather_report-- except Exception as e:- error_message = (- f"An error occurred while fetching weather data for {city}: "- f"{e!s}."- )- return error_message---WEATHER_FUNCS: List[OpenAIFunction] = [- OpenAIFunction(func) for func in [get_weather_data]-]diff --git a/camel/toolkits/__init__.py b/camel/toolkits/__init__.pyindex a02e0c5d4..6eb50d2e2 100644--- a/camel/toolkits/__init__.py+++ b/camel/toolkits/__init__.py@@ -11,13 +11,49 @@# See the License for the specific language governing permissions and# limitations under the License.# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# ruff: noqa: I001+from .openai_function import (+ OpenAIFunction,+ get_openai_function_schema,+ get_openai_tool_schema,+)+from .open_api_specs.security_config import openapi_security_config++from .google_maps_toolkit import MAP_FUNCS, GoogleMapsToolkit+from .math_toolkit import MATH_FUNCS, MathToolkit+from .open_api_toolkit import OPENAPI_FUNCS, OpenAPIToolkit+from .retrieval_toolkit import RETRIEVAL_FUNCS, RetrievalToolkit+from .search_toolkit import SEARCH_FUNCS, SearchToolkit+from .twitter_toolkit import TWITTER_FUNCS, TwitterToolkit+from .weather_toolkit import WEATHER_FUNCS, WeatherToolkit+from .slack_toolkit import SLACK_FUNCS, SlackToolkitfrom .base import BaseToolkitfrom .code_execution import CodeExecutionToolkitfrom .github_toolkit import GithubToolkit__all__ = [+ 'OpenAIFunction',+ 'get_openai_function_schema',+ 'get_openai_tool_schema',+ 'openapi_security_config',+ 'MATH_FUNCS',+ 'MAP_FUNCS',+ 'OPENAPI_FUNCS',+ 'RETRIEVAL_FUNCS',+ 'SEARCH_FUNCS',+ 'TWITTER_FUNCS',+ 'WEATHER_FUNCS',+ 'SLACK_FUNCS','BaseToolkit','GithubToolkit',+ 'MathToolkit',+ 'GoogleMapsToolkit',+ 'SearchToolkit',+ 'SlackToolkit',+ 'TwitterToolkit',+ 'WeatherToolkit',+ 'RetrievalToolkit',+ 'OpenAPIToolkit','CodeExecutionToolkit',]diff --git a/camel/toolkits/base.py b/camel/toolkits/base.pyindex 52156bb67..11cb37575 100644--- a/camel/toolkits/base.py+++ b/camel/toolkits/base.py@@ -14,7 +14,7 @@from typing import List-from camel.functions import OpenAIFunction+from .openai_function import OpenAIFunctionclass BaseToolkit:diff --git a/camel/toolkits/code_execution.py b/camel/toolkits/code_execution.pyindex 4f73c48b4..0fcc3243b 100644--- a/camel/toolkits/code_execution.py+++ b/camel/toolkits/code_execution.py@@ -13,8 +13,8 @@# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========from typing import List, Literal-from camel.functions import OpenAIFunctionfrom camel.interpreters import InternalPythonInterpreter+from camel.toolkits import OpenAIFunctionfrom .base import BaseToolkitdiff --git a/camel/toolkits/github_toolkit.py b/camel/toolkits/github_toolkit.pyindex 173649694..c0106cf6a 100644--- a/camel/toolkits/github_toolkit.py+++ b/camel/toolkits/github_toolkit.py@@ -17,10 +17,11 @@ from dataclasses import dataclassfrom datetime import datetime, timedeltafrom typing import List, Optional-from camel.functions import OpenAIFunction-from camel.toolkits.base import BaseToolkitfrom camel.utils import dependencies_required+from .base import BaseToolkit+from .openai_function import OpenAIFunction+@dataclassclass GithubIssue:diff --git a/camel/toolkits/google_maps_toolkit.py b/camel/toolkits/google_maps_toolkit.pynew file mode 100644index 000000000..82cbc2bb0--- /dev/null+++ b/camel/toolkits/google_maps_toolkit.py@@ -0,0 +1,367 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========+import os+from functools import wraps+from typing import Any, Callable, List, Optional, Tuple, Union++from camel.toolkits.base import BaseToolkit+from camel.toolkits.openai_function import OpenAIFunction+++def handle_googlemaps_exceptions(+ func: Callable[..., Any],+) -> Callable[..., Any]:+ r"""Decorator to catch and handle exceptions raised by Google Maps API+ calls.++ Args:+ func (Callable): The function to be wrapped by the decorator.++ Returns:+ Callable: A wrapper function that calls the wrapped function and+ handles exceptions.+ """++ @wraps(func)+ def wrapper(*args: Any, **kwargs: Any) -> Any:+ try:+ from googlemaps.exceptions import ( # type: ignore[import-untyped] # isort: skip+ ApiError,+ HTTPError,+ Timeout,+ TransportError,+ )+ except ImportError:+ raise ImportError(+ "Please install `googlemaps` first. You can install "+ "it by running `pip install googlemaps`."+ )++ try:+ return func(*args, **kwargs)+ except ApiError as e:+ return (+ 'An exception returned by the remote API. '+ f'Status: {e.status}, Message: {e.message}'+ )+ except HTTPError as e:+ return (+ 'An unexpected HTTP error occurred. '+ f'Status Code: {e.status_code}'+ )+ except Timeout:+ return 'The request timed out.'+ except TransportError as e:+ return (+ 'Something went wrong while trying to execute the '+ f'request. Details: {e.base_exception}'+ )+ except Exception as e:+ return f'An unexpected error occurred: {e}'++ return wrapper+++class GoogleMapsToolkit(BaseToolkit):+ r"""A class representing a toolkit for interacting with GoogleMaps API.++ This class provides methods for validating addresses, retrieving elevation,+ and fetching timezone information using the Google Maps API.+ """++ def _import_googlemaps_or_raise(self) -> Any:+ r"""Attempts to import the `googlemaps` library and returns it.++ Returns:+ module: The `googlemaps` module if successfully imported.++ Raises:+ ImportError: If the `googlemaps` library is not installed, this+ error is raised with a message instructing how to install the+ library using pip.+ """+ try:+ import googlemaps++ return googlemaps+ except ImportError:+ raise ImportError(+ "Please install `googlemaps` first. You can install "+ "it by running `pip install googlemaps`."+ )++ def _get_googlemaps_api_key(self) -> str:+ r"""Retrieve the Google Maps API key from environment variables.++ Returns:+ str: The Google Maps API key.++ Raises:+ ValueError: If the API key is not found in the environment+ variables.+ """+ # Get `GOOGLEMAPS_API_KEY` here:+ # https://console.cloud.google.com/apis/credentials+ GOOGLEMAPS_API_KEY = os.environ.get('GOOGLEMAPS_API_KEY')+ if not GOOGLEMAPS_API_KEY:+ raise ValueError(+ "`GOOGLEMAPS_API_KEY` not found in environment "+ "variables. `GOOGLEMAPS_API_KEY` API keys are "+ "generated in the `Credentials` page of the "+ "`APIs & Services` tab of "+ "https://console.cloud.google.com/apis/credentials."+ )+ return GOOGLEMAPS_API_KEY++ def get_address_description(+ self,+ address: Union[str, List[str]],+ region_code: Optional[str] = None,+ locality: Optional[str] = None,+ ) -> str:+ r"""Validates an address via Google Maps API, returns a descriptive+ summary.++ Validates an address using Google Maps API, returning a summary that+ includes information on address completion, formatted address, location+ coordinates, and metadata types that are true for the given address.++ Args:+ address (Union[str, List[str]]): The address or components to+ validate. Can be a single string or a list representing+ different parts.+ region_code (str, optional): Country code for regional restriction,+ helps narrowing down results. (default: :obj:`None`)+ locality (str, optional): Restricts validation to a specific+ locality, e.g., "Mountain View". (default: :obj:`None`)++ Returns:+ str: Summary of the address validation results, including+ information on address completion, formatted address,+ geographical coordinates (latitude and longitude), and metadata+ types true for the address.++ Raises:+ ImportError: If the `googlemaps` library is not installed.+ Exception: For unexpected errors during the address validation.+ """+ googlemaps = self._import_googlemaps_or_raise()+ google_maps_api_key = self._get_googlemaps_api_key()+ try:+ gmaps = googlemaps.Client(key=google_maps_api_key)+ except Exception as e:+ return f"Error: {e!s}"++ try:+ addressvalidation_result = gmaps.addressvalidation(+ [address],+ regionCode=region_code,+ locality=locality,+ enableUspsCass=False,+ ) # Always False as per requirements++ # Check if the result contains an error+ if 'error' in addressvalidation_result:+ error_info = addressvalidation_result['error']+ error_message = error_info.get(+ 'message', 'An unknown error occurred'+ )+ error_status = error_info.get('status', 'UNKNOWN_STATUS')+ error_code = error_info.get('code', 'UNKNOWN_CODE')+ return (+ f"Address validation failed with error: {error_message} "+ f"Status: {error_status}, Code: {error_code}"+ )++ # Assuming the successful response structure+ # includes a 'result' key+ result = addressvalidation_result['result']+ verdict = result.get('verdict', {})+ address_info = result.get('address', {})+ geocode = result.get('geocode', {})+ metadata = result.get('metadata', {})++ # Construct the descriptive string+ address_complete = (+ "Yes" if verdict.get('addressComplete', False) else "No"+ )+ formatted_address = address_info.get(+ 'formattedAddress', 'Not available'+ )+ location = geocode.get('location', {})+ latitude = location.get('latitude', 'Not available')+ longitude = location.get('longitude', 'Not available')+ true_metadata_types = [+ key for key, value in metadata.items() if value+ ]+ true_metadata_types_str = (+ ', '.join(true_metadata_types)+ if true_metadata_types+ else 'None'+ )++ description = (+ f"Address completion status: {address_complete}. "+ f"Formatted address: {formatted_address}. "+ f"Location (latitude, longitude): ({latitude}, {longitude}). "+ f"Metadata indicating true types: {true_metadata_types_str}."+ )++ return description+ except Exception as e:+ return f"An unexpected error occurred: {e!s}"++ @handle_googlemaps_exceptions+ def get_elevation(self, lat_lng: Tuple) -> str:+ r"""Retrieves elevation data for a given latitude and longitude.++ Uses the Google Maps API to fetch elevation data for the specified+ latitude and longitude. It handles exceptions gracefully and returns a+ description of the elevation, including its value in meters and the+ data resolution.++ Args:+ lat_lng (Tuple[float, float]): The latitude and longitude for+ which to retrieve elevation data.++ Returns:+ str: A description of the elevation at the specified location(s),+ including the elevation in meters and the data resolution. If+ elevation data is not available, a message indicating this is+ returned.+ """+ googlemaps = self._import_googlemaps_or_raise()+ google_maps_api_key = self._get_googlemaps_api_key()+ try:+ gmaps = googlemaps.Client(key=google_maps_api_key)+ except Exception as e:+ return f"Error: {e!s}"++ # Assuming gmaps is a configured Google Maps client instance+ elevation_result = gmaps.elevation(lat_lng)++ # Extract the elevation data from the first+ # (and presumably only) result+ if elevation_result:+ elevation = elevation_result[0]['elevation']+ location = elevation_result[0]['location']+ resolution = elevation_result[0]['resolution']++ # Format the elevation data into a natural language description+ description = (+ f"The elevation at latitude {location['lat']}, "+ f"longitude {location['lng']} "+ f"is approximately {elevation:.2f} meters above sea level, "+ f"with a data resolution of {resolution:.2f} meters."+ )+ else:+ description = (+ "Elevation data is not available for the given location."+ )++ return description++ def _format_offset_to_natural_language(self, offset: int) -> str:+ r"""Converts a time offset in seconds to a more natural language+ description using hours as the unit, with decimal places to represent+ minutes and seconds.++ Args:+ offset (int): The time offset in seconds. Can be positive,+ negative, or zero.++ Returns:+ str: A string representing the offset in hours, such as+ "+2.50 hours" or "-3.75 hours".+ """+ # Convert the offset to hours as a float+ hours = offset / 3600.0+ hours_str = f"{hours:+.2f} hour{'s' if abs(hours) != 1 else ''}"+ return hours_str++ @handle_googlemaps_exceptions+ def get_timezone(self, lat_lng: Tuple) -> str:+ r"""Retrieves timezone information for a given latitude and longitude.++ This function uses the Google Maps Timezone API to fetch timezone+ data for the specified latitude and longitude. It returns a natural+ language description of the timezone, including the timezone ID, name,+ standard time offset, daylight saving time offset, and the total+ offset from Coordinated Universal Time (UTC).++ Args:+ lat_lng (Tuple[float, float]): The latitude and longitude for+ which to retrieve elevation data.++ Returns:+ str: A descriptive string of the timezone information,+ including the timezone ID and name, standard time offset,+ daylight saving time offset, and total offset from UTC.+ """+ googlemaps = self._import_googlemaps_or_raise()+ google_maps_api_key = self._get_googlemaps_api_key()+ try:+ gmaps = googlemaps.Client(key=google_maps_api_key)+ except Exception as e:+ return f"Error: {e!s}"++ # Get timezone information+ timezone_dict = gmaps.timezone(lat_lng)++ # Extract necessary information+ dst_offset = timezone_dict[+ 'dstOffset'+ ] # Daylight Saving Time offset in seconds+ raw_offset = timezone_dict[+ 'rawOffset'+ ] # Standard time offset in seconds+ timezone_id = timezone_dict['timeZoneId']+ timezone_name = timezone_dict['timeZoneName']++ raw_offset_str = self._format_offset_to_natural_language(raw_offset)+ dst_offset_str = self._format_offset_to_natural_language(dst_offset)+ total_offset_seconds = dst_offset + raw_offset+ total_offset_str = self._format_offset_to_natural_language(+ total_offset_seconds+ )++ # Create a natural language description+ description = (+ f"Timezone ID is {timezone_id}, named {timezone_name}. "+ f"The standard time offset is {raw_offset_str}. "+ f"Daylight Saving Time offset is {dst_offset_str}. "+ f"The total offset from Coordinated Universal Time (UTC) is "+ f"{total_offset_str}, including any "+ "Daylight Saving Time adjustment "+ f"if applicable. "+ )++ return description++ def get_tools(self) -> List[OpenAIFunction]:+ r"""Returns a list of OpenAIFunction objects representing the+ functions in the toolkit.++ Returns:+ List[OpenAIFunction]: A list of OpenAIFunction objects+ representing the functions in the toolkit.+ """+ return [+ OpenAIFunction(self.get_address_description),+ OpenAIFunction(self.get_elevation),+ OpenAIFunction(self.get_timezone),+ ]+++MAP_FUNCS: List[OpenAIFunction] = GoogleMapsToolkit().get_tools()diff --git a/camel/toolkits/math_toolkit.py b/camel/toolkits/math_toolkit.pynew file mode 100644index 000000000..398f0070d--- /dev/null+++ b/camel/toolkits/math_toolkit.py@@ -0,0 +1,79 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========++from typing import List++from camel.toolkits.base import BaseToolkit+from camel.toolkits.openai_function import OpenAIFunction+++class MathToolkit(BaseToolkit):+ r"""A class representing a toolkit for mathematical operations.++ This class provides methods for basic mathematical operations such as+ addition, subtraction, and multiplication.+ """++ def add(self, a: int, b: int) -> int:+ r"""Adds two numbers.++ Args:+ a (int): The first number to be added.+ b (int): The second number to be added.++ Returns:+ integer: The sum of the two numbers.+ """+ return a + b++ def sub(self, a: int, b: int) -> int:+ r"""Do subtraction between two numbers.++ Args:+ a (int): The minuend in subtraction.+ b (int): The subtrahend in subtraction.++ Returns:+ integer: The result of subtracting :obj:`b` from :obj:`a`.+ """+ return a - b++ def mul(self, a: int, b: int) -> int:+ r"""Multiplies two integers.++ Args:+ a (int): The multiplier in the multiplication.+ b (int): The multiplicand in the multiplication.++ Returns:+ integer: The product of the two numbers.+ """+ return a * b++ def get_tools(self) -> List[OpenAIFunction]:+ r"""Returns a list of OpenAIFunction objects representing the+ functions in the toolkit.++ Returns:+ List[OpenAIFunction]: A list of OpenAIFunction objects+ representing the functions in the toolkit.+ """+ return [+ OpenAIFunction(self.add),+ OpenAIFunction(self.sub),+ OpenAIFunction(self.mul),+ ]+++MATH_FUNCS: List[OpenAIFunction] = MathToolkit().get_tools()diff --git a/camel/functions/open_api_specs/biztoc/__init__.py b/camel/toolkits/open_api_specs/biztoc/__init__.pysimilarity index 100%rename from camel/functions/open_api_specs/biztoc/__init__.pyrename to camel/toolkits/open_api_specs/biztoc/__init__.pydiff --git a/camel/functions/open_api_specs/biztoc/ai-plugin.json b/camel/toolkits/open_api_specs/biztoc/ai-plugin.jsonsimilarity index 100%rename from camel/functions/open_api_specs/biztoc/ai-plugin.jsonrename to camel/toolkits/open_api_specs/biztoc/ai-plugin.jsondiff --git a/camel/functions/open_api_specs/biztoc/openapi.yaml b/camel/toolkits/open_api_specs/biztoc/openapi.yamlsimilarity index 100%rename from camel/functions/open_api_specs/biztoc/openapi.yamlrename to camel/toolkits/open_api_specs/biztoc/openapi.yamldiff --git a/camel/functions/open_api_specs/coursera/__init__.py b/camel/toolkits/open_api_specs/coursera/__init__.pysimilarity index 100%rename from camel/functions/open_api_specs/coursera/__init__.pyrename to camel/toolkits/open_api_specs/coursera/__init__.pydiff --git a/camel/functions/open_api_specs/coursera/openapi.yaml b/camel/toolkits/open_api_specs/coursera/openapi.yamlsimilarity index 100%rename from camel/functions/open_api_specs/coursera/openapi.yamlrename to camel/toolkits/open_api_specs/coursera/openapi.yamldiff --git a/camel/functions/open_api_specs/create_qr_code/__init__.py b/camel/toolkits/open_api_specs/create_qr_code/__init__.pysimilarity index 100%rename from camel/functions/open_api_specs/create_qr_code/__init__.pyrename to camel/toolkits/open_api_specs/create_qr_code/__init__.pydiff --git a/camel/functions/open_api_specs/create_qr_code/openapi.yaml b/camel/toolkits/open_api_specs/create_qr_code/openapi.yamlsimilarity index 100%rename from camel/functions/open_api_specs/create_qr_code/openapi.yamlrename to camel/toolkits/open_api_specs/create_qr_code/openapi.yamldiff --git a/camel/functions/open_api_specs/klarna/__init__.py b/camel/toolkits/open_api_specs/klarna/__init__.pysimilarity index 100%rename from camel/functions/open_api_specs/klarna/__init__.pyrename to camel/toolkits/open_api_specs/klarna/__init__.pydiff --git a/camel/functions/open_api_specs/klarna/openapi.yaml b/camel/toolkits/open_api_specs/klarna/openapi.yamlsimilarity index 100%rename from camel/functions/open_api_specs/klarna/openapi.yamlrename to camel/toolkits/open_api_specs/klarna/openapi.yamldiff --git a/camel/functions/open_api_specs/nasa_apod/__init__.py b/camel/toolkits/open_api_specs/nasa_apod/__init__.pysimilarity index 100%rename from camel/functions/open_api_specs/nasa_apod/__init__.pyrename to camel/toolkits/open_api_specs/nasa_apod/__init__.pydiff --git a/camel/functions/open_api_specs/nasa_apod/openapi.yaml b/camel/toolkits/open_api_specs/nasa_apod/openapi.yamlsimilarity index 100%rename from camel/functions/open_api_specs/nasa_apod/openapi.yamlrename to camel/toolkits/open_api_specs/nasa_apod/openapi.yamldiff --git a/camel/functions/open_api_specs/outschool/__init__.py b/camel/toolkits/open_api_specs/outschool/__init__.pysimilarity index 100%rename from camel/functions/open_api_specs/outschool/__init__.pyrename to camel/toolkits/open_api_specs/outschool/__init__.pydiff --git a/camel/functions/open_api_specs/outschool/ai-plugin.json b/camel/toolkits/open_api_specs/outschool/ai-plugin.jsonsimilarity index 100%rename from camel/functions/open_api_specs/outschool/ai-plugin.jsonrename to camel/toolkits/open_api_specs/outschool/ai-plugin.jsondiff --git a/camel/functions/open_api_specs/outschool/openapi.yaml b/camel/toolkits/open_api_specs/outschool/openapi.yamlsimilarity index 100%rename from camel/functions/open_api_specs/outschool/openapi.yamlrename to camel/toolkits/open_api_specs/outschool/openapi.yamldiff --git a/camel/functions/open_api_specs/outschool/paths/__init__.py b/camel/toolkits/open_api_specs/outschool/paths/__init__.pysimilarity index 100%rename from camel/functions/open_api_specs/outschool/paths/__init__.pyrename to camel/toolkits/open_api_specs/outschool/paths/__init__.pydiff --git a/camel/functions/open_api_specs/outschool/paths/get_classes.py b/camel/toolkits/open_api_specs/outschool/paths/get_classes.pysimilarity index 100%rename from camel/functions/open_api_specs/outschool/paths/get_classes.pyrename to camel/toolkits/open_api_specs/outschool/paths/get_classes.pydiff --git a/camel/functions/open_api_specs/outschool/paths/search_teachers.py b/camel/toolkits/open_api_specs/outschool/paths/search_teachers.pysimilarity index 100%rename from camel/functions/open_api_specs/outschool/paths/search_teachers.pyrename to camel/toolkits/open_api_specs/outschool/paths/search_teachers.pydiff --git a/camel/functions/open_api_specs/security_config.py b/camel/toolkits/open_api_specs/security_config.pysimilarity index 100%rename from camel/functions/open_api_specs/security_config.pyrename to camel/toolkits/open_api_specs/security_config.pydiff --git a/camel/functions/open_api_specs/speak/__init__.py b/camel/toolkits/open_api_specs/speak/__init__.pysimilarity index 100%rename from camel/functions/open_api_specs/speak/__init__.pyrename to camel/toolkits/open_api_specs/speak/__init__.pydiff --git a/camel/functions/open_api_specs/speak/openapi.yaml b/camel/toolkits/open_api_specs/speak/openapi.yamlsimilarity index 100%rename from camel/functions/open_api_specs/speak/openapi.yamlrename to camel/toolkits/open_api_specs/speak/openapi.yamldiff --git a/camel/functions/open_api_specs/web_scraper/__init__.py b/camel/toolkits/open_api_specs/web_scraper/__init__.pysimilarity index 100%rename from camel/functions/open_api_specs/web_scraper/__init__.pyrename to camel/toolkits/open_api_specs/web_scraper/__init__.pydiff --git a/camel/functions/open_api_specs/web_scraper/ai-plugin.json b/camel/toolkits/open_api_specs/web_scraper/ai-plugin.jsonsimilarity index 100%rename from camel/functions/open_api_specs/web_scraper/ai-plugin.jsonrename to camel/toolkits/open_api_specs/web_scraper/ai-plugin.jsondiff --git a/camel/functions/open_api_specs/web_scraper/openapi.yaml b/camel/toolkits/open_api_specs/web_scraper/openapi.yamlsimilarity index 100%rename from camel/functions/open_api_specs/web_scraper/openapi.yamlrename to camel/toolkits/open_api_specs/web_scraper/openapi.yamldiff --git a/camel/functions/open_api_specs/web_scraper/paths/__init__.py b/camel/toolkits/open_api_specs/web_scraper/paths/__init__.pysimilarity index 100%rename from camel/functions/open_api_specs/web_scraper/paths/__init__.pyrename to camel/toolkits/open_api_specs/web_scraper/paths/__init__.pydiff --git a/camel/functions/open_api_specs/web_scraper/paths/scraper.py b/camel/toolkits/open_api_specs/web_scraper/paths/scraper.pysimilarity index 100%rename from camel/functions/open_api_specs/web_scraper/paths/scraper.pyrename to camel/toolkits/open_api_specs/web_scraper/paths/scraper.pydiff --git a/camel/toolkits/open_api_toolkit.py b/camel/toolkits/open_api_toolkit.pynew file mode 100644index 000000000..ff0a48d7d--- /dev/null+++ b/camel/toolkits/open_api_toolkit.py@@ -0,0 +1,548 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========+import json+import os+from typing import Any, Callable, Dict, List, Optional, Tuple++import requests++from camel.toolkits import OpenAIFunction, openapi_security_config+from camel.toolkits.base import BaseToolkit+from camel.types import OpenAPIName+++class OpenAPIToolkit(BaseToolkit):+ r"""A class representing a toolkit for interacting with OpenAPI APIs.++ This class provides methods for interacting with APIs based on OpenAPI+ specifications. It dynamically generates functions for each API operation+ defined in the OpenAPI specification, allowing users to make HTTP requests+ to the API endpoints.+ """++ def parse_openapi_file(+ self, openapi_spec_path: str+ ) -> Optional[Dict[str, Any]]:+ r"""Load and parse an OpenAPI specification file.++ This function utilizes the `prance.ResolvingParser` to parse and+ resolve the given OpenAPI specification file, returning the parsed+ OpenAPI specification as a dictionary.++ Args:+ openapi_spec_path (str): The file path or URL to the OpenAPI+ specification.++ Returns:+ Optional[Dict[str, Any]]: The parsed OpenAPI specification+ as a dictionary. :obj:`None` if the package is not installed.+ """+ try:+ import prance+ except Exception:+ return None++ # Load the OpenAPI spec+ parser = prance.ResolvingParser(+ openapi_spec_path, backend="openapi-spec-validator", strict=False+ )+ openapi_spec = parser.specification+ version = openapi_spec.get('openapi', {})+ if not version:+ raise ValueError(+ "OpenAPI version not specified in the spec. "+ "Only OPENAPI 3.0.x and 3.1.x are supported."+ )+ if not (version.startswith('3.0') or version.startswith('3.1')):+ raise ValueError(+ f"Unsupported OpenAPI version: {version}. "+ f"Only OPENAPI 3.0.x and 3.1.x are supported."+ )+ return openapi_spec++ def openapi_spec_to_openai_schemas(+ self, api_name: str, openapi_spec: Dict[str, Any]+ ) -> List[Dict[str, Any]]:+ r"""Convert OpenAPI specification to OpenAI schema format.++ This function iterates over the paths and operations defined in an+ OpenAPI specification, filtering out deprecated operations. For each+ operation, it constructs a schema in a format suitable for OpenAI,+ including operation metadata such as function name, description,+ parameters, and request bodies. It raises a ValueError if an operation+ lacks a description or summary.++ Args:+ api_name (str): The name of the API, used to prefix generated+ function names.+ openapi_spec (Dict[str, Any]): The OpenAPI specification as a+ dictionary.++ Returns:+ List[Dict[str, Any]]: A list of dictionaries, each representing a+ function in the OpenAI schema format, including details about+ the function's name, description, and parameters.++ Raises:+ ValueError: If an operation in the OpenAPI specification+ does not have a description or summary.++ Note:+ This function assumes that the OpenAPI specification+ follows the 3.0+ format.++ Reference:+ https://swagger.io/specification/+ """+ result = []++ for path, path_item in openapi_spec.get('paths', {}).items():+ for method, op in path_item.items():+ if op.get('deprecated') is True:+ continue++ # Get the function name from the operationId+ # or construct it from the API method, and path+ function_name = f"{api_name}"+ operation_id = op.get('operationId')+ if operation_id:+ function_name += f"_{operation_id}"+ else:+ function_name += f"{method}{path.replace('/', '_')}"++ description = op.get('description') or op.get('summary')+ if not description:+ raise ValueError(+ f"{method} {path} Operation from {api_name} "+ f"does not have a description or summary."+ )+ description += " " if description[-1] != " " else ""+ description += f"This function is from {api_name} API. "++ # If the OpenAPI spec has a description,+ # add it to the operation description+ if 'description' in openapi_spec.get('info', {}):+ description += f"{openapi_spec['info']['description']}"++ # Get the parameters for the operation, if any+ params = op.get('parameters', [])+ properties: Dict[str, Any] = {}+ required = []++ for param in params:+ if not param.get('deprecated', False):+ param_name = param['name'] + '_in_' + param['in']+ properties[param_name] = {}++ if 'description' in param:+ properties[param_name]['description'] = param[+ 'description'+ ]++ if 'schema' in param:+ if (+ properties[param_name].get('description')+ and 'description' in param['schema']+ ):+ param['schema'].pop('description')+ properties[param_name].update(param['schema'])++ if param.get('required'):+ required.append(param_name)++ # If the property dictionary does not have a+ # description, use the parameter name as+ # the description+ if 'description' not in properties[param_name]:+ properties[param_name]['description'] = param[+ 'name'+ ]++ if 'type' not in properties[param_name]:+ properties[param_name]['type'] = 'Any'++ # Process requestBody if present+ if 'requestBody' in op:+ properties['requestBody'] = {}+ requestBody = op['requestBody']+ if requestBody.get('required') is True:+ required.append('requestBody')++ content = requestBody.get('content', {})+ json_content = content.get('application/json', {})+ json_schema = json_content.get('schema', {})+ if json_schema:+ properties['requestBody'] = json_schema+ if 'description' not in properties['requestBody']:+ properties['requestBody']['description'] = (+ "The request body, with parameters specifically "+ "described under the `properties` key"+ )++ function = {+ "type": "function",+ "function": {+ "name": function_name,+ "description": description,+ "parameters": {+ "type": "object",+ "properties": properties,+ "required": required,+ },+ },+ }+ result.append(function)++ return result # Return the result list++ def openapi_function_decorator(+ self,+ api_name: str,+ base_url: str,+ path: str,+ method: str,+ openapi_security: List[Dict[str, Any]],+ sec_schemas: Dict[str, Dict[str, Any]],+ operation: Dict[str, Any],+ ) -> Callable:+ r"""Decorate a function to make HTTP requests based on OpenAPI+ specification details.++ This decorator dynamically constructs and executes an API request based+ on the provided OpenAPI operation specifications, security+ requirements, and parameters. It supports operations secured with+ `apiKey` type security schemes and automatically injects the necessary+ API keys from environment variables. Parameters in `path`, `query`,+ `header`, and `cookie` are also supported.++ Args:+ api_name (str): The name of the API, used to retrieve API key names+ and URLs from the configuration.+ base_url (str): The base URL for the API.+ path (str): The path for the API endpoint,+ relative to the base URL.+ method (str): The HTTP method (e.g., 'get', 'post')+ for the request.+ openapi_security (List[Dict[str, Any]]): The global security+ definitions as specified in the OpenAPI specs.+ sec_schemas (Dict[str, Dict[str, Any]]): Detailed security schemes.+ operation (Dict[str, Any]): A dictionary containing the OpenAPI+ operation details, including parameters and request body+ definitions.++ Returns:+ Callable: A decorator that, when applied to a function, enables the+ function to make HTTP requests based on the provided OpenAPI+ operation details.++ Raises:+ TypeError: If the security requirements include unsupported types.+ ValueError: If required API keys are missing from environment+ variables or if the content type of the request body is+ unsupported.+ """++ def inner_decorator(openapi_function: Callable) -> Callable:+ def wrapper(**kwargs):+ request_url = f"{base_url.rstrip('/')}/{path.lstrip('/')}"+ headers = {}+ params = {}+ cookies = {}++ # Security definition of operation overrides any declared+ # top-level security.+ sec_requirements = operation.get('security', openapi_security)+ avail_sec_requirement = {}+ # Write to avaliable_security_requirement only if all the+ # security_type are "apiKey"+ for security_requirement in sec_requirements:+ have_unsupported_type = False+ for sec_scheme_name, _ in security_requirement.items():+ sec_type = sec_schemas.get(sec_scheme_name).get('type')+ if sec_type != "apiKey":+ have_unsupported_type = True+ break+ if have_unsupported_type is False:+ avail_sec_requirement = security_requirement+ break++ if sec_requirements and not avail_sec_requirement:+ raise TypeError(+ "Only security schemas of type `apiKey` are supported."+ )++ for sec_scheme_name, _ in avail_sec_requirement.items():+ try:+ API_KEY_NAME = openapi_security_config.get(+ api_name+ ).get(sec_scheme_name)+ api_key_value = os.environ[API_KEY_NAME]+ except Exception:+ api_key_url = openapi_security_config.get(+ api_name+ ).get('get_api_key_url')+ raise ValueError(+ f"`{API_KEY_NAME}` not found in environment "+ f"variables. "+ f"Get `{API_KEY_NAME}` here: {api_key_url}"+ )+ request_key_name = sec_schemas.get(sec_scheme_name).get(+ 'name'+ )+ request_key_in = sec_schemas.get(sec_scheme_name).get('in')+ if request_key_in == 'query':+ params[request_key_name] = api_key_value+ elif request_key_in == 'header':+ headers[request_key_name] = api_key_value+ elif request_key_in == 'coolie':+ cookies[request_key_name] = api_key_value++ # Assign parameters to the correct position+ for param in operation.get('parameters', []):+ input_param_name = param['name'] + '_in_' + param['in']+ # Irrelevant arguments does not affect function operation+ if input_param_name in kwargs:+ if param['in'] == 'path':+ request_url = request_url.replace(+ f"{{{param['name']}}}",+ str(kwargs[input_param_name]),+ )+ elif param['in'] == 'query':+ params[param['name']] = kwargs[input_param_name]+ elif param['in'] == 'header':+ headers[param['name']] = kwargs[input_param_name]+ elif param['in'] == 'cookie':+ cookies[param['name']] = kwargs[input_param_name]++ if 'requestBody' in operation:+ request_body = kwargs.get('requestBody', {})+ content_type_list = list(+ operation.get('requestBody', {})+ .get('content', {})+ .keys()+ )+ if content_type_list:+ content_type = content_type_list[0]+ headers.update({"Content-Type": content_type})++ # send the request body based on the Content-Type+ if content_type == "application/json":+ response = requests.request(+ method.upper(),+ request_url,+ params=params,+ headers=headers,+ cookies=cookies,+ json=request_body,+ )+ else:+ raise ValueError(+ f"Unsupported content type: {content_type}"+ )+ else:+ # If there is no requestBody, no request body is sent+ response = requests.request(+ method.upper(),+ request_url,+ params=params,+ headers=headers,+ cookies=cookies,+ )++ try:+ return response.json()+ except json.JSONDecodeError:+ raise ValueError(+ "Response could not be decoded as JSON. "+ "Please check the input parameters."+ )++ return wrapper++ return inner_decorator++ def generate_openapi_funcs(+ self, api_name: str, openapi_spec: Dict[str, Any]+ ) -> List[Callable]:+ r"""Generates a list of Python functions based on+ OpenAPI specification.++ This function dynamically creates a list of callable functions that+ represent the API operations defined in an OpenAPI specification+ document. Each function is designed to perform an HTTP request+ corresponding to an API operation (e.g., GET, POST) as defined in+ the specification. The functions are decorated with+ `openapi_function_decorator`, which configures them to construct and+ send the HTTP requests with appropriate parameters, headers, and body+ content.++ Args:+ api_name (str): The name of the API, used to prefix generated+ function names.+ openapi_spec (Dict[str, Any]): The OpenAPI specification as a+ dictionary.++ Returns:+ List[Callable]: A list containing the generated functions. Each+ function, when called, will make an HTTP request according to+ its corresponding API operation defined in the OpenAPI+ specification.++ Raises:+ ValueError: If the OpenAPI specification does not contain server+ information, which is necessary for determining the base URL+ for the API requests.+ """+ # Check server information+ servers = openapi_spec.get('servers', [])+ if not servers:+ raise ValueError("No server information found in OpenAPI spec.")+ base_url = servers[0].get('url') # Use the first server URL++ # Security requirement objects for all methods+ openapi_security = openapi_spec.get('security', {})+ # Security schemas which can be reused by different methods+ sec_schemas = openapi_spec.get('components', {}).get(+ 'securitySchemes', {}+ )+ functions = []++ # Traverse paths and methods+ for path, methods in openapi_spec.get('paths', {}).items():+ for method, operation in methods.items():+ # Get the function name from the operationId+ # or construct it from the API method, and path+ operation_id = operation.get('operationId')+ if operation_id:+ function_name = f"{api_name}_{operation_id}"+ else:+ sanitized_path = path.replace('/', '_').strip('_')+ function_name = f"{api_name}_{method}_{sanitized_path}"++ @self.openapi_function_decorator(+ api_name,+ base_url,+ path,+ method,+ openapi_security,+ sec_schemas,+ operation,+ )+ def openapi_function(**kwargs):+ pass++ openapi_function.__name__ = function_name++ functions.append(openapi_function)++ return functions++ def apinames_filepaths_to_funs_schemas(+ self,+ apinames_filepaths: List[Tuple[str, str]],+ ) -> Tuple[List[Callable], List[Dict[str, Any]]]:+ r"""Combines functions and schemas from multiple OpenAPI+ specifications, using API names as keys.++ This function iterates over tuples of API names and OpenAPI spec file+ paths, parsing each spec to generate callable functions and schema+ dictionaries, all organized by API name.++ Args:+ apinames_filepaths (List[Tuple[str, str]]): A list of tuples, where+ each tuple consists of:+ - The API name (str) as the first element.+ - The file path (str) to the API's OpenAPI specification file as+ the second element.++ Returns:+ Tuple[List[Callable], List[Dict[str, Any]]]:: one of callable+ functions for API operations, and another of dictionaries+ representing the schemas from the specifications.+ """+ combined_func_lst = []+ combined_schemas_list = []+ for api_name, file_path in apinames_filepaths:+ # Parse the OpenAPI specification for each API+ current_dir = os.path.dirname(__file__)+ file_path = os.path.join(+ current_dir, 'open_api_specs', f'{api_name}', 'openapi.yaml'+ )++ openapi_spec = self.parse_openapi_file(file_path)+ if openapi_spec is None:+ return [], []++ # Generate and merge function schemas+ openapi_functions_schemas = self.openapi_spec_to_openai_schemas(+ api_name, openapi_spec+ )+ combined_schemas_list.extend(openapi_functions_schemas)++ # Generate and merge function lists+ openapi_functions_list = self.generate_openapi_funcs(+ api_name, openapi_spec+ )+ combined_func_lst.extend(openapi_functions_list)++ return combined_func_lst, combined_schemas_list++ def generate_apinames_filepaths(self) -> List[Tuple[str, str]]:+ """Generates a list of tuples containing API names and their+ corresponding file paths.++ This function iterates over the OpenAPIName enum, constructs the file+ path for each API's OpenAPI specification file, and appends a tuple of+ the API name and its file path to the list. The file paths are relative+ to the 'open_api_specs' directory located in the same directory as this+ script.++ Returns:+ List[Tuple[str, str]]: A list of tuples where each tuple contains+ two elements. The first element of each tuple is a string+ representing the name of an API, and the second element is a+ string that specifies the file path to that API's OpenAPI+ specification file.+ """+ apinames_filepaths = []+ current_dir = os.path.dirname(__file__)+ for api_name in OpenAPIName:+ file_path = os.path.join(+ current_dir,+ 'open_api_specs',+ f'{api_name.value}',+ 'openapi.yaml',+ )+ apinames_filepaths.append((api_name.value, file_path))+ return apinames_filepaths++ def get_tools(self) -> List[OpenAIFunction]:+ r"""Returns a list of OpenAIFunction objects representing the+ functions in the toolkit.++ Returns:+ List[OpenAIFunction]: A list of OpenAIFunction objects+ representing the functions in the toolkit.+ """+ apinames_filepaths = self.generate_apinames_filepaths()+ all_funcs_lst, all_schemas_lst = (+ self.apinames_filepaths_to_funs_schemas(apinames_filepaths)+ )+ return [+ OpenAIFunction(a_func, a_schema)+ for a_func, a_schema in zip(all_funcs_lst, all_schemas_lst)+ ]+++OPENAPI_FUNCS: List[OpenAIFunction] = OpenAPIToolkit().get_tools()diff --git a/camel/functions/openai_function.py b/camel/toolkits/openai_function.pysimilarity index 100%rename from camel/functions/openai_function.pyrename to camel/toolkits/openai_function.pydiff --git a/camel/toolkits/retrieval_toolkit.py b/camel/toolkits/retrieval_toolkit.pynew file mode 100644index 000000000..034e1769a--- /dev/null+++ b/camel/toolkits/retrieval_toolkit.py@@ -0,0 +1,76 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========+from typing import List, Union++from camel.retrievers import AutoRetriever+from camel.toolkits import OpenAIFunction+from camel.toolkits.base import BaseToolkit+from camel.types import StorageType+++class RetrievalToolkit(BaseToolkit):+ r"""A class representing a toolkit for information retrieval.++ This class provides methods for retrieving information from a local vector+ storage system based on a specified query.+ """++ def information_retrieval(+ self, query: str, content_input_paths: Union[str, List[str]]+ ) -> str:+ r"""Retrieves information from a local vector storage based on the+ specified query. This function connects to a local vector storage+ system and retrieves relevant information by processing the input+ query. It is essential to use this function when the answer to a+ question requires external knowledge sources.++ Args:+ query (str): The question or query for which an answer is required.+ content_input_paths (Union[str, List[str]]): Paths to local+ files or remote URLs.++ Returns:+ str: The information retrieved in response to the query, aggregated+ and formatted as a string.++ Example:+ # Retrieve information about CAMEL AI.+ information_retrieval(query = "what is CAMEL AI?",+ content_input_paths="https://www.camel-ai.org/")+ """+ auto_retriever = AutoRetriever(+ vector_storage_local_path="camel/temp_storage",+ storage_type=StorageType.QDRANT,+ )++ retrieved_info = auto_retriever.run_vector_retriever(+ query=query, content_input_paths=content_input_paths, top_k=3+ )+ return retrieved_info++ def get_tools(self) -> List[OpenAIFunction]:+ r"""Returns a list of OpenAIFunction objects representing the+ functions in the toolkit.++ Returns:+ List[OpenAIFunction]: A list of OpenAIFunction objects+ representing the functions in the toolkit.+ """+ return [+ OpenAIFunction(self.information_retrieval),+ ]+++# add the function to OpenAIFunction list+RETRIEVAL_FUNCS: List[OpenAIFunction] = RetrievalToolkit().get_tools()diff --git a/camel/toolkits/search_toolkit.py b/camel/toolkits/search_toolkit.pynew file mode 100644index 000000000..f794d6293--- /dev/null+++ b/camel/toolkits/search_toolkit.py@@ -0,0 +1,326 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========+import os+from typing import Any, Dict, List++from camel.toolkits.base import BaseToolkit+from camel.toolkits.openai_function import OpenAIFunction+++class SearchToolkit(BaseToolkit):+ r"""A class representing a toolkit for web search.++ This class provides methods for searching information on the web using+ search engines like Google, DuckDuckGo, Wikipedia and Wolfram Alpha.+ """++ def search_wiki(self, entity: str) -> str:+ r"""Search the entity in WikiPedia and return the summary of the+ required page, containing factual information about+ the given entity.++ Args:+ entity (str): The entity to be searched.++ Returns:+ str: The search result. If the page corresponding to the entity+ exists, return the summary of this entity in a string.+ """+ try:+ import wikipedia+ except ImportError:+ raise ImportError(+ "Please install `wikipedia` first. You can install it "+ "by running `pip install wikipedia`."+ )++ result: str++ try:+ result = wikipedia.summary(entity, sentences=5, auto_suggest=False)+ except wikipedia.exceptions.DisambiguationError as e:+ result = wikipedia.summary(+ e.options[0], sentences=5, auto_suggest=False+ )+ except wikipedia.exceptions.PageError:+ result = (+ "There is no page in Wikipedia corresponding to entity "+ f"{entity}, please specify another word to describe the"+ " entity to be searched."+ )+ except wikipedia.exceptions.WikipediaException as e:+ result = f"An exception occurred during the search: {e}"++ return result++ def search_duckduckgo(+ self, query: str, source: str = "text", max_results: int = 10+ ) -> List[Dict[str, Any]]:+ r"""Use DuckDuckGo search engine to search information for+ the given query.++ This function queries the DuckDuckGo API for related topics to+ the given search term. The results are formatted into a list of+ dictionaries, each representing a search result.++ Args:+ query (str): The query to be searched.+ source (str): The type of information to query (e.g., "text",+ "images", "videos"). Defaults to "text".+ max_results (int): Max number of results, defaults to `10`.++ Returns:+ List[Dict[str, Any]]: A list of dictionaries where each dictionary+ represents a search result.+ """+ from duckduckgo_search import DDGS+ from requests.exceptions import RequestException++ ddgs = DDGS()+ responses: List[Dict[str, Any]] = []++ if source == "text":+ try:+ results = ddgs.text(keywords=query, max_results=max_results)+ except RequestException as e:+ # Handle specific exceptions or general request exceptions+ responses.append({"error": f"duckduckgo search failed.{e}"})++ # Iterate over results found+ for i, result in enumerate(results, start=1):+ # Creating a response object with a similar structure+ response = {+ "result_id": i,+ "title": result["title"],+ "description": result["body"],+ "url": result["href"],+ }+ responses.append(response)++ elif source == "images":+ try:+ results = ddgs.images(keywords=query, max_results=max_results)+ except RequestException as e:+ # Handle specific exceptions or general request exceptions+ responses.append({"error": f"duckduckgo search failed.{e}"})++ # Iterate over results found+ for i, result in enumerate(results, start=1):+ # Creating a response object with a similar structure+ response = {+ "result_id": i,+ "title": result["title"],+ "image": result["image"],+ "url": result["url"],+ "source": result["source"],+ }+ responses.append(response)++ elif source == "videos":+ try:+ results = ddgs.videos(keywords=query, max_results=max_results)+ except RequestException as e:+ # Handle specific exceptions or general request exceptions+ responses.append({"error": f"duckduckgo search failed.{e}"})++ # Iterate over results found+ for i, result in enumerate(results, start=1):+ # Creating a response object with a similar structure+ response = {+ "result_id": i,+ "title": result["title"],+ "description": result["description"],+ "embed_url": result["embed_url"],+ "publisher": result["publisher"],+ "duration": result["duration"],+ "published": result["published"],+ }+ responses.append(response)++ # If no answer found, return an empty list+ return responses++ def search_google(+ self, query: str, num_result_pages: int = 10+ ) -> List[Dict[str, Any]]:+ r"""Use Google search engine to search information for the given query.++ Args:+ query (str): The query to be searched.+ num_result_pages (int): The number of result pages to retrieve.++ Returns:+ List[Dict[str, Any]]: A list of dictionaries where each dictionary+ represents a website.+ Each dictionary contains the following keys:+ - 'result_id': A number in order.+ - 'title': The title of the website.+ - 'description': A brief description of the website.+ - 'long_description': More detail of the website.+ - 'url': The URL of the website.++ Example:+ {+ 'result_id': 1,+ 'title': 'OpenAI',+ 'description': 'An organization focused on ensuring that+ artificial general intelligence benefits all of humanity.',+ 'long_description': 'OpenAI is a non-profit artificial+ intelligence research company. Our goal is to advance+ digital intelligence in the way that is most likely to+ benefit humanity as a whole',+ 'url': 'https://www.openai.com'+ }+ title, description, url of a website.+ """+ import requests++ # https://developers.google.com/custom-search/v1/overview+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")+ # https://cse.google.com/cse/all+ SEARCH_ENGINE_ID = os.getenv("SEARCH_ENGINE_ID")++ # Using the first page+ start_page_idx = 1+ # Different language may get different result+ search_language = "en"+ # How many pages to return+ num_result_pages = 10+ # Constructing the URL+ # Doc: https://developers.google.com/custom-search/v1/using_rest+ url = (+ f"https://www.googleapis.com/customsearch/v1?"+ f"key={GOOGLE_API_KEY}&cx={SEARCH_ENGINE_ID}&q={query}&start="+ f"{start_page_idx}&lr={search_language}&num={num_result_pages}"+ )++ responses = []+ # Fetch the results given the URL+ try:+ # Make the get+ result = requests.get(url)+ data = result.json()++ # Get the result items+ if "items" in data:+ search_items = data.get("items")++ # Iterate over 10 results found+ for i, search_item in enumerate(search_items, start=1):+ if (+ "og:description"+ in search_item["pagemap"]["metatags"][0]+ ):+ long_description = search_item["pagemap"]["metatags"][+ 0+ ]["og:description"]+ else:+ long_description = "N/A"+ # Get the page title+ title = search_item.get("title")+ # Page snippet+ snippet = search_item.get("snippet")++ # Extract the page url+ link = search_item.get("link")+ response = {+ "result_id": i,+ "title": title,+ "description": snippet,+ "long_description": long_description,+ "url": link,+ }+ responses.append(response)+ else:+ responses.append({"error": "google search failed."})++ except requests.RequestException:+ # Handle specific exceptions or general request exceptions+ responses.append({"error": "google search failed."})+ # If no answer found, return an empty list+ return responses++ def query_wolfram_alpha(self, query: str, is_detailed: bool) -> str:+ r"""Queries Wolfram|Alpha and returns the result. Wolfram|Alpha is an+ answer engine developed by Wolfram Research. It is offered as an online+ service that answers factual queries by computing answers from+ externally sourced data.++ Args:+ query (str): The query to send to Wolfram Alpha.+ is_detailed (bool): Whether to include additional details in the+ result.++ Returns:+ str: The result from Wolfram Alpha, formatted as a string.+ """+ try:+ import wolframalpha+ except ImportError:+ raise ImportError(+ "Please install `wolframalpha` first. You can install it by"+ " running `pip install wolframalpha`."+ )++ WOLFRAMALPHA_APP_ID = os.environ.get('WOLFRAMALPHA_APP_ID')+ if not WOLFRAMALPHA_APP_ID:+ raise ValueError(+ "`WOLFRAMALPHA_APP_ID` not found in environment "+ "variables. Get `WOLFRAMALPHA_APP_ID` here: "+ "`https://products.wolframalpha.com/api/`."+ )++ try:+ client = wolframalpha.Client(WOLFRAMALPHA_APP_ID)+ res = client.query(query)+ assumption = next(res.pods).text or "No assumption made."+ answer = next(res.results).text or "No answer found."+ except Exception as e:+ if isinstance(e, StopIteration):+ return "Wolfram Alpha wasn't able to answer it"+ else:+ error_message = (+ f"Wolfram Alpha wasn't able to answer it" f"{e!s}."+ )+ return error_message++ result = f"Assumption:\n{assumption}\n\nAnswer:\n{answer}"++ # Add additional details in the result+ if is_detailed:+ result += '\n'+ for pod in res.pods:+ result += '\n' + pod['@title'] + ':\n'+ for sub in pod.subpods:+ result += (sub.plaintext or "None") + '\n'++ return result.rstrip() # Remove trailing whitespace++ def get_tools(self) -> List[OpenAIFunction]:+ r"""Returns a list of OpenAIFunction objects representing the+ functions in the toolkit.++ Returns:+ List[OpenAIFunction]: A list of OpenAIFunction objects+ representing the functions in the toolkit.+ """+ return [+ OpenAIFunction(self.search_wiki),+ OpenAIFunction(self.search_google),+ OpenAIFunction(self.search_duckduckgo),+ OpenAIFunction(self.query_wolfram_alpha),+ ]+++SEARCH_FUNCS: List[OpenAIFunction] = SearchToolkit().get_tools()diff --git a/camel/toolkits/slack_toolkit.py b/camel/toolkits/slack_toolkit.pynew file mode 100644index 000000000..2962ef576--- /dev/null+++ b/camel/toolkits/slack_toolkit.py@@ -0,0 +1,308 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========++from __future__ import annotations++import json+import logging+import os+from typing import TYPE_CHECKING, List, Optional++from camel.toolkits.base import BaseToolkit++if TYPE_CHECKING:+ from ssl import SSLContext++ from slack_sdk import WebClient++from camel.toolkits import OpenAIFunction++logger = logging.getLogger(__name__)+++class SlackToolkit(BaseToolkit):+ r"""A class representing a toolkit for Slack operations.++ This class provides methods for Slack operations such as creating a new+ channel, joining an existing channel, leaving a channel.+ """++ def _login_slack(+ self,+ slack_token: Optional[str] = None,+ ssl: Optional[SSLContext] = None,+ ) -> WebClient:+ r"""Authenticate using the Slack API.++ Args:+ slack_token (str, optional): The Slack API token.+ If not provided, it attempts to retrieve the token from+ the environment variable SLACK_BOT_TOKEN or SLACK_USER_TOKEN.+ ssl (SSLContext, optional): SSL context for secure connections.+ Defaults to `None`.++ Returns:+ WebClient: A WebClient object for interacting with Slack API.++ Raises:+ ImportError: If slack_sdk package is not installed.+ KeyError: If SLACK_BOT_TOKEN or SLACK_USER_TOKEN+ environment variables are not set.+ """+ try:+ from slack_sdk import WebClient+ except ImportError as e:+ raise ImportError(+ "Cannot import slack_sdk. Please install the package with \+ `pip install slack_sdk`."+ ) from e+ if not slack_token:+ slack_token = os.environ.get("SLACK_BOT_TOKEN") or os.environ.get(+ "SLACK_USER_TOKEN"+ )+ if not slack_token:+ raise KeyError(+ "SLACK_BOT_TOKEN or SLACK_USER_TOKEN environment "+ "variable not set."+ )++ client = WebClient(token=slack_token, ssl=ssl)+ logger.info("Slack login successful.")+ return client++ def create_slack_channel(+ self, name: str, is_private: Optional[bool] = True+ ) -> str:+ r"""Creates a new slack channel, either public or private.++ Args:+ name (str): Name of the public or private channel to create.+ is_private (bool, optional): Whether to create a private channel+ instead of a public one. Defaults to `True`.++ Returns:+ str: JSON string containing information about Slack+ channel created.++ Raises:+ SlackApiError: If there is an error during get slack channel+ information.+ """+ from slack_sdk.errors import SlackApiError++ try:+ slack_client = self._login_slack()+ response = slack_client.conversations_create(+ name=name, is_private=is_private+ )+ channel_id = response["channel"]["id"]+ response = slack_client.conversations_archive(channel=channel_id)+ return str(response)+ except SlackApiError as e:+ return f"Error creating conversation: {e.response['error']}"++ def join_slack_channel(self, channel_id: str) -> str:+ r"""Joins an existing Slack channel.++ Args:+ channel_id (str): The ID of the Slack channel to join.++ Returns:+ str: A confirmation message indicating whether join successfully+ or an error message.++ Raises:+ SlackApiError: If there is an error during get slack channel+ information.+ """+ from slack_sdk.errors import SlackApiError++ try:+ slack_client = self._login_slack()+ response = slack_client.conversations_join(channel=channel_id)+ return str(response)+ except SlackApiError as e:+ return f"Error creating conversation: {e.response['error']}"++ def leave_slack_channel(self, channel_id: str) -> str:+ r"""Leaves an existing Slack channel.++ Args:+ channel_id (str): The ID of the Slack channel to leave.++ Returns:+ str: A confirmation message indicating whether leave successfully+ or an error message.++ Raises:+ SlackApiError: If there is an error during get slack channel+ information.+ """+ from slack_sdk.errors import SlackApiError++ try:+ slack_client = self._login_slack()+ response = slack_client.conversations_leave(channel=channel_id)+ return str(response)+ except SlackApiError as e:+ return f"Error creating conversation: {e.response['error']}"++ def get_slack_channel_information(self) -> str:+ r"""Retrieve Slack channels and return relevant information in JSON+ format.++ Returns:+ str: JSON string containing information about Slack channels.++ Raises:+ SlackApiError: If there is an error during get slack channel+ information.+ """+ from slack_sdk.errors import SlackApiError++ try:+ slack_client = self._login_slack()+ response = slack_client.conversations_list()+ conversations = response["channels"]+ # Filtering conversations and extracting required information+ filtered_result = [+ {+ key: conversation[key]+ for key in ("id", "name", "created", "num_members")+ }+ for conversation in conversations+ if all(+ key in conversation+ for key in ("id", "name", "created", "num_members")+ )+ ]+ return json.dumps(filtered_result, ensure_ascii=False)+ except SlackApiError as e:+ return f"Error creating conversation: {e.response['error']}"++ def get_slack_channel_message(self, channel_id: str) -> str:+ r"""Retrieve messages from a Slack channel.++ Args:+ channel_id (str): The ID of the Slack channel to retrieve messages+ from.++ Returns:+ str: JSON string containing filtered message data.++ Raises:+ SlackApiError: If there is an error during get+ slack channel message.+ """+ from slack_sdk.errors import SlackApiError++ try:+ slack_client = self._login_slack()+ result = slack_client.conversations_history(channel=channel_id)+ messages = result["messages"]+ filtered_messages = [+ {key: message[key] for key in ("user", "text", "ts")}+ for message in messages+ if all(key in message for key in ("user", "text", "ts"))+ ]+ return json.dumps(filtered_messages, ensure_ascii=False)+ except SlackApiError as e:+ return f"Error retrieving messages: {e.response['error']}"++ def send_slack_message(+ self,+ message: str,+ channel_id: str,+ user: Optional[str] = None,+ ) -> str:+ r"""Send a message to a Slack channel.++ Args:+ message (str): The message to send.+ channel_id (str): The ID of the Slack channel to send message.+ user (Optional[str]): The user ID of the recipient.+ Defaults to `None`.++ Returns:+ str: A confirmation message indicating whether the message was sent+ successfully or an error message.++ Raises:+ SlackApiError: If an error occurs while sending the message.+ """+ from slack_sdk.errors import SlackApiError++ try:+ slack_client = self._login_slack()+ if user:+ response = slack_client.chat_postEphemeral(+ channel=channel_id, text=message, user=user+ )+ else:+ response = slack_client.chat_postMessage(+ channel=channel_id, text=message+ )+ return str(response)+ except SlackApiError as e:+ return f"Error creating conversation: {e.response['error']}"++ def delete_slack_message(+ self,+ time_stamp: str,+ channel_id: str,+ ) -> str:+ r"""Delete a message to a Slack channel.++ Args:+ time_stamp (str): Timestamp of the message to be deleted.+ channel_id (str): The ID of the Slack channel to delete message.++ Returns:+ str: A confirmation message indicating whether the message+ was delete successfully or an error message.++ Raises:+ SlackApiError: If an error occurs while sending the message.+ """+ from slack_sdk.errors import SlackApiError++ try:+ slack_client = self._login_slack()+ response = slack_client.chat_delete(+ channel=channel_id, ts=time_stamp+ )+ return str(response)+ except SlackApiError as e:+ return f"Error creating conversation: {e.response['error']}"++ def get_tools(self) -> List[OpenAIFunction]:+ r"""Returns a list of OpenAIFunction objects representing the+ functions in the toolkit.++ Returns:+ List[OpenAIFunction]: A list of OpenAIFunction objects+ representing the functions in the toolkit.+ """+ return [+ OpenAIFunction(self.create_slack_channel),+ OpenAIFunction(self.join_slack_channel),+ OpenAIFunction(self.leave_slack_channel),+ OpenAIFunction(self.get_slack_channel_information),+ OpenAIFunction(self.get_slack_channel_message),+ OpenAIFunction(self.send_slack_message),+ OpenAIFunction(self.delete_slack_message),+ ]+++SLACK_FUNCS: List[OpenAIFunction] = SlackToolkit().get_tools()diff --git a/camel/toolkits/twitter_toolkit.py b/camel/toolkits/twitter_toolkit.pynew file mode 100644index 000000000..dc54ae1ba--- /dev/null+++ b/camel/toolkits/twitter_toolkit.py@@ -0,0 +1,522 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========+import datetime+import os+from http import HTTPStatus+from http.client import responses+from typing import List, Optional, Tuple, Union++import requests++from camel.toolkits import OpenAIFunction+from camel.toolkits.base import BaseToolkit++TWEET_TEXT_LIMIT = 280+++class TwitterToolkit(BaseToolkit):+ r"""A class representing a toolkit for Twitter operations.++ This class provides methods for creating a tweet, deleting a tweet, and+ getting the authenticated user's profile information.+ """++ def create_tweet(+ self,+ *,+ text: str,+ poll_options: Optional[List[str]] = None,+ poll_duration_minutes: Optional[int] = None,+ quote_tweet_id: Optional[Union[int, str]] = None,+ ) -> str:+ r"""Creates a new tweet, optionally including a poll or a quote tweet,+ or simply a text-only tweet.++ This function sends a POST request to the Twitter API to create a new+ tweet. The tweet can be a text-only tweet, or optionally include a poll+ or be a quote tweet. A confirmation prompt is presented to the user+ before the tweet is created.++ Args:+ text (str): The text of the tweet. The Twitter character limit for+ a single tweet is 280 characters.+ poll_options (Optional[List[str]]): A list of poll options for a+ tweet with a poll.+ poll_duration_minutes (Optional[int]): Duration of the poll in+ minutes for a tweet with a poll. This is only required+ if the request includes poll_options.+ quote_tweet_id (Optional[Union[int, str]]): Link to the tweet being+ quoted.++ Note:+ You can only provide either the `quote_tweet_id` parameter or+ the pair of `poll_duration_minutes` and `poll_options` parameters,+ not both.++ Returns:+ str: A message indicating the success of the tweet creation,+ including the tweet ID and text. If the request to the+ Twitter API is not successful, the return is an error message.++ Reference:+ https://developer.twitter.com/en/docs/twitter-api/tweets/+ manage-tweets/api-reference/post-tweets+ https://github.com/xdevplatform/Twitter-API-v2-sample-code/blob/+ main/Manage-Tweets/create_tweet.py+ """+ # validate text+ if text is None:+ return "Text cannot be None"+ elif len(text) > TWEET_TEXT_LIMIT:+ return "Text must not exceed 280 characters."++ # Validate poll options and duration+ if (poll_options is None) != (poll_duration_minutes is None):+ return (+ "Error: Both `poll_options` and `poll_duration_minutes` must "+ "be provided together or not at all."+ )++ # Validate exclusive parameters+ if quote_tweet_id is not None and (+ poll_options or poll_duration_minutes+ ):+ return (+ "Error: Cannot provide both `quote_tweet_id` and "+ "(`poll_options` or `poll_duration_minutes`)."+ )++ # Print the parameters that are not None+ params = {+ "text": text,+ "poll_options": poll_options,+ "poll_duration_minutes": poll_duration_minutes,+ "quote_tweet_id": quote_tweet_id,+ }+ print("You are going to create a tweet with following parameters:")+ for key, value in params.items():+ if value is not None:+ print(f"{key}: {value}")++ # Add a confirmation prompt at the beginning of the function+ confirm = input(+ "Are you sure you want to create this tweet? (yes/no): "+ )+ if confirm.lower() != "yes":+ return "Execution cancelled by the user."++ oauth = self._get_oauth_session()+ json_data = {}++ if poll_options is not None and poll_duration_minutes is not None:+ json_data["poll"] = {+ "options": poll_options,+ "duration_minutes": poll_duration_minutes,+ }++ if quote_tweet_id is not None:+ json_data["quote_tweet_id"] = str(quote_tweet_id) # type: ignore[assignment]++ json_data["text"] = text # type: ignore[assignment]++ # Making the request+ response = oauth.post(+ "https://api.twitter.com/2/tweets",+ json=json_data,+ )++ if response.status_code != HTTPStatus.CREATED:+ error_type = self._handle_http_error(response)+ # use string concatenation to satisfy flake8+ return (+ "Request returned a(n) "+ + str(error_type)+ + ": "+ + str(response.status_code)+ + " "+ + response.text+ )++ # Saving the response as JSON+ json_response = response.json()++ tweet_id = json_response["data"]["id"]+ tweet_text = json_response["data"]["text"]++ response_str = (+ f"Create tweet successful. "+ f"The tweet ID is: {tweet_id}. "+ f"The tweet text is: '{tweet_text}'."+ )++ return response_str++ def delete_tweet(self, tweet_id: str) -> str:+ r"""Deletes a tweet with the specified ID for an authorized user.++ This function sends a DELETE request to the Twitter API to delete+ a tweet with the specified ID. Before sending the request, it+ prompts the user to confirm the deletion.++ Args:+ tweet_id (str): The ID of the tweet to delete.++ Returns:+ str: A message indicating the result of the deletion. If the+ deletion was successful, the message includes the ID of the+ deleted tweet. If the deletion was not successful, the message+ includes an error message.++ Reference:+ https://developer.twitter.com/en/docs/twitter-api/tweets/+ manage-tweets/api-reference/delete-tweets-id+ """+ # Print the parameters that are not None+ if tweet_id is not None:+ print(+ f"You are going to delete a tweet with the following "+ f"ID: {tweet_id}"+ )++ # Add a confirmation prompt at the beginning of the function+ confirm = input(+ "Are you sure you want to delete this tweet? (yes/no): "+ )+ if confirm.lower() != "yes":+ return "Execution cancelled by the user."++ oauth = self._get_oauth_session()++ # Making the request+ response = oauth.delete(+ f"https://api.twitter.com/2/tweets/{tweet_id}",+ )++ if response.status_code != HTTPStatus.OK:+ error_type = self._handle_http_error(response)+ # use string concatenation to satisfy flake8+ return (+ "Request returned a(n) "+ + str(error_type)+ + ": "+ + str(response.status_code)+ + " "+ + response.text+ )++ # Saving the response as JSON+ json_response = response.json()+ # `deleted_status` may be True or False.+ # Defaults to False if not found.+ deleted_status = json_response.get("data", {}).get("deleted", False)+ response_str = (+ f"Delete tweet successful: {deleted_status}. "+ f"The tweet ID is: {tweet_id}. "+ )+ return response_str++ def get_my_user_profile(self) -> str:+ r"""Retrieves and formats the authenticated user's Twitter+ profile info.++ This function sends a GET request to the Twitter API to retrieve the+ authenticated user's profile information, including their pinned tweet.+ It then formats this information into a readable report.++ Returns:+ str: A formatted report of the authenticated user's Twitter profile+ information. This includes their ID, name, username,+ description, location, most recent tweet ID, profile image URL,+ account creation date, protection status, verification type,+ public metrics, and pinned tweet information. If the request to+ the Twitter API is not successful,+ the return is an error message.++ Reference:+ https://developer.twitter.com/en/docs/twitter-api/users/lookup/+ api-reference/get-users-me+ """+ oauth = self._get_oauth_session()++ tweet_fields = ["created_at", "text"]+ user_fields = [+ "created_at",+ "description",+ "id",+ "location",+ "most_recent_tweet_id",+ "name",+ "pinned_tweet_id",+ "profile_image_url",+ "protected",+ "public_metrics",+ "url",+ "username",+ "verified_type",+ ]+ params = {+ "expansions": "pinned_tweet_id",+ "tweet.fields": ",".join(tweet_fields),+ "user.fields": ",".join(user_fields),+ }++ response = oauth.get(+ "https://api.twitter.com/2/users/me", params=params+ )++ if response.status_code != HTTPStatus.OK:+ error_type = self._handle_http_error(response)+ error_message = "Request returned a(n) {}: {} {}".format(+ error_type, response.status_code, response.text+ )+ return error_message++ json_response = response.json()++ user_info = json_response.get('data', {})+ tweets = json_response.get('includes', {}).get('tweets', [{}])[0]++ user_report = ""+ user_report += f"ID: {user_info['id']}. "+ user_report += f"Name: {user_info['name']}. "+ user_report += f"Username: {user_info['username']}. "++ # Define the part of keys that need to be repeatedly processed+ user_info_keys = [+ 'description',+ 'location',+ 'most_recent_tweet_id',+ 'profile_image_url',+ ]+ for key in user_info_keys:+ value = user_info.get(key)+ if user_info.get(key):+ user_report += (+ f"{key.replace('_', ' ').capitalize()}: {value}. "+ )++ if 'created_at' in user_info:+ created_at = datetime.datetime.strptime(+ user_info['created_at'], "%Y-%m-%dT%H:%M:%S.%fZ"+ )+ date_str = created_at.strftime('%B %d, %Y at %H:%M:%S')+ user_report += f"Account created at: {date_str}. "++ protection_status = "private" if user_info['protected'] else "public"+ user_report += (+ f"Protected: This user's Tweets are {protection_status}. "+ )++ verification_messages = {+ 'blue': (+ "The user has a blue verification, typically reserved for "+ "public figures, celebrities, or global brands. "+ ),+ 'business': (+ "The user has a business verification, typically "+ "reserved for businesses and corporations. "+ ),+ 'government': (+ "The user has a government verification, typically "+ "reserved for government officials or entities. "+ ),+ 'none': "The user is not verified. ",+ }+ verification_type = user_info.get('verified_type', 'none')+ user_report += (+ f"Verified type: {verification_messages.get(verification_type)}"+ )++ if 'public_metrics' in user_info:+ user_report += "Public metrics: "+ metrics = user_info['public_metrics']+ user_report += (+ f"The user has {metrics.get('followers_count', 0)} followers, "+ f"is following {metrics.get('following_count', 0)} users, "+ f"has made {metrics.get('tweet_count', 0)} tweets, "+ f"is listed in {metrics.get('listed_count', 0)} lists, "+ f"and has received {metrics.get('like_count', 0)} likes. "+ )++ if 'pinned_tweet_id' in user_info:+ user_report += f"Pinned tweet ID: {user_info['pinned_tweet_id']}. "++ if 'created_at' in tweets and 'text' in tweets:+ user_report += "\nPinned tweet information: "+ tweet_created_at = datetime.datetime.strptime(+ tweets['created_at'], "%Y-%m-%dT%H:%M:%S.%fZ"+ )+ user_report += (+ f"Pinned tweet created at "+ f"{tweet_created_at.strftime('%B %d, %Y at %H:%M:%S')} "+ f"with text: '{tweets['text']}'."+ )++ return user_report++ def get_tools(self) -> List[OpenAIFunction]:+ r"""Returns a list of OpenAIFunction objects representing the+ functions in the toolkit.++ Returns:+ List[OpenAIFunction]: A list of OpenAIFunction objects+ representing the functions in the toolkit.+ """+ return [+ OpenAIFunction(self.create_tweet),+ OpenAIFunction(self.delete_tweet),+ OpenAIFunction(self.get_my_user_profile),+ ]++ def _get_twitter_api_key(self) -> Tuple[str, str]:+ r"""Retrieve the Twitter API key and secret from environment variables.++ Returns:+ Tuple[str, str]: A tuple containing the Twitter API key and secret.++ Raises:+ ValueError: If the API key or secret is not found in the+ environment variables.+ """+ # Get `TWITTER_CONSUMER_KEY` and `TWITTER_CONSUMER_SECRET` here:+ # https://developer.twitter.com/en/portal/products/free+ TWITTER_CONSUMER_KEY = os.environ.get("TWITTER_CONSUMER_KEY")+ TWITTER_CONSUMER_SECRET = os.environ.get("TWITTER_CONSUMER_SECRET")++ if not TWITTER_CONSUMER_KEY or not TWITTER_CONSUMER_SECRET:+ missing_keys = ", ".join(+ [+ "TWITTER_CONSUMER_KEY" if not TWITTER_CONSUMER_KEY else "",+ "TWITTER_CONSUMER_SECRET"+ if not TWITTER_CONSUMER_SECRET+ else "",+ ]+ ).strip(", ")+ raise ValueError(+ f"{missing_keys} not found in environment variables. Get them "+ "here: `https://developer.twitter.com/en/portal/products/free`."+ )+ return TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET++ def _get_oauth_session(self) -> requests.Session:+ r'''Initiates an OAuth1Session with Twitter's API and returns it.++ The function first fetches a request token, then prompts the user to+ authorize the application. After the user has authorized the+ application and provided a verifier (PIN), the function fetches an+ access token. Finally, a new OAuth1Session is created with the access+ token and returned.++ Raises:+ RuntimeError: If an error occurs while fetching the OAuth access+ token or the OAuth request token.++ Returns:+ requests_oauthlib.OAuth1Session: An OAuth1Session object+ authenticated with the user's access token.++ Reference:+ https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/+ Manage-Tweets/create_tweet.py+ https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/+ User-Lookup/get_users_me_user_context.py+ '''+ try:+ from requests_oauthlib import OAuth1Session+ except ImportError:+ raise ImportError(+ "Please install `requests_oauthlib` first. You can "+ "install it by running `pip install "+ "requests_oauthlib`."+ )++ consumer_key, consumer_secret = self._get_twitter_api_key()++ # Get request token+ request_token_url = (+ "https://api.twitter.com/oauth/request_token"+ "?oauth_callback=oob&x_auth_access_type=write"+ )+ oauth = OAuth1Session(consumer_key, client_secret=consumer_secret)++ try:+ fetch_response = oauth.fetch_request_token(request_token_url)+ except Exception as e:+ raise RuntimeError(+ f"Error occurred while fetching the OAuth access token: {e}"+ )++ resource_owner_key = fetch_response.get("oauth_token")+ resource_owner_secret = fetch_response.get("oauth_token_secret")++ # Get authorization+ base_authorization_url = "https://api.twitter.com/oauth/authorize"+ authorization_url = oauth.authorization_url(base_authorization_url)+ print("Please go here and authorize: %s" % authorization_url)+ verifier = input("Paste the PIN here: ")++ # Get the access token+ access_token_url = "https://api.twitter.com/oauth/access_token"+ oauth = OAuth1Session(+ consumer_key,+ client_secret=consumer_secret,+ resource_owner_key=resource_owner_key,+ resource_owner_secret=resource_owner_secret,+ verifier=verifier,+ )++ try:+ oauth_tokens = oauth.fetch_access_token(access_token_url)+ except Exception as e:+ raise RuntimeError(+ f"Error occurred while fetching the OAuth request token: {e}"+ )++ # Create a new OAuth1Session with the access token+ oauth = OAuth1Session(+ consumer_key,+ client_secret=consumer_secret,+ resource_owner_key=oauth_tokens["oauth_token"],+ resource_owner_secret=oauth_tokens["oauth_token_secret"],+ )+ return oauth++ def _handle_http_error(self, response: requests.Response) -> str:+ r"""Handles the HTTP response by checking the status code and+ returning an appropriate message if there is an error.++ Args:+ response (requests.Response): The HTTP response to handle.++ Returns:+ str: A string describing the error, if any. If there is no error,+ the function returns an "Unexpected Exception" message.++ Reference:+ https://github.com/tweepy/tweepy/blob/master/tweepy/client.py#L64+ """+ if response.status_code in responses:+ # For 5xx server errors, return "Twitter Server Error"+ if 500 <= response.status_code < 600:+ return "Twitter Server Error"+ else:+ error_message = responses[response.status_code] + " Error"+ return error_message+ elif not 200 <= response.status_code < 300:+ return "HTTP Exception"+ else:+ return "Unexpected Exception"+++TWITTER_FUNCS: List[OpenAIFunction] = TwitterToolkit().get_tools()diff --git a/camel/toolkits/weather_toolkit.py b/camel/toolkits/weather_toolkit.pynew file mode 100644index 000000000..72980da5a--- /dev/null+++ b/camel/toolkits/weather_toolkit.py@@ -0,0 +1,173 @@+# =========== Copyright 2023 @ 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 @ CAMEL-AI.org. All Rights Reserved. ===========+import os+from typing import List, Literal++from camel.toolkits.base import BaseToolkit+from camel.toolkits.openai_function import OpenAIFunction+++class WeatherToolkit(BaseToolkit):+ r"""A class representing a toolkit for interacting with weather data.++ This class provides methods for fetching weather data for a given city+ using the OpenWeatherMap API.+ """++ def get_openweathermap_api_key(self) -> str:+ r"""Retrieve the OpenWeatherMap API key from environment variables.++ Returns:+ str: The OpenWeatherMap API key.++ Raises:+ ValueError: If the API key is not found in the environment+ variables.+ """+ # Get `OPENWEATHERMAP_API_KEY` here: https://openweathermap.org+ OPENWEATHERMAP_API_KEY = os.environ.get('OPENWEATHERMAP_API_KEY')+ if not OPENWEATHERMAP_API_KEY:+ raise ValueError(+ "`OPENWEATHERMAP_API_KEY` not found in environment "+ "variables. Get `OPENWEATHERMAP_API_KEY` here: "+ "`https://openweathermap.org`."+ )+ return OPENWEATHERMAP_API_KEY++ def get_weather_data(+ self,+ city: str,+ temp_units: Literal['kelvin', 'celsius', 'fahrenheit'] = 'kelvin',+ wind_units: Literal[+ 'meters_sec', 'miles_hour', 'knots', 'beaufort'+ ] = 'meters_sec',+ visibility_units: Literal['meters', 'miles'] = 'meters',+ time_units: Literal['unix', 'iso', 'date'] = 'unix',+ ) -> str:+ r"""Fetch and return a comprehensive weather report for a given city+ as a string. The report includes current weather conditions,+ temperature, wind details, visibility, and sunrise/sunset times,+ all formatted as a readable string.++ The function interacts with the OpenWeatherMap API to+ retrieve the data.++ Args:+ city (str): The name of the city for which the weather information+ is desired. Format "City, CountryCode" (e.g., "Paris, FR"+ for Paris, France). If the country code is not provided,+ the API will search for the city in all countries, which+ may yield incorrect results if multiple cities with the+ same name exist.+ temp_units (Literal['kelvin', 'celsius', 'fahrenheit']): Units for+ temperature. (default: :obj:`kelvin`)+ wind_units+ (Literal['meters_sec', 'miles_hour', 'knots', 'beaufort']):+ Units for wind speed. (default: :obj:`meters_sec`)+ visibility_units (Literal['meters', 'miles']): Units for visibility+ distance. (default: :obj:`meters`)+ time_units (Literal['unix', 'iso', 'date']): Format for sunrise and+ sunset times. (default: :obj:`unix`)++ Returns:+ str: A string containing the fetched weather data, formatted in a+ readable manner. If an error occurs, a message indicating the+ error will be returned instead.++ Example of return string:+ "Weather in Paris, FR: 15°C, feels like 13°C. Max temp: 17°C,+ Min temp : 12°C.+ Wind: 5 m/s at 270 degrees. Visibility: 10 kilometers.+ Sunrise at 05:46:05 (UTC), Sunset at 18:42:20 (UTC)."++ Note:+ Please ensure that the API key is valid and has permissions+ to access the weather data.+ """+ # NOTE: This tool may not work as expected since the input arguments+ # like `time_units` should be enum types which are not supported yet.++ try:+ import pyowm+ except ImportError:+ raise ImportError(+ "Please install `pyowm` first. You can install it by running "+ "`pip install pyowm`."+ )++ OPENWEATHERMAP_API_KEY = self.get_openweathermap_api_key()+ owm = pyowm.OWM(OPENWEATHERMAP_API_KEY)+ mgr = owm.weather_manager()++ try:+ observation = mgr.weather_at_place(city)+ weather = observation.weather++ # Temperature+ temperature = weather.temperature(temp_units)++ # Wind+ wind_data = observation.weather.wind(unit=wind_units)+ wind_speed = wind_data.get('speed')+ # 'N/A' if the degree is not available+ wind_deg = wind_data.get('deg', 'N/A')++ # Visibility+ visibility_distance = observation.weather.visibility_distance+ visibility = (+ str(visibility_distance)+ if visibility_units == 'meters'+ else str(observation.weather.visibility(unit='miles'))+ )++ # Sunrise and Sunset+ sunrise_time = str(weather.sunrise_time(timeformat=time_units))+ sunset_time = str(weather.sunset_time(timeformat=time_units))++ # Compile all the weather details into a report string+ weather_report = (+ f"Weather in {city}: "+ f"{temperature['temp']}°{temp_units.title()}, "+ f"feels like "+ f"{temperature['feels_like']}°{temp_units.title()}. "+ f"Max temp: {temperature['temp_max']}°{temp_units.title()}, "+ f"Min temp: {temperature['temp_min']}°{temp_units.title()}. "+ f"Wind: {wind_speed} {wind_units} at {wind_deg} degrees. "+ f"Visibility: {visibility} {visibility_units}. "+ f"Sunrise at {sunrise_time}, Sunset at {sunset_time}."+ )++ return weather_report++ except Exception as e:+ error_message = (+ f"An error occurred while fetching weather data for {city}: "+ f"{e!s}."+ )+ return error_message++ def get_tools(self) -> List[OpenAIFunction]:+ r"""Returns a list of OpenAIFunction objects representing the+ functions in the toolkit.++ Returns:+ List[OpenAIFunction]: A list of OpenAIFunction objects+ representing the functions in the toolkit.+ """+ return [+ OpenAIFunction(self.get_weather_data),+ ]+++WEATHER_FUNCS: List[OpenAIFunction] = WeatherToolkit().get_tools()diff --git a/camel/utils/async_func.py b/camel/utils/async_func.pyindex 80837f007..377bca4f5 100644--- a/camel/utils/async_func.py+++ b/camel/utils/async_func.py@@ -14,7 +14,7 @@import asynciofrom copy import deepcopy-from camel.functions.openai_function import OpenAIFunction+from camel.toolkits import OpenAIFunctiondef sync_funcs_to_async(funcs: list[OpenAIFunction]) -> list[OpenAIFunction]:diff --git a/docs/agents/single_agent.md b/docs/agents/single_agent.mdindex 4077fe9ff..39c05941d 100644--- a/docs/agents/single_agent.md+++ b/docs/agents/single_agent.md@@ -67,7 +67,7 @@ Woohoo, your first agent is ready to play with you!### Tool Usage```python# Import the necessary functions-from camel.functions import MATH_FUNCS, SEARCH_FUNCS+from camel.toolkits import MATH_FUNCS, SEARCH_FUNCS# Initialize the agent with list of toolsagent = ChatAgent(diff --git a/docs/tutorials_and_cookbooks/agents_with_rag.md b/docs/tutorials_and_cookbooks/agents_with_rag.mdindex 89700884a..e784fe8d0 100644--- a/docs/tutorials_and_cookbooks/agents_with_rag.md+++ b/docs/tutorials_and_cookbooks/agents_with_rag.md@@ -195,7 +195,7 @@ In this section we will show how to combine the `AutoRetriever` with `RolePlayinFirst, we need to set a retriever function with well-written docstring for LLM to understand what this function is used for, the main code is the same with the Auto RAG section.```pythonfrom typing import List-from camel.functions import OpenAIFunction+from camel.toolkits import OpenAIFunctionfrom camel.retrievers import AutoRetrieverfrom camel.types import StorageType@@ -239,7 +239,7 @@ RETRIEVER_FUNCS: List[OpenAIFunction] = [Run the role-playing with defined retriever function:```pythonfrom camel.utils import role_playing_with_function-from camel.functions import MATH_FUNCS # import another function from camel+from camel.toolkits import MATH_FUNCS # import another function from camelrole_playing_with_function(task_prompt=(diff --git a/examples/function_call/github_examples.py b/examples/function_call/github_examples.pyindex f62d9dbe4..c63b407c3 100644--- a/examples/function_call/github_examples.py+++ b/examples/function_call/github_examples.py@@ -17,10 +17,9 @@ from colorama import Forefrom camel.agents import ChatAgentfrom camel.configs import ChatGPTConfig-from camel.functions import OpenAIFunctionfrom camel.messages import BaseMessagefrom camel.models import ModelFactory-from camel.toolkits import GithubToolkit+from camel.toolkits import GithubToolkit, OpenAIFunctionfrom camel.types import ModelPlatformType, ModelTypefrom camel.utils import print_text_animateddiff --git a/examples/function_call/openapi_function.py b/examples/function_call/openapi_function.pyindex e3aa227ea..643ee38f5 100644--- a/examples/function_call/openapi_function.py+++ b/examples/function_call/openapi_function.py@@ -13,9 +13,9 @@# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========from camel.agents import ChatAgentfrom camel.configs.openai_config import ChatGPTConfig-from camel.functions import OPENAPI_FUNCSfrom camel.messages import BaseMessagefrom camel.models import ModelFactory+from camel.toolkits import OPENAPI_FUNCSfrom camel.types import ModelPlatformType, ModelType# Define system messagediff --git a/examples/function_call/role_playing_with_functions.py b/examples/function_call/role_playing_with_functions.pyindex f0612a345..201244294 100644--- a/examples/function_call/role_playing_with_functions.py+++ b/examples/function_call/role_playing_with_functions.py@@ -18,15 +18,15 @@ from colorama import Forefrom camel.agents.chat_agent import FunctionCallingRecordfrom camel.configs import ChatGPTConfig-from camel.functions import (+from camel.models import ModelFactory+from camel.societies import RolePlaying+from camel.toolkits import (MAP_FUNCS,MATH_FUNCS,SEARCH_FUNCS,TWITTER_FUNCS,WEATHER_FUNCS,)-from camel.models import ModelFactory-from camel.societies import RolePlayingfrom camel.types import ModelPlatformType, ModelTypefrom camel.utils import print_text_animateddiff --git a/test/agents/test_chat_agent.py b/test/agents/test_chat_agent.pyindex 6394f4470..6c79ed92e 100644--- a/test/agents/test_chat_agent.py+++ b/test/agents/test_chat_agent.py@@ -25,13 +25,12 @@ from PIL import Imagefrom camel.agents import ChatAgentfrom camel.agents.chat_agent import FunctionCallingRecordfrom camel.configs import ChatGPTConfig-from camel.functions import MATH_FUNCS-from camel.functions.openai_function import OpenAIFunctionfrom camel.generators import SystemMessageGeneratorfrom camel.memories import MemoryRecordfrom camel.messages import BaseMessagefrom camel.models import ModelFactoryfrom camel.terminators import ResponseWordsTerminator+from camel.toolkits import MATH_FUNCS, OpenAIFunctionfrom camel.types import (ChatCompletion,ModelPlatformType,diff --git a/test/agents/test_role_playing.py b/test/agents/test_role_playing.pyindex bccbdca72..aaa7daf07 100644--- a/test/agents/test_role_playing.py+++ b/test/agents/test_role_playing.py@@ -15,11 +15,11 @@ import pytestfrom camel.agents import ChatAgent, CriticAgentfrom camel.configs import ChatGPTConfig-from camel.functions import MATH_FUNCSfrom camel.human import Humanfrom camel.messages import BaseMessagefrom camel.models import ModelFactoryfrom camel.societies import RolePlaying+from camel.toolkits import MATH_FUNCSfrom camel.types import ModelPlatformType, ModelType, RoleType, TaskTypemodel = ModelFactory.create(diff --git a/test/functions/test_code_execution.py b/test/toolkits/test_code_execution.pysimilarity index 82%rename from test/functions/test_code_execution.pyrename to test/toolkits/test_code_execution.pyindex 47653c42b..82d9e847b 100644--- a/test/functions/test_code_execution.py+++ b/test/toolkits/test_code_execution.py@@ -12,13 +12,19 @@# limitations under the License.# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+import pytest+from camel.toolkits.code_execution import CodeExecutionToolkit-def test_execute_code():- toolkit = CodeExecutionToolkit()+def code_execution_toolkit():+ return CodeExecutionToolkit()+++def test_execute_code(code_execution_toolkit):code = "x = 'a'\ny = 'b'\nx + y"- result = toolkit.execute_code(code)+ result = code_execution_toolkit.execute_code(code)# ruff: noqa: E501expected_result = f"Executed the code below:\n```py\n{code}\n```\n> Executed Results:\nab"diff --git a/test/functions/test_google_maps_function.py b/test/toolkits/test_google_maps_function.pysimilarity index 84%rename from test/functions/test_google_maps_function.pyrename to test/toolkits/test_google_maps_function.pyindex a5774e8ec..8611eec34 100644--- a/test/functions/test_google_maps_function.py+++ b/test/toolkits/test_google_maps_function.py@@ -14,11 +14,9 @@from functools import wrapsfrom unittest.mock import MagicMock, patch-from camel.functions.google_maps_function import (- get_address_description,- get_elevation,- get_timezone,-)+import pytest++from camel.toolkits import GoogleMapsToolkitdef mock_googlemaps(test_func):@@ -33,8 +31,13 @@ def mock_googlemaps(test_func):return wrapper+def google_maps_toolkit():+ return GoogleMapsToolkit()++@mock_googlemaps-def test_get_address_description(mock_get, mock_client):+def test_get_address_description(mock_get, mock_client, google_maps_toolkit):# Create a mock response for the addressvalidation methodmock_response = {'result': {@@ -62,7 +65,7 @@ def test_get_address_description(mock_get, mock_client):mock_client.return_value = mock_instance# Call the function with a test address- result = get_address_description(+ result = google_maps_toolkit.get_address_description('1600 Amphitheatre Pk', region_code='US', locality='Mountain View')@@ -77,7 +80,7 @@ def test_get_address_description(mock_get, mock_client):@mock_googlemaps-def test_get_elevation(mock_get, mock_client):+def test_get_elevation(mock_get, mock_client, google_maps_toolkit):# Create a mock response for the elevation methodmock_response = [{@@ -93,7 +96,7 @@ def test_get_elevation(mock_get, mock_client):mock_client.return_value = mock_instance# Call the function with a test latitude and longitude- result = get_elevation((40.71473, -73.99867))+ result = google_maps_toolkit.get_elevation((40.71473, -73.99867))# Verify the resultexpected_result = (@@ -105,7 +108,7 @@ def test_get_elevation(mock_get, mock_client):@mock_googlemaps-def test_get_timezone(mock_get, mock_client):+def test_get_timezone(mock_get, mock_client, google_maps_toolkit):# Create a mock response for the timezone methodmock_response = {'dstOffset': 3600,@@ -121,7 +124,7 @@ def test_get_timezone(mock_get, mock_client):mock_client.return_value = mock_instance# Call the function with a test latitude and longitude- result = get_timezone(+ result = google_maps_toolkit.get_timezone((39.603481, -119.682251)) # Coordinates for Los Angeles@@ -136,14 +139,20 @@ def test_get_timezone(mock_get, mock_client):assert result == expected_result-def test_wrong_api_key(monkeypatch):+def test_wrong_api_key(monkeypatch, google_maps_toolkit):monkeypatch.setenv('GOOGLEMAPS_API_KEY', 'invalid_api_key')expected_output = "Error: Invalid API key provided."assert (- get_address_description(+ google_maps_toolkit.get_address_description('1600 Amphitheatre Pk', region_code='US', locality='Mountain View')== expected_output)- assert get_elevation((40.714728, -73.998672)) == expected_output- assert get_timezone((40.714728, -73.998672)) == expected_output+ assert (+ google_maps_toolkit.get_elevation((40.714728, -73.998672))+ == expected_output+ )+ assert (+ google_maps_toolkit.get_timezone((40.714728, -73.998672))+ == expected_output+ )diff --git a/test/functions/test_open_api_function.py b/test/toolkits/test_open_api_function.pysimilarity index 98%rename from test/functions/test_open_api_function.pyrename to test/toolkits/test_open_api_function.pyindex 95566e7cb..7d0b4ea8b 100644--- a/test/functions/test_open_api_function.py+++ b/test/toolkits/test_open_api_function.py@@ -15,16 +15,14 @@ from unittest.mock import MagicMock, patchimport pytest-from camel.functions import (- apinames_filepaths_to_funs_schemas,- generate_apinames_filepaths,-)+from camel.toolkits import OpenAPIToolkit@pytest.fixture(scope="module")def functions_dict():- apinames_filepaths = generate_apinames_filepaths()- openapi_functions_list, _ = apinames_filepaths_to_funs_schemas(+ toolkit = OpenAPIToolkit()+ apinames_filepaths = toolkit.generate_apinames_filepaths()+ openapi_functions_list, _ = toolkit.apinames_filepaths_to_funs_schemas(apinames_filepaths)functions_dict = {func.__name__: func for func in openapi_functions_list}diff --git a/test/functions/test_openai_function.py b/test/toolkits/test_openai_function.pysimilarity index 99%rename from test/functions/test_openai_function.pyrename to test/toolkits/test_openai_function.pyindex 42db1d496..e6f16ed2f 100644--- a/test/functions/test_openai_function.py+++ b/test/toolkits/test_openai_function.py@@ -19,7 +19,7 @@ from typing import Listimport pytestfrom jsonschema.exceptions import SchemaError-from camel.functions import OpenAIFunction, get_openai_tool_schema+from camel.toolkits import OpenAIFunction, get_openai_tool_schemafrom camel.types import RoleTypefrom camel.utils import PYDANTIC_V2diff --git a/test/functions/test_search_functions.py b/test/toolkits/test_search_functions.pysimilarity index 82%rename from test/functions/test_search_functions.pyrename to test/toolkits/test_search_functions.pyindex 46f883b05..99a82d59b 100644--- a/test/functions/test_search_functions.py+++ b/test/toolkits/test_search_functions.py@@ -14,29 +14,32 @@import osfrom unittest.mock import MagicMock, patch+import pytestimport requestsimport wikipedia-from camel.functions.search_functions import (- query_wolfram_alpha,- search_duckduckgo,- search_google,- search_wiki,-)+from camel.toolkits import SearchToolkit-def test_search_wiki_normal():+def search_toolkit():+ return SearchToolkit()+++def test_search_wiki_normal(search_toolkit):expected_output = ("Erygia sigillata is a species of moth in the family Erebidae found ""in Himachal Pradesh, Northern India. The moth was officially ""recognized and classified in 1892.")- assert search_wiki("Erygia sigillata") == expected_output+ assert search_toolkit.search_wiki("Erygia sigillata") == expected_output-def test_search_wiki_not_found():- search_output = search_wiki("South Africa Women Football Team")+def test_search_wiki_not_found(search_toolkit):+ search_output = search_toolkit.search_wiki(+ "South Africa Women Football Team"+ )assert search_output.startswith("There is no page in Wikipedia corresponding to entity South Africa ""Women Football Team, please specify another word to describe the "@@ -44,11 +47,11 @@ def test_search_wiki_not_found():)-def test_search_wiki_with_ambiguity():+def test_search_wiki_with_ambiguity(search_toolkit):expected_output = wikipedia.summary("Google", sentences=5, auto_suggest=False)- assert search_wiki("Google LLC") == expected_output+ assert search_toolkit.search_wiki("Google LLC") == expected_outputdef test_google_api():@@ -78,17 +81,17 @@ def test_duckduckgo_api():assert result.status_code == 200-def test_web_search():+def test_web_search(search_toolkit):query = "What big things are happening in 2023?"- answer = search_google(query)+ answer = search_toolkit.search_google(query)assert answer is not None- answer = search_duckduckgo(query)+ answer = search_toolkit.search_duckduckgo(query)assert answer is not None@patch('wolframalpha.Client')@patch('os.environ.get')-def test_query_wolfram_alpha(mock_get, mock_client):+def test_query_wolfram_alpha(mock_get, mock_client, search_toolkit):mock_get.return_value = 'FAKE_APP_ID'# Create mock subpods objects@@ -125,7 +128,9 @@ def test_query_wolfram_alpha(mock_get, mock_client):mock_instance.query.return_value = mock_resmock_client.return_value = mock_instance- result = query_wolfram_alpha("calculate limit of sinx^2/x", True)+ result = search_toolkit.query_wolfram_alpha(+ "calculate limit of sinx^2/x", True+ )expected_output = ("Assumption:\n""lim_(x->0) (sin^2(x))/x = 0\n\n"diff --git a/test/functions/test_slack_functions.py b/test/toolkits/test_slack_functions.pysimilarity index 65%rename from test/functions/test_slack_functions.pyrename to test/toolkits/test_slack_functions.pyindex bf8ebd31f..5d74c646a 100644--- a/test/functions/test_slack_functions.py+++ b/test/toolkits/test_slack_functions.py@@ -13,20 +13,19 @@# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========from unittest.mock import MagicMock, patch-from camel.functions.slack_functions import (- create_slack_channel,- delete_slack_message,- get_slack_channel_information,- get_slack_channel_message,- join_slack_channel,- leave_slack_channel,- send_slack_message,-)---def test_create_slack_channel():+import pytest++from camel.toolkits import SlackToolkit+++def slack_toolkit():+ return SlackToolkit()+++def test_create_slack_channel(slack_toolkit):with patch(- 'camel.functions.slack_functions._login_slack'+ 'camel.toolkits.slack_toolkit.SlackToolkit._login_slack') as mock_login_slack:mock_client = MagicMock()mock_login_slack.return_value = mock_client@@ -37,7 +36,9 @@ def test_create_slack_channel():'fake_response_key': 'fake_response_value'}- result = create_slack_channel('test_channel', is_private=True)+ result = slack_toolkit.create_slack_channel(+ 'test_channel', is_private=True+ )assert result == "{'fake_response_key': 'fake_response_value'}"mock_client.conversations_create.assert_called_once_with(@@ -48,26 +49,32 @@ def test_create_slack_channel():)-def test_join_slack_channel():- with patch("camel.functions.slack_functions._login_slack") as mock_login:+def test_join_slack_channel(slack_toolkit):+ with patch(+ "camel.toolkits.slack_toolkit.SlackToolkit._login_slack"+ ) as mock_login:mock_client = MagicMock()mock_login.return_value = mock_clientmock_client.conversations_join.return_value = {}- response = join_slack_channel("123")+ response = slack_toolkit.join_slack_channel("123")assert response == "{}"-def test_leave_slack_channel():- with patch("camel.functions.slack_functions._login_slack") as mock_login:+def test_leave_slack_channel(slack_toolkit):+ with patch(+ "camel.toolkits.slack_toolkit.SlackToolkit._login_slack"+ ) as mock_login:mock_client = MagicMock()mock_login.return_value = mock_clientmock_client.conversations_leave.return_value = {}- response = leave_slack_channel("123")+ response = slack_toolkit.leave_slack_channel("123")assert response == "{}"-def test_get_slack_channel_information():- with patch("camel.functions.slack_functions._login_slack") as mock_login:+def test_get_slack_channel_information(slack_toolkit):+ with patch(+ "camel.toolkits.slack_toolkit.SlackToolkit._login_slack"+ ) as mock_login:mock_client = MagicMock()mock_login.return_value = mock_clientmock_client.conversations_list.return_value = {@@ -80,13 +87,15 @@ def test_get_slack_channel_information():}]}- response = get_slack_channel_information()+ response = slack_toolkit.get_slack_channel_information()expected_result = """[{"id": "123", "name": "test_channel", "creat"""assert expected_result in response-def test_get_slack_channel_message():- with patch("camel.functions.slack_functions._login_slack") as mock_login:+def test_get_slack_channel_message(slack_toolkit):+ with patch(+ "camel.toolkits.slack_toolkit.SlackToolkit._login_slack"+ ) as mock_login:mock_client = MagicMock()mock_login.return_value = mock_clientmock_client.conversations_history.return_value = {@@ -94,26 +103,30 @@ def test_get_slack_channel_message():{"user": "user_id", "text": "test_message", "ts": "123"}]}- response = get_slack_channel_message("123")+ response = slack_toolkit.get_slack_channel_message("123")expected_result = ('[{"user": "user_id", "text": "test_message", "ts": "123"}]')assert response == expected_result-def test_send_slack_message():- with patch("camel.functions.slack_functions._login_slack") as mock_login:+def test_send_slack_message(slack_toolkit):+ with patch(+ "camel.toolkits.slack_toolkit.SlackToolkit._login_slack"+ ) as mock_login:mock_client = MagicMock()mock_login.return_value = mock_clientmock_client.chat_postMessage.return_value = {}- response = send_slack_message("test_message", "123")+ response = slack_toolkit.send_slack_message("test_message", "123")assert response == "{}"-def test_delete_slack_message():- with patch("camel.functions.slack_functions._login_slack") as mock_login:+def test_delete_slack_message(slack_toolkit):+ with patch(+ "camel.toolkits.slack_toolkit.SlackToolkit._login_slack"+ ) as mock_login:mock_client = MagicMock()mock_login.return_value = mock_clientmock_client.chat_delete.return_value = {}- response = delete_slack_message("123", "123")+ response = slack_toolkit.delete_slack_message("123", "123")assert response == "{}"diff --git a/test/functions/test_twitter_function.py b/test/toolkits/test_twitter_function.pysimilarity index 87%rename from test/functions/test_twitter_function.pyrename to test/toolkits/test_twitter_function.pyindex 18516e413..9d7543abc 100644--- a/test/functions/test_twitter_function.py+++ b/test/toolkits/test_twitter_function.py@@ -13,14 +13,17 @@# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========from unittest.mock import MagicMock, patch-from camel.functions.twitter_function import (- create_tweet,- delete_tweet,- get_my_user_profile,-)+import pytest+from camel.toolkits import TwitterToolkit-def test_create_tweet(monkeypatch):++def twitter_toolkit():+ return TwitterToolkit()+++def test_create_tweet(monkeypatch, twitter_toolkit):# Create a mock response objectmock_response = MagicMock()mock_response.json.return_value = {@@ -40,14 +43,16 @@ def test_create_tweet(monkeypatch):monkeypatch.setattr('builtins.print', lambda x: captured_output.append(x))# Use patch to mock the get_oauth_session method- patch_path = 'camel.functions.twitter_function.get_oauth_session'+ patch_path = (+ 'camel.toolkits.twitter_toolkit.TwitterToolkit._get_oauth_session'+ )with patch(patch_path) as mock_get_oauth_session:# Configure the mock OAuth session's post method# to return the mock response objectmock_get_oauth_session.return_value.post.return_value = mock_response# Call the create_tweet function- response = create_tweet(text="Test tweet.")+ response = twitter_toolkit.create_tweet(text="Test tweet.")# Verify the outputexpected_start = (@@ -62,7 +67,7 @@ def test_create_tweet(monkeypatch):assert response == expected_response-def test_delete_tweet(monkeypatch):+def test_delete_tweet(monkeypatch, twitter_toolkit):# Delete a mock response objectmock_response = MagicMock()mock_response.json.return_value = {'data': {'deleted': True}}@@ -76,14 +81,16 @@ def test_delete_tweet(monkeypatch):monkeypatch.setattr('builtins.print', lambda x: captured_output.append(x))# Use patch to mock the get_oauth_session method- patch_path = 'camel.functions.twitter_function.get_oauth_session'+ patch_path = (+ 'camel.toolkits.twitter_toolkit.TwitterToolkit._get_oauth_session'+ )with patch(patch_path) as mock_get_oauth_session:# Configure the mock OAuth session's delete method# to return the mock response objectmock_get_oauth_session.return_value.delete.return_value = mock_response# Call the delete_tweet function- response = delete_tweet(tweet_id="11111")+ response = twitter_toolkit.delete_tweet(tweet_id="11111")# Verify the outputexpected_start = (@@ -96,7 +103,7 @@ def test_delete_tweet(monkeypatch):assert response == expected_response-def test_get_user_me(monkeypatch):+def test_get_user_me(monkeypatch, twitter_toolkit):# Mocked JSON response, anonymizedmock_json_response = {'data': {@@ -131,7 +138,9 @@ def test_get_user_me(monkeypatch):}# Use patch to mock the get_oauth_session method- patch_path = 'camel.functions.twitter_function.get_oauth_session'+ patch_path = (+ 'camel.toolkits.twitter_toolkit.TwitterToolkit._get_oauth_session'+ )with patch(patch_path) as mock_get_oauth_session:mock_response = MagicMock()mock_response.json.return_value = mock_json_response@@ -139,7 +148,7 @@ def test_get_user_me(monkeypatch):mock_get_oauth_session.return_value.get.return_value = mock_response# Call the get_user_me function- response = get_my_user_profile()+ response = twitter_toolkit.get_my_user_profile()# Verify the returned user information reportexpected_report = (diff --git a/test/functions/test_weather_function.py b/test/toolkits/test_weather_function.pysimilarity index 92%rename from test/functions/test_weather_function.pyrename to test/toolkits/test_weather_function.pyindex 06602bbed..e295ef0bd 100644--- a/test/functions/test_weather_function.py+++ b/test/toolkits/test_weather_function.py@@ -17,7 +17,7 @@ from datetime import datetime, timezoneimport pytest-from camel.functions.weather_functions import get_weather_data+from camel.toolkits import WeatherToolkit@pytest.fixture(scope="module")@@ -28,7 +28,12 @@ def api_key():return key-def test_weather(api_key):+def weather_toolkit():+ return WeatherToolkit()+++def test_weather(api_key, weather_toolkit):# Test temperature in Paris, FR.city = "Paris, FR"temp_units_options = {@@ -37,7 +42,7 @@ def test_weather(api_key):'fahrenheit': (-148, 140),}for temp_units, (temp_min, temp_max) in temp_units_options.items():- report = get_weather_data(+ report = weather_toolkit.get_weather_data(city, temp_units, 'meters_sec', 'meters', 'iso')# Parse temperature@@ -63,7 +68,9 @@ def test_weather(api_key):'beaufort': (0, 12),}for wind_units, (wind_min, wind_max) in wind_units_options.items():- report = get_weather_data(city, 'celsius', wind_units, 'meters', 'iso')+ report = weather_toolkit.get_weather_data(+ city, 'celsius', wind_units, 'meters', 'iso'+ )# Parse wind speedpattern = re.compile(rf"Wind: (-?\d+\.?\d*) {wind_units} at")match = pattern.search(report)@@ -81,7 +88,7 @@ def test_weather(api_key):visibility_units_options = {'meters': (0, 400000), 'miles': (0, 250)}for visibility_units, visibility_range in visibility_units_options.items():visibility_min, visibility_max = visibility_range- report = get_weather_data(+ report = weather_toolkit.get_weather_data(city, 'celsius', 'meters_sec', visibility_units, 'iso')# Parse visibility@@ -103,7 +110,7 @@ def test_weather(api_key):# Test each time_units optiontime_units_options = ['unix', 'iso', 'date']for time_units in time_units_options:- report = get_weather_data(+ report = weather_toolkit.get_weather_data(city, 'celsius', 'meters_sec', 'meters', time_units)# Regex to extract sunrise and sunset times based on time_units