@sock.route(f'{BASE_URL}/polish_content', websocket=True)
def polish_stream(ws):
# 1. 接收客户端发来的 JSON 字符串
raw = ws.receive()
if not raw:
return
# 2. 解析参数
params = json.loads(raw)
text = params.get('text')
lang = params.get('lang', 'cn')
fmt = params.get('return_format')
# 3. 参数校验(可选)
if not text or lang not in ['cn', 'en']:
ws.send(json.dumps({
'event': 'error',
'message': ErrorResInfo_Dict[lang]['parameter_error']
}))
return
# 4. 流式生成并发送
try:
for chunk in polish_knowledge_stream(text, lang, fmt):
ws.send(json.dumps({
'event': 'chunk',
'data': chunk
}))
# 完成通知
ws.send(json.dumps({
'event': 'complete',
'message': SuccessResInfo.message
}))
except Exception as e:
ws.send(json.dumps({
'event': 'error',
'message': ErrorResInfo_Dict[lang]['polish_failed'],
'details': str(e)
}))
def polish_text_stream(text, lang, return_format):
# 1. 拼 prompt 跟原来一样
content_prompt = (
f"\n\nOriginal content:\n{text}\n"
if lang == 'en'
else f"\n\n原内容:\n{text}\n"
)
if return_format == 'html':
content = POLISH_CONTENT_HTML_PROMPT[lang] + content_prompt
else:
content = POLISH_CONTENT_MARKDOWN_PROMPT[lang] + content_prompt
# 2. 准备请求体,并打开 stream
polish_data = deepcopy(gts_llm_data)
polish_data['stream'] = True # —— 打开流模式
polish_data['messages'] = [
{"role": "system", "content": GTSLLM_Pro_NO_THINK_SYSTEM_PROMPT[lang]},
{"role": "user", "content": content}
]
# 3. 调用你的流式请求函数,直接 yield 出来
for res in handle_stream_request(
DEEPSEEK_R1_DISTILL_72B_URL,
polish_data,
deep_seek_url_headers
):
# res 是 SimpleRes 对象,假设里面有 .output 属性
yield res.output
# 调用示例
for piece in polish_text_stream("待润色的文本", lang='zh', return_format='markdown'):
print(piece, end='', flush=True)
1320

被折叠的 条评论
为什么被折叠?



