|
| 1 | +""" |
| 2 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License" |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +from typing import Any, List, Optional |
| 18 | + |
| 19 | +from fastdeploy.input.tokenzier_client import AsyncTokenizerClient, ImageDecodeRequest |
| 20 | + |
| 21 | + |
| 22 | +class ChatResponseProcessor: |
| 23 | + """ |
| 24 | + A decoder class to build multimodal content (text/image) from token_ids. |
| 25 | +
|
| 26 | + Attributes: |
| 27 | + eoi_token_id: Token ID indicating the end of an image (<eoi>). |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + data_processor, |
| 33 | + enable_mm_output: Optional[bool] = False, |
| 34 | + eoi_token_id: Optional[int] = 101032, |
| 35 | + eos_token_id: Optional[int] = 2, |
| 36 | + decoder_base_url: Optional[str] = None, |
| 37 | + ): |
| 38 | + self.data_processor = data_processor |
| 39 | + self.enable_mm_output = enable_mm_output |
| 40 | + self.eoi_token_id = eoi_token_id |
| 41 | + self.eos_token_id = eos_token_id |
| 42 | + if decoder_base_url is not None: |
| 43 | + self.decoder_client = AsyncTokenizerClient(base_url=decoder_base_url) |
| 44 | + self._mm_buffer: List[Any] = [] # Buffer for accumulating image token_ids |
| 45 | + self._end_image_code_request_output: Optional[Any] = None |
| 46 | + self._multipart_buffer = [] |
| 47 | + |
| 48 | + def enable_multimodal_content(self): |
| 49 | + return self.enable_mm_output |
| 50 | + |
| 51 | + def accumulate_token_ids(self, request_output): |
| 52 | + decode_type = request_output["outputs"].get("decode_type", 0) |
| 53 | + |
| 54 | + if not self._multipart_buffer: |
| 55 | + self._multipart_buffer.append({"decode_type": decode_type, "request_output": request_output}) |
| 56 | + else: |
| 57 | + last_part = self._multipart_buffer[-1] |
| 58 | + |
| 59 | + if last_part["decode_type"] == decode_type: |
| 60 | + last_token_ids = last_part["request_output"]["outputs"]["token_ids"] |
| 61 | + last_token_ids.extend(request_output["outputs"]["token_ids"]) |
| 62 | + request_output["outputs"]["token_ids"] = last_token_ids |
| 63 | + last_part["request_output"] = request_output |
| 64 | + else: |
| 65 | + self._multipart_buffer.append({"decode_type": decode_type, "request_output": request_output}) |
| 66 | + |
| 67 | + async def process_response_chat(self, request_outputs, stream, enable_thinking, include_stop_str_in_output): |
| 68 | + """ |
| 69 | + Process a list of responses into a generator that yields each processed response as it's generated. |
| 70 | + Args: |
| 71 | + request_outputs: The list of outputs to be processed. |
| 72 | + stream: Whether or not to stream the output. |
| 73 | + enable_thinking: Whether or not to show thinking messages. |
| 74 | + include_stop_str_in_output: Whether or not to include stop strings in the output. |
| 75 | + """ |
| 76 | + for request_output in request_outputs: |
| 77 | + if not self.enable_mm_output: |
| 78 | + yield self.data_processor.process_response_dict( |
| 79 | + response_dict=request_output, |
| 80 | + stream=stream, |
| 81 | + enable_thinking=enable_thinking, |
| 82 | + include_stop_str_in_output=include_stop_str_in_output, |
| 83 | + ) |
| 84 | + elif stream: |
| 85 | + decode_type = request_output["outputs"].get("decode_type", 0) |
| 86 | + token_ids = request_output["outputs"]["token_ids"] |
| 87 | + if decode_type == 0: |
| 88 | + if self.eoi_token_id and self.eoi_token_id in token_ids: |
| 89 | + if self._mm_buffer: |
| 90 | + all_tokens = self._mm_buffer |
| 91 | + self._mm_buffer = [] |
| 92 | + image = {"type": "image"} |
| 93 | + if self.decoder_client: |
| 94 | + req_id = request_output["request_id"] |
| 95 | + image_ret = await self.decoder_client.decode_image( |
| 96 | + request=ImageDecodeRequest(req_id=req_id, data=all_tokens) |
| 97 | + ) |
| 98 | + image["url"] = image_ret["http_url"] |
| 99 | + image_output = self._end_image_code_request_output |
| 100 | + image_output["outputs"]["multipart"] = [image] |
| 101 | + image_output["outputs"]["token_ids"] = all_tokens |
| 102 | + yield image_output |
| 103 | + |
| 104 | + self.data_processor.process_response_dict( |
| 105 | + response_dict=request_output, |
| 106 | + stream=stream, |
| 107 | + enable_thinking=enable_thinking, |
| 108 | + include_stop_str_in_output=include_stop_str_in_output, |
| 109 | + ) |
| 110 | + text = {"type": "text", "text": request_output["outputs"]["text"]} |
| 111 | + request_output["outputs"]["multipart"] = [text] |
| 112 | + yield request_output |
| 113 | + |
| 114 | + elif decode_type == 1: |
| 115 | + self._mm_buffer.extend(token_ids) |
| 116 | + self._end_image_code_request_output = request_output |
| 117 | + else: |
| 118 | + self.accumulate_token_ids(request_output) |
| 119 | + token_ids = request_output["outputs"]["token_ids"] |
| 120 | + if token_ids[-1] == self.eos_token_id: |
| 121 | + multipart = [] |
| 122 | + for part in self._multipart_buffer: |
| 123 | + if part["decode_type"] == 0: |
| 124 | + self.data_processor.process_response_dict( |
| 125 | + response_dict=part["request_output"], |
| 126 | + stream=False, |
| 127 | + enable_thinking=enable_thinking, |
| 128 | + include_stop_str_in_output=include_stop_str_in_output, |
| 129 | + ) |
| 130 | + text = {"type": "text", "text": part["request_output"]["outputs"]["text"]} |
| 131 | + multipart.append(text) |
| 132 | + elif part["decode_type"] == 1: |
| 133 | + image = {"type": "image"} |
| 134 | + if self.decoder_client: |
| 135 | + req_id = part["request_output"]["request_id"] |
| 136 | + all_tokens = part["request_output"]["outputs"]["token_ids"] |
| 137 | + image_ret = await self.decoder_client.decode_image( |
| 138 | + request=ImageDecodeRequest(req_id=req_id, data=all_tokens) |
| 139 | + ) |
| 140 | + image["url"] = image_ret["http_url"] |
| 141 | + multipart.append(image) |
| 142 | + |
| 143 | + lasrt_request_output = self._multipart_buffer[-1]["request_output"] |
| 144 | + lasrt_request_output["outputs"]["multipart"] = multipart |
| 145 | + yield lasrt_request_output |
0 commit comments