-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmistral_tracer.py
More file actions
324 lines (282 loc) · 10 KB
/
mistral_tracer.py
File metadata and controls
324 lines (282 loc) · 10 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""Module with methods used to trace Mistral LLMs."""
import json
import logging
import time
from functools import wraps
from typing import Any, Dict, Iterator, Optional, Union, TYPE_CHECKING
try:
import mistralai
HAVE_MISTRAL = True
except ImportError:
HAVE_MISTRAL = False
if TYPE_CHECKING:
import mistralai
from ..tracing import tracer
logger = logging.getLogger(__name__)
def trace_mistral(
client: "mistralai.Mistral",
) -> "mistralai.Mistral":
"""Patch the Mistral client to trace chat completions.
The following information is collected for each chat completion:
- start_time: The time when the completion was requested.
- end_time: The time when the completion was received.
- latency: The time it took to generate the completion.
- tokens: The total number of tokens used to generate the completion.
- prompt_tokens: The number of tokens in the prompt.
- completion_tokens: The number of tokens in the completion.
- model: The model used to generate the completion.
- model_parameters: The parameters used to configure the model.
- raw_output: The raw output of the model.
- inputs: The inputs used to generate the completion.
- metadata: Additional metadata about the completion. For example, the time it
took to generate the first token, when streaming.
Parameters
----------
client : mistralai.Mistral
The Mistral client to patch.
Returns
-------
mistralai.Mistral
The patched Mistral client.
"""
if not HAVE_MISTRAL:
raise ImportError(
"Mistral library is not installed. Please install it with: pip install mistralai"
)
stream_func = client.chat.stream
create_func = client.chat.complete
@wraps(stream_func)
def traced_stream_func(*args, **kwargs):
inference_id = kwargs.pop("inference_id", None)
return handle_streaming_create(
*args,
**kwargs,
create_func=stream_func,
inference_id=inference_id,
)
@wraps(create_func)
def traced_create_func(*args, **kwargs):
inference_id = kwargs.pop("inference_id", None)
return handle_non_streaming_create(
*args,
**kwargs,
create_func=create_func,
inference_id=inference_id,
)
client.chat.stream = traced_stream_func
client.chat.complete = traced_create_func
return client
def handle_streaming_create(
create_func: callable,
*args,
inference_id: Optional[str] = None,
**kwargs,
) -> Iterator[Any]:
"""Handles the create method when streaming is enabled.
Parameters
----------
create_func : callable
The create method to handle.
inference_id : Optional[str], optional
A user-generated inference id, by default None
Returns
-------
Iterator[Any]
A generator that yields the chunks of the completion.
"""
chunks = create_func(*args, **kwargs)
return stream_chunks(
chunks=chunks,
kwargs=kwargs,
inference_id=inference_id,
)
def stream_chunks(
chunks: Iterator[Any],
kwargs: Dict[str, any],
inference_id: Optional[str] = None,
):
"""Streams the chunks of the completion and traces the completion."""
collected_output_data = []
collected_function_call = {
"name": "",
"arguments": "",
}
raw_outputs = []
start_time = time.time()
end_time = None
first_token_time = None
num_of_completion_tokens = None
latency = None
try:
i = 0
for i, chunk in enumerate(chunks):
raw_outputs.append(chunk.model_dump())
if i == 0:
first_token_time = time.time()
if i > 0:
num_of_completion_tokens = i + 1
delta = chunk.data.choices[0].delta
if delta.content:
collected_output_data.append(delta.content)
elif delta.tool_calls:
if delta.tool_calls[0].function.name:
collected_function_call["name"] += delta.tool_calls[0].function.name
if delta.tool_calls[0].function.arguments:
collected_function_call["arguments"] += delta.tool_calls[0].function.arguments
yield chunk
end_time = time.time()
latency = (end_time - start_time) * 1000
# pylint: disable=broad-except
except Exception as e:
logger.error("Failed yield chunk. %s", e)
finally:
# Try to add step to the trace
try:
collected_output_data = [message for message in collected_output_data if message is not None]
if collected_output_data:
output_data = "".join(collected_output_data)
else:
collected_function_call["arguments"] = json.loads(collected_function_call["arguments"])
output_data = collected_function_call
# Get usage data from the last chunk
usage = chunk.model_dump()["data"].get("usage", {})
trace_args = create_trace_args(
end_time=end_time,
inputs={"prompt": kwargs["messages"]},
output=output_data,
latency=latency,
tokens=usage.get("total_tokens", num_of_completion_tokens),
prompt_tokens=usage.get("prompt_tokens", 0),
completion_tokens=usage.get("completion_tokens", num_of_completion_tokens),
model=kwargs.get("model"),
model_parameters=get_model_parameters(kwargs),
raw_output=raw_outputs,
id=inference_id,
metadata={"timeToFirstToken": ((first_token_time - start_time) * 1000 if first_token_time else None)},
)
add_to_trace(
**trace_args,
)
# pylint: disable=broad-except
except Exception as e:
logger.error(
"Failed to trace the create chat completion request with Openlayer. %s",
e,
)
def handle_non_streaming_create(
create_func: callable,
*args,
inference_id: Optional[str] = None,
**kwargs,
) -> "mistralai.models.ChatCompletionResponse":
"""Handles the create method when streaming is disabled.
Parameters
----------
create_func : callable
The create method to handle.
inference_id : Optional[str], optional
A user-generated inference id, by default None
Returns
-------
mistralai.models.ChatCompletionResponse
The chat completion response.
"""
start_time = time.time()
response = create_func(*args, **kwargs)
end_time = time.time()
# Try to add step to the trace
try:
output_data = parse_non_streaming_output_data(response)
trace_args = create_trace_args(
end_time=end_time,
inputs={"prompt": kwargs["messages"]},
output=output_data,
latency=(end_time - start_time) * 1000,
tokens=response.usage.total_tokens,
prompt_tokens=response.usage.prompt_tokens,
completion_tokens=response.usage.completion_tokens,
model=response.model,
model_parameters=get_model_parameters(kwargs),
raw_output=response.model_dump(),
id=inference_id,
)
add_to_trace(
**trace_args,
)
# pylint: disable=broad-except
except Exception as e:
logger.error("Failed to trace the create chat completion request with Openlayer. %s", e)
return response
def parse_non_streaming_output_data(
response: "mistralai.models.ChatCompletionResponse",
) -> Union[str, Dict[str, Any], None]:
"""Parses the output data from a non-streaming completion.
Parameters
----------
response : mistralai.models.ChatCompletionResponse
The chat completion response.
Returns
-------
Union[str, Dict[str, Any], None]
The parsed output data.
"""
output_content = response.choices[0].message.content
output_tool_calls = response.choices[0].message.tool_calls
if output_content:
output_data = output_content.strip()
elif output_tool_calls:
function_call = {
"name": output_tool_calls[0].function.name,
"arguments": json.loads(output_tool_calls[0].function.arguments),
}
output_data = function_call
else:
output_data = None
return output_data
def get_model_parameters(kwargs: Dict[str, Any]) -> Dict[str, Any]:
"""Gets the model parameters from the kwargs."""
return {
"temperature": kwargs.get("temperature", 0.7),
"top_p": kwargs.get("top_p", 1.0),
"max_tokens": kwargs.get("max_tokens"),
"min_tokens": kwargs.get("min_tokens"),
"stream": kwargs.get("stream", False),
"stop": kwargs.get("stop", None),
"random_seed": kwargs.get("random_seed"),
"response_format": kwargs.get("response_format", "text"),
"safe_prompt": kwargs.get("safe_prompt", False),
}
def create_trace_args(
end_time: float,
inputs: Dict,
output: str,
latency: float,
tokens: int,
prompt_tokens: int,
completion_tokens: int,
model: str,
model_parameters: Optional[Dict] = None,
metadata: Optional[Dict] = None,
raw_output: Optional[str] = None,
id: Optional[str] = None,
) -> Dict:
"""Returns a dictionary with the trace arguments."""
trace_args = {
"end_time": end_time,
"inputs": inputs,
"output": output,
"latency": latency,
"tokens": tokens,
"prompt_tokens": prompt_tokens,
"completion_tokens": completion_tokens,
"model": model,
"model_parameters": model_parameters,
"raw_output": raw_output,
"metadata": metadata if metadata else {},
}
if id:
trace_args["id"] = id
return trace_args
def add_to_trace(**kwargs) -> None:
"""Add a chat completion step to the trace."""
tracer.add_chat_completion_step_to_trace(**kwargs, name="Mistral Chat Completion", provider="Mistral")