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 support for litellm library (#596)

ZackYule8951cd356581
diff --git a/camel/configs/__init__.py b/camel/configs/__init__.py
index 3b8209c37..298ac50f3 100644
--- a/camel/configs/__init__.py
+++ b/camel/configs/__init__.py
@@ -13,6 +13,7 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from .anthropic_config import ANTHROPIC_API_PARAMS, AnthropicConfig
from .base_config import BaseConfig
+from .litellm_config import LITELLM_API_PARAMS, LiteLLMConfig
from .openai_config import (
OPENAI_API_PARAMS,
ChatGPTConfig,
@@ -26,4 +27,6 @@ __all__ = [
'AnthropicConfig',
'ANTHROPIC_API_PARAMS',
'OpenSourceConfig',
+ 'LiteLLMConfig',
+ 'LITELLM_API_PARAMS',
]
diff --git a/camel/configs/litellm_config.py b/camel/configs/litellm_config.py
new file mode 100644
index 000000000..947e572e7
--- /dev/null
+++ b/camel/configs/litellm_config.py
@@ -0,0 +1,113 @@
+# =========== 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, field
+from typing import List, Optional, Union
+
+from camel.configs.base_config import BaseConfig
+
+
+@dataclass(frozen=True)
+class LiteLLMConfig(BaseConfig):
+ r"""Defines the parameters for generating chat completions using the
+ 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
+ controlling randomness. (default: None)
+ top_p (Optional[float], optional): Top-p parameter for nucleus
+ sampling. (default: None)
+ n (Optional[int], optional): Number of completions to generate.
+ (default: None)
+ stream (Optional[bool], optional): Whether to return a streaming
+ response. (default: None)
+ stream_options (Optional[dict], optional): Options for the streaming
+ response. (default: None)
+ stop (Optional[Union[str, List[str]]], optional): Sequences where the
+ API will stop generating further tokens. (default: None)
+ max_tokens (Optional[int], optional): Maximum number of tokens to
+ generate. (default: None)
+ presence_penalty (Optional[float], optional): Penalize new tokens
+ based on their existence in the text so far. (default: None)
+ frequency_penalty (Optional[float], optional): Penalize new tokens
+ based on their frequency in the text so far. (default: None)
+ logit_bias (Optional[dict], optional): Modify the probability of
+ specific tokens appearing in the completion. (default: None)
+ user (Optional[str], optional): A unique identifier representing the
+ end-user. (default: None)
+ response_format (Optional[dict], optional): Response format
+ parameters. (default: None)
+ seed (Optional[int], optional): Random seed. (default: None)
+ tools (Optional[List], optional): List of tools. (default: None)
+ tool_choice (Optional[Union[str, dict]], optional): Tool choice
+ parameters. (default: None)
+ logprobs (Optional[bool], optional): Whether to return log
+ probabilities of the output tokens. (default: None)
+ top_logprobs (Optional[int], optional): Number of most likely tokens
+ to return at each token position. (default: None)
+ 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
+ provider. (default: None)
+ max_retries (Optional[int], optional): Maximum number of retries.
+ (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
+ n: Optional[int] = None
+ stream: Optional[bool] = None
+ stream_options: Optional[dict] = None
+ stop: Optional[Union[str, List[str]]] = None
+ max_tokens: Optional[int] = None
+ presence_penalty: Optional[float] = None
+ frequency_penalty: Optional[float] = None
+ logit_bias: Optional[dict] = field(default_factory=dict)
+ user: Optional[str] = None
+ response_format: Optional[dict] = None
+ seed: Optional[int] = None
+ tools: Optional[List] = field(default_factory=list)
+ 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
+ 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
+
+
+LITELLM_API_PARAMS = {param for param in asdict(LiteLLMConfig()).keys()}
diff --git a/camel/models/__init__.py b/camel/models/__init__.py
index 3220eb0aa..92c79fac4 100644
--- a/camel/models/__init__.py
+++ b/camel/models/__init__.py
@@ -13,6 +13,7 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from .anthropic_model import AnthropicModel
from .base_model import BaseModelBackend
+from .litellm_model import LiteLLMModel
from .model_factory import ModelFactory
from .open_source_model import OpenSourceModel
from .openai_audio_models import OpenAIAudioModels
@@ -26,5 +27,6 @@ __all__ = [
'StubModel',
'OpenSourceModel',
'ModelFactory',
+ 'LiteLLMModel',
'OpenAIAudioModels',
]
diff --git a/camel/models/litellm_model.py b/camel/models/litellm_model.py
new file mode 100644
index 000000000..b3c3cb11e
--- /dev/null
+++ b/camel/models/litellm_model.py
@@ -0,0 +1,112 @@
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
+
+from camel.configs import LITELLM_API_PARAMS
+from camel.messages import OpenAIMessage
+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.
+
+ def __init__(
+ self, model_type: str, model_config_dict: Dict[str, Any]
+ ) -> None:
+ r"""Constructor for LiteLLM backend.
+
+ Args:
+ model_type (str): Model for which a backend is created,
+ such as GPT-3.5-turbo, Claude-2, etc.
+ model_config_dict (Dict[str, Any]): A dictionary of parameters for
+ the model configuration.
+ """
+ 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()
+
+ @property
+ def client(self):
+ if self._client is None:
+ from litellm import completion
+
+ self._client = completion
+ return self._client
+
+ @property
+ def token_counter(self) -> LiteLLMTokenCounter:
+ r"""Initialize the token counter for the model backend.
+
+ Returns:
+ LiteLLMTokenCounter: The token counter following the model's
+ tokenization style.
+ """
+ if not self._token_counter:
+ self._token_counter = LiteLLMTokenCounter(self.model_type)
+ return self._token_counter
+
+ def run(
+ self,
+ messages: List[OpenAIMessage],
+ ) -> Union['ModelResponse', 'CustomStreamWrapper']:
+ r"""Runs inference of LiteLLM chat completion.
+
+ Args:
+ messages (List[OpenAIMessage]): Message list with the chat history
+ in OpenAI format.
+
+ Returns:
+ Union[ModelResponse, CustomStreamWrapper]:
+ `ModelResponse` in the non-stream mode, or
+ `CustomStreamWrapper` in the stream mode.
+ """
+ response = self.client(
+ model=self.model_type,
+ messages=messages,
+ **self.model_config_dict,
+ )
+ return response
+
+ def check_model_config(self):
+ r"""Check whether the model configuration contains any unexpected
+ arguments to LiteLLM API.
+
+ Raises:
+ ValueError: If the model configuration dictionary contains any
+ unexpected arguments.
+ """
+ for param in self.model_config_dict:
+ if param not in LITELLM_API_PARAMS:
+ raise ValueError(
+ 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/utils/__init__.py b/camel/utils/__init__.py
index 69b71cb47..3f4244575 100644
--- a/camel/utils/__init__.py
+++ b/camel/utils/__init__.py
@@ -32,6 +32,7 @@ from .constants import Constants
from .token_counting import (
AnthropicTokenCounter,
BaseTokenCounter,
+ LiteLLMTokenCounter,
OpenAITokenCounter,
OpenSourceTokenCounter,
get_model_encoding,
@@ -53,6 +54,7 @@ __all__ = [
'BaseTokenCounter',
'OpenAITokenCounter',
'OpenSourceTokenCounter',
+ 'LiteLLMTokenCounter',
'Constants',
'text_extract_from_web',
'create_chunks',
diff --git a/camel/utils/token_counting.py b/camel/utils/token_counting.py
index 21e43ddda..35fbe7871 100644
--- a/camel/utils/token_counting.py
+++ b/camel/utils/token_counting.py
@@ -301,6 +301,58 @@ class AnthropicTokenCounter(BaseTokenCounter):
return self.client.count_tokens(prompt)
+class LiteLLMTokenCounter:
+ def __init__(self, model_type: str):
+ r"""Constructor for the token counter for LiteLLM models.
+
+ Args:
+ model_type (str): Model type for which tokens will be counted.
+ """
+ self.model_type = model_type
+ self._token_counter = None
+ self._completion_cost = None
+
+ @property
+ def token_counter(self):
+ if self._token_counter is None:
+ from litellm import token_counter
+
+ self._token_counter = token_counter
+ return self._token_counter
+
+ @property
+ def completion_cost(self):
+ if self._completion_cost is None:
+ from litellm import completion_cost
+
+ self._completion_cost = completion_cost
+ return self._completion_cost
+
+ def count_tokens_from_messages(self, messages: List[OpenAIMessage]) -> int:
+ r"""Count number of tokens in the provided message list using
+ the tokenizer specific to this type of model.
+
+ Args:
+ messages (List[OpenAIMessage]): Message list with the chat history
+ in LiteLLM API format.
+
+ Returns:
+ int: Number of tokens in the messages.
+ """
+ return self.token_counter(model=self.model_type, messages=messages)
+
+ def calculate_cost_from_response(self, response: dict) -> float:
+ r"""Calculate the cost of the given completion response.
+
+ Args:
+ response (dict): The completion response from LiteLLM.
+
+ Returns:
+ float: The cost of the completion call in USD.
+ """
+ return self.completion_cost(completion_response=response)
+
+
def count_tokens_from_image(
image: Image.Image, detail: OpenAIVisionDetailType
) -> int:
diff --git a/examples/litellm_models/litellm_model_example.py b/examples/litellm_models/litellm_model_example.py
new file mode 100644
index 000000000..8fca124b1
--- /dev/null
+++ b/examples/litellm_models/litellm_model_example.py
@@ -0,0 +1,109 @@
+# =========== 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 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.
+===============================================================================
+"""
diff --git a/poetry.lock b/poetry.lock
index 9ef338067..5b460f508 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -2491,6 +2491,32 @@ files = [
{file = "lazy_object_proxy-1.10.0-pp310.pp311.pp312.pp38.pp39-none-any.whl", hash = "sha256:80fa48bd89c8f2f456fc0765c11c23bf5af827febacd2f523ca5bc1893fcc09d"},
]
+[[package]]
+name = "litellm"
+version = "1.40.12"
+description = "Library to easily interface with LLM API providers"
+optional = true
+python-versions = "!=2.7.*,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,!=3.7.*,>=3.8"
+files = [
+ {file = "litellm-1.40.12-py3-none-any.whl", hash = "sha256:42f1648507f29c60543ba5fdf35d38fc161694da043b201508225bae50d3328c"},
+ {file = "litellm-1.40.12.tar.gz", hash = "sha256:366bb9c3694b9ef59b3d073bb37ff9ca175ab4090dc187b0a11d2b21db3a6a5d"},
+]
+
+[package.dependencies]
+aiohttp = "*"
+click = "*"
+importlib-metadata = ">=6.8.0"
+jinja2 = ">=3.1.2,<4.0.0"
+openai = ">=1.27.0"
+python-dotenv = ">=0.2.0"
+requests = ">=2.31.0,<3.0.0"
+tiktoken = ">=0.7.0"
+tokenizers = "*"
+
+[package.extras]
+extra-proxy = ["azure-identity (>=1.15.0,<2.0.0)", "azure-keyvault-secrets (>=4.8.0,<5.0.0)", "google-cloud-kms (>=2.21.3,<3.0.0)", "prisma (==0.11.0)", "resend (>=0.8.0,<0.9.0)"]
+proxy = ["PyJWT (>=2.8.0,<3.0.0)", "apscheduler (>=3.10.4,<4.0.0)", "backoff", "cryptography (>=42.0.5,<43.0.0)", "fastapi (>=0.111.0,<0.112.0)", "fastapi-sso (>=0.10.0,<0.11.0)", "gunicorn (>=22.0.0,<23.0.0)", "orjson (>=3.9.7,<4.0.0)", "python-multipart (>=0.0.9,<0.0.10)", "pyyaml (>=6.0.1,<7.0.0)", "rq", "uvicorn (>=0.22.0,<0.23.0)"]
+
[[package]]
name = "lxml"
version = "5.2.2"
@@ -2572,13 +2598,9 @@ files = [
{file = "lxml-5.2.2-cp36-cp36m-win_amd64.whl", hash = "sha256:edcfa83e03370032a489430215c1e7783128808fd3e2e0a3225deee278585196"},
{file = "lxml-5.2.2-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:28bf95177400066596cdbcfc933312493799382879da504633d16cf60bba735b"},
{file = "lxml-5.2.2-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:3a745cc98d504d5bd2c19b10c79c61c7c3df9222629f1b6210c0368177589fb8"},
- {file = "lxml-5.2.2-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1b590b39ef90c6b22ec0be925b211298e810b4856909c8ca60d27ffbca6c12e6"},
{file = "lxml-5.2.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b336b0416828022bfd5a2e3083e7f5ba54b96242159f83c7e3eebaec752f1716"},
- {file = "lxml-5.2.2-cp37-cp37m-manylinux_2_28_aarch64.whl", hash = "sha256:c2faf60c583af0d135e853c86ac2735ce178f0e338a3c7f9ae8f622fd2eb788c"},
{file = "lxml-5.2.2-cp37-cp37m-manylinux_2_28_x86_64.whl", hash = "sha256:4bc6cb140a7a0ad1f7bc37e018d0ed690b7b6520ade518285dc3171f7a117905"},
- {file = "lxml-5.2.2-cp37-cp37m-musllinux_1_1_aarch64.whl", hash = "sha256:7ff762670cada8e05b32bf1e4dc50b140790909caa8303cfddc4d702b71ea184"},
{file = "lxml-5.2.2-cp37-cp37m-musllinux_1_1_x86_64.whl", hash = "sha256:57f0a0bbc9868e10ebe874e9f129d2917750adf008fe7b9c1598c0fbbfdde6a6"},
- {file = "lxml-5.2.2-cp37-cp37m-musllinux_1_2_aarch64.whl", hash = "sha256:a6d2092797b388342c1bc932077ad232f914351932353e2e8706851c870bca1f"},
{file = "lxml-5.2.2-cp37-cp37m-musllinux_1_2_x86_64.whl", hash = "sha256:60499fe961b21264e17a471ec296dcbf4365fbea611bf9e303ab69db7159ce61"},
{file = "lxml-5.2.2-cp37-cp37m-win32.whl", hash = "sha256:d9b342c76003c6b9336a80efcc766748a333573abf9350f4094ee46b006ec18f"},
{file = "lxml-5.2.2-cp37-cp37m-win_amd64.whl", hash = "sha256:b16db2770517b8799c79aa80f4053cd6f8b716f21f8aca962725a9565ce3ee40"},
@@ -5364,28 +5386,28 @@ files = [
[[package]]
name = "ruff"
-version = "0.4.8"
+version = "0.4.9"
description = "An extremely fast Python linter and code formatter, written in Rust."
optional = false
python-versions = ">=3.7"
files = [
- {file = "ruff-0.4.8-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:7663a6d78f6adb0eab270fa9cf1ff2d28618ca3a652b60f2a234d92b9ec89066"},
- {file = "ruff-0.4.8-py3-none-macosx_11_0_arm64.whl", hash = "sha256:eeceb78da8afb6de0ddada93112869852d04f1cd0f6b80fe464fd4e35c330913"},
- {file = "ruff-0.4.8-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:aad360893e92486662ef3be0a339c5ca3c1b109e0134fcd37d534d4be9fb8de3"},
- {file = "ruff-0.4.8-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:284c2e3f3396fb05f5f803c9fffb53ebbe09a3ebe7dda2929ed8d73ded736deb"},
- {file = "ruff-0.4.8-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:a7354f921e3fbe04d2a62d46707e569f9315e1a613307f7311a935743c51a764"},
- {file = "ruff-0.4.8-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:72584676164e15a68a15778fd1b17c28a519e7a0622161eb2debdcdabdc71883"},
- {file = "ruff-0.4.8-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:9678d5c9b43315f323af2233a04d747409d1e3aa6789620083a82d1066a35199"},
- {file = "ruff-0.4.8-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:704977a658131651a22b5ebeb28b717ef42ac6ee3b11e91dc87b633b5d83142b"},
- {file = "ruff-0.4.8-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d05f8d6f0c3cce5026cecd83b7a143dcad503045857bc49662f736437380ad45"},
- {file = "ruff-0.4.8-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:6ea874950daca5697309d976c9afba830d3bf0ed66887481d6bca1673fc5b66a"},
- {file = "ruff-0.4.8-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:fc95aac2943ddf360376be9aa3107c8cf9640083940a8c5bd824be692d2216dc"},
- {file = "ruff-0.4.8-py3-none-musllinux_1_2_i686.whl", hash = "sha256:384154a1c3f4bf537bac69f33720957ee49ac8d484bfc91720cc94172026ceed"},
- {file = "ruff-0.4.8-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:e9d5ce97cacc99878aa0d084c626a15cd21e6b3d53fd6f9112b7fc485918e1fa"},
- {file = "ruff-0.4.8-py3-none-win32.whl", hash = "sha256:6d795d7639212c2dfd01991259460101c22aabf420d9b943f153ab9d9706e6a9"},
- {file = "ruff-0.4.8-py3-none-win_amd64.whl", hash = "sha256:e14a3a095d07560a9d6769a72f781d73259655919d9b396c650fc98a8157555d"},
- {file = "ruff-0.4.8-py3-none-win_arm64.whl", hash = "sha256:14019a06dbe29b608f6b7cbcec300e3170a8d86efaddb7b23405cb7f7dcaf780"},
- {file = "ruff-0.4.8.tar.gz", hash = "sha256:16d717b1d57b2e2fd68bd0bf80fb43931b79d05a7131aa477d66fc40fbd86268"},
+ {file = "ruff-0.4.9-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:b262ed08d036ebe162123170b35703aaf9daffecb698cd367a8d585157732991"},
+ {file = "ruff-0.4.9-py3-none-macosx_11_0_arm64.whl", hash = "sha256:98ec2775fd2d856dc405635e5ee4ff177920f2141b8e2d9eb5bd6efd50e80317"},
+ {file = "ruff-0.4.9-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4555056049d46d8a381f746680db1c46e67ac3b00d714606304077682832998e"},
+ {file = "ruff-0.4.9-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:e91175fbe48f8a2174c9aad70438fe9cb0a5732c4159b2a10a3565fea2d94cde"},
+ {file = "ruff-0.4.9-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:0e8e7b95673f22e0efd3571fb5b0cf71a5eaaa3cc8a776584f3b2cc878e46bff"},
+ {file = "ruff-0.4.9-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:2d45ddc6d82e1190ea737341326ecbc9a61447ba331b0a8962869fcada758505"},
+ {file = "ruff-0.4.9-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:78de3fdb95c4af084087628132336772b1c5044f6e710739d440fc0bccf4d321"},
+ {file = "ruff-0.4.9-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:06b60f91bfa5514bb689b500a25ba48e897d18fea14dce14b48a0c40d1635893"},
+ {file = "ruff-0.4.9-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:88bffe9c6a454bf8529f9ab9091c99490578a593cc9f9822b7fc065ee0712a06"},
+ {file = "ruff-0.4.9-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:673bddb893f21ab47a8334c8e0ea7fd6598ecc8e698da75bcd12a7b9d0a3206e"},
+ {file = "ruff-0.4.9-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:8c1aff58c31948cc66d0b22951aa19edb5af0a3af40c936340cd32a8b1ab7438"},
+ {file = "ruff-0.4.9-py3-none-musllinux_1_2_i686.whl", hash = "sha256:784d3ec9bd6493c3b720a0b76f741e6c2d7d44f6b2be87f5eef1ae8cc1d54c84"},
+ {file = "ruff-0.4.9-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:732dd550bfa5d85af8c3c6cbc47ba5b67c6aed8a89e2f011b908fc88f87649db"},
+ {file = "ruff-0.4.9-py3-none-win32.whl", hash = "sha256:8064590fd1a50dcf4909c268b0e7c2498253273309ad3d97e4a752bb9df4f521"},
+ {file = "ruff-0.4.9-py3-none-win_amd64.whl", hash = "sha256:e0a22c4157e53d006530c902107c7f550b9233e9706313ab57b892d7197d8e52"},
+ {file = "ruff-0.4.9-py3-none-win_arm64.whl", hash = "sha256:5d5460f789ccf4efd43f265a58538a2c24dbce15dbf560676e430375f20a8198"},
+ {file = "ruff-0.4.9.tar.gz", hash = "sha256:f1cb0828ac9533ba0135d148d214e284711ede33640465e706772645483427e3"},
]
[[package]]
@@ -7483,10 +7505,11 @@ doc = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "rst.linke
test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools", "jaraco.test", "more-itertools", "pytest (>=6,!=8.1.*)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-ignore-flaky", "pytest-mypy", "pytest-ruff (>=0.2.1)"]
[extras]
-all = ["PyMuPDF", "accelerate", "beautifulsoup4", "cohere", "datasets", "diffusers", "discord.py", "docx2txt", "duckduckgo-search", "googlemaps", "imageio", "neo4j", "newspaper3k", "openapi-spec-validator", "opencv-python", "pillow", "prance", "pyTelegramBotAPI", "pydub", "pygithub", "pymilvus", "pyowm", "qdrant-client", "rank-bm25", "requests_oauthlib", "sentence-transformers", "sentencepiece", "slack-sdk", "soundfile", "torch", "transformers", "unstructured", "wikipedia", "wolframalpha"]
+all = ["PyMuPDF", "accelerate", "beautifulsoup4", "cohere", "datasets", "diffusers", "discord.py", "docx2txt", "duckduckgo-search", "googlemaps", "imageio", "litellm", "neo4j", "newspaper3k", "openapi-spec-validator", "opencv-python", "pillow", "prance", "pyTelegramBotAPI", "pydub", "pygithub", "pymilvus", "pyowm", "qdrant-client", "rank-bm25", "requests_oauthlib", "sentence-transformers", "sentencepiece", "slack-sdk", "soundfile", "torch", "transformers", "unstructured", "wikipedia", "wolframalpha"]
encoders = ["sentence-transformers"]
graph-storages = ["neo4j"]
huggingface-agent = ["accelerate", "datasets", "diffusers", "opencv-python", "sentencepiece", "soundfile", "torch", "transformers"]
+model-platforms = ["anthropic", "litellm"]
retrievers = ["cohere", "rank-bm25"]
test = ["mock", "pytest", "pytest-asyncio"]
tools = ["PyMuPDF", "beautifulsoup4", "discord.py", "docx2txt", "duckduckgo-search", "googlemaps", "imageio", "newspaper3k", "openapi-spec-validator", "pillow", "prance", "pyTelegramBotAPI", "pydub", "pygithub", "pyowm", "requests_oauthlib", "slack-sdk", "unstructured", "wikipedia", "wolframalpha"]
@@ -7495,4 +7518,4 @@ vector-databases = ["pymilvus", "qdrant-client"]
[metadata]
lock-version = "2.0"
python-versions = ">=3.8.1,<3.12"
-content-hash = "a63364a15ab4f42e42adb0f07bb458ac00d0c49e1d39320492b98d943be8a743"
+content-hash = "d8e97625b518148a76f35b56d7bcb499bffeb92614097e7e52e4acac6ebf7e40"
diff --git a/pyproject.toml b/pyproject.toml
index ec7a76100..77545ddbc 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -40,6 +40,9 @@ docstring-parser = "^0.15"
pydantic = ">=1.9,<3"
curl_cffi = "0.6.2"
+# model-platforms
+litellm = { version = "^1.38.1", optional = true }
+
# huggingface-agent
transformers = { version = "^4", optional = true }
diffusers = { version = "^0", optional = true }
@@ -94,6 +97,11 @@ mock = { version = "^5", optional = true}
[tool.poetry.extras]
test = ["pytest", "mock", "pytest-asyncio"]
+model-platforms = [
+ "anthropic",
+ "litellm",
+]
+
huggingface-agent = [
"transformers",
"diffusers",
@@ -176,6 +184,7 @@ all = [
"imageio",
"slack-sdk",
"pydub",
+ "pillow",
# vector-database
"qdrant-client",
"pymilvus",
@@ -187,7 +196,8 @@ all = [
"neo4j",
# retrievers
"rank-bm25",
- "pillow",
+ # model platforms
+ "litellm",
]
[tool.poetry.group.dev]
@@ -309,7 +319,8 @@ module = [
"slack-sdk",
"pydub",
"pygithub",
+ "litellm",
"pyTelegramBotAPI",
- "discord.py"
+ "discord.py",
]
ignore_missing_imports = true
\ No newline at end of file
diff --git a/test/models/test_litellm_model.py b/test/models/test_litellm_model.py
new file mode 100644
index 000000000..0a40595c9
--- /dev/null
+++ b/test/models/test_litellm_model.py
@@ -0,0 +1,63 @@
+# =========== 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 ChatGPTConfig, OpenSourceConfig
+from camel.models import LiteLLMModel
+from camel.types import ModelType
+from camel.utils import LiteLLMTokenCounter
+
+
+ "model_type",
+ [
+ ModelType.GPT_3_5_TURBO,
+ ModelType.GPT_4,
+ ModelType.GPT_4_32K,
+ ModelType.GPT_4_TURBO,
+ ModelType.GPT_4O,
+ ],
+)
+def test_litellm_model(model_type):
+ model_config_dict = ChatGPTConfig().__dict__
+ model = LiteLLMModel(model_type, model_config_dict)
+ assert model.model_type == model_type
+ assert model.model_config_dict == model_config_dict
+ assert isinstance(model.token_counter, LiteLLMTokenCounter)
+ assert isinstance(model.model_type.value_for_tiktoken, str)
+ assert isinstance(model.model_type.token_limit, int)
+
+
+def test_litellm_model_unexpected_argument():
+ model_type = ModelType.GPT_4
+ model_config = OpenSourceConfig(
+ model_path="vicuna-7b-v1.5",
+ server_url="http://localhost:8000/v1",
+ )
+ model_config_dict = model_config.__dict__
+
+ with pytest.raises(
+ ValueError,
+ match=re.escape(
+ (
+ "Unexpected argument `model_path` is "
+ "input into LiteLLM model backend."
+ )
+ ),
+ ):
+ _ = LiteLLMModel(model_type, model_config_dict)