|
| 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