🐫 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:Update all model implementation by inheriting from OpenAICompatibleModel (#2195)
Tao Sun4c0fc0604a4d
diff --git a/camel/models/aiml_model.py b/camel/models/aiml_model.pyindex 3e62cad0b..91b48ce16 100644--- a/camel/models/aiml_model.py+++ b/camel/models/aiml_model.py@@ -12,29 +12,19 @@# 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 openai import AsyncOpenAI, AsyncStream, OpenAI, Stream-from pydantic import BaseModel+from typing import Any, Dict, Optional, Unionfrom camel.configs import AIML_API_PARAMS, AIMLConfig-from camel.messages import OpenAIMessage-from camel.models._utils import try_modify_message_with_format-from camel.models.base_model import BaseModelBackend-from camel.types import (- ChatCompletion,- ChatCompletionChunk,- ModelType,-)+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelTypefrom camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class AIMLModel(BaseModelBackend):- r"""AIML API in a unified BaseModelBackend interface.+class AIMLModel(OpenAICompatibleModel):+ r"""AIML API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -77,87 +67,14 @@ class AIMLModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout- )- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- def _prepare_request(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Dict[str, Any]:- request_config = self.model_config_dict.copy()- if tools:- request_config["tools"] = tools- if response_format:- # AIML API does not natively support response format- try_modify_message_with_format(messages[-1], response_format)- return request_config-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- r"""Runs inference of AIML 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.- """- request_config = self._prepare_request(- messages, response_format, tools+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- response = self._client.chat.completions.create(- messages=messages, model=self.model_type, **request_config- )- return response-- async def _arun(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- request_config = self._prepare_request(- messages, response_format, tools- )- response = await self._async_client.chat.completions.create(- messages=messages, model=self.model_type, **request_config- )- return response-- @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(ModelType.GPT_4O_MINI)- return self._token_counter-def check_model_config(self):r"""Check whether the model configuration contains anyunexpected arguments to AIML API.@@ -172,13 +89,3 @@ class AIMLModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into AIML model backend.")-- @property- def stream(self) -> bool:- """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/models/anthropic_model.py b/camel/models/anthropic_model.pyindex 76aac1328..c658284e1 100644--- a/camel/models/anthropic_model.py+++ b/camel/models/anthropic_model.py@@ -12,14 +12,11 @@# 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 typing import Any, Dict, Optional, Unionfrom camel.configs import ANTHROPIC_API_PARAMS, AnthropicConfig-from camel.messages import OpenAIMessage-from camel.models.base_model import BaseModelBackend-from camel.types import ChatCompletion, ModelType+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelTypefrom camel.utils import (AnthropicTokenCounter,BaseTokenCounter,@@ -28,8 +25,8 @@ from camel.utils import ()-class AnthropicModel(BaseModelBackend):- r"""Anthropic API in a unified BaseModelBackend interface.+class AnthropicModel(OpenAICompatibleModel):+ r"""Anthropic API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -66,8 +63,6 @@ class AnthropicModel(BaseModelBackend):token_counter: Optional[BaseTokenCounter] = None,timeout: Optional[float] = None,) -> None:- from openai import AsyncOpenAI, OpenAI-if model_config_dict is None:model_config_dict = AnthropicConfig().as_dict()api_key = api_key or os.environ.get("ANTHROPIC_API_KEY")@@ -78,14 +73,12 @@ class AnthropicModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout- )- self.client = OpenAI(- base_url=self._url, api_key=self._api_key, timeout=self._timeout- )-- self.async_client = AsyncOpenAI(- api_key=self._api_key, base_url=self._url, timeout=self._timeout+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)@property@@ -100,54 +93,6 @@ class AnthropicModel(BaseModelBackend):self._token_counter = AnthropicTokenCounter(self.model_type)return self._token_counter- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ):- r"""Run inference of Anthropic chat completion.-- Args:- messages (List[OpenAIMessage]): Message list with the chat history- in OpenAI API format.-- Returns:- ChatCompletion: Response in the OpenAI API format.- """- response = self.client.chat.completions.create(- model=self.model_type,- messages=messages,- **self.model_config_dict,- tools=tools, # type: ignore[arg-type]- )-- return response-- async def _arun(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> ChatCompletion:- r"""Run inference of Anthropic chat completion.-- Args:- messages (List[OpenAIMessage]): Message list with the chat history- in OpenAI API format.-- Returns:- ChatCompletion: Response in the OpenAI API format.- """- response = await self.async_client.chat.completions.create(- model=self.model_type,- messages=messages,- **self.model_config_dict,- tools=tools, # type: ignore[arg-type]- )-- return response-def check_model_config(self):r"""Check whether the model configuration is valid for anthropicmodel backends.@@ -162,13 +107,3 @@ class AnthropicModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into Anthropic 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 self.model_config_dict.get("stream", False)diff --git a/camel/models/deepseek_model.py b/camel/models/deepseek_model.pyindex aa1d837f0..4123e7377 100644--- a/camel/models/deepseek_model.py+++ b/camel/models/deepseek_model.py@@ -15,20 +15,20 @@import osfrom typing import Any, Dict, List, Optional, Type, Union-from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream+from openai import AsyncStream, Streamfrom pydantic import BaseModelfrom camel.configs import DEEPSEEK_API_PARAMS, DeepSeekConfigfrom camel.logger import get_loggerfrom camel.messages import OpenAIMessagefrom camel.models._utils import try_modify_message_with_format-from camel.models.base_model import BaseModelBackend+from camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.types import (ChatCompletion,ChatCompletionChunk,ModelType,)-from camel.utils import BaseTokenCounter, OpenAITokenCounter, api_keys_required+from camel.utils import BaseTokenCounter, api_keys_requiredlogger = get_logger(__name__)@@ -43,8 +43,8 @@ REASONSER_UNSUPPORTED_PARAMS = []-class DeepSeekModel(BaseModelBackend):- r"""DeepSeek API in a unified BaseModelBackend interface.+class DeepSeekModel(OpenAICompatibleModel):+ r"""DeepSeek API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -92,37 +92,14 @@ class DeepSeekModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- @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],@@ -270,13 +247,3 @@ class DeepSeekModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into DeepSeek 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 self.model_config_dict.get("stream", False)diff --git a/camel/models/gemini_model.py b/camel/models/gemini_model.pyindex 87328b8de..5a643caa1 100644--- a/camel/models/gemini_model.py+++ b/camel/models/gemini_model.py@@ -14,12 +14,12 @@import osfrom typing import Any, Dict, List, Optional, Type, Union-from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream+from openai import AsyncStream, Streamfrom pydantic import BaseModelfrom camel.configs import Gemini_API_PARAMS, GeminiConfigfrom camel.messages import OpenAIMessage-from camel.models import BaseModelBackend+from camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.types import (ChatCompletion,ChatCompletionChunk,@@ -27,13 +27,12 @@ from camel.types import ()from camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class GeminiModel(BaseModelBackend):- r"""Gemini API in a unified BaseModelBackend interface.+class GeminiModel(OpenAICompatibleModel):+ r"""Gemini API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -80,19 +79,12 @@ class GeminiModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout- )- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)def _process_messages(self, messages) -> List[OpenAIMessage]:@@ -247,50 +239,6 @@ class GeminiModel(BaseModelBackend):**request_config,)- def _request_parse(- self,- messages: List[OpenAIMessage],- response_format: Type[BaseModel],- ) -> ChatCompletion:- request_config = self.model_config_dict.copy()-- request_config["response_format"] = response_format- request_config.pop("stream", None)-- return self._client.beta.chat.completions.parse(- messages=messages,- model=self.model_type,- **request_config,- )-- async def _arequest_parse(- self,- messages: List[OpenAIMessage],- response_format: Type[BaseModel],- ) -> ChatCompletion:- request_config = self.model_config_dict.copy()-- request_config["response_format"] = response_format- request_config.pop("stream", None)-- return await self._async_client.beta.chat.completions.parse(- messages=messages,- model=self.model_type,- **request_config,- )-- @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(ModelType.GPT_4O_MINI)- return self._token_counter-def check_model_config(self):r"""Check whether the model configuration contains anyunexpected arguments to Gemini API.@@ -305,13 +253,3 @@ class GeminiModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into Gemini 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 self.model_config_dict.get('stream', False)diff --git a/camel/models/groq_model.py b/camel/models/groq_model.pyindex ac6edcaa7..9790c3eea 100644--- a/camel/models/groq_model.py+++ b/camel/models/groq_model.py@@ -14,13 +14,13 @@import osfrom typing import Any, Dict, List, Optional, Type, Union-from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream+from openai import AsyncStream, Streamfrom pydantic import BaseModelfrom camel.configs import GROQ_API_PARAMS, GroqConfigfrom camel.messages import OpenAIMessage-from camel.models import BaseModelBackendfrom camel.models._utils import try_modify_message_with_format+from camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.types import (ChatCompletion,ChatCompletionChunk,@@ -28,13 +28,12 @@ from camel.types import ()from camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class GroqModel(BaseModelBackend):- r"""LLM API served by Groq in a unified BaseModelBackend interface.+class GroqModel(OpenAICompatibleModel):+ r"""LLM API served by Groq in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -75,32 +74,13 @@ class GroqModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- @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(ModelType.GPT_4O_MINI)- return self._token_counterdef _prepare_request(self,@@ -198,13 +178,3 @@ class GroqModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into Groq 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 self.model_config_dict.get("stream", False)diff --git a/camel/models/internlm_model.py b/camel/models/internlm_model.pyindex 8227ba419..6ee2fd42a 100644--- a/camel/models/internlm_model.py+++ b/camel/models/internlm_model.py@@ -15,12 +15,12 @@import osfrom typing import Any, Dict, List, Optional, Type, Union-from openai import AsyncStream, OpenAI, Stream+from openai import AsyncStreamfrom pydantic import BaseModelfrom camel.configs import INTERNLM_API_PARAMS, InternLMConfigfrom camel.messages import OpenAIMessage-from camel.models import BaseModelBackend+from camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.types import (ChatCompletion,ChatCompletionChunk,@@ -28,13 +28,12 @@ from camel.types import ()from camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class InternLMModel(BaseModelBackend):- r"""InternLM API in a unified BaseModelBackend interface.+class InternLMModel(OpenAICompatibleModel):+ r"""InternLM API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -71,8 +70,7 @@ class InternLMModel(BaseModelBackend):token_counter: Optional[BaseTokenCounter] = None,timeout: Optional[float] = None,) -> None:- if model_config_dict is None:- model_config_dict = InternLMConfig().as_dict()+ self.model_config = model_config_dict or InternLMConfig().as_dict()api_key = api_key or os.environ.get("INTERNLM_API_KEY")url = url or os.environ.get("INTERNLM_API_BASE_URL",@@ -80,39 +78,14 @@ class InternLMModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout- )- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,+ model_type=model_type,+ model_config_dict=self.model_config,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- r"""Runs inference of InternLM 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-async def _arun(self,messages: List[OpenAIMessage],@@ -121,19 +94,6 @@ class InternLMModel(BaseModelBackend):) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:raise NotImplementedError("InternLM does not support async inference.")- @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_4O_MINI)- return self._token_counter-def check_model_config(self):r"""Check whether the model configuration contains anyunexpected arguments to InternLM API.@@ -148,13 +108,3 @@ class InternLMModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into InternLM 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 self.model_config_dict.get('stream', False)diff --git a/camel/models/modelscope_model.py b/camel/models/modelscope_model.pyindex c025ec07d..727742228 100644--- a/camel/models/modelscope_model.py+++ b/camel/models/modelscope_model.py@@ -13,29 +13,19 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import os-from typing import Any, Dict, List, Optional, Type, Union--from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream-from pydantic import BaseModel+from typing import Any, Dict, Optional, Unionfrom camel.configs import MODELSCOPE_API_PARAMS, ModelScopeConfig-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,- ChatCompletionChunk,- ModelType,-)+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelTypefrom camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class ModelScopeModel(BaseModelBackend):- r"""ModelScope API in a unified BaseModelBackend interface.+class ModelScopeModel(OpenAICompatibleModel):+ r"""ModelScope API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -83,104 +73,13 @@ class ModelScopeModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout- )- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- async def _arun(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- r"""Runs inference of ModelScope chat completion.-- Args:- messages (List[OpenAIMessage]): Message list with the chat history- in OpenAI API format.-- Returns:- Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- `ChatCompletion` in the non-stream mode, or- `AsyncStream[ChatCompletionChunk]` in the stream mode.- """- request_config = self._prepare_request(- messages, response_format, tools- )-- response = await self._async_client.chat.completions.create(- messages=messages,- model=self.model_type,- **request_config,- )- return response-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- r"""Runs inference of ModelScope 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.- """- request_config = self._prepare_request(- messages, response_format, tools- )-- response = self._client.chat.completions.create(- messages=messages,- model=self.model_type,- **request_config,+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- return response-- def _prepare_request(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Dict[str, Any]:- request_config = self.model_config_dict.copy()- 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-- @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_4O_MINI)- return self._token_counterdef check_model_config(self):r"""Check whether the model configuration contains any@@ -196,13 +95,3 @@ class ModelScopeModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into ModelScope 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 self.model_config_dict.get('stream', False)diff --git a/camel/models/moonshot_model.py b/camel/models/moonshot_model.pyindex 05526397c..d41dd500e 100644--- a/camel/models/moonshot_model.py+++ b/camel/models/moonshot_model.py@@ -15,13 +15,12 @@import osfrom typing import Any, Dict, List, Optional, Type, Union-from openai import AsyncStream, OpenAI, Stream+from openai import AsyncStreamfrom pydantic import BaseModelfrom camel.configs import MOONSHOT_API_PARAMS, MoonshotConfigfrom camel.messages import OpenAIMessage-from camel.models import BaseModelBackend-from camel.models._utils import try_modify_message_with_format+from camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.types import (ChatCompletion,ChatCompletionChunk,@@ -29,13 +28,12 @@ from camel.types import ()from camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class MoonshotModel(BaseModelBackend):- r"""Moonshot API in a unified BaseModelBackend interface.+class MoonshotModel(OpenAICompatibleModel):+ r"""Moonshot API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -77,55 +75,13 @@ class MoonshotModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- self._client = OpenAI(- api_key=self._api_key,- timeout=self._timeout,- max_retries=3,- base_url=self._url,- )-- def _prepare_request(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Dict[str, Any]:- request_config = self.model_config_dict.copy()- if tools:- request_config["tools"] = tools- elif response_format:- try_modify_message_with_format(messages[-1], response_format)- return request_config-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- r"""Runs inference of Moonshot 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.- """- request_config = self._prepare_request(- messages, response_format, tools- )-- response = self._client.chat.completions.create(- messages=messages,- model=self.model_type,- **request_config,- )- return responseasync def _arun(self,@@ -135,18 +91,6 @@ class MoonshotModel(BaseModelBackend):) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:raise NotImplementedError("Moonshot does not support async inference.")- @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_4O_MINI)- return self._token_counter-def check_model_config(self):r"""Check whether the model configuration contains anyunexpected arguments to Moonshot API.@@ -161,13 +105,3 @@ class MoonshotModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into Moonshot 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 self.model_config_dict.get('stream', False)diff --git a/camel/models/nemotron_model.py b/camel/models/nemotron_model.pyindex 2c5e94d1b..1c66cec33 100644--- a/camel/models/nemotron_model.py+++ b/camel/models/nemotron_model.py@@ -12,21 +12,17 @@# 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 typing import Optional, Union-from openai import AsyncOpenAI, OpenAI-from pydantic import BaseModel--from camel.messages import OpenAIMessage-from camel.models import BaseModelBackend-from camel.types import ChatCompletion, ModelType+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelTypefrom camel.utils import (BaseTokenCounter,api_keys_required,)-class NemotronModel(BaseModelBackend):+class NemotronModel(OpenAICompatibleModel):r"""Nemotron model API backend with OpenAI compatibility.Args:@@ -63,58 +59,6 @@ class NemotronModel(BaseModelBackend):api_key = api_key or os.environ.get("NVIDIA_API_KEY")timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(model_type, {}, api_key, url, None, timeout)- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- base_url=self._url,- api_key=self._api_key,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- base_url=self._url,- api_key=self._api_key,- )-- 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 OpenAI chat completion asynchronously.-- Args:- messages (List[OpenAIMessage]): Message list.-- Returns:- ChatCompletion.- """- response = await self._async_client.chat.completions.create(- messages=messages,- model=self.model_type,- )- return response-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> ChatCompletion:- r"""Runs inference of OpenAI chat completion.-- Args:- messages (List[OpenAIMessage]): Message list.-- Returns:- ChatCompletion.- """- response = self._client.chat.completions.create(- messages=messages,- model=self.model_type,- )- return response@propertydef token_counter(self) -> BaseTokenCounter:diff --git a/camel/models/nvidia_model.py b/camel/models/nvidia_model.pyindex 09193b26b..3d162d40e 100644--- a/camel/models/nvidia_model.py+++ b/camel/models/nvidia_model.py@@ -13,24 +13,16 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import os-from typing import Any, Dict, List, Optional, Type, Union--from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream-from openai.types.chat import (- ChatCompletion,- ChatCompletionChunk,-)-from pydantic import BaseModel+from typing import Any, Dict, Optional, Unionfrom camel.configs import NVIDIA_API_PARAMS, NvidiaConfig-from camel.messages import OpenAIMessage-from camel.models import BaseModelBackend+from camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.types import ModelType-from camel.utils import BaseTokenCounter, OpenAITokenCounter, api_keys_required+from camel.utils import BaseTokenCounter, api_keys_required-class NvidiaModel(BaseModelBackend):- r"""NVIDIA API in a unified BaseModelBackend interface.+class NvidiaModel(OpenAICompatibleModel):+ r"""NVIDIA API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -75,95 +67,13 @@ class NvidiaModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout- )- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- async def _arun(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- r"""Runs inference of NVIDIA chat completion.-- Args:- messages (List[OpenAIMessage]): Message list with the chat history- in OpenAI API format.-- Returns:- Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- `ChatCompletion` in the non-stream mode, or- `AsyncStream[ChatCompletionChunk]` in the stream mode.- """-- # Remove tool-related parameters if no tools are specified- config = dict(self.model_config_dict)- if not config.get("tools"): # None or empty list- config.pop("tools", None)- config.pop("tool_choice", None)-- response = await self._async_client.chat.completions.create(- messages=messages,- model=self.model_type,- **config,- )- return response-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- r"""Runs inference of NVIDIA 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.- """-- # Remove tool-related parameters if no tools are specified- config = dict(self.model_config_dict)- if not config.get('tools'): # None or empty list- config.pop('tools', None)- config.pop('tool_choice', None)-- response = self._client.chat.completions.create(- messages=messages,- model=self.model_type,- **config,+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- 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_4O_MINI)- return self._token_counterdef check_model_config(self):r"""Check whether the model configuration contains any@@ -179,13 +89,3 @@ class NvidiaModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into NVIDIA 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 self.model_config_dict.get('stream', False)diff --git a/camel/models/ollama_model.py b/camel/models/ollama_model.pyindex b61eeafb7..02f9d2a6d 100644--- a/camel/models/ollama_model.py+++ b/camel/models/ollama_model.py@@ -13,24 +13,15 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import osimport subprocess-from typing import Any, Dict, List, Optional, Type, Union--from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream-from pydantic import BaseModel+from typing import Any, Dict, Optional, Unionfrom camel.configs import OLLAMA_API_PARAMS, OllamaConfig-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,- ChatCompletionChunk,- ModelType,-)-from camel.utils import BaseTokenCounter, OpenAITokenCounter+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelType+from camel.utils import BaseTokenCounter-class OllamaModel(BaseModelBackend):+class OllamaModel(OpenAICompatibleModel):r"""Ollama service interface.Args:@@ -72,23 +63,16 @@ class OllamaModel(BaseModelBackend):url = url or os.environ.get("OLLAMA_BASE_URL")timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)+if not self._url:self._start_server()- # Use OpenAI client as interface call Ollama- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key="Set-but-ignored", # required but ignored- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key="Set-but-ignored", # required but ignored- base_url=self._url,- )def _start_server(self) -> None:r"""Starts the Ollama server in a subprocess."""@@ -106,18 +90,6 @@ class OllamaModel(BaseModelBackend):except Exception as e:print(f"Failed to start Ollama server: {e}.")- @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(ModelType.GPT_4O_MINI)- return self._token_counter-def check_model_config(self):r"""Check whether the model configuration contains anyunexpected arguments to Ollama API.@@ -132,168 +104,3 @@ class OllamaModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into Ollama model backend.")-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- r"""Runs inference of Ollama chat completion.-- Args:- messages (List[OpenAIMessage]): Message list with the chat history- in OpenAI API format.- response_format (Optional[Type[BaseModel]]): The format of the- response.- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to- use for the request.-- Returns:- Union[ChatCompletion, Stream[ChatCompletionChunk]]:- `ChatCompletion` in the non-stream mode, or- `Stream[ChatCompletionChunk]` in the stream mode.- """- response_format = response_format or self.model_config_dict.get(- "response_format", None- )- # For Ollama, the tool calling will be broken with response_format- if response_format and not tools:- return self._request_parse(messages, response_format, tools)- else:- return self._request_chat_completion(- messages, response_format, tools- )-- async def _arun(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- r"""Runs inference of Ollama chat completion in async mode.-- Args:- messages (List[OpenAIMessage]): Message list with the chat history- in OpenAI API format.- response_format (Optional[Type[BaseModel]]): The format of the- response.- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to- use for the request.-- Returns:- Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- `ChatCompletion` in the non-stream mode, or- `AsyncStream[ChatCompletionChunk]` in the stream mode.- """- response_format = response_format or self.model_config_dict.get(- "response_format", None- )- if response_format:- return await self._arequest_parse(messages, response_format, tools)- else:- return await self._arequest_chat_completion(- messages, response_format, tools- )-- def _prepare_chat_completion_config(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Dict[str, Any]:- request_config = self.model_config_dict.copy()-- if tools:- request_config["tools"] = tools- if response_format:- try_modify_message_with_format(messages[-1], response_format)- request_config["response_format"] = {"type": "json_object"}-- return request_config-- def _request_chat_completion(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- request_config = self._prepare_chat_completion_config(- messages, response_format, tools- )-- return self._client.chat.completions.create(- messages=messages,- model=self.model_type,- **request_config,- )-- async def _arequest_chat_completion(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- request_config = self._prepare_chat_completion_config(- messages, response_format, tools- )-- return await self._async_client.chat.completions.create(- messages=messages,- model=self.model_type,- **request_config,- )-- def _request_parse(- self,- messages: List[OpenAIMessage],- response_format: Type[BaseModel],- tools: Optional[List[Dict[str, Any]]] = None,- ) -> ChatCompletion:- import copy-- request_config = copy.deepcopy(self.model_config_dict)- # Remove stream from request_config since Ollama does not support it- # when structured response is used- request_config["response_format"] = response_format- request_config.pop("stream", None)- if tools is not None:- request_config["tools"] = tools-- return self._client.beta.chat.completions.parse(- messages=messages,- model=self.model_type,- **request_config,- )-- async def _arequest_parse(- self,- messages: List[OpenAIMessage],- response_format: Type[BaseModel],- tools: Optional[List[Dict[str, Any]]] = None,- ) -> ChatCompletion:- import copy-- request_config = copy.deepcopy(self.model_config_dict)- # Remove stream from request_config since Ollama does not support it- # when structured response is used- request_config["response_format"] = response_format- request_config.pop("stream", None)- if tools is not None:- request_config["tools"] = tools-- return await self._async_client.beta.chat.completions.parse(- messages=messages,- model=self.model_type,- **request_config,- )-- @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/models/openai_compatible_model.py b/camel/models/openai_compatible_model.pyindex 07e39ecb5..338e4ea3e 100644--- a/camel/models/openai_compatible_model.py+++ b/camel/models/openai_compatible_model.py@@ -19,7 +19,7 @@ from openai import AsyncOpenAI, AsyncStream, OpenAI, Streamfrom pydantic import BaseModelfrom camel.messages import OpenAIMessage-from camel.models import BaseModelBackend+from camel.models.base_model import BaseModelBackendfrom camel.types import (ChatCompletion,ChatCompletionChunk,diff --git a/camel/models/openrouter_model.py b/camel/models/openrouter_model.pyindex 20965a06f..50afe46b3 100644--- a/camel/models/openrouter_model.py+++ b/camel/models/openrouter_model.py@@ -14,13 +14,13 @@import osfrom typing import Any, Dict, List, Optional, Type, Union-from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream+from openai import AsyncStream, Streamfrom pydantic import BaseModelfrom camel.configs import OPENROUTER_API_PARAMS, OpenRouterConfigfrom camel.messages import OpenAIMessage-from camel.models import BaseModelBackendfrom camel.models._utils import try_modify_message_with_format+from camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.types import (ChatCompletion,ChatCompletionChunk,@@ -28,13 +28,13 @@ from camel.types import ()from camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class OpenRouterModel(BaseModelBackend):- r"""LLM API served by OpenRouter in a unified BaseModelBackend interface.+class OpenRouterModel(OpenAICompatibleModel):+ r"""LLM API served by OpenRouter in a unified OpenAICompatibleModel+ interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -75,32 +75,13 @@ class OpenRouterModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- @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(ModelType.GPT_4O_MINI)- return self._token_counterdef _prepare_request(self,@@ -198,13 +179,3 @@ class OpenRouterModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into OpenRouter 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 self.model_config_dict.get("stream", False)diff --git a/camel/models/ppio_model.py b/camel/models/ppio_model.pyindex ccc0a37ac..ab67382b5 100644--- a/camel/models/ppio_model.py+++ b/camel/models/ppio_model.py@@ -13,27 +13,18 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import os-from typing import Any, Dict, List, Optional, Type, Union--from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream-from pydantic import BaseModel+from typing import Any, Dict, Optional, Unionfrom camel.configs import PPIO_API_PARAMS, PPIOConfig-from camel.messages import OpenAIMessage-from camel.models import BaseModelBackend-from camel.types import (- ChatCompletion,- ChatCompletionChunk,- ModelType,-)+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelTypefrom camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class PPIOModel(BaseModelBackend):+class PPIOModel(OpenAICompatibleModel):r"""Constructor for PPIO backend with OpenAI compatibility.Args:@@ -79,85 +70,15 @@ class PPIOModel(BaseModelBackend):"PPIO_API_BASE_URL", "https://api.ppinfra.com/v3/openai")timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))-super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- async def _arun(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[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, AsyncStream[ChatCompletionChunk]]:- `ChatCompletion` in the non-stream mode, or- `AsyncStream[ChatCompletionChunk]` in the stream mode.- """- response = await self._async_client.chat.completions.create(- messages=messages,- model=self.model_type,- **self.model_config_dict,- )- return response-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> 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_4O_MINI)- return self._token_counter-def check_model_config(self):r"""Check whether the model configuration contains anyunexpected arguments to PPIO API.@@ -172,13 +93,3 @@ class PPIOModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into PPIO 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 self.model_config_dict.get('stream', False)diff --git a/camel/models/qwen_model.py b/camel/models/qwen_model.pyindex a3df5390c..aae80107d 100644--- a/camel/models/qwen_model.py+++ b/camel/models/qwen_model.py@@ -15,13 +15,13 @@import osfrom typing import Any, Dict, List, Optional, Type, Union-from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream+from openai import AsyncStream, Streamfrom pydantic import BaseModelfrom camel.configs import QWEN_API_PARAMS, QwenConfigfrom camel.messages import OpenAIMessage-from camel.models import BaseModelBackendfrom camel.models._utils import try_modify_message_with_format+from camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.types import (ChatCompletion,ChatCompletionChunk,@@ -29,13 +29,12 @@ from camel.types import ()from camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class QwenModel(BaseModelBackend):- r"""Qwen API in a unified BaseModelBackend interface.+class QwenModel(OpenAICompatibleModel):+ r"""Qwen API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -81,19 +80,12 @@ class QwenModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout- )- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)async def _arun(@@ -167,19 +159,6 @@ class QwenModel(BaseModelBackend):return request_config- @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_4O_MINI)- return self._token_counter-def check_model_config(self):r"""Check whether the model configuration contains anyunexpected arguments to Qwen API.@@ -194,13 +173,3 @@ class QwenModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into Qwen 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 self.model_config_dict.get('stream', False)diff --git a/camel/models/reka_model.py b/camel/models/reka_model.pyindex 129332508..a487bea1c 100644--- a/camel/models/reka_model.py+++ b/camel/models/reka_model.py@@ -41,7 +41,7 @@ except (ImportError, AttributeError):class RekaModel(BaseModelBackend):- r"""Reka API in a unified BaseModelBackend interface.+ r"""Reka API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend isdiff --git a/camel/models/siliconflow_model.py b/camel/models/siliconflow_model.pyindex b77274f7e..b6eb02d49 100644--- a/camel/models/siliconflow_model.py+++ b/camel/models/siliconflow_model.py@@ -14,12 +14,12 @@import osfrom typing import Any, Dict, List, Optional, Type, Union-from openai import AsyncStream, OpenAI, Stream+from openai import AsyncStreamfrom pydantic import BaseModelfrom camel.configs import SILICONFLOW_API_PARAMS, SiliconFlowConfigfrom camel.messages import OpenAIMessage-from camel.models.base_model import BaseModelBackend+from camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.types import (ChatCompletion,ChatCompletionChunk,@@ -27,13 +27,12 @@ from camel.types import ()from camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class SiliconFlowModel(BaseModelBackend):- r"""SiliconFlow API in a unified BaseModelBackend interface.+class SiliconFlowModel(OpenAICompatibleModel):+ r"""SiliconFlow API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -80,38 +79,13 @@ class SiliconFlowModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- r"""Runs inference of SiliconFlow 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 responseasync def _arun(self,@@ -123,18 +97,6 @@ class SiliconFlowModel(BaseModelBackend):"SiliconFlow does not support async inference.")- @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(ModelType.GPT_4O_MINI)- return self._token_counter-def check_model_config(self):r"""Check whether the model configuration contains anyunexpected arguments to SiliconFlow API.@@ -149,13 +111,3 @@ class SiliconFlowModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into SiliconFlow model backend.")-- @property- def stream(self) -> bool:- """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/models/togetherai_model.py b/camel/models/togetherai_model.pyindex a089e9af8..546386dc0 100644--- a/camel/models/togetherai_model.py+++ b/camel/models/togetherai_model.py@@ -13,27 +13,18 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import os-from typing import Any, Dict, List, Optional, Type, Union--from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream-from pydantic import BaseModel+from typing import Any, Dict, Optional, Unionfrom camel.configs import TOGETHERAI_API_PARAMS, TogetherAIConfig-from camel.messages import OpenAIMessage-from camel.models import BaseModelBackend-from camel.types import (- ChatCompletion,- ChatCompletionChunk,- ModelType,-)+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelTypefrom camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class TogetherAIModel(BaseModelBackend):+class TogetherAIModel(OpenAICompatibleModel):r"""Constructor for Together AI backend with OpenAI compatibility.Args:@@ -80,160 +71,12 @@ class TogetherAIModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout- )-- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- @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(ModelType.GPT_4O_MINI)- return self._token_counter-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> 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.- response_format (Optional[Type[BaseModel]]): The format of the- response.- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to- use for the request.-- Returns:- Union[ChatCompletion, Stream[ChatCompletionChunk]]:- `ChatCompletion` in the non-stream mode, or- `Stream[ChatCompletionChunk]` in the stream mode.- """- response_format = response_format or self.model_config_dict.get(- "response_format", None- )- if response_format:- return self._request_parse(messages, response_format, tools)- else:- return self._request_chat_completion(messages, tools)-- async def _arun(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- r"""Runs inference of OpenAI chat completion in async mode.-- Args:- messages (List[OpenAIMessage]): Message list with the chat history- in OpenAI API format.- response_format (Optional[Type[BaseModel]]): The format of the- response.- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to- use for the request.-- Returns:- Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- `ChatCompletion` in the non-stream mode, or- `AsyncStream[ChatCompletionChunk]` in the stream mode.- """- response_format = response_format or self.model_config_dict.get(- "response_format", None- )- if response_format:- return await self._arequest_parse(messages, response_format, tools)- else:- return await self._arequest_chat_completion(messages, tools)-- def _request_chat_completion(- self,- messages: List[OpenAIMessage],- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- request_config = self.model_config_dict.copy()-- if tools:- request_config["tools"] = tools-- return self._client.chat.completions.create(- messages=messages,- model=self.model_type,- **request_config,- )-- async def _arequest_chat_completion(- self,- messages: List[OpenAIMessage],- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- request_config = self.model_config_dict.copy()-- if tools:- request_config["tools"] = tools-- return await self._async_client.chat.completions.create(- messages=messages,- model=self.model_type,- **request_config,- )-- def _request_parse(- self,- messages: List[OpenAIMessage],- response_format: Type[BaseModel],- tools: Optional[List[Dict[str, Any]]] = None,- ) -> ChatCompletion:- request_config = self.model_config_dict.copy()-- request_config["response_format"] = response_format-- if tools is not None:- request_config["tools"] = tools-- return self._client.beta.chat.completions.parse(- messages=messages,- model=self.model_type,- **request_config,- )-- async def _arequest_parse(- self,- messages: List[OpenAIMessage],- response_format: Type[BaseModel],- tools: Optional[List[Dict[str, Any]]] = None,- ) -> ChatCompletion:- request_config = self.model_config_dict.copy()-- request_config["response_format"] = response_format-- if tools is not None:- request_config["tools"] = tools-- return await self._async_client.beta.chat.completions.parse(- messages=messages,- model=self.model_type,- **request_config,+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)def check_model_config(self):@@ -250,13 +93,3 @@ class TogetherAIModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into TogetherAI 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 self.model_config_dict.get('stream', False)diff --git a/camel/models/vllm_model.py b/camel/models/vllm_model.pyindex a076a7c30..b6e4ccccb 100644--- a/camel/models/vllm_model.py+++ b/camel/models/vllm_model.py@@ -13,24 +13,16 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import osimport subprocess-from typing import Any, Dict, List, Optional, Type, Union--from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream-from pydantic import BaseModel+from typing import Any, Dict, Optional, Unionfrom camel.configs import VLLM_API_PARAMS, VLLMConfig-from camel.messages import OpenAIMessage-from camel.models import BaseModelBackend-from camel.types import (- ChatCompletion,- ChatCompletionChunk,- ModelType,-)-from camel.utils import BaseTokenCounter, OpenAITokenCounter+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelType+from camel.utils import BaseTokenCounter# flake8: noqa: E501-class VLLMModel(BaseModelBackend):+class VLLMModel(OpenAICompatibleModel):r"""vLLM service interface.Args:@@ -73,23 +65,15 @@ class VLLMModel(BaseModelBackend):url = url or os.environ.get("VLLM_BASE_URL")timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)if not self._url:self._start_server()- # Use OpenAI client as interface call vLLM- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key="EMPTY", # required but ignored- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key="EMPTY", # required but ignored- base_url=self._url,- )def _start_server(self) -> None:r"""Starts the vllm server in a subprocess."""@@ -107,187 +91,6 @@ class VLLMModel(BaseModelBackend):except Exception as e:print(f"Failed to start vllm server: {e}.")- @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(ModelType.GPT_4O_MINI)- return self._token_counter-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> 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.- response_format (Optional[Type[BaseModel]]): The format of the- response.- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to- use for the request.-- Returns:- Union[ChatCompletion, Stream[ChatCompletionChunk]]:- `ChatCompletion` in the non-stream mode, or- `Stream[ChatCompletionChunk]` in the stream mode.- """- response_format = response_format or self.model_config_dict.get(- "response_format", None- )- if response_format:- return self._request_parse(messages, response_format, tools)- else:- return self._request_chat_completion(messages, tools)-- async def _arun(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- r"""Runs inference of OpenAI chat completion in async mode.-- Args:- messages (List[OpenAIMessage]): Message list with the chat history- in OpenAI API format.- response_format (Optional[Type[BaseModel]]): The format of the- response.- tools (Optional[List[Dict[str, Any]]]): The schema of the tools to- use for the request.-- Returns:- Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- `ChatCompletion` in the non-stream mode, or- `AsyncStream[ChatCompletionChunk]` in the stream mode.- """- response_format = response_format or self.model_config_dict.get(- "response_format", None- )- if response_format:- return await self._arequest_parse(messages, response_format, tools)- else:- return await self._arequest_chat_completion(messages, tools)-- def _request_chat_completion(- self,- messages: List[OpenAIMessage],- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- request_config = self.model_config_dict.copy()-- if tools:- request_config["tools"] = tools-- # Remove additionalProperties from each tool's function parameters- if tools and "tools" in request_config:- for tool in request_config["tools"]:- if "function" in tool and "parameters" in tool["function"]:- tool["function"]["parameters"].pop(- "additionalProperties", None- )-- return self._client.chat.completions.create(- messages=messages,- model=self.model_type,- **request_config,- )-- async def _arequest_chat_completion(- self,- messages: List[OpenAIMessage],- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- request_config = self.model_config_dict.copy()-- if tools:- request_config["tools"] = tools- # Remove additionalProperties from each tool's function parameters- if "tools" in request_config:- for tool in request_config["tools"]:- if "function" in tool and "parameters" in tool["function"]:- tool["function"]["parameters"].pop(- "additionalProperties", None- )-- return await self._async_client.chat.completions.create(- messages=messages,- model=self.model_type,- **request_config,- )-- def _request_parse(- self,- messages: List[OpenAIMessage],- response_format: Type[BaseModel],- tools: Optional[List[Dict[str, Any]]] = None,- ) -> ChatCompletion:- request_config = self.model_config_dict.copy()-- request_config["response_format"] = response_format- request_config.pop("stream", None)- if tools is not None:- # Create a deep copy of tools to avoid modifying the original- import copy-- request_config["tools"] = copy.deepcopy(tools)- # Remove additionalProperties and strict from each tool's function- # parameters since vLLM does not support them- if "tools" in request_config:- for tool in request_config["tools"]:- if "function" in tool and "parameters" in tool["function"]:- tool["function"]["parameters"].pop(- "additionalProperties", None- )- if "strict" in tool.get("function", {}):- tool["function"].pop("strict")-- return self._client.beta.chat.completions.parse(- messages=messages,- model=self.model_type,- **request_config,- )-- async def _arequest_parse(- self,- messages: List[OpenAIMessage],- response_format: Type[BaseModel],- tools: Optional[List[Dict[str, Any]]] = None,- ) -> ChatCompletion:- request_config = self.model_config_dict.copy()-- request_config["response_format"] = response_format- request_config.pop("stream", None)- if tools is not None:- # Create a deep copy of tools to avoid modifying the original- import copy-- request_config["tools"] = copy.deepcopy(tools)- # Remove additionalProperties and strict from each tool's function- # parameters since vLLM does not support them- if "tools" in request_config:- for tool in request_config["tools"]:- if "function" in tool and "parameters" in tool["function"]:- tool["function"]["parameters"].pop(- "additionalProperties", None- )- if "strict" in tool.get("function", {}):- tool["function"].pop("strict")-- return await self._async_client.beta.chat.completions.parse(- messages=messages,- model=self.model_type,- **request_config,- )-def check_model_config(self):r"""Check whether the model configuration contains anyunexpected arguments to vLLM API.@@ -302,13 +105,3 @@ class VLLMModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into vLLM 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 self.model_config_dict.get('stream', False)diff --git a/camel/models/volcano_model.py b/camel/models/volcano_model.pyindex 790f9b4a8..6ee1d3d84 100644--- a/camel/models/volcano_model.py+++ b/camel/models/volcano_model.py@@ -20,13 +20,12 @@ from camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.types import ModelTypefrom camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)class VolcanoModel(OpenAICompatibleModel):- r"""Volcano Engine API in a unified BaseModelBackend interface.+ r"""Volcano Engine API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -75,19 +74,6 @@ class VolcanoModel(OpenAICompatibleModel):model_type, model_config_dict, api_key, url, token_counter, timeout)- @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:- # Use OpenAI token counter as an approximation- self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)- return self._token_counter-def check_model_config(self):r"""Check whether the model configuration is valid for Volcanomodel backends.diff --git a/camel/models/yi_model.py b/camel/models/yi_model.pyindex b90854970..a89c70080 100644--- a/camel/models/yi_model.py+++ b/camel/models/yi_model.py@@ -13,28 +13,19 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import os-from typing import Any, Dict, List, Optional, Type, Union--from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream-from pydantic import BaseModel+from typing import Any, Dict, Optional, Unionfrom camel.configs import YI_API_PARAMS, YiConfig-from camel.messages import OpenAIMessage-from camel.models import BaseModelBackend-from camel.types import (- ChatCompletion,- ChatCompletionChunk,- ModelType,-)+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelTypefrom camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class YiModel(BaseModelBackend):- r"""Yi API in a unified BaseModelBackend interface.+class YiModel(OpenAICompatibleModel):+ r"""Yi API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -79,81 +70,13 @@ class YiModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout- )- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- async def _arun(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- r"""Runs inference of Yi chat completion.-- Args:- messages (List[OpenAIMessage]): Message list with the chat history- in OpenAI API format.-- Returns:- Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- `ChatCompletion` in the non-stream mode, or- `AsyncStream[ChatCompletionChunk]` in the stream mode.- """- response = await self._async_client.chat.completions.create(- messages=messages,- model=self.model_type,- **self.model_config_dict,+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- return response-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- r"""Runs inference of Yi 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_4O_MINI)- return self._token_counterdef check_model_config(self):r"""Check whether the model configuration contains any@@ -169,13 +92,3 @@ class YiModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into Yi 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 self.model_config_dict.get('stream', False)diff --git a/camel/models/zhipuai_model.py b/camel/models/zhipuai_model.pyindex ef28fac50..965328187 100644--- a/camel/models/zhipuai_model.py+++ b/camel/models/zhipuai_model.py@@ -13,28 +13,19 @@# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========import os-from typing import Any, Dict, List, Optional, Type, Union--from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream-from pydantic import BaseModel+from typing import Any, Dict, Optional, Unionfrom camel.configs import ZHIPUAI_API_PARAMS, ZhipuAIConfig-from camel.messages import OpenAIMessage-from camel.models import BaseModelBackend-from camel.types import (- ChatCompletion,- ChatCompletionChunk,- ModelType,-)+from camel.models.openai_compatible_model import OpenAICompatibleModel+from camel.types import ModelTypefrom camel.utils import (BaseTokenCounter,- OpenAITokenCounter,api_keys_required,)-class ZhipuAIModel(BaseModelBackend):- r"""ZhipuAI API in a unified BaseModelBackend interface.+class ZhipuAIModel(OpenAICompatibleModel):+ r"""ZhipuAI API in a unified OpenAICompatibleModel interface.Args:model_type (Union[ModelType, str]): Model for which a backend is@@ -79,85 +70,13 @@ class ZhipuAIModel(BaseModelBackend):)timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))super().__init__(- model_type, model_config_dict, api_key, url, token_counter, timeout- )- self._client = OpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )- self._async_client = AsyncOpenAI(- timeout=self._timeout,- max_retries=3,- api_key=self._api_key,- base_url=self._url,- )-- async def _arun(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> Union[ChatCompletion, AsyncStream[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, AsyncStream[ChatCompletionChunk]]:- `ChatCompletion` in the non-stream mode, or- `AsyncStream[ChatCompletionChunk]` in the stream mode.- """- # Use OpenAI client as interface call ZhipuAI- # Reference: https://open.bigmodel.cn/dev/api#openai_sdk- response = await self._async_client.chat.completions.create(- messages=messages,- model=self.model_type,- **self.model_config_dict,+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=url,+ token_counter=token_counter,+ timeout=timeout,)- return response-- def _run(- self,- messages: List[OpenAIMessage],- response_format: Optional[Type[BaseModel]] = None,- tools: Optional[List[Dict[str, Any]]] = None,- ) -> 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.- """- # Use OpenAI client as interface call ZhipuAI- # Reference: https://open.bigmodel.cn/dev/api#openai_sdk- 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_4O_MINI)- return self._token_counterdef check_model_config(self):r"""Check whether the model configuration contains any@@ -173,13 +92,3 @@ class ZhipuAIModel(BaseModelBackend):f"Unexpected argument `{param}` is ""input into ZhipuAI 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 self.model_config_dict.get('stream', False)