Skip to content

Commit

Permalink
Lazy instantiate Aleph Alpha Client to pass regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
JosselinSomervilleRoberts committed Nov 7, 2023
1 parent e5436d3 commit 48e01c9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
3 changes: 0 additions & 3 deletions src/helm/proxy/clients/aleph_alpha_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import requests
from typing import Any, Dict, List

from aleph_alpha_client import Client as AlephAlphaPythonClient

from helm.common.cache import CacheConfig
from helm.common.request import wrap_request_time, Request, RequestResult, Sequence, Token
from helm.proxy.tokenizers.tokenizer import Tokenizer
Expand All @@ -16,7 +14,6 @@ class AlephAlphaClient(CachingClient):
def __init__(self, api_key: str, tokenizer: Tokenizer, cache_config: CacheConfig):
super().__init__(cache_config=cache_config, tokenizer=tokenizer)
self.api_key: str = api_key
self._aleph_alpha_client = AlephAlphaPythonClient(token=api_key)

def _send_request(self, endpoint: str, raw_request: Dict[str, Any]) -> Dict[str, Any]:
response = requests.request(
Expand Down
4 changes: 3 additions & 1 deletion src/helm/proxy/tokenizers/aleph_alpha_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AlephAlphaTokenizer(CachingTokenizer):
def __init__(self, api_key: str, cache_config: CacheConfig) -> None:
super().__init__(cache_config)
self.api_key: str = api_key
self._aleph_alpha_client = AlephAlphaPythonClient(token=api_key)
self._aleph_alpha_client = AlephAlphaPythonClient(token=api_key) if api_key else None
self._tokenizer_name_to_tokenizer: Dict[str, InternalTokenizer] = {}

def _get_tokenizer(self, tokenizer_name: str) -> InternalTokenizer:
Expand All @@ -40,6 +40,8 @@ def _get_tokenizer(self, tokenizer_name: str) -> InternalTokenizer:

# Check if the tokenizer is cached
if tokenizer_name not in self._tokenizer_name_to_tokenizer:
if self._aleph_alpha_client is None:
raise ValueError("Aleph Alpha API key not set.")
self._tokenizer_name_to_tokenizer[tokenizer_name] = self._aleph_alpha_client.tokenizer(tokenizer_name)
hlog(f"Initialized tokenizer: {tokenizer_name}")
return self._tokenizer_name_to_tokenizer[tokenizer_name]
Expand Down

0 comments on commit 48e01c9

Please sign in to comment.