1
1
import unittest
2
+ from typing import List
2
3
from unittest .mock import Mock
3
4
4
- from fastdeploy .entrypoints .openai .serving_completion import OpenAIServingCompletion
5
+ from fastdeploy .entrypoints .openai .serving_completion import (
6
+ CompletionRequest ,
7
+ OpenAIServingCompletion ,
8
+ RequestOutput ,
9
+ )
5
10
6
11
7
12
class TestOpenAIServingCompletion (unittest .TestCase ):
@@ -11,7 +16,7 @@ def test_calc_finish_reason_tool_calls(self):
11
16
engine_client = Mock ()
12
17
engine_client .reasoning_parser = "ernie_x1"
13
18
# 创建一个OpenAIServingCompletion实例
14
- serving_completion = OpenAIServingCompletion (engine_client , "pid" , "ips" )
19
+ serving_completion = OpenAIServingCompletion (engine_client , "pid" , "ips" , 360 )
15
20
# 创建一个模拟的output,并设置finish_reason为"tool_calls"
16
21
output = {"finish_reason" : "tool_calls" }
17
22
# 调用calc_finish_reason方法
@@ -24,7 +29,7 @@ def test_calc_finish_reason_stop(self):
24
29
engine_client = Mock ()
25
30
engine_client .reasoning_parser = "ernie_x1"
26
31
# 创建一个OpenAIServingCompletion实例
27
- serving_completion = OpenAIServingCompletion (engine_client , "pid" , "ips" )
32
+ serving_completion = OpenAIServingCompletion (engine_client , "pid" , "ips" , 360 )
28
33
# 创建一个模拟的output,并设置finish_reason为其他值
29
34
output = {"finish_reason" : "other_reason" }
30
35
# 调用calc_finish_reason方法
@@ -36,14 +41,71 @@ def test_calc_finish_reason_length(self):
36
41
# 创建一个模拟的engine_client
37
42
engine_client = Mock ()
38
43
# 创建一个OpenAIServingCompletion实例
39
- serving_completion = OpenAIServingCompletion (engine_client , "pid" , "ips" )
44
+ serving_completion = OpenAIServingCompletion (engine_client , "pid" , "ips" , 360 )
40
45
# 创建一个模拟的output
41
46
output = {}
42
47
# 调用calc_finish_reason方法
43
48
result = serving_completion .calc_finish_reason (100 , 100 , output )
44
49
# 断言结果为"length"
45
50
assert result == "length"
46
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
+
47
109
48
110
if __name__ == "__main__" :
49
111
unittest .main ()
0 commit comments