[Bugfix][Frontend] Report Cohere stop sequences correctly (#51556)
Signed-off-by: cherry77-cloud <1615405@qq.com> Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
co-authored by
OpenAI Codex
parent
dc5101fb1b
commit
65b7662d3f
@@ -75,6 +75,7 @@ def _build_chat_completion_response(
|
||||
reasoning: str | None = None,
|
||||
tool_calls: list[dict[str, Any]] | None = None,
|
||||
finish_reason: str | None = "stop",
|
||||
stop_reason: int | str | None = None,
|
||||
citations: list[Any] | None = None,
|
||||
usage: dict[str, Any] | None = None,
|
||||
kv_transfer_params: dict[str, Any] | None = None,
|
||||
@@ -93,7 +94,14 @@ def _build_chat_completion_response(
|
||||
object="chat.completion",
|
||||
created=0,
|
||||
model="m",
|
||||
choices=[{"index": 0, "message": message, "finish_reason": finish_reason}],
|
||||
choices=[
|
||||
{
|
||||
"index": 0,
|
||||
"message": message,
|
||||
"finish_reason": finish_reason,
|
||||
"stop_reason": stop_reason,
|
||||
}
|
||||
],
|
||||
# ``usage`` is a required field on ChatCompletionResponse, but the
|
||||
# production code defensively handles ``None`` -> no usage block;
|
||||
# we round-trip that behavior by post-setting the attribute below.
|
||||
@@ -137,6 +145,11 @@ class TestMapFinishReason:
|
||||
def test_unknown_reason_defaults_to_complete(self):
|
||||
assert _map_finish_reason("not_a_real_reason") == "COMPLETE"
|
||||
|
||||
def test_only_string_stop_reasons_are_stop_sequences(self):
|
||||
assert _map_finish_reason("stop", "<END>") == "STOP_SEQUENCE"
|
||||
assert _map_finish_reason("stop", 42) == "COMPLETE"
|
||||
assert _map_finish_reason("stop", None) == "COMPLETE"
|
||||
|
||||
def test_finish_reason_map_is_complete(self):
|
||||
# Sanity check that the lookup table covers all documented states.
|
||||
assert set(_FINISH_REASON_MAP) == {
|
||||
@@ -1488,6 +1501,14 @@ class TestChatCompletionToV2:
|
||||
assert v2.message.tool_plan is None
|
||||
assert v2.usage is None
|
||||
|
||||
def test_stop_sequence_finish_reason(self):
|
||||
serving = _serving()
|
||||
resp = _build_chat_completion_response(
|
||||
finish_reason="stop", stop_reason="<END>"
|
||||
)
|
||||
v2 = serving._chat_completion_to_v2(resp, _make_request())
|
||||
assert v2.finish_reason == "STOP_SEQUENCE"
|
||||
|
||||
def test_reasoning_model_keeps_thinking_with_tool_calls(self):
|
||||
# Reasoning Command models: ``thinking`` block stays in
|
||||
# ``message.content`` and ``tool_plan`` is left unset, even when
|
||||
|
||||
@@ -81,6 +81,7 @@ def _make_chunk(
|
||||
reasoning: str | None = None,
|
||||
tool_calls: list[dict[str, Any]] | None = None,
|
||||
finish_reason: str | None = None,
|
||||
stop_reason: int | str | None = None,
|
||||
usage: dict[str, Any] | None = None,
|
||||
omit_choices: bool = False,
|
||||
citations: list[Any] | None = None,
|
||||
@@ -107,7 +108,12 @@ def _make_chunk(
|
||||
}
|
||||
if not omit_choices:
|
||||
payload["choices"] = [
|
||||
{"index": 0, "delta": delta, "finish_reason": finish_reason}
|
||||
{
|
||||
"index": 0,
|
||||
"delta": delta,
|
||||
"finish_reason": finish_reason,
|
||||
"stop_reason": stop_reason,
|
||||
}
|
||||
]
|
||||
else:
|
||||
payload["choices"] = []
|
||||
@@ -652,6 +658,23 @@ class TestChatCompletionStreamToV2:
|
||||
assert end_payload["delta"]["finish_reason"] == "COMPLETE"
|
||||
assert end_payload["delta"]["usage"]["billed_units"]["input_tokens"] == 4
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stop_sequence_finish_reason(self):
|
||||
serving = _serving()
|
||||
frames = await _drain(
|
||||
serving,
|
||||
[
|
||||
_make_chunk(role="assistant"),
|
||||
_make_chunk(
|
||||
finish_reason="stop",
|
||||
stop_reason="<END>",
|
||||
),
|
||||
_make_chunk(omit_choices=True),
|
||||
],
|
||||
)
|
||||
end_payload = _parse_event(frames[-2])
|
||||
assert end_payload["delta"]["finish_reason"] == "STOP_SEQUENCE"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_text_then_tool_call_closes_text_first(self):
|
||||
serving = _serving(is_reasoning_model=True)
|
||||
|
||||
Reference in New Issue
Block a user