Skip to content

Commit 727c7ad

Browse files
committed
[BugFix] v1/completions add finish_reason
1 parent ccc7f1b commit 727c7ad

File tree

2 files changed

+69
-16
lines changed

2 files changed

+69
-16
lines changed

fastdeploy/entrypoints/openai/serving_completion.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ async def completion_full_generator(
234234
if dealer is not None:
235235
dealer.close()
236236

237+
def calc_finish_reason(self, max_tokens, token_num, output):
238+
if max_tokens is None or token_num != max_tokens:
239+
if self.engine_client.reasoning_parser == "ernie_x1" and output.get("finish_reason", "") == "tool_calls":
240+
return "tool_calls"
241+
else:
242+
return "stop"
243+
else:
244+
return "length"
245+
237246
async def completion_stream_generator(
238247
self,
239248
request: CompletionRequest,
@@ -334,19 +343,13 @@ async def completion_stream_generator(
334343
logprobs=logprobs_res,
335344
)
336345
)
337-
if res["finished"]:
338-
if request.max_tokens is None or output_tokens[idx] + 1 != request.max_tokens:
339-
chunk.choices[0].finish_reason = "stop"
340-
if (
341-
self.engine_client.reasoning_parser == "ernie_x1"
342-
and output.get("finish_reason", "") == "tool_calls"
343-
):
344-
chunk.choices[0].finish_reason = "tool_calls"
345-
else:
346-
chunk.choices[0].finish_reason = "length"
347-
348346
output_tokens[idx] += 1
349347

348+
if res["finished"]:
349+
choices[-1].finish_reason = self.calc_finish_reason(
350+
request.max_tokens, output_tokens[idx], output
351+
)
352+
350353
if len(choices) == max_streaming_response_tokens or res["finished"]:
351354
chunk = CompletionStreamResponse(
352355
id=request_id,
@@ -433,6 +436,11 @@ def request_output_to_completion_response(
433436
token_ids = output["token_ids"]
434437
output_text = output["text"]
435438

439+
num_generated_tokens += final_res["output_token_ids"]
440+
num_prompt_tokens += len(prompt_token_ids)
441+
442+
finish_reason = self.calc_finish_reason(request.max_tokens, final_res["output_token_ids"], output)
443+
436444
choice_data = CompletionResponseChoice(
437445
token_ids=token_ids,
438446
index=len(choices),
@@ -442,14 +450,10 @@ def request_output_to_completion_response(
442450
reasoning_content=output.get("reasoning_content"),
443451
tool_calls=output.get("tool_call_content"),
444452
logprobs=aggregated_logprobs,
445-
finish_reason=None,
453+
finish_reason=finish_reason,
446454
)
447455
choices.append(choice_data)
448456

449-
num_generated_tokens += final_res["output_token_ids"]
450-
451-
num_prompt_tokens += len(prompt_token_ids)
452-
453457
usage = UsageInfo(
454458
prompt_tokens=num_prompt_tokens,
455459
completion_tokens=num_generated_tokens,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import unittest
2+
from unittest.mock import Mock
3+
4+
from fastdeploy.entrypoints.openai.serving_completion import OpenAIServingCompletion
5+
6+
7+
class TestOpenAIServingCompletion(unittest.TestCase):
8+
9+
def test_calc_finish_reason_tool_calls(self):
10+
# 创建一个模拟的engine_client,并设置reasoning_parser为"ernie_x1"
11+
engine_client = Mock()
12+
engine_client.reasoning_parser = "ernie_x1"
13+
# 创建一个OpenAIServingCompletion实例
14+
serving_completion = OpenAIServingCompletion(engine_client, "pid", "ips")
15+
# 创建一个模拟的output,并设置finish_reason为"tool_calls"
16+
output = {"finish_reason": "tool_calls"}
17+
# 调用calc_finish_reason方法
18+
result = serving_completion.calc_finish_reason(None, 100, output)
19+
# 断言结果为"tool_calls"
20+
assert result == "tool_calls"
21+
22+
def test_calc_finish_reason_stop(self):
23+
# 创建一个模拟的engine_client,并设置reasoning_parser为"ernie_x1"
24+
engine_client = Mock()
25+
engine_client.reasoning_parser = "ernie_x1"
26+
# 创建一个OpenAIServingCompletion实例
27+
serving_completion = OpenAIServingCompletion(engine_client, "pid", "ips")
28+
# 创建一个模拟的output,并设置finish_reason为其他值
29+
output = {"finish_reason": "other_reason"}
30+
# 调用calc_finish_reason方法
31+
result = serving_completion.calc_finish_reason(None, 100, output)
32+
# 断言结果为"stop"
33+
assert result == "stop"
34+
35+
def test_calc_finish_reason_length(self):
36+
# 创建一个模拟的engine_client
37+
engine_client = Mock()
38+
# 创建一个OpenAIServingCompletion实例
39+
serving_completion = OpenAIServingCompletion(engine_client, "pid", "ips")
40+
# 创建一个模拟的output
41+
output = {}
42+
# 调用calc_finish_reason方法
43+
result = serving_completion.calc_finish_reason(100, 100, output)
44+
# 断言结果为"length"
45+
assert result == "length"
46+
47+
48+
if __name__ == "__main__":
49+
unittest.main()

0 commit comments

Comments
 (0)