流式调用LLM并安全地拼接为完整字符串(避免UTF-8多字节字符截断) Args: system_prompt: 系统提示词 user_prompt: 用户提示词 **kwargs: 额外参数(temperature, top_p等) Returns: 完整的响应字符串
(self, system_prompt: str, user_prompt: str, **kwargs)
| 132 | |
| 133 | @with_retry(LLM_RETRY_CONFIG) |
| 134 | def stream_invoke_to_string(self, system_prompt: str, user_prompt: str, **kwargs) -> str: |
| 135 | """ |
| 136 | 流式调用LLM并安全地拼接为完整字符串(避免UTF-8多字节字符截断) |
| 137 | |
| 138 | Args: |
| 139 | system_prompt: 系统提示词 |
| 140 | user_prompt: 用户提示词 |
| 141 | **kwargs: 额外参数(temperature, top_p等) |
| 142 | |
| 143 | Returns: |
| 144 | 完整的响应字符串 |
| 145 | """ |
| 146 | # 以字节形式收集所有块 |
| 147 | byte_chunks = [] |
| 148 | for chunk in self.stream_invoke(system_prompt, user_prompt, **kwargs): |
| 149 | byte_chunks.append(chunk.encode('utf-8')) |
| 150 | |
| 151 | # 拼接所有字节,然后一次性解码 |
| 152 | if byte_chunks: |
| 153 | return b''.join(byte_chunks).decode('utf-8', errors='replace') |
| 154 | return "" |
| 155 | |
| 156 | @staticmethod |
| 157 | def validate_response(response: Optional[str]) -> str: |