你想用 Dify + DeepSeek + Flask 搭建一个大模型问答系统,并且将回答内容生成 Word 文档保存到本地吗?下面我给你一个整体思路和示例代码框架,帮助你快速实现:
方案概述
- 前端/接口 用 Flask 提供 HTTP 接口,接收用户提问。
- 调用大模型 使用 Dify + DeepSeek API(假设你已经有调用接口的方法)得到回答文本。
- 生成 Word 文档 使用 Python 的
python-docx
库将回答写入 Word 文件。 - 保存到本地 将生成的
.docx
文件保存在服务器指定目录。 - 返回下载链接或路径 让用户可以下载或查看。
关键技术点
- Flask 快速搭建接口
- 通过 DeepSeek / Dify 调用大模型回答
- python-docx 生成 Word 文件
- 文件保存与访问权限控制
环境准备
pip install flask python-docx requests
示例代码
from flask import Flask, request, send_from_directory, jsonify
from docx import Document
import os
import requests
app = Flask(__name__)
# 配置保存路径
SAVE_FOLDER = './generated_docs'
os.makedirs(SAVE_FOLDER, exist_ok=True)
# 假设调用大模型的函数 (你需根据实际API改写)
def query_deepseek(question: str) -> str:
# 这里用伪代码演示调用Dify/DeepSeek接口
url = "https://api.deepseek.example.com/generate_answer"
headers = {"Authorization": "Bearer YOUR_API_KEY"}
data = {"question": question}
resp = requests.post(url, json=data, headers=headers)
if resp.status_code == 200:
return resp.json().get("answer", "")
else:
return "调用模型失败,请稍后重试。"
@app.route('/ask', methods=['POST'])
def ask():
data = request.json
question = data.get('question')
if not question:
return jsonify({"error": "问题不能为空"}), 400
# 调用大模型
answer = query_deepseek(question)
# 生成 Word 文档
doc = Document()
doc.add_heading("问题", level=1)
doc.add_paragraph(question)
doc.add_heading("回答", level=1)
doc.add_paragraph(answer)
# 保存文件,文件名用时间戳或UUID避免冲突
import time
filename = f"answer_{int(time.time())}.docx"
filepath = os.path.join(SAVE_FOLDER, filename)
doc.save(filepath)
# 返回文件名,客户端可请求下载
return jsonify({"filename": filename, "message": "回答生成成功"})
@app.route('/download/<filename>', methods=['GET'])
def download_file(filename):
return send_from_directory(SAVE_FOLDER, filename, as_attachment=True)
if __name__ == '__main__':
app.run(debug=True, port=5000)
使用说明
- 启动 Flask 服务
- 发送 POST 请求到
/ask
,JSON 体示例:
{
"question": "什么是人工智能?"
}
- 得到返回 JSON:
{
"filename": "answer_1681234567.docx",
"message": "回答生成成功"
}
- 浏览器或客户端访问
/download/answer_1681234567.docx
下载文件。
注意事项
- 你需要替换
query_deepseek
函数中的 API 地址和认证方式为你实际使用的 Dify 或 DeepSeek 接口。 - 文件保存目录权限要配置正确,确保 Flask 进程有读写权限。
- 生产环境建议加入身份认证和访问控制,避免文件泄漏。
- 可根据需求美化 Word 格式,如添加标题样式、段落间距、页眉页脚等。
完整示例:Flask + Dify/DeepSeek 模拟接口 + Word 文件生成
from flask import Flask, request, send_from_directory, jsonify
from docx import Document
import os
import time
import uuid
import requests
app = Flask(__name__)
# 文件保存目录
SAVE_FOLDER = './generated_docs'
os.makedirs(SAVE_FOLDER, exist_ok=True)
# 模拟调用大模型接口函数(请替换为你实际的 Dify/DeepSeek API)
def query_dify_deepseek(question: str) -> str:
# 这里演示使用伪API,实际要替换成你的接口
# 示例模拟返回
# 你也可以用 requests.post 调用真实接口
dummy_answer = f"这是针对问题【{question}】的大模型模拟回答内容。"
return dummy_answer
@app.route('/ask', methods=['POST'])
def ask():
data = request.json
question = data.get('question')
if not question:
return jsonify({"error": "问题不能为空"}), 400
# 调用大模型接口
answer = query_dify_deepseek(question)
# 生成 Word 文档
doc = Document()
doc.add_heading("问题", level=1)
doc.add_paragraph(question)
doc.add_heading("回答", level=1)
doc.add_paragraph(answer)
# 生成唯一文件名
filename = f"answer_{int(time.time())}_{uuid.uuid4().hex[:8]}.docx"
filepath = os.path.join(SAVE_FOLDER, filename)
doc.save(filepath)
return jsonify({"filename": filename, "message": "回答生成成功"})
@app.route('/download/<filename>', methods=['GET'])
def download(filename):
try:
return send_from_directory(SAVE_FOLDER, filename, as_attachment=True)
except Exception as e:
return jsonify({"error": "文件不存在或无法访问", "details": str(e)}), 404
if __name__ == '__main__':
app.run(debug=True, port=5000)
使用方法
- 启动服务:
python your_flask_file.py
- 发送请求(示例用 curl):
curl -X POST http://127.0.0.1:5000/ask -H "Content-Type: application/json" -d '{"question":"什么是人工智能?"}'
- 返回示例:
{
"filename": "answer_1681234567_a1b2c3d4.docx",
"message": "回答生成成功"
}
- 下载文件:
浏览器访问
http://127.0.0.1:5000/download/answer_1681234567_a1b2c3d4.docx
即可下载 Word 文件。
下一步
- 你可以把
query_dify_deepseek
替换为真实接口调用逻辑,传入 API Key,调用大模型生成回答。 - 需要生成更复杂 Word 文档格式,如表格、图片等也可以告诉我帮你写。
- 需要前端页面调用和展示,也可以帮你写一个简易的 Web UI。
发表回复