Commit e49972b0 authored by haileyschoelkopf's avatar haileyschoelkopf
Browse files

make AnthropicLM class still registered when missing anthropic extra

parent fa2d592a
...@@ -2,11 +2,7 @@ from . import huggingface ...@@ -2,11 +2,7 @@ from . import huggingface
from . import openai_completions from . import openai_completions
from . import textsynth from . import textsynth
from . import dummy from . import dummy
from . import anthropic_llms
try:
import anthropic
from . import anthropic_llms
except Exception:
raise "anthropic library is not yet installed"
# TODO: implement __all__ # TODO: implement __all__
...@@ -3,13 +3,12 @@ from lm_eval.api.model import LM ...@@ -3,13 +3,12 @@ from lm_eval.api.model import LM
from lm_eval.api.registry import register_model from lm_eval.api.registry import register_model
from tqdm import tqdm from tqdm import tqdm
import time import time
import anthropic
from lm_eval.logger import eval_logger from lm_eval.logger import eval_logger
from typing import List, Literal, Any from typing import List, Literal, Any
def anthropic_completion( def anthropic_completion(
client: anthropic.Anthropic, client, #: anthropic.Anthropic,
model: str, model: str,
prompt: str, prompt: str,
max_tokens_to_sample: int, max_tokens_to_sample: int,
...@@ -21,6 +20,15 @@ def anthropic_completion( ...@@ -21,6 +20,15 @@ def anthropic_completion(
Retry with back-off until they respond Retry with back-off until they respond
""" """
try:
import anthropic
except ModuleNotFoundError:
raise Exception(
"attempted to use 'anthropic' LM type, but package `anthropic` is not installed. \
please install anthropic via `pip install lm-eval[anthropic]` or `pip install -e .[anthropic]`",
)
backoff_time = 3 backoff_time = 3
while True: while True:
try: try:
...@@ -68,6 +76,14 @@ class AnthropicLM(LM): ...@@ -68,6 +76,14 @@ class AnthropicLM(LM):
""" """
super().__init__() super().__init__()
try:
import anthropic
except ModuleNotFoundError:
raise Exception(
"attempted to use 'anthropic' LM type, but package `anthropic` is not installed. \
please install anthropic via `pip install lm-eval[anthropic]` or `pip install -e .[anthropic]`",
)
self.model = model self.model = model
# defaults to os.environ.get("ANTHROPIC_API_KEY") # defaults to os.environ.get("ANTHROPIC_API_KEY")
self.client = anthropic.Anthropic() self.client = anthropic.Anthropic()
...@@ -135,10 +151,10 @@ class AnthropicLM(LM): ...@@ -135,10 +151,10 @@ class AnthropicLM(LM):
res.append(response) res.append(response)
self.cache_hook.add_partial("greedy_until", request, response) self.cache_hook.add_partial("greedy_until", request, response)
except anthropic.APIConnectionError as e: except anthropic.APIConnectionError as e: # noqa: F821
eval_logger.critical(f"Server unreachable: {e.__cause__}") eval_logger.critical(f"Server unreachable: {e.__cause__}")
break break
except anthropic.APIStatusError as e: except anthropic.APIStatusError as e: # noqa: F821
eval_logger.critical(f"API error {e.status_code}: {e.message}") eval_logger.critical(f"API error {e.status_code}: {e.message}")
break break
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment