🐫 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
Fix: Import anthropic package when used since it's optional (#1319)
CaelumF1659bd90ef41
diff --git a/camel/models/anthropic_model.py b/camel/models/anthropic_model.pyindex 42ad19d53..970093999 100644--- a/camel/models/anthropic_model.py+++ b/camel/models/anthropic_model.py@@ -14,8 +14,6 @@import osfrom typing import Any, Dict, List, Literal, Optional, Union-from anthropic.types.beta import BetaMessageParam-from camel.configs import ANTHROPIC_API_PARAMS, AnthropicConfigfrom camel.messages import OpenAIMessagefrom camel.models.base_model import BaseModelBackend@@ -99,6 +97,7 @@ class AnthropicModel(BaseModelBackend):self._token_counter = AnthropicTokenCounter(self.model_type)return self._token_counter+ @dependencies_required('anthropic')def count_tokens_from_prompt(self, prompt: str, role: Literal["user", "assistant"]) -> int:@@ -112,6 +111,8 @@ class AnthropicModel(BaseModelBackend):Returns:int: The number of tokens in the prompt."""+ from anthropic.types.beta import BetaMessageParam+return self.client.beta.messages.count_tokens(messages=[BetaMessageParam(content=prompt, role=role)],model=self.model_type,diff --git a/camel/utils/token_counting.py b/camel/utils/token_counting.pyindex 85513d4d4..2f831e662 100644--- a/camel/utils/token_counting.py+++ b/camel/utils/token_counting.py@@ -20,7 +20,6 @@ from io import BytesIOfrom math import ceilfrom typing import TYPE_CHECKING, List, Optional-from anthropic.types.beta import BetaMessageParamfrom PIL import Imagefrom camel.logger import get_logger@@ -234,6 +233,7 @@ class AnthropicTokenCounter(BaseTokenCounter):self.client = Anthropic()self.model = model+ @dependencies_required('anthropic')def count_tokens_from_messages(self, messages: List[OpenAIMessage]) -> int:r"""Count number of tokens in the provided message list usingloaded tokenizer specific for this type of model.@@ -245,6 +245,8 @@ class AnthropicTokenCounter(BaseTokenCounter):Returns:int: Number of tokens in the messages."""+ from anthropic.types.beta import BetaMessageParam+return self.client.beta.messages.count_tokens(messages=[BetaMessageParam(