2026-07-06 11:46:16 +05:30
|
|
|
#
|
|
|
|
|
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
from api.utils import api_utils
|
|
|
|
|
|
|
|
|
|
|
2026-07-10 08:29:32 +05:30
|
|
|
def test_get_data_openai_stream_chunk_matches_openai_shape(monkeypatch):
|
2026-07-06 11:46:16 +05:30
|
|
|
monkeypatch.setattr(api_utils.time, "time", lambda: 1234567890.9)
|
|
|
|
|
|
2026-07-10 08:29:32 +05:30
|
|
|
data = api_utils.get_data_openai(id="chatcmpl-test", model="test-model", content="chunk", stream=True)
|
2026-07-06 11:46:16 +05:30
|
|
|
|
|
|
|
|
assert data == {
|
|
|
|
|
"id": "chatcmpl-test",
|
|
|
|
|
"object": "chat.completion.chunk",
|
2026-07-10 08:29:32 +05:30
|
|
|
"created": 1234567890,
|
2026-07-06 11:46:16 +05:30
|
|
|
"model": "test-model",
|
2026-07-10 08:29:32 +05:30
|
|
|
"system_fingerprint": "",
|
|
|
|
|
"usage": None,
|
2026-07-06 11:46:16 +05:30
|
|
|
"choices": [
|
|
|
|
|
{
|
2026-07-10 08:29:32 +05:30
|
|
|
"delta": {
|
|
|
|
|
"content": "chunk",
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"function_call": None,
|
|
|
|
|
"tool_calls": None,
|
|
|
|
|
},
|
2026-07-06 11:46:16 +05:30
|
|
|
"finish_reason": None,
|
|
|
|
|
"index": 0,
|
2026-07-10 08:29:32 +05:30
|
|
|
"logprobs": None,
|
2026-07-06 11:46:16 +05:30
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
}
|
2026-07-10 08:29:32 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_data_openai_stream_preserves_explicit_created_value():
|
|
|
|
|
data = api_utils.get_data_openai(created=0, content="chunk", stream=True)
|
|
|
|
|
|
|
|
|
|
assert data["created"] == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_data_openai_terminal_stream_chunk_includes_usage():
|
|
|
|
|
data = api_utils.get_data_openai(prompt_tokens=3, completion_tokens=5, content=None, finish_reason="stop", stream=True)
|
|
|
|
|
|
|
|
|
|
assert data["usage"] == {
|
|
|
|
|
"prompt_tokens": 3,
|
|
|
|
|
"completion_tokens": 5,
|
|
|
|
|
"total_tokens": 8,
|
|
|
|
|
"completion_tokens_details": {
|
|
|
|
|
"reasoning_tokens": 0,
|
|
|
|
|
"accepted_prediction_tokens": 0,
|
|
|
|
|
"rejected_prediction_tokens": 0,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
assert data["choices"][0]["finish_reason"] == "stop"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_data_openai_stream_delta_allows_reference_payload():
|
|
|
|
|
data = api_utils.get_data_openai(content="chunk", stream=True)
|
|
|
|
|
|
|
|
|
|
data["choices"][0]["delta"]["reference"] = {"chunks": []}
|
|
|
|
|
|
|
|
|
|
assert data["choices"][0]["delta"]["reference"] == {"chunks": []}
|