Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llm/default_plugins/openai_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1986,8 +1986,12 @@ def combine_chunks(chunks: List) -> dict:
# those later on
logprobs = []
usage = {}
provider_extras = {}

for item in chunks:
item_dump = item.model_dump()
if item_dump.get("timings") is not None:
provider_extras["timings"] = item_dump["timings"]
if item.usage:
usage = item.usage.model_dump()
for choice in item.choices:
Expand Down Expand Up @@ -2017,6 +2021,7 @@ def combine_chunks(chunks: List) -> dict:
}
if logprobs:
combined["logprobs"] = logprobs
combined.update(provider_extras)
if chunks:
for key in ("id", "object", "model", "created", "index"):
value = getattr(chunks[0], key, None)
Expand Down
48 changes: 48 additions & 0 deletions tests/test_cli_openai_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import llm
from llm.cli import cli
from llm.default_plugins.openai_models import combine_chunks
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
import pytest
import sqlite_utils

Expand Down Expand Up @@ -43,6 +45,52 @@ def test_openai_models(mocked_models):
)


def test_combine_chunks_preserves_streaming_timings():
chunks = [
ChatCompletionChunk.model_validate(
{
"id": "chatcmpl-llamacpp",
"object": "chat.completion.chunk",
"created": 1779911036,
"model": "qwen36",
"choices": [
{
"index": 0,
"delta": {"role": "assistant", "content": "Hi"},
"finish_reason": None,
}
],
}
),
ChatCompletionChunk.model_validate(
{
"id": "chatcmpl-llamacpp",
"object": "chat.completion.chunk",
"created": 1779911036,
"model": "qwen36",
"choices": [],
"usage": {
"prompt_tokens": 22,
"completion_tokens": 7,
"total_tokens": 29,
},
"timings": {
"prompt_per_second": 9.953251387913042,
"predicted_per_second": 6.667936749857116,
},
}
),
]

combined = combine_chunks(chunks)

assert combined["content"] == "Hi"
assert combined["usage"]["prompt_tokens"] == 22
assert combined["usage"]["completion_tokens"] == 7
assert combined["timings"]["prompt_per_second"] == 9.953251387913042
assert combined["timings"]["predicted_per_second"] == 6.667936749857116


def test_openai_options_min_max():
options = {
"temperature": [0, 2],
Expand Down