Skip to content

Commit fe20946

Browse files
authored
Release/2.1 (#3361)
* [BugFix] v1/completions add finish_reason * update TestOpenAIServingCompletion for merge
1 parent b4bb54b commit fe20946

File tree

2 files changed

+128
-12
lines changed

2 files changed

+128
-12
lines changed

fastdeploy/entrypoints/openai/serving_completion.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ async def completion_full_generator(
234234
dealer.close()
235235
self.engine_client.semaphore.release()
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,
@@ -432,6 +435,8 @@ def request_output_to_completion_response(
432435
token_ids = output["token_ids"]
433436
output_text = output["text"]
434437

438+
finish_reason = self.calc_finish_reason(request.max_tokens, final_res["output_token_ids"], output)
439+
435440
choice_data = CompletionResponseChoice(
436441
token_ids=token_ids,
437442
index=len(choices),
@@ -441,7 +446,7 @@ def request_output_to_completion_response(
441446
reasoning_content=output.get("reasoning_content"),
442447
tool_calls=output.get("tool_call_content"),
443448
logprobs=aggregated_logprobs,
444-
finish_reason=None,
449+
finish_reason=finish_reason,
445450
)
446451
choices.append(choice_data)
447452

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import unittest
2+
from typing import List
3+
from unittest.mock import Mock
4+
5+
from fastdeploy.entrypoints.openai.serving_completion import (
6+
CompletionRequest,
7+
OpenAIServingCompletion,
8+
RequestOutput,
9+
)
10+
11+
12+
class TestOpenAIServingCompletion(unittest.TestCase):
13+
14+
def test_calc_finish_reason_tool_calls(self):
15+
# 创建一个模拟的engine_client,并设置reasoning_parser为"ernie_x1"
16+
engine_client = Mock()
17+
engine_client.reasoning_parser = "ernie_x1"
18+
# 创建一个OpenAIServingCompletion实例
19+
serving_completion = OpenAIServingCompletion(engine_client, "pid", "ips", 360)
20+
# 创建一个模拟的output,并设置finish_reason为"tool_calls"
21+
output = {"finish_reason": "tool_calls"}
22+
# 调用calc_finish_reason方法
23+
result = serving_completion.calc_finish_reason(None, 100, output)
24+
# 断言结果为"tool_calls"
25+
assert result == "tool_calls"
26+
27+
def test_calc_finish_reason_stop(self):
28+
# 创建一个模拟的engine_client,并设置reasoning_parser为"ernie_x1"
29+
engine_client = Mock()
30+
engine_client.reasoning_parser = "ernie_x1"
31+
# 创建一个OpenAIServingCompletion实例
32+
serving_completion = OpenAIServingCompletion(engine_client, "pid", "ips", 360)
33+
# 创建一个模拟的output,并设置finish_reason为其他值
34+
output = {"finish_reason": "other_reason"}
35+
# 调用calc_finish_reason方法
36+
result = serving_completion.calc_finish_reason(None, 100, output)
37+
# 断言结果为"stop"
38+
assert result == "stop"
39+
40+
def test_calc_finish_reason_length(self):
41+
# 创建一个模拟的engine_client
42+
engine_client = Mock()
43+
# 创建一个OpenAIServingCompletion实例
44+
serving_completion = OpenAIServingCompletion(engine_client, "pid", "ips", 360)
45+
# 创建一个模拟的output
46+
output = {}
47+
# 调用calc_finish_reason方法
48+
result = serving_completion.calc_finish_reason(100, 100, output)
49+
# 断言结果为"length"
50+
assert result == "length"
51+
52+
def test_request_output_to_completion_response(self):
53+
engine_client = Mock()
54+
# 创建一个OpenAIServingCompletion实例
55+
openai_serving_completion = OpenAIServingCompletion(engine_client, "pid", "ips", 360)
56+
final_res_batch: List[RequestOutput] = [
57+
{
58+
"prompt": "Hello, world!",
59+
"outputs": {
60+
"token_ids": [1, 2, 3],
61+
"text": " world!",
62+
"top_logprobs": {
63+
"a": 0.1,
64+
"b": 0.2,
65+
},
66+
},
67+
"output_token_ids": 3,
68+
},
69+
{
70+
"prompt": "Hello, world!",
71+
"outputs": {
72+
"token_ids": [4, 5, 6],
73+
"text": " world!",
74+
"top_logprobs": {
75+
"a": 0.3,
76+
"b": 0.4,
77+
},
78+
},
79+
"output_token_ids": 3,
80+
},
81+
]
82+
83+
request: CompletionRequest = Mock()
84+
request_id = "test_request_id"
85+
created_time = 1655136000
86+
model_name = "test_model"
87+
prompt_batched_token_ids = [[1, 2, 3], [4, 5, 6]]
88+
completion_batched_token_ids = [[7, 8, 9], [10, 11, 12]]
89+
90+
completion_response = openai_serving_completion.request_output_to_completion_response(
91+
final_res_batch=final_res_batch,
92+
request=request,
93+
request_id=request_id,
94+
created_time=created_time,
95+
model_name=model_name,
96+
prompt_batched_token_ids=prompt_batched_token_ids,
97+
completion_batched_token_ids=completion_batched_token_ids,
98+
)
99+
100+
assert completion_response.id == request_id
101+
assert completion_response.created == created_time
102+
assert completion_response.model == model_name
103+
assert len(completion_response.choices) == 2
104+
105+
# 验证 choices 的 text 属性
106+
assert completion_response.choices[0].text == "Hello, world! world!"
107+
assert completion_response.choices[1].text == "Hello, world! world!"
108+
109+
110+
if __name__ == "__main__":
111+
unittest.main()

0 commit comments

Comments
 (0)