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

feat: Add config file for models using openai interface (#680)

Wendong-Fand711b414f900
diff --git a/.github/workflows/pytest_package.yml b/.github/workflows/pytest_package.yml
index dd63c8e8b..65a879661 100644
--- a/.github/workflows/pytest_package.yml
+++ b/.github/workflows/pytest_package.yml
@@ -31,6 +31,8 @@ jobs:
COHERE_API_KEY: "${{ secrets.COHERE_API_KEY }}"
NVIDIA_API_BASE_URL: "${{ secrets.NVIDIA_API_BASE_URL }}"
NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"
+ ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
+ ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
run: poetry run pytest --fast-test-mode test/
pytest_package_llm_test:
@@ -51,6 +53,8 @@ jobs:
COHERE_API_KEY: "${{ secrets.COHERE_API_KEY }}"
NVIDIA_API_BASE_URL: "${{ secrets.NVIDIA_API_BASE_URL }}"
NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"
+ ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
+ ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
run: poetry run pytest --llm-test-only test/
pytest_package_very_slow_test:
@@ -71,4 +75,6 @@ jobs:
COHERE_API_KEY: "${{ secrets.COHERE_API_KEY }}"
NVIDIA_API_BASE_URL: "${{ secrets.NVIDIA_API_BASE_URL }}"
NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"
+ ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
+ ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
run: poetry run pytest --very-slow-test-only test/
diff --git a/camel/configs/__init__.py b/camel/configs/__init__.py
index 298ac50f3..bedcf0e95 100644
--- a/camel/configs/__init__.py
+++ b/camel/configs/__init__.py
@@ -14,11 +14,13 @@
from .anthropic_config import ANTHROPIC_API_PARAMS, AnthropicConfig
from .base_config import BaseConfig
from .litellm_config import LITELLM_API_PARAMS, LiteLLMConfig
+from .ollama_config import OLLAMA_API_PARAMS, OllamaConfig
from .openai_config import (
OPENAI_API_PARAMS,
ChatGPTConfig,
OpenSourceConfig,
)
+from .zhipuai_config import ZHIPUAI_API_PARAMS, ZhipuAIConfig
__all__ = [
'BaseConfig',
@@ -29,4 +31,8 @@ __all__ = [
'OpenSourceConfig',
'LiteLLMConfig',
'LITELLM_API_PARAMS',
+ 'OllamaConfig',
+ 'OLLAMA_API_PARAMS',
+ 'ZhipuAIConfig',
+ 'ZHIPUAI_API_PARAMS',
]
diff --git a/camel/configs/litellm_config.py b/camel/configs/litellm_config.py
index 947e572e7..975bee8ea 100644
--- a/camel/configs/litellm_config.py
+++ b/camel/configs/litellm_config.py
@@ -13,11 +13,14 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from __future__ import annotations
-from dataclasses import asdict, dataclass, field
-from typing import List, Optional, Union
+from dataclasses import asdict, dataclass
+from typing import TYPE_CHECKING, List, Optional, Union
from camel.configs.base_config import BaseConfig
+if TYPE_CHECKING:
+ from camel.functions import OpenAIFunction
+
@dataclass(frozen=True)
class LiteLLMConfig(BaseConfig):
@@ -25,9 +28,6 @@ class LiteLLMConfig(BaseConfig):
LiteLLM API.
Args:
- model (str): The name of the language model to use for text completion.
- messages (List): A list of message objects representing the
- conversation context. (default: [])
timeout (Optional[Union[float, str]], optional): Request timeout.
(default: None)
temperature (Optional[float], optional): Temperature parameter for
@@ -65,12 +65,7 @@ class LiteLLMConfig(BaseConfig):
deployment_id (Optional[str], optional): Deployment ID. (default: None)
extra_headers (Optional[dict], optional): Additional headers for the
request. (default: None)
- base_url (Optional[str], optional): Base URL for the API. (default:
- None)
api_version (Optional[str], optional): API version. (default: None)
- api_key (Optional[str], optional): API key. (default: None)
- model_list (Optional[list], optional): List of API base, version,
- keys. (default: None)
mock_response (Optional[str], optional): Mock completion response for
testing or debugging. (default: None)
custom_llm_provider (Optional[str], optional): Non-OpenAI LLM
@@ -79,8 +74,6 @@ class LiteLLMConfig(BaseConfig):
(default: None)
"""
- model: str = "gpt-3.5-turbo"
- messages: List = field(default_factory=list)
timeout: Optional[Union[float, str]] = None
temperature: Optional[float] = None
top_p: Optional[float] = None
@@ -91,20 +84,17 @@ class LiteLLMConfig(BaseConfig):
max_tokens: Optional[int] = None
presence_penalty: Optional[float] = None
frequency_penalty: Optional[float] = None
- logit_bias: Optional[dict] = field(default_factory=dict)
+ logit_bias: Optional[dict] = None
user: Optional[str] = None
response_format: Optional[dict] = None
seed: Optional[int] = None
- tools: Optional[List] = field(default_factory=list)
+ tools: Optional[list[OpenAIFunction]] = None
tool_choice: Optional[Union[str, dict]] = None
logprobs: Optional[bool] = None
top_logprobs: Optional[int] = None
deployment_id: Optional[str] = None
- extra_headers: Optional[dict] = field(default_factory=dict)
- base_url: Optional[str] = None
+ extra_headers: Optional[dict] = None
api_version: Optional[str] = None
- api_key: Optional[str] = None
- model_list: Optional[list] = field(default_factory=list)
mock_response: Optional[str] = None
custom_llm_provider: Optional[str] = None
max_retries: Optional[int] = None
diff --git a/camel/configs/ollama_config.py b/camel/configs/ollama_config.py
new file mode 100644
index 000000000..613e3f646
--- /dev/null
+++ b/camel/configs/ollama_config.py
@@ -0,0 +1,85 @@
+# =========== 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 __future__ import annotations
+
+from dataclasses import asdict, dataclass
+from typing import Sequence
+
+from openai._types import NOT_GIVEN, NotGiven
+
+from camel.configs.base_config import BaseConfig
+
+
+@dataclass(frozen=True)
+class OllamaConfig(BaseConfig):
+ r"""Defines the parameters for generating chat completions using OpenAI
+ compatibility
+
+ Reference: https://github.com/ollama/ollama/blob/main/docs/openai.md
+
+ Args:
+ temperature (float, optional): Sampling temperature to use, between
+ :obj:`0` and :obj:`2`. Higher values make the output more random,
+ while lower values make it more focused and deterministic.
+ (default: :obj:`0.2`)
+ top_p (float, optional): An alternative to sampling with temperature,
+ called nucleus sampling, where the model considers the results of
+ the tokens with top_p probability mass. So :obj:`0.1` means only
+ the tokens comprising the top 10% probability mass are considered.
+ (default: :obj:`1.0`)
+ response_format (object, optional): An object specifying the format
+ that the model must output. Compatible with GPT-4 Turbo and all
+ GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to
+ {"type": "json_object"} enables JSON mode, which guarantees the
+ message the model generates is valid JSON. Important: when using
+ JSON mode, you must also instruct the model to produce JSON
+ yourself via a system or user message. Without this, the model
+ may generate an unending stream of whitespace until the generation
+ reaches the token limit, resulting in a long-running and seemingly
+ "stuck" request. Also note that the message content may be
+ partially cut off if finish_reason="length", which indicates the
+ generation exceeded max_tokens or the conversation exceeded the
+ max context length.
+ stream (bool, optional): If True, partial message deltas will be sent
+ as data-only server-sent events as they become available.
+ (default: :obj:`False`)
+ stop (str or list, optional): Up to :obj:`4` sequences where the API
+ will stop generating further tokens. (default: :obj:`None`)
+ max_tokens (int, optional): The maximum number of tokens to generate
+ in the chat completion. The total length of input tokens and
+ generated tokens is limited by the model's context length.
+ (default: :obj:`None`)
+ presence_penalty (float, optional): Number between :obj:`-2.0` and
+ :obj:`2.0`. Positive values penalize new tokens based on whether
+ they appear in the text so far, increasing the model's likelihood
+ to talk about new topics. See more information about frequency and
+ presence penalties. (default: :obj:`0.0`)
+ frequency_penalty (float, optional): Number between :obj:`-2.0` and
+ :obj:`2.0`. Positive values penalize new tokens based on their
+ existing frequency in the text so far, decreasing the model's
+ likelihood to repeat the same line verbatim. See more information
+ about frequency and presence penalties. (default: :obj:`0.0`)
+ """
+
+ temperature: float = 0.2
+ top_p: float = 1.0
+ stream: bool = False
+ stop: str | Sequence[str] | NotGiven = NOT_GIVEN
+ max_tokens: int | NotGiven = NOT_GIVEN
+ presence_penalty: float = 0.0
+ response_format: dict | NotGiven = NOT_GIVEN
+ frequency_penalty: float = 0.0
+
+
+OLLAMA_API_PARAMS = {param for param in asdict(OllamaConfig()).keys()}
diff --git a/camel/configs/zhipuai_config.py b/camel/configs/zhipuai_config.py
new file mode 100644
index 000000000..a1ffed0c6
--- /dev/null
+++ b/camel/configs/zhipuai_config.py
@@ -0,0 +1,78 @@
+# =========== 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 __future__ import annotations
+
+from dataclasses import asdict, dataclass
+from typing import TYPE_CHECKING, Optional, Sequence
+
+from openai._types import NOT_GIVEN, NotGiven
+
+from camel.configs.base_config import BaseConfig
+
+if TYPE_CHECKING:
+ from camel.functions import OpenAIFunction
+
+
+@dataclass(frozen=True)
+class ZhipuAIConfig(BaseConfig):
+ r"""Defines the parameters for generating chat completions using OpenAI
+ compatibility
+
+ Reference: https://open.bigmodel.cn/dev/api#glm-4v
+
+ Args:
+ temperature (float, optional): Sampling temperature to use, between
+ :obj:`0` and :obj:`2`. Higher values make the output more random,
+ while lower values make it more focused and deterministic.
+ (default: :obj:`0.2`)
+ top_p (float, optional): An alternative to sampling with temperature,
+ called nucleus sampling, where the model considers the results of
+ the tokens with top_p probability mass. So :obj:`0.1` means only
+ the tokens comprising the top 10% probability mass are considered.
+ (default: :obj:`0.6`)
+ stream (bool, optional): If True, partial message deltas will be sent
+ as data-only server-sent events as they become available.
+ (default: :obj:`False`)
+ stop (str or list, optional): Up to :obj:`4` sequences where the API
+ will stop generating further tokens. (default: :obj:`None`)
+ max_tokens (int, optional): The maximum number of tokens to generate
+ in the chat completion. The total length of input tokens and
+ generated tokens is limited by the model's context length.
+ (default: :obj:`None`)
+ tools (list[OpenAIFunction], optional): A list of tools the model may
+ call. Currently, only functions are supported as a tool. Use this
+ to provide a list of functions the model may generate JSON inputs
+ for. A max of 128 functions are supported.
+ tool_choice (Union[dict[str, str], str], optional): Controls which (if
+ any) tool is called by the model. :obj:`"none"` means the model
+ will not call any tool and instead generates a message.
+ :obj:`"auto"` means the model can pick between generating a
+ message or calling one or more tools. :obj:`"required"` means the
+ model must call one or more tools. Specifying a particular tool
+ via {"type": "function", "function": {"name": "my_function"}}
+ forces the model to call that tool. :obj:`"none"` is the default
+ when no tools are present. :obj:`"auto"` is the default if tools
+ are present.
+ """
+
+ temperature: float = 0.2
+ top_p: float = 0.6
+ stream: bool = False
+ stop: str | Sequence[str] | NotGiven = NOT_GIVEN
+ max_tokens: int | NotGiven = NOT_GIVEN
+ tools: Optional[list[OpenAIFunction]] = None
+ tool_choice: Optional[dict[str, str] | str] = None
+
+
+ZHIPUAI_API_PARAMS = {param for param in asdict(ZhipuAIConfig()).keys()}
diff --git a/camel/models/anthropic_model.py b/camel/models/anthropic_model.py
index d33a23e63..153343bd6 100644
--- a/camel/models/anthropic_model.py
+++ b/camel/models/anthropic_model.py
@@ -46,11 +46,13 @@ class AnthropicModel(BaseModelBackend):
be fed into Anthropic.messages.create().
api_key (Optional[str]): The API key for authenticating with the
Anthropic service. (default: :obj:`None`)
- url (Optional[str]): The url to the model service.
+ url (Optional[str]): The url to the Anthropic service. (default:
+ :obj:`None`)
"""
super().__init__(model_type, model_config_dict, api_key, url)
self._api_key = api_key or os.environ.get("ANTHROPIC_API_KEY")
- self.client = Anthropic(api_key=self._api_key, base_url=url)
+ self._url = url or os.environ.get("ANTHROPIC_API_BASE_URL")
+ self.client = Anthropic(api_key=self._api_key, base_url=self._url)
self._token_counter: Optional[BaseTokenCounter] = None
def _convert_response_from_anthropic_to_openai(self, response):
diff --git a/camel/models/litellm_model.py b/camel/models/litellm_model.py
index b3c3cb11e..83b0b77a4 100644
--- a/camel/models/litellm_model.py
+++ b/camel/models/litellm_model.py
@@ -11,24 +11,25 @@
# 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 TYPE_CHECKING, Any, Dict, List, Optional, Union
+from typing import Any, Dict, List, Optional
from camel.configs import LITELLM_API_PARAMS
from camel.messages import OpenAIMessage
+from camel.types import ChatCompletion
from camel.utils import LiteLLMTokenCounter
-if TYPE_CHECKING:
- from litellm.utils import CustomStreamWrapper, ModelResponse
-
class LiteLLMModel:
r"""Constructor for LiteLLM backend with OpenAI compatibility."""
- # NOTE: Currently "stream": True is not supported with LiteLLM due to the
- # limitation of the current camel design.
+ # NOTE: Currently stream mode is not supported.
def __init__(
- self, model_type: str, model_config_dict: Dict[str, Any]
+ self,
+ model_type: str,
+ model_config_dict: Dict[str, Any],
+ api_key: Optional[str] = None,
+ url: Optional[str] = None,
) -> None:
r"""Constructor for LiteLLM backend.
@@ -37,12 +38,48 @@ class LiteLLMModel:
such as GPT-3.5-turbo, Claude-2, etc.
model_config_dict (Dict[str, Any]): A dictionary of parameters for
the model configuration.
+ api_key (Optional[str]): The API key for authenticating with the
+ model service. (default: :obj:`None`)
+ url (Optional[str]): The url to the model service. (default:
+ :obj:`None`)
"""
self.model_type = model_type
self.model_config_dict = model_config_dict
self._client = None
self._token_counter: Optional[LiteLLMTokenCounter] = None
self.check_model_config()
+ self._url = url
+ self._api_key = api_key
+
+ def _convert_response_from_litellm_to_openai(
+ self, response
+ ) -> ChatCompletion:
+ r"""Converts a response from the LiteLLM format to the OpenAI format.
+
+ Parameters:
+ response (LiteLLMResponse): The response object from LiteLLM.
+
+ Returns:
+ ChatCompletion: The response object in OpenAI's format.
+ """
+ return ChatCompletion.construct(
+ id=response.id,
+ choices=[
+ {
+ "index": response.choices[0].index,
+ "message": {
+ "role": response.choices[0].message.role,
+ "content": response.choices[0].message.content,
+ },
+ "finish_reason": response.choices[0].finish_reason,
+ }
+ ],
+ created=response.created,
+ model=response.model,
+ object=response.object,
+ system_fingerprint=response.system_fingerprint,
+ usage=response.usage,
+ )
@property
def client(self):
@@ -67,7 +104,7 @@ class LiteLLMModel:
def run(
self,
messages: List[OpenAIMessage],
- ) -> Union['ModelResponse', 'CustomStreamWrapper']:
+ ) -> ChatCompletion:
r"""Runs inference of LiteLLM chat completion.
Args:
@@ -75,15 +112,16 @@ class LiteLLMModel:
in OpenAI format.
Returns:
- Union[ModelResponse, CustomStreamWrapper]:
- `ModelResponse` in the non-stream mode, or
- `CustomStreamWrapper` in the stream mode.
+ ChatCompletion
"""
response = self.client(
+ api_key=self._api_key,
+ base_url=self._url,
model=self.model_type,
messages=messages,
**self.model_config_dict,
)
+ response = self._convert_response_from_litellm_to_openai(response)
return response
def check_model_config(self):
@@ -100,13 +138,3 @@ class LiteLLMModel:
f"Unexpected argument `{param}` is "
"input into LiteLLM 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/model_factory.py b/camel/models/model_factory.py
index 86f9978dc..b56f9df2d 100644
--- a/camel/models/model_factory.py
+++ b/camel/models/model_factory.py
@@ -80,6 +80,7 @@ class ModelFactory:
elif isinstance(model_type, str):
if model_platform.is_ollama:
model_class = OllamaModel
+ return model_class(model_type, model_config_dict, url)
elif model_platform.is_litellm:
model_class = LiteLLMModel
else:
diff --git a/camel/models/nemotron_model.py b/camel/models/nemotron_model.py
index 659e0b587..10e337b41 100644
--- a/camel/models/nemotron_model.py
+++ b/camel/models/nemotron_model.py
@@ -33,6 +33,7 @@ class NemotronModel:
self,
model_type: ModelType,
api_key: Optional[str] = None,
+ url: Optional[str] = None,
) -> None:
r"""Constructor for Nvidia backend.
@@ -40,14 +41,21 @@ class NemotronModel:
model_type (ModelType): Model for which a backend is created.
api_key (Optional[str]): The API key for authenticating with the
Nvidia service. (default: :obj:`None`)
+ url (Optional[str]): The url to the Nvidia service. (default:
+ :obj:`None`)
"""
self.model_type = model_type
- url = os.environ.get('NVIDIA_API_BASE_URL', None)
+ self._url = url or os.environ.get("NVIDIA_API_BASE_URL")
self._api_key = api_key or os.environ.get("NVIDIA_API_KEY")
- if not url or not self._api_key:
- raise ValueError("The NVIDIA API base url and key should be set.")
+ if not self._url or not self._api_key:
+ raise ValueError(
+ "NVIDIA_API_BASE_URL and NVIDIA_API_KEY should be set."
+ )
self._client = OpenAI(
- timeout=60, max_retries=3, base_url=url, api_key=self._api_key
+ timeout=60,
+ max_retries=3,
+ base_url=self._url,
+ api_key=self._api_key,
)
self._token_counter: Optional[BaseTokenCounter] = None
diff --git a/camel/models/ollama_model.py b/camel/models/ollama_model.py
index 9fc8b0178..a7de1fd0d 100644
--- a/camel/models/ollama_model.py
+++ b/camel/models/ollama_model.py
@@ -11,12 +11,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-import os
from typing import Any, Dict, List, Optional, Union
from openai import OpenAI, Stream
-from camel.configs import OPENAI_API_PARAMS
+from camel.configs import OLLAMA_API_PARAMS
from camel.messages import OpenAIMessage
from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
from camel.utils import BaseTokenCounter, OpenAITokenCounter
@@ -25,39 +24,34 @@ from camel.utils import BaseTokenCounter, OpenAITokenCounter
class OllamaModel:
r"""Ollama service interface."""
- # NOTE: Current `ModelType and `TokenCounter` desigen is not suitable,
- # stream mode is not supported
-
def __init__(
self,
model_type: str,
model_config_dict: Dict[str, Any],
- api_key: Optional[str] = None,
url: Optional[str] = None,
) -> None:
r"""Constructor for Ollama backend with OpenAI compatibility.
+ # Reference: https://github.com/ollama/ollama/blob/main/docs/openai.md
+
Args:
model_type (str): 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 (Optional[str]): The API key for authenticating with the
- model service. (default: :obj:`None`)
- url (Optional[str]): The url to the model service.
+ url (Optional[str]): The url to the model service. (default:
+ :obj:`None`)
"""
self.model_type = model_type
self.model_config_dict = model_config_dict
- self._url = url or os.environ.get('OPENAI_API_BASE_URL')
- self._api_key = api_key or os.environ.get("OPENAI_API_KEY")
# Use OpenAI cilent as interface call Ollama
- # Reference: https://github.com/ollama/ollama/blob/main/docs/openai.md
self._client = OpenAI(
timeout=60,
max_retries=3,
- base_url=self._url,
- api_key=self._api_key,
+ base_url=url,
+ api_key="ollama", # required but ignored
)
self._token_counter: Optional[BaseTokenCounter] = None
+ self.check_model_config()
@property
def token_counter(self) -> BaseTokenCounter:
@@ -74,17 +68,17 @@ class OllamaModel:
def check_model_config(self):
r"""Check whether the model configuration contains any
- unexpected arguments to OpenAI API.
+ unexpected arguments to Ollama API.
Raises:
ValueError: If the model configuration dictionary contains any
unexpected arguments to OpenAI API.
"""
for param in self.model_config_dict:
- if param not in OPENAI_API_PARAMS:
+ if param not in OLLAMA_API_PARAMS:
raise ValueError(
f"Unexpected argument `{param}` is "
- "input into OpenAI model backend."
+ "input into Ollama model backend."
)
def run(
diff --git a/camel/models/openai_audio_models.py b/camel/models/openai_audio_models.py
index 1b2f09c1d..419efebb2 100644
--- a/camel/models/openai_audio_models.py
+++ b/camel/models/openai_audio_models.py
@@ -25,10 +25,18 @@ class OpenAIAudioModels:
def __init__(
self,
+ api_key: Optional[str] = None,
+ url: Optional[str] = None,
) -> None:
r"""Initialize an instance of OpenAI."""
- url = os.environ.get('OPENAI_API_BASE_URL')
- self._client = OpenAI(timeout=120, max_retries=3, base_url=url)
+ self._url = url or os.environ.get("OPENAI_API_BASE_URL")
+ self._api_key = api_key or os.environ.get("OPENAI_API_KEY")
+ self._client = OpenAI(
+ timeout=120,
+ max_retries=3,
+ base_url=self._url,
+ api_key=self._api_key,
+ )
def text_to_speech(
self,
diff --git a/camel/models/openai_model.py b/camel/models/openai_model.py
index ddfe86d7f..a25876b99 100644
--- a/camel/models/openai_model.py
+++ b/camel/models/openai_model.py
@@ -46,7 +46,8 @@ class OpenAIModel(BaseModelBackend):
be fed into openai.ChatCompletion.create().
api_key (Optional[str]): The API key for authenticating with the
OpenAI service. (default: :obj:`None`)
- url (Optional[str]): The url to the OpenAI service.
+ url (Optional[str]): The url to the OpenAI service. (default:
+ :obj:`None`)
"""
super().__init__(model_type, model_config_dict, api_key, url)
self._url = url or os.environ.get("OPENAI_API_BASE_URL")
diff --git a/camel/models/zhipuai_model.py b/camel/models/zhipuai_model.py
index 61c4350da..2011d828a 100644
--- a/camel/models/zhipuai_model.py
+++ b/camel/models/zhipuai_model.py
@@ -17,7 +17,7 @@ from typing import Any, Dict, List, Optional, Union
from openai import OpenAI, Stream
-from camel.configs import OPENAI_API_PARAMS
+from camel.configs import ZHIPUAI_API_PARAMS
from camel.messages import OpenAIMessage
from camel.models import BaseModelBackend
from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
@@ -47,10 +47,16 @@ class ZhipuAIModel(BaseModelBackend):
be fed into openai.ChatCompletion.create().
api_key (Optional[str]): The API key for authenticating with the
ZhipuAI service. (default: :obj:`None`)
+ url (Optional[str]): The url to the ZhipuAI service. (default:
+ :obj:`None`)
"""
super().__init__(model_type, model_config_dict)
self._url = url or os.environ.get("ZHIPUAI_API_BASE_URL")
self._api_key = api_key or os.environ.get("ZHIPUAI_API_KEY")
+ if not self._url or not self._api_key:
+ raise ValueError(
+ "ZHIPUAI_API_BASE_URL and ZHIPUAI_API_KEY should be set."
+ )
self._client = OpenAI(
timeout=60,
max_retries=3,
@@ -104,13 +110,13 @@ class ZhipuAIModel(BaseModelBackend):
Raises:
ValueError: If the model configuration dictionary contains any
- unexpected arguments to OpenAI API.
+ unexpected arguments to ZhipuAI API.
"""
for param in self.model_config_dict:
- if param not in OPENAI_API_PARAMS:
+ if param not in ZHIPUAI_API_PARAMS:
raise ValueError(
f"Unexpected argument `{param}` is "
- "input into OpenAI model backend."
+ "input into ZhipuAI model backend."
)
pass
diff --git a/examples/models/litellm_model_example.py b/examples/models/litellm_model_example.py
index 8fca124b1..309ca847e 100644
--- a/examples/models/litellm_model_example.py
+++ b/examples/models/litellm_model_example.py
@@ -11,99 +11,40 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from colorama import Fore
-
-from camel.models.litellm_model import LiteLLMModel
-from camel.utils import print_text_animated
-
-# Define model type and configuration
-model_type = "gpt-3.5-turbo"
-model_config_dict = {
- "temperature": 0.7,
- "max_tokens": 500,
- "stream": False, # Set to True to enable stream mode
-}
-
-# Create an instance of LiteLLMModel
-model = LiteLLMModel(model_type, model_config_dict)
-
-# Define test messages
-messages = [
- {"content": "You are a helpful assistant.", "role": "system"},
- {
- "content": "Can you explain what is the LiteLLMModel",
- "role": "user",
- },
-]
-
-# Run the model
-response = model.run(messages)
-
-# Print the response
-print(response)
-
-# Print the cost
-cost = model.token_counter.calculate_cost_from_response(response)
-print(f"The cost is ${cost}")
-
-output_messages = ""
-
-for choice in response.choices:
- chat_message = choice.message.content or ""
- output_messages += chat_message
-
-print(model.token_counter.count_tokens_from_messages(messages))
-
-messages.append({"content": output_messages, "role": "assistant"})
-
-# Print the number of token consumed
-token_consumed = model.token_counter.count_tokens_from_messages(messages)
-print(f"The token consumed is {token_consumed}")
-
-# Print the response
-print_text_animated(Fore.GREEN + "AI Assistant:\n\n" f"{output_messages}\n")
-"""
-===============================================================================
-ModelResponse(id='chatcmpl-9ZIkZBUVvRPU80osTkzoPxrLINB7S', choices=[Choices
-(finish_reason='stop', index=0, message=Message(content='The `LiteLLMModel` is
-a type of language model developed by OpenAI, specifically designed to be
-lightweight and efficient for tasks such as text generation and natural
-language processing. It is a smaller and simplified version of the larger GPT
-(Generative Pre-trained Transformer) models, making it more accessible for
-applications with limited computational resources. The LiteLLMModel is trained
-on a large corpus of text data and can generate coherent and contextually
-relevant text based on the input it receives. Its compact size makes it
-suitable for deployment on devices with constraints on memory and processing
-power.', role='assistant'))], created=1718200583, model='gpt-3.5-turbo-0125',
-object='chat.completion', system_fingerprint=None, usage=Usage
-(completion_tokens=116, prompt_tokens=27, total_tokens=143))
-===============================================================================
-"""
-
-"""
-===============================================================================
-The cost is $0.0001875
-===============================================================================
-"""
-
-"""
-===============================================================================
-The token consumed is 147
-===============================================================================
-"""
-
-"""
-===============================================================================
-AI Assistant:
-
-The `LiteLLMModel` is a type of language model developed by OpenAI,
-specifically designed to be lightweight and efficient for tasks such as text
-generation and natural language processing. It is a smaller and simplified
-version of the larger GPT (Generative Pre-trained Transformer) models, making
-it more accessible for applications with limited computational resources. The
-LiteLLMModel is trained on a large corpus of text data and can generate
-coherent and contextually relevant text based on the input it receives. Its
-compact size makes it suitable for deployment on devices with constraints on
-memory and processing power.
-===============================================================================
-"""
+from camel.agents import ChatAgent
+from camel.configs import LiteLLMConfig
+from camel.messages import BaseMessage
+from camel.models import ModelFactory
+from camel.types import ModelPlatformType
+
+model = ModelFactory.create(
+ model_platform=ModelPlatformType.LITELLM,
+ model_type="gpt-4o",
+ model_config_dict=LiteLLMConfig(temperature=0.2).__dict__,
+)
+
+# Define system message
+sys_msg = BaseMessage.make_assistant_message(
+ role_name="Assistant",
+ content="You are a helpful assistant.",
+)
+
+# Set agent
+camel_agent = ChatAgent(system_message=sys_msg, model=model, token_limit=500)
+
+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.""",
+)
+
+# Get response information
+response = camel_agent.step(user_msg)
+print(response.msgs[0].content)
+'''
+===============================================================================
+Hello CAMEL AI! It's great to see a community dedicated to the study of
+autonomous and communicative agents. Your work in advancing open-source AI is
+incredibly important and inspiring. Keep up the fantastic work!
+===============================================================================
+'''
diff --git a/examples/open_source_models/ollama_model.py b/examples/models/ollama_model_example.py
similarity index 69%
rename from examples/open_source_models/ollama_model.py
rename to examples/models/ollama_model_example.py
index 7c7b2f4ba..b7d695ef1 100644
--- a/examples/open_source_models/ollama_model.py
+++ b/examples/models/ollama_model_example.py
@@ -29,44 +29,22 @@ assistant_sys_msg = BaseMessage.make_assistant_message(
content="You are a helpful assistant.",
)
agent = ChatAgent(assistant_sys_msg, model=ollama_model, token_limit=4096)
-agent.reset()
user_msg = BaseMessage.make_user_message(
- role_name="User", content="say hi to me"
+ 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)
-""" Content in Llama3ModelFile:
-===============================================================================
-FROM llama3
-
-# Set parameters
-
-PARAMETER temperature 0.8
-PARAMETER stop Result
-
-# Sets a custom system message to specify the behavior of the chat assistant
-
-# Leaving it blank for now.
-
-SYSTEM """ """
-===============================================================================
"""
-
-
-""" Content in .sh file:
===============================================================================
-#!/bin/zsh
-
-# variables
-model_name="llama3"
-custom_model_name="camel-llama3"
-
-#get the base model
-ollama pull $model_name
-
-#create the model file
-ollama create $custom_model_name -f ./Llama3ModelFile
+Hi there! *waves* Hi to the amazing team at CAMEL AI - Autonomous and
+Communicative Agents Laboratory! It's great to connect with you all. I'm
+excited to learn more about your work in developing autonomous and
+communicative agents, exploring the intersection of artificial intelligence,
+robotics, and human-computer interaction. Keep pushing the boundaries of
+what's possible!
===============================================================================
"""
diff --git a/examples/models/role_playing_with_claude.py b/examples/models/role_playing_with_claude.py
index aae0ae790..c53e8b8e8 100644
--- a/examples/models/role_playing_with_claude.py
+++ b/examples/models/role_playing_with_claude.py
@@ -27,7 +27,7 @@ def main(model_type=None) -> None:
role: ModelFactory.create(
model_platform=ModelPlatformType.ANTHROPIC,
model_type=model_type,
- model_config=AnthropicConfig(),
+ model_config_dict=AnthropicConfig().__dict__,
)
for role in ["assistant", "user", "task-specify"]
}
diff --git a/examples/models/zhipuai_model_example.py b/examples/models/zhipuai_model_example.py
index 9715fde49..504df4589 100644
--- a/examples/models/zhipuai_model_example.py
+++ b/examples/models/zhipuai_model_example.py
@@ -13,7 +13,7 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from camel.agents import ChatAgent
-from camel.configs import ChatGPTConfig
+from camel.configs import ZhipuAIConfig
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
@@ -21,7 +21,7 @@ from camel.types import ModelPlatformType, ModelType
model = ModelFactory.create(
model_platform=ModelPlatformType.ZHIPU,
model_type=ModelType.GLM_4,
- model_config=ChatGPTConfig(temperature=0.2),
+ model_config_dict=ZhipuAIConfig(temperature=0.2).__dict__,
)
# Define system message
@@ -35,8 +35,8 @@ camel_agent = ChatAgent(system_message=sys_msg, model=model)
user_msg = BaseMessage.make_user_message(
role_name="User",
- content="I want to practice my legs today."
- "Help me make a fitness and diet plan",
+ content="""Say hi to CAMEL AI, one open-source community
+ dedicated to the study of autonomous and communicative agents.""",
)
# Get response information
@@ -44,71 +44,9 @@ response = camel_agent.step(user_msg)
print(response.msgs[0].content)
'''
===============================================================================
-Certainly! Focusing on leg workouts can help improve strength, endurance, and
-overall lower-body fitness. Here's a sample fitness
-and diet plan for leg training:
-
-**Fitness Plan:**
-
-1. **Warm-Up:**
- - 5-10 minutes of light cardio (jogging, cycling, or jumping jacks)
- - Leg swings (forward and backward)
- - Hip circles
-
-2. **Strength Training:**
- - Squats: 3 sets of 8-12 reps
- - Deadlifts: 3 sets of 8-12 reps
- - Lunges: 3 sets of 10-12 reps per leg
- - Leg press: 3 sets of 10-12 reps
- - Calf raises: 3 sets of 15-20 reps
-
-3. **Cardio:**
- - Hill sprints: 5-8 reps of 30-second sprints
- - Cycling or stationary biking: 20-30 minutes at moderate intensity
-
-4. **Cool Down:**
- - Stretching (focus on the legs, hip flexors, and hamstrings)
- - Foam rolling (optional)
-
-**Diet Plan:**
-
-1. **Breakfast:**
- - Greek yogurt with mixed berries and a tablespoon of chia seeds
- - Whole-grain toast with avocado
-
-2. **Snack:**
- - A banana with a tablespoon of natural peanut butter
-
-3. **Lunch:**
- - Grilled chicken breast with quinoa and steamed vegetables
- - A side of mixed greens with a light vinaigrette
-
-4. **Snack:**
- - A serving of mixed nuts and dried fruits
-
-5. **Dinner:**
- - Baked salmon with sweet potato and roasted asparagus
- - A side of lentil soup or a bean salad
-
-6. **Post-Workout Snack:**
- - A protein shake or a serving of cottage cheese with fruit
-
-7. **Hydration:**
- - Drink plenty of water throughout the day to
- stay hydrated, especially after workouts.
-
-**Tips:**
-
-- Ensure you get enough rest and recovery, as leg workouts
-can be demanding on the body.
-- Listen to your body and adjust the weights and reps
-according to your fitness level.
-- Make sure to include a variety of nutrients in your diet to
-support muscle recovery and overall health.
-- Consult a fitness professional or trainer if you need personalized
-guidance or have any pre-existing health conditions.
-
-Remember, consistency is key to seeing results, so stick to
-your plan and modify it as needed to suit your goals and progress.
+Hello to CAMEL AI and its community! As a helpful assistant, I'm here to
+provide assistance, answer questions, and support the study of autonomous and
+communicative agents to the best of my abilities. If you have any specific
+questions or need guidance on a particular topic, feel free to ask!
===============================================================================
'''
diff --git a/test/models/test_litellm_model.py b/test/models/test_litellm_model.py
index 0a40595c9..6fd33c649 100644
--- a/test/models/test_litellm_model.py
+++ b/test/models/test_litellm_model.py
@@ -15,7 +15,7 @@ import re
import pytest
-from camel.configs import ChatGPTConfig, OpenSourceConfig
+from camel.configs import LiteLLMConfig, OpenSourceConfig
from camel.models import LiteLLMModel
from camel.types import ModelType
from camel.utils import LiteLLMTokenCounter
@@ -33,7 +33,7 @@ from camel.utils import LiteLLMTokenCounter
],
)
def test_litellm_model(model_type):
- model_config_dict = ChatGPTConfig().__dict__
+ model_config_dict = LiteLLMConfig().__dict__
model = LiteLLMModel(model_type, model_config_dict)
assert model.model_type == model_type
assert model.model_config_dict == model_config_dict
diff --git a/test/models/test_ollama_model.py b/test/models/test_ollama_model.py
new file mode 100644
index 000000000..1754447c9
--- /dev/null
+++ b/test/models/test_ollama_model.py
@@ -0,0 +1,59 @@
+# =========== 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. ===========
+import re
+
+import pytest
+
+from camel.configs import OllamaConfig
+from camel.models import OllamaModel
+from camel.types import ModelType
+from camel.utils import OpenAITokenCounter
+
+
+ "model_type",
+ [
+ ModelType.GPT_3_5_TURBO,
+ ModelType.GPT_4,
+ ModelType.GPT_4_32K,
+ ModelType.GPT_4_TURBO,
+ ModelType.GPT_4O,
+ ],
+)
+def test_ollama_model(model_type):
+ model_config_dict = OllamaConfig().__dict__
+ model = OllamaModel(model_type, model_config_dict)
+ assert model.model_type == model_type
+ assert model.model_config_dict == model_config_dict
+ assert isinstance(model.token_counter, OpenAITokenCounter)
+ assert isinstance(model.model_type.value_for_tiktoken, str)
+ assert isinstance(model.model_type.token_limit, int)
+
+
+def test_ollama_model_unexpected_argument():
+ model_type = ModelType.GPT_4
+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}
+
+ with pytest.raises(
+ ValueError,
+ match=re.escape(
+ (
+ "Unexpected argument `model_path` is "
+ "input into Ollama model backend."
+ )
+ ),
+ ):
+ _ = OllamaModel(model_type, model_config_dict)
diff --git a/test/models/test_zhipuai_model.py b/test/models/test_zhipuai_model.py
index b373e1424..8568d022b 100644
--- a/test/models/test_zhipuai_model.py
+++ b/test/models/test_zhipuai_model.py
@@ -15,7 +15,7 @@ import re
import pytest
-from camel.configs import ChatGPTConfig, OpenSourceConfig
+from camel.configs import OpenSourceConfig, ZhipuAIConfig
from camel.models import ZhipuAIModel
from camel.types import ModelType
from camel.utils import OpenAITokenCounter
@@ -31,7 +31,7 @@ from camel.utils import OpenAITokenCounter
],
)
def test_zhipuai_model(model_type):
- model_config_dict = ChatGPTConfig().__dict__
+ model_config_dict = ZhipuAIConfig().__dict__
model = ZhipuAIModel(model_type, model_config_dict)
assert model.model_type == model_type
assert model.model_config_dict == model_config_dict
@@ -54,7 +54,7 @@ def test_zhipuai_model_unexpected_argument():
match=re.escape(
(
"Unexpected argument `model_path` is "
- "input into OpenAI model backend."
+ "input into ZhipuAI model backend."
)
),
):