创建包含增量token的结果对象 Args: current_result: 当前的RequestOutput对象 previous_count: 之前已处理的token数量 pos: 在prompts列表中的位置 prompts: 原始提示词列表 chat_template_kwargs: 聊天模板参数,包含enable_thinking等配置 Returns: RequestOutput: 包含
(
self, current_result, previous_count, pos, prompts, chat_template_kwargs: Optional[dict[str, Any]] = None
)
| 701 | pbar.close() |
| 702 | |
| 703 | def _create_incremental_result( |
| 704 | self, current_result, previous_count, pos, prompts, chat_template_kwargs: Optional[dict[str, Any]] = None |
| 705 | ): |
| 706 | """ |
| 707 | 创建包含增量token的结果对象 |
| 708 | |
| 709 | Args: |
| 710 | current_result: 当前的RequestOutput对象 |
| 711 | previous_count: 之前已处理的token数量 |
| 712 | pos: 在prompts列表中的位置 |
| 713 | prompts: 原始提示词列表 |
| 714 | chat_template_kwargs: 聊天模板参数,包含enable_thinking等配置 |
| 715 | |
| 716 | Returns: |
| 717 | RequestOutput: 包含增量更新的结果对象 |
| 718 | """ |
| 719 | # Create a copy of current result for incremental update |
| 720 | from copy import deepcopy |
| 721 | |
| 722 | incremental_result = deepcopy(current_result) |
| 723 | |
| 724 | # Extract only new tokens |
| 725 | if current_result.outputs.token_ids and len(current_result.outputs.token_ids) > previous_count: |
| 726 | new_token_ids = current_result.outputs.token_ids[previous_count:] |
| 727 | incremental_result.outputs.token_ids = new_token_ids |
| 728 | |
| 729 | # Get enable_thinking from chat_template_kwargs, default to False |
| 730 | enable_thinking = False |
| 731 | if chat_template_kwargs: |
| 732 | enable_thinking = chat_template_kwargs.get("enable_thinking", False) |
| 733 | |
| 734 | # Construct response_dict format and call process_response_dict_streaming |
| 735 | response_dict = { |
| 736 | "request_id": current_result.request_id, |
| 737 | "finished": current_result.finished, |
| 738 | "outputs": { |
| 739 | "token_ids": new_token_ids, |
| 740 | }, |
| 741 | } |
| 742 | |
| 743 | processed_response = self.llm_engine.data_processor.process_response_dict_streaming( |
| 744 | response_dict, stream=True, enable_thinking=enable_thinking, include_stop_str_in_output=False |
| 745 | ) |
| 746 | |
| 747 | # Extract incremental text |
| 748 | incremental_result.outputs.text = processed_response["outputs"]["text"] |
| 749 | |
| 750 | # Set the prompt |
| 751 | if isinstance(prompts, list): |
| 752 | incremental_result.prompt = prompts[pos] |
| 753 | else: |
| 754 | incremental_result.prompt = prompts |
| 755 | |
| 756 | return incremental_result |
| 757 | |
| 758 | def _validate_tools(self, raw_tools: Any) -> Optional[list[dict]]: |
| 759 | """ |