Skip to content
Merged
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: 3 additions & 2 deletions python/src/edge0/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def _chat_once(server: QueueServer, payload: dict):
"choices": [{
"index": 0,
"message": message,
"finish_reason": "tool_calls" if tool_calls else "stop",
"finish_reason": ("tool_calls" if tool_calls
else meta["finish_reason"]),
}],
"usage": meta["usage"],
}
Expand Down Expand Up @@ -158,7 +159,7 @@ def on_token(tid: int):
def produce():
try:
tokens, meta = server.chat(req, on_token=on_token)
finish_reason = "stop"
finish_reason = meta["finish_reason"]
delta = {}
if buffering:
text = decode_tokens(server.engine, tokens)
Expand Down
7 changes: 6 additions & 1 deletion python/src/edge0/server/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,13 @@ def run(self, on_token=None) -> tuple[list[int], dict]:
"completion_tokens": len(tokens),
"total_tokens": len(ids) + len(tokens),
}
# ``generate`` stops at EOS (which it does not return) or after
# ``max_new_tokens`` tokens, so reaching the cap means the reply was
# cut off: OpenAI reports that as ``"length"``.
finish_reason = ("length" if len(tokens) >= gen.max_new_tokens
else "stop")
meta = {"wall_s": round(time.perf_counter() - t0, 3)}
return tokens, {"usage": usage, **meta}
return tokens, {"usage": usage, "finish_reason": finish_reason, **meta}


class QueueServer:
Expand Down
40 changes: 40 additions & 0 deletions python/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,46 @@ def test_chat_once_response_shape():
assert out["usage"]["completion_tokens"] == 3


class CappedEngine(FakeEngine):
"""FakeEngine whose generate() honours ``max_new_tokens`` like the real
engine: it returns at most that many tokens (the reply is cut off)."""

def generate(self, ids, gen_config=None, on_token=None, **kw):
toks = [11, 12, 13][:gen_config.max_new_tokens]
self.generated.append((list(ids), gen_config))
if on_token is not None:
for t in toks:
on_token(t)
return toks


@pytest.mark.parametrize("max_tokens, finish_reason", [
(2, "length"), # cut off at the cap
(3, "length"), # exactly at the cap: generate() never saw EOS
(8, "stop"), # EOS before the cap
])
def test_chat_once_finish_reason_length_when_capped(max_tokens, finish_reason):
srv = QueueServer(CappedEngine())
out = _chat_once(srv, {
"messages": [{"role": "user", "content": "hi"}],
"max_tokens": max_tokens,
})
assert out["choices"][0]["finish_reason"] == finish_reason
assert out["usage"]["completion_tokens"] == min(max_tokens, 3)


def test_chat_stream_finish_reason_length_when_capped():
srv = QueueServer(CappedEngine())
body = b"".join(_chat_stream(srv, {
"messages": [{"role": "user", "content": "hi"}],
"stream": True, "max_completion_tokens": 2,
}))
events = [e for e in body.decode("utf-8").split("\n\n") if e]
chunks = [json.loads(e[6:]) for e in events[:-1]] # drop [DONE]
assert chunks[-1]["choices"][0]["finish_reason"] == "length"
assert chunks[-1]["usage"]["completion_tokens"] == 2


# ---- tool calls (#115) -----------------------------------------------------


Expand Down
Loading