Files
dsProject/dsLightRag/Test/TTS/T1_LLM.py

47 lines
1.5 KiB
Python
Raw Normal View History

2025-08-31 09:37:21 +08:00
import re
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from Util.LlmUtil import get_llm_response
def stream_and_split_text(prompt):
"""
流式获取LLM输出并按句子分割
@param prompt: 提示文本
@return: 生成器每次产生一个完整句子
"""
buffer = ""
# 使用LlmUtil中的get_llm_response函数获取流式响应
for content in get_llm_response(prompt, stream=True):
buffer += content
# 使用正则表达式检测句子结束
sentences = re.split(r'([。!?.!?])', buffer)
if len(sentences) > 1:
# 提取完整句子
for i in range(0, len(sentences)-1, 2):
if i+1 < len(sentences):
sentence = sentences[i] + sentences[i+1]
yield sentence
# 保留不完整的部分
buffer = sentences[-1]
# 处理最后剩余的部分
if buffer:
yield buffer
def main():
"""
测试stream_and_split_text函数
"""
test_prompt = "请简单介绍一下人工智能的发展历史。包括从图灵测试到深度学习的演进过程。"
print("测试文本:", test_prompt)
print("\n分割后的句子:")
for i, sentence in enumerate(stream_and_split_text(test_prompt), 1):
print(f"句子 {i}: {sentence}")
if __name__ == "__main__":
main()