Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

Created models subpackage (#167)

Dmitrii Khizbullin6a27e23127e7
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 1d9f90ae0..06e9a33b1 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -21,7 +21,7 @@ from tenacity.wait import wait_exponential
from camel.agents import BaseAgent
from camel.configs import ChatGPTConfig
from camel.messages import ChatMessage, MessageType, SystemMessage
-from camel.model_backend import ModelBackend, ModelFactory
+from camel.models import BaseModelBackend, ModelFactory
from camel.typing import ModelType, RoleType
from camel.utils import (
get_model_token_limit,
@@ -87,7 +87,7 @@ class ChatAgent(BaseAgent):
self.model_token_limit: int = get_model_token_limit(self.model)
self.message_window_size: Optional[int] = message_window_size
- self.model_backend: ModelBackend = ModelFactory.create(
+ self.model_backend: BaseModelBackend = ModelFactory.create(
self.model, self.model_config.__dict__)
self.terminated: bool = False
@@ -180,7 +180,7 @@ class ChatAgent(BaseAgent):
info: Dict[str, Any]
if num_tokens < self.model_token_limit:
- response = self.model_backend.run(messages=openai_messages)
+ response = self.model_backend.run(openai_messages)
if not isinstance(response, dict):
raise RuntimeError("OpenAI returned unexpected struct")
output_messages = [
diff --git a/camel/model_backend.py b/camel/model_backend.py
deleted file mode 100644
index b8ef5745a..000000000
--- a/camel/model_backend.py
+++ /dev/null
@@ -1,101 +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 abc import ABC, abstractmethod
-from typing import Any, Dict
-
-from camel.typing import ModelType
-
-
-class ModelBackend(ABC):
- r"""Base class for different model backends.
- May be OpenAI API, a local LLM, a stub for unit tests, etc."""
-
- @abstractmethod
- def run(self, *args, **kwargs) -> Dict[str, Any]:
- r"""Runs the query to the backend model.
-
- Raises:
- RuntimeError: if the return value from OpenAI API
- is not a dict that is expected.
-
- Returns:
- Dict[str, Any]: All backends must return a dict in OpenAI format.
- """
- pass
-
-
-class OpenAIModel(ModelBackend):
- r"""OpenAI API in a unified ModelBackend interface."""
-
- def __init__(self, model_type: ModelType, model_config_dict: Dict) -> None:
- super().__init__()
- self.model_type = model_type
- self.model_config_dict = model_config_dict
-
- def run(self, *args, **kwargs) -> Dict[str, Any]:
-
- import openai
- response = openai.ChatCompletion.create(*args, **kwargs,
- model=self.model_type.value,
- **self.model_config_dict)
- if not isinstance(response, Dict):
- raise RuntimeError("Unexpected return from OpenAI API")
- return response
-
-
-class StubModel(ModelBackend):
- r"""A dummy model used for unit tests."""
-
- def __init__(self, *args, **kwargs) -> None:
- super().__init__()
-
- def run(self, *args, **kwargs) -> Dict[str, Any]:
- ARBITRARY_STRING = "Lorem Ipsum"
-
- return dict(
- id="stub_model_id",
- usage=dict(),
- choices=[
- dict(finish_reason="stop",
- message=dict(content=ARBITRARY_STRING, role="assistant"))
- ],
- )
-
-
-class ModelFactory:
- r"""Factory of backend models.
-
- Raises:
- ValueError: in case the provided model type is unknown.
- """
-
- @staticmethod
- def create(model_type: ModelType, model_config_dict: Dict) -> ModelBackend:
- default_model_type = ModelType.GPT_3_5_TURBO
-
- if model_type in {
- ModelType.GPT_3_5_TURBO, ModelType.GPT_4, ModelType.GPT_4_32k,
- None
- }:
- model_class = OpenAIModel
- elif model_type == ModelType.STUB:
- model_class = StubModel
- else:
- raise ValueError("Unknown model")
-
- if model_type is None:
- model_type = default_model_type
-
- inst = model_class(model_type, model_config_dict)
- return inst
diff --git a/camel/models/__init__.py b/camel/models/__init__.py
new file mode 100644
index 000000000..da0ad1a6f
--- /dev/null
+++ b/camel/models/__init__.py
@@ -0,0 +1,24 @@
+# =========== 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 .base_model import BaseModelBackend
+from .openai_model import OpenAIModel
+from .stub_model import StubModel
+from .model_factory import ModelFactory
+
+__all__ = [
+ 'BaseModelBackend',
+ 'OpenAIModel',
+ 'StubModel',
+ 'ModelFactory',
+]
diff --git a/camel/models/base_model.py b/camel/models/base_model.py
new file mode 100644
index 000000000..cae0952d5
--- /dev/null
+++ b/camel/models/base_model.py
@@ -0,0 +1,37 @@
+# =========== 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 abc import ABC, abstractmethod
+from typing import Any, Dict, List
+
+
+class BaseModelBackend(ABC):
+ r"""Base class for different model backends.
+ May be OpenAI API, a local LLM, a stub for unit tests, etc."""
+
+ @abstractmethod
+ def run(self, messages: List[Dict]) -> Dict[str, Any]:
+ r"""Runs the query to the backend model.
+
+ Args:
+ messages (List[Dict]): message list with the chat history
+ in OpenAI API format.
+
+ Raises:
+ RuntimeError: if the return value from OpenAI API
+ is not a dict that is expected.
+
+ Returns:
+ Dict[str, Any]: All backends must return a dict in OpenAI format.
+ """
+ pass
diff --git a/camel/models/model_factory.py b/camel/models/model_factory.py
new file mode 100644
index 000000000..2ee0565fa
--- /dev/null
+++ b/camel/models/model_factory.py
@@ -0,0 +1,54 @@
+# =========== 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 Any, Dict
+
+from camel.models import BaseModelBackend, OpenAIModel, StubModel
+from camel.typing import ModelType
+
+
+class ModelFactory:
+ r"""Factory of backend models.
+
+ Raises:
+ ValueError: in case the provided model type is unknown.
+ """
+
+ @staticmethod
+ def create(model_type: ModelType,
+ model_config_dict: Dict) -> BaseModelBackend:
+ r"""Creates an instance of `BaseModelBackend` of the specified type.
+
+ Args:
+ model_type (ModelType): Model for which a backend is created.
+ model_config_dict (Dict): a dictionary that will be fed into
+ the backend constructor.
+
+ Raises:
+ ValueError: If there is not backend for the model.
+
+ Returns:
+ BaseModelBackend: The initialized backend.
+ """
+ model_class: Any
+ if model_type in {
+ ModelType.GPT_3_5_TURBO, ModelType.GPT_4, ModelType.GPT_4_32k
+ }:
+ model_class = OpenAIModel
+ elif model_type == ModelType.STUB:
+ model_class = StubModel
+ else:
+ raise ValueError("Unknown model")
+
+ inst = model_class(model_type, model_config_dict)
+ return inst
diff --git a/camel/models/openai_model.py b/camel/models/openai_model.py
new file mode 100644
index 000000000..e09f80911
--- /dev/null
+++ b/camel/models/openai_model.py
@@ -0,0 +1,55 @@
+# =========== 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 Any, Dict, List
+
+from camel.messages import OpenAIMessage
+from camel.models import BaseModelBackend
+from camel.typing import ModelType
+
+
+class OpenAIModel(BaseModelBackend):
+ r"""OpenAI API in a unified BaseModelBackend interface."""
+
+ def __init__(self, model_type: ModelType,
+ model_config_dict: Dict[str, Any]) -> None:
+ r"""Constructor for OpenAI backend.
+
+ Args:
+ model_type (ModelType): Model for which a backend is created,
+ one of GPT_* series.
+ model_config_dict (Dict[str, Any]): a dictionary that will
+ be fed into openai.ChatCompletion.create().
+ """
+ super().__init__()
+ self.model_type = model_type
+ self.model_config_dict = model_config_dict
+
+ def run(self, messages: List[Dict]) -> Dict[str, Any]:
+ r"""Run inference of OpenAI chat completion.
+
+ Args:
+ messages (List[Dict]): message list with the chat history
+ in OpenAI API format.
+
+ Returns:
+ Dict[str, Any]: Response in the OpenAI API format.
+ """
+ import openai
+ messages_openai: List[OpenAIMessage] = messages
+ response = openai.ChatCompletion.create(messages=messages_openai,
+ model=self.model_type.value,
+ **self.model_config_dict)
+ if not isinstance(response, Dict):
+ raise RuntimeError("Unexpected return from OpenAI API")
+ return response
diff --git a/camel/models/stub_model.py b/camel/models/stub_model.py
new file mode 100644
index 000000000..d8e760198
--- /dev/null
+++ b/camel/models/stub_model.py
@@ -0,0 +1,42 @@
+# =========== 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 Any, Dict, List
+
+from camel.models import BaseModelBackend
+
+
+class StubModel(BaseModelBackend):
+ r"""A dummy model used for unit tests."""
+
+ def __init__(self, *args, **kwargs) -> None:
+ r"""All arguments are unused for the dummy model."""
+ super().__init__()
+
+ def run(self, messages: List[Dict]) -> Dict[str, Any]:
+ r"""Run fake inference by returning a fixed string.
+ All arguments are unused for the dummy model.
+
+ Returns:
+ Dict[str, Any]: Response in the OpenAI API format.
+ """
+ ARBITRARY_STRING = "Lorem Ipsum"
+
+ return dict(
+ id="stub_model_id",
+ usage=dict(),
+ choices=[
+ dict(finish_reason="stop",
+ message=dict(content=ARBITRARY_STRING, role="assistant"))
+ ],
+ )
diff --git a/examples/test/test_ai_society_example.py b/examples/test/test_ai_society_example.py
index d5c024df4..9a023cf35 100644
--- a/examples/test/test_ai_society_example.py
+++ b/examples/test/test_ai_society_example.py
@@ -20,7 +20,3 @@ from camel.typing import ModelType
@pytest.mark.slow
def test_ai_society_role_playing_example():
examples.ai_society.role_playing.main(ModelType.STUB)
-
-
-if __name__ == "__main__":
- pytest.main()
diff --git a/test/test_model_backend.py b/test/models/test_model_factory.py
similarity index 93%
rename from test/test_model_backend.py
rename to test/models/test_model_factory.py
index 94f22d9d2..44aef8429 100644
--- a/test/test_model_backend.py
+++ b/test/models/test_model_factory.py
@@ -14,19 +14,18 @@
import pytest
from camel.configs import ChatGPTConfig
-from camel.model_backend import ModelFactory
+from camel.models import ModelFactory
from camel.typing import ModelType
parametrize = pytest.mark.parametrize('model', [
ModelType.STUB,
- pytest.param(None, marks=pytest.mark.model_backend),
pytest.param(ModelType.GPT_3_5_TURBO, marks=pytest.mark.model_backend),
pytest.param(ModelType.GPT_4, marks=pytest.mark.model_backend),
])
@parametrize
-def test_openai_model(model):
+def test_model_factory(model):
model_config_dict = ChatGPTConfig().__dict__
model_inst = ModelFactory.create(model, model_config_dict)
messages = [
@@ -45,7 +44,7 @@ def test_openai_model(model):
'Do not add anything else.')
},
]
- response = model_inst.run(messages=messages)
+ response = model_inst.run(messages)
assert isinstance(response, dict)
assert 'id' in response
assert isinstance(response['id'], str)