🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: Integrate watson x (#2297)
Tao Sunb814b84e64b6
diff --git a/.github/workflows/build_package.yml b/.github/workflows/build_package.ymlindex fc084a1f5..27431f008 100644--- a/.github/workflows/build_package.yml+++ b/.github/workflows/build_package.yml@@ -108,6 +108,8 @@ jobs:BEDROCK_API_BASE_URL: "${{ secrets.BEDROCK_API_BASE_URL }}"NETMIND_API_KEY: "${{ secrets.NETMIND_API_KEY }}"NOVITA_API_KEY: "${{ secrets.NOVITA_API_KEY }}"+ WATSONX_API_KEY: "${{ secrets.WATSONX_API_KEY }}"+ WATSONX_PROJECT_ID: "${{ secrets.WATSONX_PROJECT_ID }}"run: |source test_venv/bin/activatepytest --fast-test-mode ./testdiff --git a/.github/workflows/pytest_package.yml b/.github/workflows/pytest_package.ymlindex bd64419c7..fc623e813 100644--- a/.github/workflows/pytest_package.yml+++ b/.github/workflows/pytest_package.yml@@ -73,6 +73,8 @@ jobs:BEDROCK_API_BASE_URL: "${{ secrets.BEDROCK_API_BASE_URL }}"NETMIND_API_KEY: "${{ secrets.NETMIND_API_KEY }}"NOVITA_API_KEY: "${{ secrets.NOVITA_API_KEY }}"+ WATSONX_API_KEY: "${{ secrets.WATSONX_API_KEY }}"+ WATSONX_PROJECT_ID: "${{ secrets.WATSONX_PROJECT_ID }}"run: |source .venv/bin/activateuv pip install -e ".[all, dev, docs]"@@ -138,6 +140,8 @@ jobs:BEDROCK_API_BASE_URL: "${{ secrets.BEDROCK_API_BASE_URL }}"NETMIND_API_KEY: "${{ secrets.NETMIND_API_KEY }}"NOVITA_API_KEY: "${{ secrets.NOVITA_API_KEY }}"+ WATSONX_API_KEY: "${{ secrets.WATSONX_API_KEY }}"+ WATSONX_PROJECT_ID: "${{ secrets.WATSONX_PROJECT_ID }}"run: |source .venv/bin/activateuv pip install -e ".[all, dev, docs]"@@ -201,6 +205,8 @@ jobs:BEDROCK_API_BASE_URL: "${{ secrets.BEDROCK_API_BASE_URL }}"NETMIND_API_KEY: "${{ secrets.NETMIND_API_KEY }}"NOVITA_API_KEY: "${{ secrets.NOVITA_API_KEY }}"+ WATSONX_API_KEY: "${{ secrets.WATSONX_API_KEY }}"+ WATSONX_PROJECT_ID: "${{ secrets.WATSONX_PROJECT_ID }}"run: |source .venv/bin/activateuv pip install -e ".[all, dev, docs]"diff --git a/camel/configs/__init__.py b/camel/configs/__init__.pyindex 55225172b..657ab4ee5 100644--- a/camel/configs/__init__.py+++ b/camel/configs/__init__.py@@ -44,6 +44,7 @@ from .sglang_config import SGLANG_API_PARAMS, SGLangConfigfrom .siliconflow_config import SILICONFLOW_API_PARAMS, SiliconFlowConfigfrom .togetherai_config import TOGETHERAI_API_PARAMS, TogetherAIConfigfrom .vllm_config import VLLM_API_PARAMS, VLLMConfig+from .watsonx_config import WATSONX_API_PARAMS, WatsonXConfigfrom .yi_config import YI_API_PARAMS, YiConfigfrom .zhipuai_config import ZHIPUAI_API_PARAMS, ZhipuAIConfig@@ -109,4 +110,6 @@ __all__ = ['OPENROUTER_API_PARAMS','LMSTUDIO_API_PARAMS','LMStudioConfig',+ 'WatsonXConfig',+ 'WATSONX_API_PARAMS',]diff --git a/camel/configs/watsonx_config.py b/camel/configs/watsonx_config.pynew file mode 100644index 000000000..8977ae99a--- /dev/null+++ b/camel/configs/watsonx_config.py@@ -0,0 +1,96 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+from __future__ import annotations++from typing import Dict, List, Literal, Optional++from camel.configs.base_config import BaseConfig+++class WatsonXConfig(BaseConfig):+ r"""Defines the parameters for generating chat completions using the+ IBM WatsonX API.++ See: https://ibm.github.io/watsonx-ai-python-sdk/fm_schema.html++ Args:+ frequency_penalty (float, optional): Number between -2.0 and 2.0.+ Positive values penalize new tokens based on their existing+ frequency in the text so far, decreasing the model's likelihood+ to repeat the same line verbatim.+ (default: :obj:`None`)+ logprobs (bool, optional): Whether to return log probabilities of the+ output tokens or not. If true, returns the log probabilities of+ each output token returned in the content of message.+ (default: :obj:`None`)+ top_logprobs (int, optional): An integer between 0 and 20 specifying+ the number of most likely tokens to return at each token position,+ each with an associated log probability.+ (default: :obj:`None`)+ presence_penalty (float, optional): Number between -2.0 and 2.0.+ Positive values penalize new tokens based on whether they appear+ in the text so far, increasing the model's likelihood to talk+ about new topics.+ (default: :obj:`None`)+ temperature (float, optional): What sampling temperature to use,+ between 0 and 2. Higher values like 0.8 will make the output+ more random, while lower values like 0.2 will make it more focused+ and deterministic. We generally recommend altering this or top_p+ but not both.+ (default: :obj:`None`)+ max_tokens (int, optional): The maximum number of tokens to generate+ in the chat completion. The total length of input tokens and+ generated tokens is limited by the model's context length.+ (default: :obj:`None`)+ time_limit (int, optional): The maximum amount of time in seconds+ that the API will spend generating a response.+ (default: :obj:`None`)+ top_p (float, optional): Controls the randomness of the generated+ results. Lower values lead to less randomness, while higher+ values increase randomness. (default: :obj:`None`)+ n (int, optional): How many chat completion choices to generate for+ each input message. Note that you will be charged based on the+ total number of tokens generated.+ (default: :obj:`None`)+ logit_biaslogit_bias (Optional[dict], optional): Modify probability+ of specific tokens appearing in the completion.+ (default: :obj:`None`)+ seed (int, optional): If specified, the system will make a best effort+ to sample deterministically, such that repeated requests with the+ same seed and parameters should return the same result.+ (default: :obj:`None`)+ stop (List[str], optional): Up to 4 sequences where the API will stop+ generating further tokens.+ (default: :obj:`None`)+ tool_choice_options (Literal["none", "auto"], optional): The options+ for the tool choice.+ (default: :obj:`"auto"`)+ """++ frequency_penalty: Optional[float] = None+ logprobs: Optional[bool] = None+ top_logprobs: Optional[int] = None+ presence_penalty: Optional[float] = None+ temperature: Optional[float] = None+ max_tokens: Optional[int] = None+ time_limit: Optional[int] = None+ top_p: Optional[float] = None+ n: Optional[int] = None+ logit_bias: Optional[Dict] = None+ seed: Optional[int] = None+ stop: Optional[List[str]] = None+ tool_choice_options: Literal["none", "auto"] = "auto"+++WATSONX_API_PARAMS = {param for param in WatsonXConfig.model_fields.keys()}diff --git a/camel/models/__init__.py b/camel/models/__init__.pyindex 443ee3e52..479c54215 100644--- a/camel/models/__init__.py+++ b/camel/models/__init__.py@@ -49,6 +49,7 @@ from .stub_model import StubModelfrom .togetherai_model import TogetherAIModelfrom .vllm_model import VLLMModelfrom .volcano_model import VolcanoModel+from .watsonx_model import WatsonXModelfrom .yi_model import YiModelfrom .zhipuai_model import ZhipuAIModel@@ -94,4 +95,5 @@ __all__ = ['SiliconFlowModel','VolcanoModel','LMStudioModel',+ 'WatsonXModel',]diff --git a/camel/models/model_factory.py b/camel/models/model_factory.pyindex 342e9a9ec..79ea7c0b8 100644--- a/camel/models/model_factory.py+++ b/camel/models/model_factory.py@@ -48,6 +48,7 @@ from camel.models.stub_model import StubModelfrom camel.models.togetherai_model import TogetherAIModelfrom camel.models.vllm_model import VLLMModelfrom camel.models.volcano_model import VolcanoModel+from camel.models.watsonx_model import WatsonXModelfrom camel.models.yi_model import YiModelfrom camel.models.zhipuai_model import ZhipuAIModelfrom camel.types import ModelPlatformType, ModelType, UnifiedModelType@@ -168,6 +169,8 @@ class ModelFactory:model_class = NovitaModelelif model_type == ModelType.STUB:model_class = StubModel+ elif model_type.is_watsonx:+ model_class = WatsonXModelif model_class is None:raise ValueError(diff --git a/camel/models/watsonx_model.py b/camel/models/watsonx_model.pynew file mode 100644index 000000000..15f8b81ad--- /dev/null+++ b/camel/models/watsonx_model.py@@ -0,0 +1,253 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+import os+from typing import Any, Dict, List, Optional, Type, Union++from pydantic import BaseModel++from camel.configs import WATSONX_API_PARAMS, WatsonXConfig+from camel.logger import get_logger+from camel.messages import OpenAIMessage+from camel.models import BaseModelBackend+from camel.models._utils import try_modify_message_with_format+from camel.types import ChatCompletion, ModelType+from camel.utils import (+ BaseTokenCounter,+ OpenAITokenCounter,+ api_keys_required,+)++logger = get_logger(__name__)+++class WatsonXModel(BaseModelBackend):+ r"""WatsonX API in a unified BaseModelBackend interface.++ Args:+ model_type (Union[ModelType, str]): Model type for which a backend is+ created, one of WatsonX series.+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary+ that will be fed into :obj:`ModelInference.chat()`.+ If :obj:`None`, :obj:`WatsonXConfig().as_dict()` will be used.+ (default: :obj:`None`)+ api_key (Optional[str], optional): The API key for authenticating with+ the WatsonX service. (default: :obj:`None`)+ url (Optional[str], optional): The url to the WatsonX service.+ (default: :obj:`None`)+ project_id (Optional[str], optional): The project ID authenticating+ with the WatsonX service. (default: :obj:`None`)+ token_counter (Optional[BaseTokenCounter], optional): Token counter to+ use for the model. If not provided, :obj:`OpenAITokenCounter(+ ModelType.GPT_4O_MINI)` will be used.+ (default: :obj:`None`)+ timeout (Optional[float], optional): The timeout value in seconds for+ API calls. If not provided, will fall back to the MODEL_TIMEOUT+ environment variable or default to 180 seconds.+ (default: :obj:`None`)+ """++ @api_keys_required(+ [+ ("api_key", 'WATSONX_API_KEY'),+ ("project_id", 'WATSONX_PROJECT_ID'),+ ]+ )+ def __init__(+ self,+ model_type: Union[ModelType, str],+ model_config_dict: Optional[Dict[str, Any]] = None,+ api_key: Optional[str] = None,+ url: Optional[str] = None,+ project_id: Optional[str] = None,+ token_counter: Optional[BaseTokenCounter] = None,+ timeout: Optional[float] = None,+ ):+ from ibm_watsonx_ai import APIClient, Credentials+ from ibm_watsonx_ai.foundation_models import ModelInference++ if model_config_dict is None:+ model_config_dict = WatsonXConfig().as_dict()++ api_key = api_key or os.environ.get("WATSONX_API_KEY")+ url = url or os.environ.get(+ "WATSONX_URL", "https://jp-tok.ml.cloud.ibm.com"+ )+ project_id = project_id or os.environ.get("WATSONX_PROJECT_ID")++ timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))+ super().__init__(+ model_type, model_config_dict, api_key, url, token_counter, timeout+ )++ self._project_id = project_id+ credentials = Credentials(api_key=self._api_key, url=self._url)+ client = APIClient(credentials, project_id=self._project_id)++ self._model = ModelInference(+ model_id=self.model_type,+ api_client=client,+ params=model_config_dict,+ )++ def _to_openai_response(self, response: Dict[str, Any]) -> ChatCompletion:+ r"""Convert WatsonX response to OpenAI format."""+ if not response:+ raise ValueError("Empty response from WatsonX API")++ # Extract usage information+ usage = response.get("usage", {})++ # Create OpenAI-compatible response+ obj = ChatCompletion.construct(+ id=response.get("id", ""),+ choices=response.get("choices", []),+ created=response.get("created"),+ model=self.model_type,+ object="chat.completion",+ usage=usage,+ )+ return obj++ @property+ def token_counter(self) -> BaseTokenCounter:+ r"""Initialize the token counter for the model backend.++ Returns:+ BaseTokenCounter: The token counter following the model's+ tokenization style.+ """+ if not self._token_counter:+ self._token_counter = OpenAITokenCounter(+ model=ModelType.GPT_4O_MINI+ )+ return self._token_counter++ def _prepare_request(+ self,+ messages: List[OpenAIMessage],+ response_format: Optional[Type[BaseModel]] = None,+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> Dict[str, Any]:+ import copy++ request_config = copy.deepcopy(self.model_config_dict)++ if tools:+ request_config["tools"] = tools+ elif response_format:+ try_modify_message_with_format(messages[-1], response_format)+ request_config["response_format"] = {"type": "json_object"}++ return request_config++ def _run(+ self,+ messages: List[OpenAIMessage],+ response_format: Optional[Type[BaseModel]] = None,+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> ChatCompletion:+ r"""Runs inference of WatsonX chat completion.++ Args:+ messages (List[OpenAIMessage]): Message list with the chat history+ in OpenAI API format.+ response_format (Optional[Type[BaseModel]], optional): The+ response format. (default: :obj:`None`)+ tools (Optional[List[Dict[str, Any]]], optional): tools to use.+ (default: :obj:`None`)++ Returns:+ ChatCompletion.+ """+ try:+ request_config = self._prepare_request(+ messages, response_format, tools+ )++ # WatsonX expects messages as a list of dictionaries+ response = self._model.chat(+ messages=messages,+ params=request_config,+ tools=tools,+ )++ openai_response = self._to_openai_response(response)+ return openai_response++ except Exception as e:+ logger.error(f"Unexpected error when calling WatsonX API: {e!s}")+ raise++ async def _arun(+ self,+ messages: List[OpenAIMessage],+ response_format: Optional[Type[BaseModel]] = None,+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> ChatCompletion:+ r"""Runs inference of WatsonX chat completion asynchronously.++ Args:+ messages (List[OpenAIMessage]): Message list with the chat history+ in OpenAI API format.+ response_format (Optional[Type[BaseModel]], optional): The+ response format. (default: :obj:`None`)+ tools (Optional[List[Dict[str, Any]]], optional): tools to use.+ (default: :obj:`None`)++ Returns:+ ChatCompletion.+ """+ try:+ request_config = self._prepare_request(+ messages, response_format, tools+ )++ # WatsonX expects messages as a list of dictionaries+ response = await self._model.achat(+ messages=messages,+ params=request_config,+ tools=tools,+ )++ openai_response = self._to_openai_response(response)+ return openai_response++ except Exception as e:+ logger.error(f"Unexpected error when calling WatsonX API: {e!s}")+ raise++ def check_model_config(self):+ r"""Check whether the model configuration contains any unexpected+ arguments to WatsonX API.++ Raises:+ ValueError: If the model configuration dictionary contains any+ unexpected arguments to WatsonX API.+ """+ for param in self.model_config_dict:+ if param not in WATSONX_API_PARAMS:+ raise ValueError(+ f"Unexpected argument `{param}` is "+ "input into WatsonX model backend."+ )++ @property+ def stream(self) -> bool:+ r"""Returns whether the model is in stream mode, which sends partial+ results each time.++ Returns:+ bool: Whether the model is in stream mode.+ """+ return Falsediff --git a/camel/types/enums.py b/camel/types/enums.pyindex 8eda5fd04..a8203a61b 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -352,6 +352,22 @@ class ModelType(UnifiedModelType, Enum):MODELSCOPE_MINISTRAL_8B_INSTRUCT = "mistralai/Ministral-8B-Instruct-2410"MODELSCOPE_DEEPSEEK_V3_0324 = "deepseek-ai/DeepSeek-V3-0324"+ # WatsonX models+ WATSONX_GRANITE_3_8B_INSTRUCT = "ibm/granite-3-8b-instruct"+ WATSONX_LLAMA_3_3_70B_INSTRUCT = "meta-llama/llama-3-3-70b-instruct"+ WATSONX_LLAMA_3_2_1B_INSTRUCT = "meta-llama/llama-3-2-1b-instruct"+ WATSONX_LLAMA_3_2_3B_INSTRUCT = "meta-llama/llama-3-2-3b-instruct"+ WATSONX_LLAMA_3_2_11B_VISION_INSTRUCT = (+ "meta-llama/llama-3-2-11b-vision-instruct"+ )+ WATSONX_LLAMA_3_2_90B_VISION_INSTRUCT = (+ "meta-llama/llama-3-2-90b-vision-instruct"+ )+ WATSONX_LLAMA_GUARD_3_11B_VISION_INSTRUCT = (+ "meta-llama/llama-guard-3-11b-vision-instruct"+ )+ WATSONX_MISTRAL_LARGE = "mistralai/mistral-large"+def __str__(self):return self.value@@ -775,6 +791,19 @@ class ModelType(UnifiedModelType, Enum):ModelType.SILICONFLOW_PRO_THUDM_GLM_4_9B_CHAT,}+ @property+ def is_watsonx(self) -> bool:+ return self in {+ ModelType.WATSONX_GRANITE_3_8B_INSTRUCT,+ ModelType.WATSONX_LLAMA_3_3_70B_INSTRUCT,+ ModelType.WATSONX_LLAMA_3_2_1B_INSTRUCT,+ ModelType.WATSONX_LLAMA_3_2_3B_INSTRUCT,+ ModelType.WATSONX_LLAMA_3_2_11B_VISION_INSTRUCT,+ ModelType.WATSONX_LLAMA_3_2_90B_VISION_INSTRUCT,+ ModelType.WATSONX_LLAMA_GUARD_3_11B_VISION_INSTRUCT,+ ModelType.WATSONX_MISTRAL_LARGE,+ }+@propertydef is_novita(self) -> bool:return self in {@@ -874,6 +903,7 @@ class ModelType(UnifiedModelType, Enum):ModelType.LMSTUDIO_GEMMA_3_4B,ModelType.LMSTUDIO_GEMMA_3_12B,ModelType.LMSTUDIO_GEMMA_3_27B,+ ModelType.WATSONX_GRANITE_3_8B_INSTRUCT,ModelType.NOVITA_L3_8B_STHENO_V3_2,ModelType.NOVITA_LLAMA_3_8B,ModelType.NOVITA_GEMMA_2_9B_IT,@@ -967,6 +997,7 @@ class ModelType(UnifiedModelType, Enum):ModelType.MODELSCOPE_MINISTRAL_8B_INSTRUCT,ModelType.MODELSCOPE_DEEPSEEK_V3_0324,ModelType.OPENROUTER_LLAMA_3_1_405B,+ ModelType.WATSONX_MISTRAL_LARGE,ModelType.NOVITA_QWEN_32B,ModelType.NOVITA_LLAMA_3_1_70B,ModelType.NOVITA_MISTRAL_7B,@@ -1073,6 +1104,12 @@ class ModelType(UnifiedModelType, Enum):ModelType.OPENROUTER_LLAMA_3_1_70B,ModelType.PPIO_LLAMA_3_3_70B,ModelType.OPENROUTER_LLAMA_4_SCOUT,+ ModelType.WATSONX_LLAMA_3_3_70B_INSTRUCT,+ ModelType.WATSONX_LLAMA_3_2_1B_INSTRUCT,+ ModelType.WATSONX_LLAMA_3_2_3B_INSTRUCT,+ ModelType.WATSONX_LLAMA_3_2_11B_VISION_INSTRUCT,+ ModelType.WATSONX_LLAMA_3_2_90B_VISION_INSTRUCT,+ ModelType.WATSONX_LLAMA_GUARD_3_11B_VISION_INSTRUCT,ModelType.NOVITA_LLAMA_4_SCOUT_17B,ModelType.NOVITA_LLAMA_3_3_70B,ModelType.NOVITA_MISTRAL_NEMO,@@ -1326,6 +1363,7 @@ class ModelPlatformType(Enum):VOLCANO = "volcano"NETMIND = "netmind"NOVITA = "novita"+ WATSONX = "watsonx"@classmethoddef from_name(cls, name):diff --git a/camel/types/unified_model_type.py b/camel/types/unified_model_type.pyindex ba50595d2..411e431f5 100644--- a/camel/types/unified_model_type.py+++ b/camel/types/unified_model_type.py@@ -158,6 +158,11 @@ class UnifiedModelType(str):r"""Returns whether the model is a Novita served model."""return True+ @property+ def is_watsonx(self) -> bool:+ r"""Returns whether the model is a WatsonX served model."""+ return True+@propertydef support_native_structured_output(self) -> bool:r"""Returns whether the model supports native structured output."""diff --git a/docs/key_modules/models.md b/docs/key_modules/models.mdindex 78b78d2a3..bf26d406e 100644--- a/docs/key_modules/models.md+++ b/docs/key_modules/models.md@@ -41,7 +41,7 @@ CAMEL supports a wide range of models, including [OpenAI’s GPT series](https:/| **AIML** | [supported models](https://docs.aimlapi.com/api-overview/model-database/text-models) || **ModelScope** | [supported models](https://www.modelscope.cn/docs/model-service/API-Inference/intro) || **AWS Bedrock** | [supported models](https://us-west-2.console.aws.amazon.com/bedrock/home?region=us-west-2#/) |-+| **IBM WatsonX** | [supported models](https://jp-tok.dataplatform.cloud.ibm.com/samples?context=wx&tab=foundation-model) |## 3. How to Use Models via API Callsdiff --git a/examples/models/watsonx_model_example.py b/examples/models/watsonx_model_example.pynew file mode 100644index 000000000..ab849a99c--- /dev/null+++ b/examples/models/watsonx_model_example.py@@ -0,0 +1,47 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+from camel.agents import ChatAgent+from camel.configs import WatsonXConfig+from camel.models import ModelFactory+from camel.toolkits import MathToolkit+from camel.types import ModelPlatformType, ModelType++tools = [*MathToolkit().get_tools()]++model = ModelFactory.create(+ model_platform=ModelPlatformType.WATSONX,+ model_type=ModelType.WATSONX_LLAMA_3_3_70B_INSTRUCT,+ model_config_dict=WatsonXConfig(temperature=0.2).as_dict(),+)++# Define system message+sys_msg = "You are a helpful assistant."++# Set agent+camel_agent = ChatAgent(system_message=sys_msg, model=model, tools=tools)++user_msg = "Assume now is 2024 in the Gregorian calendar, University of Oxford was set up in 1096, estimate the current age of University of Oxford" # noqa: E501++# Get response information+response = camel_agent.step(user_msg)+print(response.msgs[0].content)+print(response.info['tool_calls'])++'''+==============================================================================+The University of Oxford is approximately 928 years old in the year 2024.+[ToolCallingRecord(tool_name='sub', args={'a': 2024, 'b': 1096}, result=928,+tool_call_id='call_05f85b0fdd9241be912883')]+==============================================================================+'''diff --git a/pyproject.toml b/pyproject.tomlindex 1470ccc96..24c100c3d 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -172,6 +172,7 @@ model_platforms = ["anthropic>=0.47.0,<0.50.0","cohere>=5.11.0,<6","fish-audio-sdk>=2024.12.5,<2025",+ "ibm-watsonx-ai>=1.3.11",]huggingface = ["transformers>=4,<5",@@ -354,6 +355,7 @@ all = ["pyautogui>=0.9.54,<0.10","pyobvector>=0.1.18","scrapegraph-py>=1.12.0,<2",+ "ibm-watsonx-ai>=1.3.11",][project.urls]@@ -541,6 +543,7 @@ module = ["pyautogui","pyobvector.*","scrapegraph_py.*",+ "ibm_watsonx_ai.*",]ignore_missing_imports = truediff --git a/test/models/test_watsonx_model.py b/test/models/test_watsonx_model.pynew file mode 100644index 000000000..a12f91c79--- /dev/null+++ b/test/models/test_watsonx_model.py@@ -0,0 +1,79 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++import re++import pytest++from camel.configs import WatsonXConfig+from camel.models import ModelFactory, WatsonXModel+from camel.types import ModelPlatformType+from camel.utils import OpenAITokenCounter++[email protected]_backend+ "model_type",+ [+ "meta-llama/llama-3-3-70b-instruct",+ "ibm/granite-3-8b-instruct",+ "mistralai/mistral-large",+ ],+)+def test_watsonx_model(model_type):+ # Skip test as it requires valid WatsonX API credentials+ pytest.skip("Skipping test that requires valid WatsonX API credentials")++ model_config_dict = WatsonXConfig().as_dict()+ model = WatsonXModel(model_type, model_config_dict)+ assert model.model_type == model_type+ assert model.model_config_dict == model_config_dict+ assert isinstance(model.token_counter, OpenAITokenCounter)++[email protected]_backend+ "model_type",+ [+ "meta-llama/llama-3-3-70b-instruct",+ "ibm/granite-3-8b-instruct",+ ],+)+def test_watsonx_model_create(model_type):+ # Skip test as it requires valid WatsonX API credentials+ pytest.skip("Skipping test that requires valid WatsonX API credentials")++ model = ModelFactory.create(+ model_platform=ModelPlatformType.WATSONX,+ model_type=model_type,+ model_config_dict=WatsonXConfig(temperature=0.7).as_dict(),+ )+ assert model.model_type == model_type++[email protected]_backend+def test_watsonx_model_unexpected_argument():+ model_type = "meta-llama/llama-3-3-70b-instruct"+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}++ with pytest.raises(+ ValueError,+ match=re.escape(+ (+ "Unexpected argument `model_path` is "+ "input into WatsonX model backend."+ )+ ),+ ):+ _ = WatsonXModel(model_type, model_config_dict)diff --git a/uv.lock b/uv.lockindex 7a9b00652..dc939411a 100644--- a/uv.lock+++ b/uv.lock@@ -545,6 +545,7 @@ dependencies = [{ name = "colorama" },{ name = "docstring-parser" },{ name = "httpx" },+ { name = "ibm-watsonx-ai" },{ name = "jsonschema" },{ name = "mcp" },{ name = "openai" },@@ -591,6 +592,7 @@ all = [{ name = "googlemaps" },{ name = "gradio" },{ name = "html2text" },+ { name = "ibm-watsonx-ai" },{ name = "imageio", extra = ["pyav"] },{ name = "ipykernel" },{ name = "jupyter-client" },@@ -763,6 +765,7 @@ model-platforms = [{ name = "anthropic" },{ name = "cohere" },{ name = "fish-audio-sdk" },+ { name = "ibm-watsonx-ai" },{ name = "litellm" },{ name = "mistralai" },{ name = "reka-api" },@@ -969,6 +972,9 @@ requires-dist = [{ name = "html2text", marker = "extra == 'owl'", specifier = ">=2024.2.26" },{ name = "html2text", marker = "extra == 'web-tools'", specifier = ">=2024.2.26" },{ name = "httpx", specifier = ">=0.28.0,<1.0.0.dev0" },+ { name = "ibm-watsonx-ai", specifier = ">=1.3.11" },+ { name = "ibm-watsonx-ai", marker = "extra == 'all'", specifier = ">=1.3.11" },+ { name = "ibm-watsonx-ai", marker = "extra == 'model-platforms'", specifier = ">=1.3.11" },{ name = "imageio", extras = ["pyav"], marker = "extra == 'all'", specifier = ">=2.34.2,<3" },{ name = "imageio", extras = ["pyav"], marker = "extra == 'media-tools'", specifier = ">=2.34.2,<3" },{ name = "imageio", extras = ["pyav"], marker = "extra == 'owl'", specifier = ">=2.34.2,<3" },@@ -2742,6 +2748,58 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/48/30/47d0bf6072f7252e6521f3447ccfa40b421b6824517f82854703d0f5a98b/hyperframe-6.1.0-py3-none-any.whl", hash = "sha256:b03380493a519fce58ea5af42e4a42317bf9bd425596f7a0835ffce80f1a42e5", size = 13007 },]+[[package]]+name = "ibm-cos-sdk"+version = "2.13.5"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "ibm-cos-sdk-core" },+ { name = "ibm-cos-sdk-s3transfer" },+ { name = "jmespath" },+]+sdist = { url = "https://files.pythonhosted.org/packages/e4/e6/908791df6fd31d94e80be9597242eac2b942c28b0d4ea1fc6b963d4a6be3/ibm-cos-sdk-2.13.5.tar.gz", hash = "sha256:1aff7f9863ac9072a3db2f0053bec99478b26f3fb5fa797ce96a15bbb13cd40e", size = 58638 }++[[package]]+name = "ibm-cos-sdk-core"+version = "2.13.5"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "jmespath" },+ { name = "python-dateutil" },+ { name = "requests" },+ { name = "urllib3" },+]+sdist = { url = "https://files.pythonhosted.org/packages/c7/ff/cabdf3d7892c3607297ffb7d77c3caafe2f50f876d29e90c44ac67495cef/ibm-cos-sdk-core-2.13.5.tar.gz", hash = "sha256:d3a99d8b06b3f8c00b1a9501f85538d592463e63ddf8cec32672ab5a0b107b83", size = 1101815 }++[[package]]+name = "ibm-cos-sdk-s3transfer"+version = "2.13.5"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "ibm-cos-sdk-core" },+]+sdist = { url = "https://files.pythonhosted.org/packages/6d/42/f2faefd0c3360928336b93a14db2aff25f556aa50252188efd1ba363e371/ibm-cos-sdk-s3transfer-2.13.5.tar.gz", hash = "sha256:9649b1f2201c6de96ff5a6b5a3686de3a809e6ef3b8b12c7c4f2f7ce72da7749", size = 139491 }++[[package]]+name = "ibm-watsonx-ai"+version = "1.3.11"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "certifi" },+ { name = "httpx" },+ { name = "ibm-cos-sdk" },+ { name = "lomond" },+ { name = "packaging" },+ { name = "pandas" },+ { name = "requests" },+ { name = "tabulate" },+ { name = "urllib3" },+]+sdist = { url = "https://files.pythonhosted.org/packages/10/fd/f7ff11c084ff2af39214dd79531c63b4c798b98266e58126d999ff1fdeb9/ibm_watsonx_ai-1.3.11.tar.gz", hash = "sha256:36d4bf38af6795e3113d9e58d0d47d08fe8bc55347bfe9546820a4a09064981c", size = 714156 }+wheels = [+ { url = "https://files.pythonhosted.org/packages/40/af/28627792e9694403b0abb1823e2f17214c3cecd63d642535a1a5d9bef028/ibm_watsonx_ai-1.3.11-py3-none-any.whl", hash = "sha256:7d6571069d241856b6fa7854a786b5f13d5e769c3bc82681594b4ee3bc1c6577", size = 1115138 },+]+[[package]]name = "identify"version = "2.6.10"@@ -3257,6 +3315,18 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/ec/ee/745092a4acf931548eae4f442d366b5e0f7282fb4054d6989045c74bd2c2/litellm-1.67.0-py3-none-any.whl", hash = "sha256:d297126f45eea8d8a3df9c0de1d9491ff20e78dab5d1aa3820602082501ba89e", size = 7601690 },]+[[package]]+name = "lomond"+version = "0.3.3"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "six" },+]+sdist = { url = "https://files.pythonhosted.org/packages/c0/9e/ef7813c910d4a893f2bc763ce9246269f55cc68db21dc1327e376d6a2d02/lomond-0.3.3.tar.gz", hash = "sha256:427936596b144b4ec387ead99aac1560b77c8a78107d3d49415d3abbe79acbd3", size = 28789 }+wheels = [+ { url = "https://files.pythonhosted.org/packages/0f/b1/02eebed49c754b01b17de7705caa8c4ceecfb4f926cdafc220c863584360/lomond-0.3.3-py2.py3-none-any.whl", hash = "sha256:df1dd4dd7b802a12b71907ab1abb08b8ce9950195311207579379eb3b1553de7", size = 35512 },+]+[[package]]name = "lxml"version = "5.3.2"@@ -5202,11 +5272,11 @@ wheels = [[[package]]name = "pyjwt"-version = "2.10.1"+version = "2.9.0"source = { registry = "https://pypi.org/simple" }-sdist = { url = "https://files.pythonhosted.org/packages/e7/46/bd74733ff231675599650d3e47f361794b22ef3e3770998dda30d3b63726/pyjwt-2.10.1.tar.gz", hash = "sha256:3cc5772eb20009233caf06e9d8a0577824723b44e6648ee0a2aedb6cf9381953", size = 87785 }+sdist = { url = "https://files.pythonhosted.org/packages/fb/68/ce067f09fca4abeca8771fe667d89cc347d1e99da3e093112ac329c6020e/pyjwt-2.9.0.tar.gz", hash = "sha256:7e1e5b56cc735432a7369cbfa0efe50fa113ebecdc04ae6922deba8b84582d0c", size = 78825 }wheels = [- { url = "https://files.pythonhosted.org/packages/61/ad/689f02752eeec26aed679477e80e632ef1b682313be70793d798c1d5fc8f/PyJWT-2.10.1-py3-none-any.whl", hash = "sha256:dcdd193e30abefd5debf142f9adfcdd2b58004e644f25406ffaebd50bd98dacb", size = 22997 },+ { url = "https://files.pythonhosted.org/packages/79/84/0fdf9b18ba31d69877bd39c9cd6052b47f3761e9910c15de788e519f079f/PyJWT-2.9.0-py3-none-any.whl", hash = "sha256:3b02fb0f44517787776cf48f2ae25d8e14f300e6d7545a4315cee571a415e850", size = 22344 },][package.optional-dependencies]@@ -7433,11 +7503,11 @@ wheels = [[[package]]name = "urllib3"-version = "2.4.0"+version = "2.1.0"source = { registry = "https://pypi.org/simple" }-sdist = { url = "https://files.pythonhosted.org/packages/8a/78/16493d9c386d8e60e442a35feac5e00f0913c0f4b7c217c11e8ec2ff53e0/urllib3-2.4.0.tar.gz", hash = "sha256:414bc6535b787febd7567804cc015fee39daab8ad86268f1310a9250697de466", size = 390672 }+sdist = { url = "https://files.pythonhosted.org/packages/36/dd/a6b232f449e1bc71802a5b7950dc3675d32c6dbc2a1bd6d71f065551adb6/urllib3-2.1.0.tar.gz", hash = "sha256:df7aa8afb0148fa78488e7899b2c59b5f4ffcfa82e6c54ccb9dd37c1d7b52d54", size = 263900 }wheels = [- { url = "https://files.pythonhosted.org/packages/6b/11/cc635220681e93a0183390e26485430ca2c7b5f9d33b15c74c2861cb8091/urllib3-2.4.0-py3-none-any.whl", hash = "sha256:4e16665048960a0900c702d4a66415956a584919c03361cac9f1df5c5dd7e813", size = 128680 },+ { url = "https://files.pythonhosted.org/packages/96/94/c31f58c7a7f470d5665935262ebd7455c7e4c7782eb525658d3dbf4b9403/urllib3-2.1.0-py3-none-any.whl", hash = "sha256:55901e917a5896a349ff771be919f8bd99aff50b79fe58fec595eb37bbc56bb3", size = 104579 },][package.optional-dependencies]