-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
test_rest.py
72 lines (64 loc) 路 2.08 KB
/
test_rest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# mypy: ignore-errors
import pytest
import spacy
import srsly # type: ignore[import]
PIPE_CFG = {
"backend": {
"@llm_backends": "spacy.REST.v1",
"api": "OpenAI",
"config": {"temperature": 0.3, "model": "text-davinci-003"},
},
"task": {"@llm_tasks": "spacy.NoOp.v1"},
}
@pytest.mark.external
def test_initialization():
"""Test initialization and simple run"""
nlp = spacy.blank("en")
nlp.add_pipe("llm", config=PIPE_CFG)
nlp("This is a test.")
@pytest.mark.external
@pytest.mark.parametrize("strict", (False, True))
def test_rest_backend_error_handling(strict: bool):
"""Test error handling for default/minimal REST backend.
strict (bool): Whether to use strict mode.
"""
nlp = spacy.blank("en")
nlp.add_pipe(
"llm",
config={
"task": {"@llm_tasks": "spacy.NoOp.v1"},
"backend": {"config": {"model": "x-text-davinci-003"}, "strict": strict},
},
)
if strict:
with pytest.raises(
ValueError,
match="API call failed: {'error': {'message': 'The model `x-text-davinci-003` does not exist', 'type': "
"'invalid_request_error', 'param': None, 'code': None}}.",
):
nlp("this is a test")
else:
response = nlp.get_pipe("llm")._backend(["this is a test"])
assert len(response) == 1
response = srsly.json_loads(response[0])
assert (
response["error"]["message"]
== "The model `x-text-davinci-003` does not exist"
)
def test_retries():
"""Test retry mechanism."""
# Run with 0 tries.
nlp = spacy.blank("en")
nlp.add_pipe(
"llm",
config={
"task": {"@llm_tasks": "spacy.NoOp.v1"},
"backend": {"config": {"model": "text-davinci-003"}, "max_tries": 0},
},
)
with pytest.raises(
ConnectionError,
match="OpenAI API could not be reached within 30 seconds in 0 attempts. Check your network connection and the "
"availability of the OpenAI API.",
):
nlp("this is a test")