-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix: merge streaming tool deltas by logical index #3329
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
Closed
FU-max-boop
wants to merge
3
commits into
openai:main
from
FU-max-boop:fix/merge-duplicate-tool-call-deltas
+242
−87
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import cast | ||
|
|
||
| from openai.types.chat import ChatCompletionChunk | ||
| from openai.lib.streaming.chat import ChatCompletionStreamState | ||
| from openai.lib.streaming._deltas import accumulate_delta as accumulate_chat_delta | ||
| from openai.lib.streaming._assistants import accumulate_delta as accumulate_assistant_delta | ||
| from openai.types.chat.chat_completion_chunk import ( | ||
| Choice, | ||
| ChoiceDelta, | ||
| ChoiceDeltaToolCall, | ||
| ChoiceDeltaToolCallFunction, | ||
| ) | ||
|
|
||
|
|
||
| def test_accumulate_delta_merges_duplicate_indexed_entries_on_initial_chunk() -> None: | ||
| acc: dict[object, object] = {"tool_calls": None} | ||
|
|
||
| accumulate_chat_delta( | ||
| acc, | ||
| { | ||
| "tool_calls": [ | ||
| { | ||
| "index": 0, | ||
| "id": "call_abc", | ||
| "function": {"name": "get_weather"}, | ||
| "type": "function", | ||
| }, | ||
| {"index": 0, "function": {"arguments": '{"city"'}}, | ||
| ] | ||
| }, | ||
| ) | ||
| accumulate_chat_delta(acc, {"tool_calls": [{"index": 0, "function": {"arguments": ': "London"}'}}]}) | ||
|
|
||
| assert acc == { | ||
| "tool_calls": [ | ||
| { | ||
| "index": 0, | ||
| "id": "call_abc", | ||
| "function": {"name": "get_weather", "arguments": '{"city": "London"}'}, | ||
| "type": "function", | ||
| } | ||
| ] | ||
| } | ||
|
|
||
|
|
||
| def test_chat_completion_state_merges_duplicate_indexed_entries_on_initial_chunk() -> None: | ||
| state = ChatCompletionStreamState() | ||
|
|
||
| state.handle_chunk( | ||
| ChatCompletionChunk( | ||
| id="chatcmpl_abc", | ||
| choices=[ | ||
| Choice( | ||
| delta=ChoiceDelta( | ||
| role="assistant", | ||
| tool_calls=[ | ||
| ChoiceDeltaToolCall( | ||
| index=0, | ||
| id="call_abc", | ||
| function=ChoiceDeltaToolCallFunction(name="get_weather", arguments='{"city"'), | ||
| type="function", | ||
| ), | ||
| ChoiceDeltaToolCall( | ||
| index=0, | ||
| function=ChoiceDeltaToolCallFunction(arguments=': "London"}'), | ||
| ), | ||
| ], | ||
| ), | ||
| finish_reason=None, | ||
| index=0, | ||
| logprobs=None, | ||
| ) | ||
| ], | ||
| created=1, | ||
| model="gpt-test", | ||
| object="chat.completion.chunk", | ||
| ) | ||
| ) | ||
| state.handle_chunk( | ||
| ChatCompletionChunk( | ||
| id="chatcmpl_abc", | ||
| choices=[ | ||
| Choice(delta=ChoiceDelta(), finish_reason="tool_calls", index=0, logprobs=None), | ||
| ], | ||
| created=1, | ||
| model="gpt-test", | ||
| object="chat.completion.chunk", | ||
| ) | ||
| ) | ||
|
|
||
| tool_calls = state.get_final_completion().choices[0].message.tool_calls | ||
| assert tool_calls is not None | ||
| assert len(tool_calls) == 1 | ||
| assert tool_calls[0].id == "call_abc" | ||
| assert tool_calls[0].function.name == "get_weather" | ||
| assert tool_calls[0].function.arguments == '{"city": "London"}' | ||
|
|
||
|
|
||
| def test_assistant_accumulate_delta_uses_logical_index_for_initial_chunk() -> None: | ||
| acc: dict[object, object] = {} | ||
|
|
||
| accumulate_assistant_delta( | ||
| acc, | ||
| { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_abc", "function": {"name": "get_weather"}, "type": "function"}, | ||
| {"index": 0, "function": {"arguments": '{"path"'}}, | ||
| {"index": 1, "id": "call_def", "function": {"name": "list_files"}, "type": "function"}, | ||
| ] | ||
| }, | ||
| ) | ||
| accumulate_assistant_delta( | ||
| acc, | ||
| { | ||
| "tool_calls": [ | ||
| {"index": 1, "function": {"arguments": '{"limit": 10}'}}, | ||
| {"index": 0, "function": {"arguments": ': "."}'}}, | ||
| ] | ||
| }, | ||
| ) | ||
|
|
||
| assert acc == { | ||
| "tool_calls": [ | ||
| { | ||
| "index": 0, | ||
| "id": "call_abc", | ||
| "function": {"name": "get_weather", "arguments": '{"path": "."}'}, | ||
| "type": "function", | ||
| }, | ||
| { | ||
| "index": 1, | ||
| "id": "call_def", | ||
| "function": {"name": "list_files", "arguments": '{"limit": 10}'}, | ||
| "type": "function", | ||
| }, | ||
| ] | ||
| } | ||
|
|
||
|
|
||
| def test_assistant_accumulate_delta_merges_indexed_delta_into_full_snapshot() -> None: | ||
| acc: dict[object, object] = { | ||
| "tool_calls": [ | ||
| { | ||
| "id": "call_abc", | ||
| "function": {"name": "get_weather", "arguments": ""}, | ||
| "type": "function", | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| accumulate_assistant_delta( | ||
| acc, | ||
| {"tool_calls": [{"index": 0, "function": {"arguments": '{"city": "London"}'}}]}, | ||
| ) | ||
|
|
||
| tool_calls = cast(list[object], acc["tool_calls"]) | ||
| assert len(tool_calls) == 1 | ||
| assert tool_calls[0] == { | ||
| "index": 0, | ||
| "id": "call_abc", | ||
| "function": {"name": "get_weather", "arguments": '{"city": "London"}'}, | ||
| "type": "function", | ||
| } |
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.