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

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.py
index 42ad19d53..970093999 100644
--- a/camel/models/anthropic_model.py
+++ b/camel/models/anthropic_model.py
@@ -14,8 +14,6 @@
import os
from typing import Any, Dict, List, Literal, Optional, Union
-from anthropic.types.beta import BetaMessageParam
-
from camel.configs import ANTHROPIC_API_PARAMS, AnthropicConfig
from camel.messages import OpenAIMessage
from 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.py
index 85513d4d4..2f831e662 100644
--- a/camel/utils/token_counting.py
+++ b/camel/utils/token_counting.py
@@ -20,7 +20,6 @@ from io import BytesIO
from math import ceil
from typing import TYPE_CHECKING, List, Optional
-from anthropic.types.beta import BetaMessageParam
from PIL import Image
from 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 using
loaded 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(