forked from mistralai/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
227 lines (210 loc) · 7.7 KB
/
utils.py
File metadata and controls
227 lines (210 loc) · 7.7 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import contextlib
import unittest.mock as mock
from typing import List
import orjson
from httpx import Response
@contextlib.contextmanager
def mock_stream_response(status_code: int, content: List[str]):
response = mock.Mock(Response)
response.status_code = status_code
response.iter_lines.return_value = iter(content)
yield response
@contextlib.asynccontextmanager
async def mock_async_stream_response(status_code: int, content: List[str]):
response = mock.Mock(Response)
response.status_code = status_code
async def async_iter(content: List[str]):
for line in content:
yield line
response.aiter_lines.return_value = async_iter(content)
yield response
def mock_response(
status_code: int, content: str, is_json: bool = True
) -> mock.MagicMock:
response = mock.Mock(Response)
response.status_code = status_code
if is_json:
response.json = mock.MagicMock()
response.json.return_value = orjson.loads(content)
response.text = content
return response
def mock_list_models_response_payload() -> str:
return orjson.dumps(
{
"object": "list",
"data": [
{
"id": "mistral-medium",
"object": "model",
"created": 1703186988,
"owned_by": "mistralai",
"root": None,
"parent": None,
"permission": [
{
"id": "modelperm-15bebaf316264adb84b891bf06a84933",
"object": "model_permission",
"created": 1703186988,
"allow_create_engine": False,
"allow_sampling": True,
"allow_logprobs": False,
"allow_search_indices": False,
"allow_view": True,
"allow_fine_tuning": False,
"organization": "*",
"group": None,
"is_blocking": False,
}
],
},
{
"id": "mistral-small",
"object": "model",
"created": 1703186988,
"owned_by": "mistralai",
"root": None,
"parent": None,
"permission": [
{
"id": "modelperm-d0dced5c703242fa862f4ca3f241c00e",
"object": "model_permission",
"created": 1703186988,
"allow_create_engine": False,
"allow_sampling": True,
"allow_logprobs": False,
"allow_search_indices": False,
"allow_view": True,
"allow_fine_tuning": False,
"organization": "*",
"group": None,
"is_blocking": False,
}
],
},
{
"id": "mistral-tiny",
"object": "model",
"created": 1703186988,
"owned_by": "mistralai",
"root": None,
"parent": None,
"permission": [
{
"id": "modelperm-0e64e727c3a94f17b29f8895d4be2910",
"object": "model_permission",
"created": 1703186988,
"allow_create_engine": False,
"allow_sampling": True,
"allow_logprobs": False,
"allow_search_indices": False,
"allow_view": True,
"allow_fine_tuning": False,
"organization": "*",
"group": None,
"is_blocking": False,
}
],
},
{
"id": "mistral-embed",
"object": "model",
"created": 1703186988,
"owned_by": "mistralai",
"root": None,
"parent": None,
"permission": [
{
"id": "modelperm-ebdff9046f524e628059447b5932e3ad",
"object": "model_permission",
"created": 1703186988,
"allow_create_engine": False,
"allow_sampling": True,
"allow_logprobs": False,
"allow_search_indices": False,
"allow_view": True,
"allow_fine_tuning": False,
"organization": "*",
"group": None,
"is_blocking": False,
}
],
},
],
}
)
def mock_embedding_response_payload(batch_size: int = 1) -> str:
return orjson.dumps(
{
"id": "embd-98c8c60e3fbf4fc49658eddaf447357c",
"object": "list",
"data": [
{
"object": "embedding",
"embedding": [-0.018585205078125, 0.027099609375, 0.02587890625],
"index": 0,
}
]
* batch_size,
"model": "mistral-embed",
"usage": {"prompt_tokens": 90, "total_tokens": 90, "completion_tokens": 0},
}
).decode()
def mock_chat_response_payload():
return orjson.dumps(
{
"id": "chat-98c8c60e3fbf4fc49658eddaf447357c",
"object": "chat.completion",
"created": 1703165682,
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "What is the best French cheese?",
},
"index": 0,
}
],
"model": "mistral-small",
"usage": {"prompt_tokens": 90, "total_tokens": 90, "completion_tokens": 0},
}
).decode()
def mock_chat_response_streaming_payload():
return [
"data: "
+ orjson.dumps(
{
"id": "cmpl-8cd9019d21ba490aa6b9740f5d0a883e",
"model": "mistral-small",
"choices": [
{
"index": 0,
"delta": {"role": "assistant"},
"finish_reason": None,
}
],
}
).decode()
+ "\n\n",
*[
"data: "
+ orjson.dumps(
{
"id": "cmpl-8cd9019d21ba490aa6b9740f5d0a883e",
"object": "chat.completion.chunk",
"created": 1703168544,
"model": "mistral-small",
"choices": [
{
"index": i,
"delta": {"content": f"stream response {i}"},
"finish_reason": None,
}
],
}
).decode()
+ "\n\n"
for i in range(10)
],
"data: [DONE]\n\n",
]