main
HuangHai 4 months ago
parent f9c0417ccf
commit 9cc8ee8559

@ -127,7 +127,6 @@ async def get_docx_stream(
chunk_content = chunk.choices[0].delta.content chunk_content = chunk.choices[0].delta.content
# 逐字拆分并返回 # 逐字拆分并返回
for char in chunk_content: for char in chunk_content:
# print(char, end="", flush=True) # 逐字输出到控制台
yield char.encode("utf-8") # 将字符编码为 UTF-8 字节 yield char.encode("utf-8") # 将字符编码为 UTF-8 字节
summary += char # 将内容拼接到 summary 中 summary += char # 将内容拼接到 summary 中
@ -136,14 +135,15 @@ async def get_docx_stream(
# 记录到数据库 # 记录到数据库
update_question_by_id(db, question_id, docx_file_name=filename) update_question_by_id(db, question_id, docx_file_name=filename)
except Exception as e: except Exception as e:
# 如果发生异常,返回错误信息 # 如果发生异常,返回错误信息
error_response = json.dumps({ error_response = json.dumps({
"success": False, "success": False,
"message": f"生成Word文件失败: {str(e)}" "message": f"生成Word文件失败: {str(e)}"
}) })
# print(error_response) # 输出错误信息到控制台
yield error_response.encode("utf-8") # 将错误信息编码为 UTF-8 字节 yield error_response.encode("utf-8") # 将错误信息编码为 UTF-8 字节
finally: finally:
# 确保资源释放 # 确保资源释放
if "response" in locals(): if "response" in locals():
@ -156,6 +156,7 @@ async def get_docx_stream(
headers={ headers={
"Cache-Control": "no-cache", # 禁用缓存 "Cache-Control": "no-cache", # 禁用缓存
"Content-Type": "text/plain; charset=utf-8", # 设置内容类型和字符编码 "Content-Type": "text/plain; charset=utf-8", # 设置内容类型和字符编码
"X-Accel-Buffering": "no", # 禁用 Nginx 缓冲(如果使用 Nginx
} }
) )

Loading…
Cancel
Save