forked from openai/openai-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_endpoints.py
More file actions
88 lines (67 loc) · 2.16 KB
/
Copy pathtest_endpoints.py
File metadata and controls
88 lines (67 loc) · 2.16 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import io
import json
import pytest
import openai
from openai import error
# FILE TESTS
def test_file_upload():
result = openai.File.create(
file=io.StringIO(
json.dumps({"prompt": "test file data", "completion": "tada"})
),
purpose="fine-tune",
)
assert result.purpose == "fine-tune"
assert "id" in result
result = openai.File.retrieve(id=result.id)
assert result.status == "uploaded"
# CHAT COMPLETION TESTS
def test_chat_completions():
result = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello!"}]
)
assert len(result.choices) == 1
def test_chat_completions_multiple():
result = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello!"}], n=5
)
assert len(result.choices) == 5
def test_chat_completions_streaming():
result = None
events = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}],
stream=True,
)
for result in events:
assert len(result.choices) == 1
# COMPLETION TESTS
def test_completions():
result = openai.Completion.create(prompt="This was a test", n=5, engine="ada")
assert len(result.choices) == 5
def test_completions_multiple_prompts():
result = openai.Completion.create(
prompt=["This was a test", "This was another test"], n=5, engine="ada"
)
assert len(result.choices) == 10
def test_completions_model():
result = openai.Completion.create(prompt="This was a test", n=5, model="ada")
assert len(result.choices) == 5
assert result.model.startswith("ada")
def test_timeout_raises_error():
# A query that should take awhile to return
with pytest.raises(error.Timeout):
openai.Completion.create(
prompt="test" * 1000,
n=10,
model="ada",
max_tokens=100,
request_timeout=0.01,
)
def test_timeout_does_not_error():
# A query that should be fast
openai.Completion.create(
prompt="test",
model="ada",
request_timeout=10,
)