Skip to content

Commit b8fe312

Browse files
committed
Make anthropic import optional for tests
1 parent 61ac2f7 commit b8fe312

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

dsp/modules/anthropic.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
import backoff
33
import json
44
from typing import Optional, Any
5-
from anthropic import Anthropic, RateLimitError
65

76
from dsp.modules.lm import LM
87
import logging
98

9+
try:
10+
import anthropic
11+
anthropic_rate_limit = anthropic.RateLimitError
12+
except ImportError:
13+
anthropic_rate_limit = Exception
14+
1015

1116
logger = logging.getLogger(__name__)
1217

@@ -39,6 +44,12 @@ def __init__(
3944
**kwargs
4045
):
4146
super().__init__(model)
47+
48+
try:
49+
from anthropic import Anthropic, RateLimitError
50+
except ImportError as err:
51+
raise ImportError("Claude requires `pip install anthropic`.") from err
52+
4253
self.provider = "anthropic"
4354
self.api_key = api_key = os.environ.get("ANTHROPIC_API_KEY") if api_key is None else api_key
4455
self.api_base = BASE_URL if api_base is None else api_base
@@ -84,7 +95,7 @@ def basic_request(self, prompt: str, **kwargs):
8495

8596
@backoff.on_exception(
8697
backoff.expo,
87-
(RateLimitError),
98+
(anthropic_rate_limit),
8899
max_time=1000,
89100
max_tries=8,
90101
on_backoff=backoff_hdlr,

0 commit comments

Comments
 (0)