-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reusing usage reported by the upstream OpenAI via include_usage …
…option (#151)
- Loading branch information
Showing
6 changed files
with
249 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import TypeVar | ||
|
||
from aidial_sdk.utils.merge_chunks import merge | ||
|
||
_Chunk = TypeVar("_Chunk", bound=dict) | ||
|
||
|
||
def merge_chunks(*chunks: _Chunk) -> _Chunk: | ||
""" | ||
The recursive merging procedure that avoids merging top-level atomic fields | ||
(e.g. "id", "created", "model", "object", "system_fingerprint") and | ||
instead chooses an _override_ merging strategy for such fields. | ||
Non-atomic fields (e.g. "choice", "usage") are merged following | ||
the standard recursive merging procedure. | ||
""" | ||
|
||
assert len(chunks) > 0, "At least one chunk must be provided" | ||
|
||
target = chunks[0] | ||
|
||
for chunk in chunks[1:]: | ||
source = chunk.copy() | ||
for key, value in list(source.items()): | ||
if not isinstance(value, (list, dict)) and value is not None: | ||
target[key] = value | ||
del source[key] | ||
target = merge(target, source) | ||
|
||
return target |
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
Oops, something went wrong.