MCPcopy Index your code
hub / github.com/dataease/SQLBot / process_stream

Function process_stream

backend/apps/chat/task/llm.py:1765–1860  ·  view source on GitHub ↗
(res: Iterator[BaseMessageChunk],
                   token_usage: Dict[str, Any] = None,
                   enable_tag_parsing: bool = settings.PARSE_REASONING_BLOCK_ENABLED,
                   start_tag: str = settings.DEFAULT_REASONING_CONTENT_START,
                   end_tag: str = settings.DEFAULT_REASONING_CONTENT_END
                   )

Source from the content-addressed store, hash-verified

1763
1764
1765def process_stream(res: Iterator[BaseMessageChunk],
1766 token_usage: Dict[str, Any] = None,
1767 enable_tag_parsing: bool = settings.PARSE_REASONING_BLOCK_ENABLED,
1768 start_tag: str = settings.DEFAULT_REASONING_CONTENT_START,
1769 end_tag: str = settings.DEFAULT_REASONING_CONTENT_END
1770 ):
1771 if token_usage is None:
1772 token_usage = {}
1773 in_thinking_block = False # 标记是否在思考过程块中
1774 current_thinking = '' # 当前收集的思考过程内容
1775 pending_start_tag = '' # 用于缓存可能被截断的开始标签部分
1776
1777 for chunk in res:
1778 SQLBotLogUtil.info(chunk)
1779 reasoning_content_chunk = ''
1780 content = chunk.content
1781 output_content = '' # 实际要输出的内容
1782
1783 # 检查additional_kwargs中的reasoning_content
1784 if 'reasoning_content' in chunk.additional_kwargs:
1785 reasoning_content = chunk.additional_kwargs.get('reasoning_content', '')
1786 if reasoning_content is None:
1787 reasoning_content = ''
1788
1789 # 累积additional_kwargs中的思考内容到current_thinking
1790 current_thinking += reasoning_content
1791 reasoning_content_chunk = reasoning_content
1792
1793 # 只有当current_thinking不是空字符串时才跳过标签解析
1794 if not in_thinking_block and current_thinking.strip() != '':
1795 output_content = content # 正常输出content
1796 yield {
1797 'content': output_content,
1798 'reasoning_content': reasoning_content_chunk
1799 }
1800 get_token_usage(chunk, token_usage)
1801 continue # 跳过后续的标签解析逻辑
1802
1803 # 如果没有有效的思考内容,并且启用了标签解析,才执行标签解析逻辑
1804 # 如果有缓存的开始标签部分,先拼接当前内容
1805 if pending_start_tag:
1806 content = pending_start_tag + content
1807 pending_start_tag = ''
1808
1809 # 检查是否开始思考过程块(处理可能被截断的开始标签)
1810 if enable_tag_parsing and not in_thinking_block and start_tag:
1811 if start_tag in content:
1812 start_idx = content.index(start_tag)
1813 # 只有当开始标签前面没有其他文本时才认为是真正的思考块开始
1814 if start_idx == 0 or content[:start_idx].strip() == '':
1815 # 完整标签存在且前面没有其他文本
1816 output_content += content[:start_idx] # 输出开始标签之前的内容
1817 content = content[start_idx + len(start_tag):] # 移除开始标签
1818 in_thinking_block = True
1819 else:
1820 # 开始标签前面有其他文本,不认为是思考块开始
1821 output_content += content
1822 content = ''

Callers 8

generate_analysisMethod · 0.85
generate_predictMethod · 0.85
select_datasourceMethod · 0.85
generate_sqlMethod · 0.85
generate_with_sub_sqlMethod · 0.85
build_table_filterMethod · 0.85
generate_chartMethod · 0.85

Calls 4

get_token_usageFunction · 0.85
indexMethod · 0.80
getMethod · 0.65
infoMethod · 0.45

Tested by

no test coverage detected