Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
lm-evaluation-harness
Commits
eabadf46
"vscode:/vscode.git/clone" did not exist on "a57b7cf8499b268402982127c0a594c0fc3ba5f3"
Commit
eabadf46
authored
Aug 05, 2023
by
baberabb
Browse files
added type hints
parent
e9b938f2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
16 deletions
+31
-16
lm_eval/models/anthropic_llms.py
lm_eval/models/anthropic_llms.py
+31
-16
No files found.
lm_eval/models/anthropic_llms.py
View file @
eabadf46
import
os
from
lm_eval.api.model
import
LM
from
lm_eval.api.registry
import
register_model
from
tqdm
import
tqdm
import
time
from
lm_eval.logger
import
eval_logger
from
typing
import
List
,
Literal
,
Any
from
typing
import
List
,
Literal
,
Any
,
Tuple
,
Optional
def
anthropic_completion
(
...
...
@@ -15,10 +14,25 @@ def anthropic_completion(
temperature
:
float
,
stop
:
List
[
str
],
**
kwargs
:
Any
,
):
"""Query Anthropic API for completion.
Retry with back-off until they respond
)
->
str
:
"""Wrapper function around the Anthropic completion API client with exponential back-off
in case of RateLimitError.
params:
client: anthropic.Anthropic
Anthropic API client
model: str
Anthropic model e.g. 'claude-instant-v1', 'claude-2'
prompt: str
Prompt to feed to the model
max_tokens_to_sample: int
Maximum number of tokens to sample from the model
temperature: float
Sampling temperature
stop: List[str]
List of stop sequences
kwargs: Any
Additional model_args to pass to the API client
"""
try
:
...
...
@@ -29,7 +43,7 @@ def anthropic_completion(
please install anthropic via `pip install lm-eval[anthropic]` or `pip install -e .[anthropic]`"
,
)
backoff_time
=
3
backoff_time
:
float
=
3
while
True
:
try
:
response
=
client
.
completions
.
create
(
...
...
@@ -94,15 +108,15 @@ please install anthropic via `pip install lm-eval[anthropic]` or `pip install -e
@
property
def
eot_token_id
(
self
):
# Not sure but anthropic.
AI
_PROMPT
-> [203, 203, 50803, 30]
# Not sure but anthropic.
HUMAN
_PROMPT
?
raise
NotImplementedError
(
"No idea about anthropic tokenization."
)
@
property
def
max_length
(
self
):
def
max_length
(
self
)
->
int
:
return
2048
@
property
def
max_gen_toks
(
self
):
def
max_gen_toks
(
self
)
->
int
:
return
self
.
max_tokens_to_sample
@
property
...
...
@@ -124,14 +138,15 @@ please install anthropic via `pip install lm-eval[anthropic]` or `pip install -e
def
_loglikelihood_tokens
(
self
,
requests
,
disable_tqdm
=
False
):
raise
NotImplementedError
(
"No support for logits."
)
def
greedy_until
(
self
,
requests
):
def
greedy_until
(
self
,
requests
)
->
List
[
str
]:
if
not
requests
:
return
[]
requests
=
[
req
.
args
for
req
in
requests
]
_
requests
:
List
[
Tuple
[
str
,
dict
]]
=
[
req
.
args
for
req
in
requests
]
res
=
[]
for
request
in
tqdm
(
requests
):
for
request
in
tqdm
(
_
requests
):
try
:
inp
=
request
[
0
]
request_args
=
request
[
1
]
...
...
@@ -145,16 +160,16 @@ please install anthropic via `pip install lm-eval[anthropic]` or `pip install -e
prompt
=
inp
,
max_tokens_to_sample
=
max_gen_toks
,
temperature
=
temperature
,
# TODO: implement non-greedy sampling for Anthropic
stop
=
until
,
stop
=
until
,
# type: ignore
**
self
.
kwargs
,
)
res
.
append
(
response
)
self
.
cache_hook
.
add_partial
(
"greedy_until"
,
request
,
response
)
except
anthropic
.
APIConnectionError
as
e
:
# noqa: F821
except
anthropic
.
APIConnectionError
as
e
:
#
type: ignore #
noqa: F821
eval_logger
.
critical
(
f
"Server unreachable:
{
e
.
__cause__
}
"
)
break
except
anthropic
.
APIStatusError
as
e
:
# noqa: F821
except
anthropic
.
APIStatusError
as
e
:
#
type: ignore #
noqa: F821
eval_logger
.
critical
(
f
"API error
{
e
.
status_code
}
:
{
e
.
message
}
"
)
break
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment