|
3 | 3 | # SPDX-License-Identifier: Apache-2.0
|
4 | 4 |
|
5 | 5 | from dataclasses import asdict
|
| 6 | +from datetime import datetime |
6 | 7 | from typing import Any, Callable, Dict, Iterable, List, Optional, Union
|
7 | 8 |
|
8 | 9 | from haystack import component, default_from_dict, default_to_dict, logging
|
@@ -217,18 +218,26 @@ def _stream_and_build_response(
|
217 | 218 | self, hf_output: Iterable["TextGenerationStreamOutput"], streaming_callback: Callable[[StreamingChunk], None]
|
218 | 219 | ):
|
219 | 220 | chunks: List[StreamingChunk] = []
|
| 221 | + first_chunk_time = None |
| 222 | + |
220 | 223 | for chunk in hf_output:
|
221 | 224 | token: TextGenerationOutputToken = chunk.token
|
222 | 225 | if token.special:
|
223 | 226 | continue
|
| 227 | + |
224 | 228 | chunk_metadata = {**asdict(token), **(asdict(chunk.details) if chunk.details else {})}
|
| 229 | + if first_chunk_time is None: |
| 230 | + first_chunk_time = datetime.now().isoformat() |
| 231 | + |
225 | 232 | stream_chunk = StreamingChunk(token.text, chunk_metadata)
|
226 | 233 | chunks.append(stream_chunk)
|
227 | 234 | streaming_callback(stream_chunk)
|
| 235 | + |
228 | 236 | metadata = {
|
229 | 237 | "finish_reason": chunks[-1].meta.get("finish_reason", None),
|
230 | 238 | "model": self._client.model,
|
231 | 239 | "usage": {"completion_tokens": chunks[-1].meta.get("generated_tokens", 0)},
|
| 240 | + "completion_start_time": first_chunk_time, |
232 | 241 | }
|
233 | 242 | return {"replies": ["".join([chunk.content for chunk in chunks])], "meta": [metadata]}
|
234 | 243 |
|
|
0 commit comments