🐫 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: Openai compatibility model support (#815)
Wendong-Fan1a91fa62c44f
diff --git a/camel/models/__init__.py b/camel/models/__init__.pyindex 833217ce2..5525e11e1 100644--- a/camel/models/__init__.py+++ b/camel/models/__init__.py@@ -23,6 +23,7 @@ from .nemotron_model import NemotronModelfrom .ollama_model import OllamaModelfrom .open_source_model import OpenSourceModelfrom .openai_audio_models import OpenAIAudioModels+from .openai_compatibility_model import OpenAICompatibilityModelfrom .openai_model import OpenAIModelfrom .stub_model import StubModelfrom .vllm_model import VLLMModel@@ -45,4 +46,5 @@ __all__ = ['OllamaModel','VLLMModel','GeminiModel',+ 'OpenAICompatibilityModel',]diff --git a/camel/models/model_factory.py b/camel/models/model_factory.pyindex 5bac5f1c5..772bb37d1 100644--- a/camel/models/model_factory.py+++ b/camel/models/model_factory.py@@ -22,6 +22,7 @@ from camel.models.litellm_model import LiteLLMModelfrom camel.models.mistral_model import MistralModelfrom camel.models.ollama_model import OllamaModelfrom camel.models.open_source_model import OpenSourceModel+from camel.models.openai_compatibility_model import OpenAICompatibilityModelfrom camel.models.openai_model import OpenAIModelfrom camel.models.stub_model import StubModelfrom camel.models.vllm_model import VLLMModel@@ -105,11 +106,10 @@ class ModelFactory:)elif model_platform.is_vllm:model_class = VLLMModel- return model_class(- model_type, model_config_dict, url, api_key, token_counter- )elif model_platform.is_litellm:model_class = LiteLLMModel+ elif model_platform.is_openai_compatibility_model:+ model_class = OpenAICompatibilityModelelse:raise ValueError(f"Unknown pair of model platform `{model_platform}` "diff --git a/camel/models/openai_compatibility_model.py b/camel/models/openai_compatibility_model.pynew file mode 100644index 000000000..adadd24c1--- /dev/null+++ b/camel/models/openai_compatibility_model.py@@ -0,0 +1,105 @@+# =========== 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, Optional, Union++from openai import OpenAI, Stream++from camel.messages import OpenAIMessage+from camel.types import ChatCompletion, ChatCompletionChunk, ModelType+from camel.utils import (+ BaseTokenCounter,+ OpenAITokenCounter,+)+++class OpenAICompatibilityModel:+ r"""Constructor for model backend supporting OpenAI compatibility."""++ def __init__(+ self,+ model_type: str,+ model_config_dict: Dict[str, Any],+ api_key: str,+ url: str,+ token_counter: Optional[BaseTokenCounter] = None,+ ) -> None:+ r"""Constructor for model backend.++ Args:+ model_type (ModelType): Model for which a backend is created.+ model_config_dict (Dict[str, Any]): A dictionary that will+ be fed into openai.ChatCompletion.create().+ api_key (str): The API key for authenticating with the+ model service. (default: :obj:`None`)+ url (str): The url to the model service. (default:+ :obj:`None`)+ token_counter (Optional[BaseTokenCounter]): Token counter to use+ for the model. If not provided, `OpenAITokenCounter(ModelType.+ GPT_3_5_TURBO)` will be used.+ """+ self.model_type = model_type+ self.model_config_dict = model_config_dict+ self._token_counter = token_counter+ self._client = OpenAI(+ timeout=60,+ max_retries=3,+ api_key=api_key,+ base_url=url,+ )++ def run(+ self,+ messages: List[OpenAIMessage],+ ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:+ r"""Runs inference of OpenAI chat completion.++ Args:+ messages (List[OpenAIMessage]): Message list with the chat history+ in OpenAI API format.++ Returns:+ Union[ChatCompletion, Stream[ChatCompletionChunk]]:+ `ChatCompletion` in the non-stream mode, or+ `Stream[ChatCompletionChunk]` in the stream mode.+ """+ response = self._client.chat.completions.create(+ messages=messages,+ model=self.model_type,+ **self.model_config_dict,+ )+ return response++ @property+ def token_counter(self) -> BaseTokenCounter:+ r"""Initialize the token counter for the model backend.++ Returns:+ OpenAITokenCounter: The token counter following the model's+ tokenization style.+ """++ if not self._token_counter:+ self._token_counter = OpenAITokenCounter(ModelType.GPT_3_5_TURBO)+ return self._token_counter++ @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 self.model_config_dict.get('stream', False)diff --git a/camel/types/enums.py b/camel/types/enums.pyindex 0cbdef041..54847fedd 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -448,6 +448,7 @@ class ModelPlatformType(Enum):GEMINI = "gemini"VLLM = "vllm"MISTRAL = "mistral"+ OPENAICOMPATIBILITYMODEL = "openai-compatibility-model"@propertydef is_openai(self) -> bool:@@ -499,6 +500,12 @@ class ModelPlatformType(Enum):r"""Returns whether this platform is opensource."""return self is ModelPlatformType.OPENSOURCE+ @property+ def is_openai_compatibility_model(self) -> bool:+ r"""Returns whether this is a platform supporting openai+ compatibility"""+ return self is ModelPlatformType.OPENAICOMPATIBILITYMODEL+@propertydef is_gemini(self) -> bool:r"""Returns whether this platform is Gemini."""diff --git a/examples/models/ollama_model_example.py b/examples/models/ollama_model_example.pyindex b7d695ef1..b48c4f1ef 100644--- a/examples/models/ollama_model_example.py+++ b/examples/models/ollama_model_example.py@@ -19,7 +19,7 @@ from camel.types import ModelPlatformTypeollama_model = ModelFactory.create(model_platform=ModelPlatformType.OLLAMA,- model_type="camelai-llama3",+ model_type="llama3",url="http://localhost:11434/v1",model_config_dict={"temperature": 0.4},)diff --git a/examples/models/openai_compatibility_model_example.py b/examples/models/openai_compatibility_model_example.pynew file mode 100644index 000000000..4f1f1f00d--- /dev/null+++ b/examples/models/openai_compatibility_model_example.py@@ -0,0 +1,51 @@+# =========== 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 camel.agents import ChatAgent+from camel.messages import BaseMessage+from camel.models import ModelFactory+from camel.types import ModelPlatformType++# Take calling model from DashScope as an example+# Refer: https://dashscope.console.aliyun.com/overview+model = ModelFactory.create(+ model_platform=ModelPlatformType.OPENAICOMPATIBILITYMODEL,+ model_type="qwen-plus",+ api_key="sk-xxxx",+ url="https://dashscope.aliyuncs.com/compatible-mode/v1",+ model_config_dict={"temperature": 0.4},+)++assistant_sys_msg = BaseMessage.make_assistant_message(+ role_name="Assistant",+ content="You are a helpful assistant.",+)++agent = ChatAgent(assistant_sys_msg, model=model, token_limit=4096)++user_msg = BaseMessage.make_user_message(+ role_name="User",+ content="""Say hi to CAMEL AI, one open-source community+ dedicated to the study of autonomous and communicative agents.""",+)+assistant_response = agent.step(user_msg)+print(assistant_response.msg.content)++"""+===============================================================================+Hi to the CAMEL AI community! It's great to connect with an open-source+community focused on the study of autonomous and communicative agents. How can+I assist you or your projects today?+===============================================================================+"""