forked from mistralai/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_completion.py
More file actions
99 lines (88 loc) · 3.4 KB
/
test_completion.py
File metadata and controls
99 lines (88 loc) · 3.4 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
89
90
91
92
93
94
95
96
97
98
99
from mistralai.models.chat_completion import (
ChatCompletionResponse,
ChatCompletionStreamResponse,
)
from .utils import (
mock_completion_response_payload,
mock_response,
mock_stream_response,
)
class TestCompletion:
def test_completion(self, client):
client._client.request.return_value = mock_response(
200,
mock_completion_response_payload(),
)
result = client.completion(
model="mistral-small-latest",
prompt="def add(a, b):",
suffix="return a + b",
temperature=0.5,
max_tokens=50,
top_p=0.9,
random_seed=42,
)
client._client.request.assert_called_once_with(
"post",
"https://api.mistral.ai/v1/fim/completions",
headers={
"User-Agent": f"mistral-client-python/{client._version}",
"Accept": "application/json",
"Authorization": "Bearer test_api_key",
"Content-Type": "application/json",
},
json={
"model": "mistral-small-latest",
"prompt": "def add(a, b):",
"suffix": "return a + b",
"stream": False,
"temperature": 0.5,
"max_tokens": 50,
"top_p": 0.9,
"random_seed": 42,
},
data=None,
)
assert isinstance(result, ChatCompletionResponse), "Should return an ChatCompletionResponse"
assert len(result.choices) == 1
assert result.choices[0].index == 0
assert result.object == "chat.completion"
def test_completion_streaming(self, client):
client._client.stream.return_value = mock_stream_response(
200,
mock_completion_response_payload(),
)
result = client.completion_stream(
model="mistral-small-latest", prompt="def add(a, b):", suffix="return a + b", stop=["#"]
)
results = list(result)
client._client.stream.assert_called_once_with(
"post",
"https://api.mistral.ai/v1/fim/completions",
headers={
"User-Agent": f"mistral-client-python/{client._version}",
"Accept": "text/event-stream",
"Authorization": "Bearer test_api_key",
"Content-Type": "application/json",
},
json={
"model": "mistral-small-latest",
"prompt": "def add(a, b):",
"suffix": "return a + b",
"stream": True,
"stop": ["#"],
},
data=None,
)
for i, result in enumerate(results):
if i == 0:
assert isinstance(result, ChatCompletionStreamResponse), "Should return an ChatCompletionStreamResponse"
assert len(result.choices) == 1
assert result.choices[0].index == 0
assert result.choices[0].delta.role == "assistant"
else:
assert isinstance(result, ChatCompletionStreamResponse), "Should return an ChatCompletionStreamResponse"
assert len(result.choices) == 1
assert result.choices[0].index == i - 1
assert result.choices[0].delta.content == f"stream response {i - 1}"
assert result.object == "chat.completion.chunk"