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

chore: add timeout setting for all model backend (#2186)

Wendong-Fan794ceb4c0051
diff --git a/camel/models/cohere_model.py b/camel/models/cohere_model.py
index 544825410..6beee324c 100644
--- a/camel/models/cohere_model.py
+++ b/camel/models/cohere_model.py
@@ -44,7 +44,28 @@ except (ImportError, AttributeError):
class CohereModel(BaseModelBackend):
- r"""Cohere API in a unified BaseModelBackend interface."""
+ r"""Cohere API in a unified BaseModelBackend interface.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created, one of Cohere series.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`cohere.ClientV2().chat()`. If
+ :obj:`None`, :obj:`CohereConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating with
+ the Cohere service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the Cohere service.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter(
+ ModelType.GPT_4O_MINI)` will be used.
+ (default: :obj:`None`)
+ timeout (Optional[float], optional): The timeout value in seconds for
+ API calls. If not provided, will fall back to the MODEL_TIMEOUT
+ environment variable or default to 180 seconds.
+ (default: :obj:`None`)
+ """
@api_keys_required(
[
@@ -58,6 +79,7 @@ class CohereModel(BaseModelBackend):
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
+ timeout: Optional[float] = None,
):
import cohere
@@ -66,11 +88,17 @@ class CohereModel(BaseModelBackend):
api_key = api_key or os.environ.get("COHERE_API_KEY")
url = url or os.environ.get("COHERE_API_BASE_URL")
+
+ timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
super().__init__(
- model_type, model_config_dict, api_key, url, token_counter
+ model_type, model_config_dict, api_key, url, token_counter, timeout
+ )
+ self._client = cohere.ClientV2(
+ timeout=self._timeout, api_key=self._api_key
+ )
+ self._async_client = cohere.AsyncClientV2(
+ timeout=self._timeout, api_key=self._api_key
)
- self._client = cohere.ClientV2(api_key=self._api_key)
- self._async_client = cohere.AsyncClientV2(api_key=self._api_key)
def _to_openai_response(self, response: 'ChatResponse') -> ChatCompletion:
if response.usage and response.usage.tokens:
diff --git a/camel/models/litellm_model.py b/camel/models/litellm_model.py
index f5acf950e..e0d602485 100644
--- a/camel/models/litellm_model.py
+++ b/camel/models/litellm_model.py
@@ -11,6 +11,7 @@
# See the License for the specific language governing permissions and
# 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
@@ -33,8 +34,8 @@ class LiteLLMModel(BaseModelBackend):
model_type (Union[ModelType, str]): Model for which a backend is
created, such as GPT-3.5-turbo, Claude-2, etc.
model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
- that will be fed into:obj:`openai.ChatCompletion.create()`.
- If:obj:`None`, :obj:`LiteLLMConfig().as_dict()` will be used.
+ that will be fed into:obj:`completion()`. If:obj:`None`,
+ :obj:`LiteLLMConfig().as_dict()` will be used.
(default: :obj:`None`)
api_key (Optional[str], optional): The API key for authenticating with
the model service. (default: :obj:`None`)
@@ -43,6 +44,10 @@ class LiteLLMModel(BaseModelBackend):
token_counter (Optional[BaseTokenCounter], optional): Token counter to
use for the model. If not provided, :obj:`LiteLLMTokenCounter` will
be used. (default: :obj:`None`)
+ timeout (Optional[float], optional): The timeout value in seconds for
+ API calls. If not provided, will fall back to the MODEL_TIMEOUT
+ environment variable or default to 180 seconds.
+ (default: :obj:`None`)
"""
# NOTE: Currently stream mode is not supported.
@@ -55,16 +60,19 @@ class LiteLLMModel(BaseModelBackend):
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
+ timeout: Optional[float] = None,
) -> None:
from litellm import completion
if model_config_dict is None:
model_config_dict = LiteLLMConfig().as_dict()
-
+ timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
super().__init__(
- model_type, model_config_dict, api_key, url, token_counter
+ model_type, model_config_dict, api_key, url, token_counter, timeout
+ )
+ self.client = completion(
+ timeout=self._timeout, api_key=self._api_key, base_url=self._url
)
- self.client = completion
def _convert_response_from_litellm_to_openai(
self, response
diff --git a/camel/models/mistral_model.py b/camel/models/mistral_model.py
index a878263a6..ee4189984 100644
--- a/camel/models/mistral_model.py
+++ b/camel/models/mistral_model.py
@@ -62,6 +62,10 @@ class MistralModel(BaseModelBackend):
token_counter (Optional[BaseTokenCounter], optional): Token counter to
use for the model. If not provided, :obj:`OpenAITokenCounter` will
be used. (default: :obj:`None`)
+ timeout (Optional[float], optional): The timeout value in seconds for
+ API calls. If not provided, will fall back to the MODEL_TIMEOUT
+ environment variable or default to 180 seconds.
+ (default: :obj:`None`)
"""
@api_keys_required(
@@ -77,6 +81,7 @@ class MistralModel(BaseModelBackend):
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
+ timeout: Optional[float] = None,
) -> None:
from mistralai import Mistral
@@ -85,10 +90,17 @@ class MistralModel(BaseModelBackend):
api_key = api_key or os.environ.get("MISTRAL_API_KEY")
url = url or os.environ.get("MISTRAL_API_BASE_URL")
+ timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
super().__init__(
- model_type, model_config_dict, api_key, url, token_counter
+ model_type, model_config_dict, api_key, url, token_counter, timeout
+ )
+ self._client = Mistral(
+ timeout_ms=int(self._timeout)
+ if self._timeout is not None
+ else None,
+ api_key=self._api_key,
+ server_url=self._url,
)
- self._client = Mistral(api_key=self._api_key, server_url=self._url)
def _to_openai_response(
self, response: 'ChatCompletionResponse'
diff --git a/camel/models/reka_model.py b/camel/models/reka_model.py
index 4dae7dd34..129332508 100644
--- a/camel/models/reka_model.py
+++ b/camel/models/reka_model.py
@@ -56,6 +56,10 @@ class RekaModel(BaseModelBackend):
token_counter (Optional[BaseTokenCounter], optional): Token counter to
use for the model. If not provided, :obj:`OpenAITokenCounter` will
be used. (default: :obj:`None`)
+ timeout (Optional[float], optional): The timeout value in seconds for
+ API calls. If not provided, will fall back to the MODEL_TIMEOUT
+ environment variable or default to 180 seconds.
+ (default: :obj:`None`)
"""
@api_keys_required(
@@ -71,6 +75,7 @@ class RekaModel(BaseModelBackend):
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
+ timeout: Optional[float] = None,
) -> None:
from reka.client import AsyncReka, Reka
@@ -78,12 +83,15 @@ class RekaModel(BaseModelBackend):
model_config_dict = RekaConfig().as_dict()
api_key = api_key or os.environ.get("REKA_API_KEY")
url = url or os.environ.get("REKA_API_BASE_URL")
+ timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
super().__init__(
- model_type, model_config_dict, api_key, url, token_counter
+ model_type, model_config_dict, api_key, url, token_counter, timeout
+ )
+ self._client = Reka(
+ api_key=self._api_key, base_url=self._url, timeout=self._timeout
)
- self._client = Reka(api_key=self._api_key, base_url=self._url)
self._async_client = AsyncReka(
- api_key=self._api_key, base_url=self._url
+ api_key=self._api_key, base_url=self._url, timeout=self._timeout
)
def _convert_reka_to_openai_response(
diff --git a/camel/models/volcano_model.py b/camel/models/volcano_model.py
index 8b3c2e2ff..790f9b4a8 100644
--- a/camel/models/volcano_model.py
+++ b/camel/models/volcano_model.py
@@ -41,6 +41,10 @@ class VolcanoModel(OpenAICompatibleModel):
token_counter (Optional[BaseTokenCounter], optional): Token counter to
use for the model. If not provided, :obj:`OpenAITokenCounter`
will be used. (default: :obj:`None`)
+ timeout (Optional[float], optional): The timeout value in seconds for
+ API calls. If not provided, will fall back to the MODEL_TIMEOUT
+ environment variable or default to 180 seconds.
+ (default: :obj:`None`)
"""
@api_keys_required(
@@ -55,6 +59,7 @@ class VolcanoModel(OpenAICompatibleModel):
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
+ timeout: Optional[float] = None,
) -> None:
if model_config_dict is None:
model_config_dict = {}
@@ -65,9 +70,9 @@ class VolcanoModel(OpenAICompatibleModel):
or os.environ.get("VOLCANO_API_BASE_URL")
or "https://ark.cn-beijing.volces.com/api/v3"
)
-
+ timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
super().__init__(
- model_type, model_config_dict, api_key, url, token_counter
+ model_type, model_config_dict, api_key, url, token_counter, timeout
)
@property