-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: Update streaming chunk #9424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sjrl
wants to merge
25
commits into
main
Choose a base branch
from
update-streaming-chunk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
0446fe5
Start expanding StreamingChunk
sjrl f2ddbff
First pass at expanding Streaming Chunk
sjrl 6cb7a31
Working version!
sjrl 005ef69
Some tweaks and also make ToolInvoker stream a chunk with a finish re…
sjrl e29b6f2
Properly update test
sjrl 5914d5b
Change to tool_name, remove kw_only since its python 3.10 only and up…
sjrl d141b47
Add reno
sjrl ac51918
Some cleanup
sjrl 012c0bb
Fix unit tests
sjrl 6048328
Fix mypy and integration test
sjrl 010c037
Fix pylint
sjrl 93758fd
Merge branch 'main' of github.com:deepset-ai/haystack into update-str…
sjrl f43477d
Start refactoring huggingface local api
sjrl a907d9e
Refactor openai generator and chat generator to reuse util methods
sjrl ced8fd8
Did some reorg
sjrl 22314b8
Reusue utility method in HuggingFaceAPI
sjrl bc306d3
Merge branch 'main' of github.com:deepset-ai/haystack into update-str…
sjrl b625395
Merge branch 'main' of github.com:deepset-ai/haystack into update-str…
sjrl 8cbefeb
Get rid of unneeded default values in tests
sjrl 7cac572
Update conversion of streaming chunks to chat message to not rely on …
sjrl 4bfbe58
Fix tests and loosen check in StreamingChunk post_init
sjrl 3f8f661
Fixes
sjrl 51c8440
Fix license header
sjrl 658b47b
Add start and index to HFAPIGenerator
sjrl 27ca068
Fix mypy
sjrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -3,11 +3,34 @@ | |
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from dataclasses import dataclass, field | ||
from typing import Any, Awaitable, Callable, Dict, Optional, Union | ||
from typing import Any, Awaitable, Callable, Dict, List, Optional, Union | ||
|
||
from haystack.dataclasses.chat_message import ToolCallResult | ||
from haystack.utils.asynchronous import is_callable_async_compatible | ||
|
||
|
||
# Similar to ChoiceDeltaToolCall from OpenAI | ||
@dataclass(kw_only=True) | ||
class ToolCallDelta: | ||
""" | ||
Represents a Tool call prepared by the model, usually contained in an assistant message. | ||
|
||
:param id: The ID of the Tool call. | ||
:param name: The name of the Tool to call. | ||
:param arguments: Either the full arguments in JSON format or a delta of the arguments. | ||
""" | ||
|
||
id: Optional[str] = None # noqa: A003 | ||
name: Optional[str] = None | ||
arguments: Optional[str] = None | ||
|
||
def __post_init__(self): | ||
if self.name is None and self.arguments is None: | ||
raise ValueError("At least one of tool_name or arguments must be provided.") | ||
# NOTE: We allow for name and arguments to both be present because some providers like Mistral provide the | ||
# name and full arguments in one chunk | ||
|
||
|
||
@dataclass | ||
class StreamingChunk: | ||
""" | ||
|
@@ -17,10 +40,27 @@ class StreamingChunk: | |
|
||
:param content: The content of the message chunk as a string. | ||
:param meta: A dictionary containing metadata related to the message chunk. | ||
:param index: An optional integer index representing which content block this chunk belongs to. | ||
:param tool_call: An optional ToolCallDelta object representing a tool call associated with the message chunk. | ||
:param tool_call_result: An optional ToolCallResult object representing the result of a tool call. | ||
:param start: A boolean indicating whether this chunk marks the start of a content block. | ||
""" | ||
|
||
content: str | ||
meta: Dict[str, Any] = field(default_factory=dict, hash=False) | ||
index: Optional[int] = None | ||
tool_call: Optional[ToolCallDelta] = None | ||
tool_call_result: Optional[ToolCallResult] = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about making content = Union[str, ToolCallDelta, ToolCallResult] but this would be a breaking change b/c users expect content to always be a string. And this breaks StreamingChunk to ChatMessage implementations (mostly in private methods). |
||
start: Optional[bool] = None | ||
|
||
def __post_init__(self): | ||
fields_set = sum(bool(x) for x in (self.content, self.tool_call, self.tool_call_result)) | ||
if fields_set > 1: | ||
raise ValueError( | ||
"Only one of `content`, `tool_call`, or `tool_call_result` may be set in a StreamingChunk. " | ||
f"Got content: '{self.content}', tool_call: '{self.tool_call}', " | ||
f"tool_call_result: '{self.tool_call_result}'" | ||
) | ||
|
||
|
||
SyncStreamingCallbackT = Callable[[StreamingChunk], None] | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.