🐫 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: Support loading model configs from YAML and JSON files (#1961)
Saransh2f7c111f9d2a
diff --git a/camel/models/model_factory.py b/camel/models/model_factory.pyindex 91f580cfa..ff9d5f339 100644--- a/camel/models/model_factory.py+++ b/camel/models/model_factory.py@@ -11,8 +11,11 @@# See the License for the specific language governing permissions and# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+import jsonfrom typing import Dict, Optional, Type, Union+import yaml+from camel.models.aiml_model import AIMLModelfrom camel.models.anthropic_model import AnthropicModelfrom camel.models.azure_openai_model import AzureOpenAIModel@@ -165,3 +168,116 @@ class ModelFactory:token_counter=token_counter,timeout=timeout,)++ @classmethod+ def __parse_model_platform(+ cls, model_platform_str: str+ ) -> ModelPlatformType:+ r"""Parses a string and returns the corresponding ModelPlatformType+ enum.++ Args:+ model_platform_str (str): The platform name as a string. Can be in+ the form "ModelPlatformType.<NAME>" or simply "<NAME>".++ Returns:+ ModelPlatformType: The matching enum value.++ Raises:+ ValueError: If the platform name is not a valid member of+ ModelPlatformType.+ """++ try:+ if model_platform_str.startswith("ModelPlatformType."):+ platform_name = model_platform_str.split('.')[-1]+ else:+ platform_name = model_platform_str.upper()++ if platform_name not in ModelPlatformType.__members__:+ raise ValueError(+ f"Invalid model platform: {platform_name}. "+ f"Valid options: "+ f"{', '.join(ModelPlatformType.__members__.keys())}"+ )++ return ModelPlatformType[platform_name]++ except KeyError:+ raise KeyError(f"Invalid model platform: {model_platform_str}")++ @classmethod+ def __load_yaml(cls, filepath: str) -> Dict:+ r"""Loads and parses a YAML file into a dictionary.++ Args:+ filepath (str): Path to the YAML configuration file.++ Returns:+ Dict: The parsed YAML content as a dictionary.+ """+ with open(filepath, 'r') as file:+ config = yaml.safe_load(file)++ return config++ @classmethod+ def __load_json(cls, filepath: str) -> Dict:+ r"""Loads and parses a JSON file into a dictionary.++ Args:+ filepath (str): Path to the JSON configuration file.++ Returns:+ Dict: The parsed JSON content as a dictionary.+ """+ with open(filepath, 'r') as file:+ config = json.load(file)++ return config++ @classmethod+ def create_from_yaml(cls, filepath: str) -> BaseModelBackend:+ r"""Creates and returns a model base backend instance+ from a YAML configuration file.++ Args:+ filepath (str): Path to the YAML file containing model+ configuration.++ Returns:+ BaseModelBackend: An instance of the model backend based on the+ configuration.+ """++ config = cls.__load_yaml(filepath)+ config["model_platform"] = cls.__parse_model_platform(+ config["model_platform"]+ )++ model = ModelFactory.create(**config)++ return model++ @classmethod+ def create_from_json(cls, filepath: str) -> BaseModelBackend:+ r"""Creates and returns a base model backend instance+ from a JSON configuration file.++ Args:+ filepath (str): Path to the JSON file containing model+ configuration.++ Returns:+ BaseModelBackend: An instance of the model backend based on the+ configuration.+ """++ config = cls.__load_json(filepath)+ config["model_platform"] = cls.__parse_model_platform(+ config["model_platform"]+ )++ model = ModelFactory.create(**config)++ return modeldiff --git a/examples/models/config_files/config.json b/examples/models/config_files/config.jsonnew file mode 100644index 000000000..f63378734--- /dev/null+++ b/examples/models/config_files/config.json@@ -0,0 +1,10 @@+{+ "model_platform": "OPENAI_COMPATIBLE_MODEL",+ "model_type": "grok-beta",+ "api_key": "xai-...",+ "url": "https://api.x.ai/v1",+ "model_config_dict": {+ "max_tokens": 2000+ }+}+\ No newline at end of filediff --git a/examples/models/config_files/config.yaml b/examples/models/config_files/config.yamlnew file mode 100644index 000000000..4ce5829a9--- /dev/null+++ b/examples/models/config_files/config.yaml@@ -0,0 +1,6 @@+model_platform: OPENAI_COMPATIBLE_MODEL+model_type: grok-beta+api_key: xai-...+url: https://api.x.ai/v1+model_config_dict:+ max_tokens: 2000diff --git a/examples/models/model_manger.py b/examples/models/model_manger.pyindex 0ce9ec6e4..9a55ebaa0 100644--- a/examples/models/model_manger.py+++ b/examples/models/model_manger.py@@ -61,6 +61,34 @@ user_msg = """What is the meaning of life, the universe, and everything?"""assistant_response = agent.step(user_msg)print(assistant_response.msg.content)++# Creating a model instance by loading model configs from a JSON file.+model_inst_from_json = ModelFactory.create_from_json(+ "config_files/config.json"+)++# Using the same system message and user message.+agent_1 = ChatAgent(+ system_message=assistant_sys_msg, model=model_inst_from_json+)++agent_1_response = agent.step(user_msg)+print(agent_1_response.msg.content)+++# Creating a model instance by loading model configs fomr a YAML file.+model_inst_from_yaml = ModelFactory.create_from_yaml(+ "config_files/config.yaml"+)++agent_2 = ChatAgent(+ system_message=assistant_sys_msg, model=model_inst_from_yaml+)++agent_2_response = agent_2.step(user_msg)+print(agent_2_response.msg.content)++"""===============================================================================The phrase "the meaning of life, the universe, and everything" is famouslydiff --git a/test/models/test_config_file/json_configs/claude2_config.json b/test/models/test_config_file/json_configs/claude2_config.jsonnew file mode 100644index 000000000..755bde69c--- /dev/null+++ b/test/models/test_config_file/json_configs/claude2_config.json@@ -0,0 +1,11 @@+{+ "model_platform": "anthropic",+ "model_type": "claude-2.0",+ "model_config_dict": {+ "temperature": 0.6,+ "max_tokens": 2000+ },+ "token_counter": null,+ "api_key": "your_anthropic_api_key",+ "url": "https://api.anthropic.com/v1"+}diff --git a/test/models/test_config_file/json_configs/claude2_openai_token_counter.json b/test/models/test_config_file/json_configs/claude2_openai_token_counter.jsonnew file mode 100644index 000000000..2430ca7f2--- /dev/null+++ b/test/models/test_config_file/json_configs/claude2_openai_token_counter.json@@ -0,0 +1,14 @@+{+ "model_platform": "anthropic",+ "model_type": "claude-2.0",+ "model_config_dict": {+ "temperature": 0.6,+ "max_tokens": 2000+ },+ "token_counter": {+ "type": "OpenAITokenCounter",+ "model_type": "gpt-3.5-turbo"+ },+ "api_key": "your_anthropic_api_key",+ "url": "https://api.anthropic.com/v1"+}diff --git a/test/models/test_config_file/json_configs/gemini_1.5_flash_config.json b/test/models/test_config_file/json_configs/gemini_1.5_flash_config.jsonnew file mode 100644index 000000000..dc6c08644--- /dev/null+++ b/test/models/test_config_file/json_configs/gemini_1.5_flash_config.json@@ -0,0 +1,14 @@+{+ "model_platform": "gemini",+ "model_type": "gemini-1.5-flash",+ "model_config_dict": {+ "temperature": 0.6,+ "max_tokens": 2500+ },+ "token_counter": {+ "type": "OpenAITokenCounter",+ "model_type": "gpt-4o-mini"+ },+ "api_key": "your_gemini_api_key",+ "url": "https://api.gemini.com/v1"+}diff --git a/test/models/test_config_file/json_configs/ollama_config.json b/test/models/test_config_file/json_configs/ollama_config.jsonnew file mode 100644index 000000000..fe4dfe4f6--- /dev/null+++ b/test/models/test_config_file/json_configs/ollama_config.json@@ -0,0 +1,11 @@+{+ "model_platform": "ollama",+ "model_type": "llama3.2",+ "model_config_dict": {+ "temperature": 0.5,+ "top_p": 0.9+ },+ "token_counter": null,+ "api_key": null,+ "url": null+}diff --git a/test/models/test_config_file/json_configs/ollama_openai_token_counter.json b/test/models/test_config_file/json_configs/ollama_openai_token_counter.jsonnew file mode 100644index 000000000..22b7546eb--- /dev/null+++ b/test/models/test_config_file/json_configs/ollama_openai_token_counter.json@@ -0,0 +1,14 @@+{+ "model_platform": "ollama",+ "model_type": "llama3.2",+ "model_config_dict": {+ "temperature": 0.5,+ "top_p": 0.9+ },+ "token_counter": {+ "type": "OpenAITokenCounter",+ "model_type": "gpt-4o-mini"+ },+ "api_key": null,+ "url": null+}diff --git a/test/models/test_config_file/json_configs/openai3.5_turbo_config.json b/test/models/test_config_file/json_configs/openai3.5_turbo_config.jsonnew file mode 100644index 000000000..9bb4c7a94--- /dev/null+++ b/test/models/test_config_file/json_configs/openai3.5_turbo_config.json@@ -0,0 +1,8 @@+{+ "model_platform": "OPENAI",+ "model_type": "GPT_3_5_TURBO",+ "model_config_dict": {},+ "token_counter": "OpenAITokenCounter",+ "api_key": null,+ "url": null+}\ No newline at end of filediff --git a/test/models/test_config_file/json_configs/openai4o_config.json b/test/models/test_config_file/json_configs/openai4o_config.jsonnew file mode 100644index 000000000..cdb80d6bc--- /dev/null+++ b/test/models/test_config_file/json_configs/openai4o_config.json@@ -0,0 +1,11 @@+{+ "model_platform": "openai",+ "model_type": "gpt-4o-mini",+ "model_config_dict": {+ "temperature": 0.7,+ "max_tokens": 1500+ },+ "token_counter": null,+ "api_key": "your_openai_api_key",+ "url": "https://api.openai.com/v1"+}diff --git a/test/models/test_config_file/yaml_configs/claude2_config.yaml b/test/models/test_config_file/yaml_configs/claude2_config.yamlnew file mode 100644index 000000000..5b65d67cb--- /dev/null+++ b/test/models/test_config_file/yaml_configs/claude2_config.yaml@@ -0,0 +1,9 @@+model_platform: ANTHROPIC+model_type: CLAUDE_2_0+model_config_dict:+ temperature: 0.7+ top_p: 0.9+ max_tokens: 1024+token_counter: null+api_key: test_key+url: nulldiff --git a/test/models/test_config_file/yaml_configs/claude2_openai_config.yaml b/test/models/test_config_file/yaml_configs/claude2_openai_config.yamlnew file mode 100644index 000000000..0d3ff5c9c--- /dev/null+++ b/test/models/test_config_file/yaml_configs/claude2_openai_config.yaml@@ -0,0 +1,9 @@+model_platform: ANTHROPIC+model_type: CLAUDE_2_0+model_config_dict:+ temperature: 0.7+ top_p: 0.9+ max_tokens: 1024+token_counter: OpenAITokenCounter+api_key: test_key+url: nulldiff --git a/test/models/test_config_file/yaml_configs/gemini_1.5_flash_config.yaml b/test/models/test_config_file/yaml_configs/gemini_1.5_flash_config.yamlnew file mode 100644index 000000000..45c360ce1--- /dev/null+++ b/test/models/test_config_file/yaml_configs/gemini_1.5_flash_config.yaml@@ -0,0 +1,9 @@+model_platform: GEMINI+model_type: GEMINI_1_5_FLASH+model_config_dict:+ temperature: 0.6+ max_tokens: 1024+token_counter: OpenAITokenCounter+api_key: test_key+url: null+diff --git a/test/models/test_config_file/yaml_configs/ollama_config.yaml b/test/models/test_config_file/yaml_configs/ollama_config.yamlnew file mode 100644index 000000000..4d0c29b2e--- /dev/null+++ b/test/models/test_config_file/yaml_configs/ollama_config.yaml@@ -0,0 +1,9 @@+model_platform: OLLAMA+model_type: llama3.2+model_config_dict:+ temperature: 0.7+ max_tokens: 1024+token_counter: null+api_key: test_key+url: null+diff --git a/test/models/test_config_file/yaml_configs/ollama_openai_config.yaml b/test/models/test_config_file/yaml_configs/ollama_openai_config.yamlnew file mode 100644index 000000000..182f7f4fc--- /dev/null+++ b/test/models/test_config_file/yaml_configs/ollama_openai_config.yaml@@ -0,0 +1,9 @@+model_platform: OLLAMA+model_type: llama3.2+model_config_dict:+ temperature: 0.7+ max_tokens: 1024+token_counter: OpenAITokenCounter+api_key: test_key+url: null+diff --git a/test/models/test_config_file/yaml_configs/openai3.5_turbo_config.yaml b/test/models/test_config_file/yaml_configs/openai3.5_turbo_config.yamlnew file mode 100644index 000000000..e63cec6ae--- /dev/null+++ b/test/models/test_config_file/yaml_configs/openai3.5_turbo_config.yaml@@ -0,0 +1,9 @@+model_platform: OPENAI+model_type: GPT_3_5_TURBO+model_config_dict:+ temperature: 0.7+ max_tokens: 1024+token_counter: null+api_key: test_key+url: null+diff --git a/test/models/test_config_file/yaml_configs/openai4o_config.yaml b/test/models/test_config_file/yaml_configs/openai4o_config.yamlnew file mode 100644index 000000000..4fea8b918--- /dev/null+++ b/test/models/test_config_file/yaml_configs/openai4o_config.yaml@@ -0,0 +1,9 @@+model_platform: OPENAI+model_type: GPT_4O+model_config_dict:+ temperature: 0.7+ max_tokens: 1024+token_counter: null+api_key: test_key+url: null+diff --git a/test/models/test_model_factory.py b/test/models/test_model_factory.pyindex 58f4a6fed..a1c296816 100644--- a/test/models/test_model_factory.py+++ b/test/models/test_model_factory.py@@ -11,6 +11,10 @@# 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 pathlib import Path+from unittest.mock import patch+import pytestfrom camel.configs import (@@ -158,3 +162,109 @@ def test_model_factory(model_platform, model_type):assert 'role' in messageassert isinstance(message['role'], str)assert message['role'] == 'assistant'+++json_config_files = [+ "test_config_file/json_configs/claude2_config.json",+ "test_config_file/json_configs/claude2_openai_token_counter.json",+ "test_config_file/json_configs/gemini_1.5_flash_config.json",+ "test_config_file/json_configs/ollama_config.json",+ "test_config_file/json_configs/ollama_openai_token_counter.json",+ "test_config_file/json_configs/openai3.5_turbo_config.json",+ "test_config_file/json_configs/openai4o_config.json",+]++[email protected]("config_file", json_config_files)[email protected](os.environ, {"OPENAI_API_KEY": "test_key"})+@patch("camel.models.base_model.BaseModelBackend.run")+def test_model_factory_json(mock_run, config_file):+ r"""Test ModelFactory with JSON files without actual API calls."""++ # Get the directory of the current test file+ test_dir = Path(__file__).parent+ json_path = test_dir / config_file+ if not json_path.exists():+ pytest.skip(f"Skipping file {json_path}")++ mock_run.return_value = {+ "id": "test_response",+ "usage": {"total_tokens": 10},+ "choices": [+ {+ "finish_reason": "stop",+ "message": {"role": "assistant", "content": "Hello, test!"},+ }+ ],+ }++ model_inst = ModelFactory.create_from_json(str(json_path))++ messages = [+ {"role": "system", "content": "Initialize system"},+ {"role": "user", "content": "Hello"},+ ]++ response = model_inst.run(messages)++ assert isinstance(response, dict)+ assert response["id"] == "test_response"+ assert "usage" in response+ assert "choices" in response+ assert response["choices"][0]["message"]["role"] == "assistant"+ assert response["choices"][0]["message"]["content"] == "Hello, test!"++ print(f"JSON file {config_file} loaded successfully.")+++yaml_config_files = [+ "test_config_file/yaml_configs/claude2_config.yaml",+ "test_config_file/yaml_configs/claude2_openai_config.yaml",+ "test_config_file/yaml_configs/gemini_1.5_flash_config.yaml",+ "test_config_file/yaml_configs/ollama_config.yaml",+ "test_config_file/yaml_configs/ollama_openai_config.yaml",+ "test_config_file/yaml_configs/openai3.5_turbo_config.yaml",+ "test_config_file/yaml_configs/openai4o_config.yaml",+]++[email protected]("config_file", yaml_config_files)[email protected](os.environ, {"OPENAI_API_KEY": "test_key"})+@patch("camel.models.base_model.BaseModelBackend.run")+def test_model_factory_yaml(mock_run, config_file):+ r"""Test ModelFactory with YAML files without real API calls."""++ # Get the directory of the current test file+ test_dir = Path(__file__).parent+ yaml_path = test_dir / config_file+ if not yaml_path.exists():+ pytest.skip(f"Skipping file {yaml_path}")++ mock_run.return_value = {+ "id": "test_response",+ "usage": {"total_tokens": 10},+ "choices": [+ {+ "finish_reason": "stop",+ "message": {"role": "assistant", "content": "Hello, test!"},+ }+ ],+ }++ model_inst = ModelFactory.create_from_yaml(str(yaml_path))++ messages = [+ {"role": "system", "content": "Initialize system"},+ {"role": "user", "content": "Hello"},+ ]++ response = model_inst.run(messages)++ assert isinstance(response, dict)+ assert response["id"] == "test_response"+ assert "usage" in response+ assert "choices" in response+ assert response["choices"][0]["message"]["role"] == "assistant"+ assert response["choices"][0]["message"]["content"] == "Hello, test!"++ print(f" YAML file {config_file} loaded successfully.")