-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add completion start time timestamp to relevant generators (#8728)
* OpenAIChatGenerator - add completion_start_time * HuggingFaceAPIChatGenerator - add completion_start_time * Add tests * Add reno note * Relax condition for cached responses * Add completion_start_time timestamping to non-chat generators * Update haystack/components/generators/chat/hugging_face_api.py Co-authored-by: Stefano Fiorucci <[email protected]> * PR feedback --------- Co-authored-by: Stefano Fiorucci <[email protected]>
- Loading branch information
Showing
9 changed files
with
71 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
releasenotes/notes/add-streaming-completion-timestamp-c0ad3b8698a2d575.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
enhancements: | ||
- | | ||
Added completion_start_time metadata to track time-to-first-token (TTFT) in streaming responses from Hugging Face API and OpenAI (Azure). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from datetime import datetime | ||
import os | ||
from unittest.mock import MagicMock, Mock, patch | ||
|
||
|
@@ -503,9 +504,13 @@ def test_live_run_serverless_streaming(self): | |
assert isinstance(response["replies"], list) | ||
assert len(response["replies"]) > 0 | ||
assert [isinstance(reply, ChatMessage) for reply in response["replies"]] | ||
assert "usage" in response["replies"][0].meta | ||
assert "prompt_tokens" in response["replies"][0].meta["usage"] | ||
assert "completion_tokens" in response["replies"][0].meta["usage"] | ||
|
||
response_meta = response["replies"][0].meta | ||
assert "completion_start_time" in response_meta | ||
assert datetime.fromisoformat(response_meta["completion_start_time"]) <= datetime.now() | ||
assert "usage" in response_meta | ||
assert "prompt_tokens" in response_meta["usage"] | ||
assert "completion_tokens" in response_meta["usage"] | ||
|
||
@pytest.mark.integration | ||
@pytest.mark.skipif( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from datetime import datetime | ||
import logging | ||
import os | ||
from typing import List | ||
|
@@ -286,6 +287,9 @@ def __call__(self, chunk: StreamingChunk) -> None: | |
assert "gpt-4o-mini" in metadata["model"] | ||
assert metadata["finish_reason"] == "stop" | ||
|
||
assert "completion_start_time" in metadata | ||
assert datetime.fromisoformat(metadata["completion_start_time"]) <= datetime.now() | ||
|
||
# unfortunately, the usage is not available for streaming calls | ||
# we keep the key in the metadata for compatibility | ||
assert "usage" in metadata and len(metadata["usage"]) == 0 | ||
|