从零开始构建一个 RAG + Flask 问答系统

🧠 用 Flask + HuggingFace + OpenRouter 打造一个免费的 RAG 问答系统(完整教学)

✅ 项目介绍

我们要做一个网页,可以上传 .txt 文档,然后问它问题,它会回答你。

这个功能背后用了 AI 模型,但我们不会花钱,全程使用免费的模型和服务!


🧱 步骤一:准备环境

1. 创建一个新文件夹

复制代码
mkdir rag_flask_app
cd rag_flask_app

2. 创建并激活虚拟环境(可选但推荐)

复制代码
conda create -n rag_env python=3.10 -y
conda activate rag_env

3. 安装依赖

创建一个 requirements.txt 文件:

复制代码
flask
llama-index>=0.10.0
sentence-transformers
llama-index-embeddings-huggingface
openai

安装依赖:

复制代码
pip install -r requirements.txt

📁 步骤二:构建项目结构

复制代码
mkdir templates
mkdir uploads
touch app.py templates/index.html

🧠 步骤三:写入核心代码

🔹 app.py(主逻辑)

复制代码
from flask import Flask, render_template, request
import os
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.openai import OpenAI
from llama_index.core.settings import Settings

# 初始化 Flask
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)

# 设置模型
embed_model = HuggingFaceEmbedding(model_name="all-MiniLM-L6-v2")  # 本地嵌入模型
llm = OpenAI(
    api_base="https://openrouter.ai/api/v1",
    api_key="sk-or-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",  # 👉 填你的 OpenRouter Key
    model="gpt-3.5-turbo"
)

# 应用到 LlamaIndex 设置
Settings.embed_model = embed_model
Settings.llm = llm

index = None

@app.route('/', methods=['GET', 'POST'])
def index_page():
    global index
    response = ""

    if request.method == 'POST':
        if 'file' in request.files:
            file = request.files['file']
            filepath = os.path.join(UPLOAD_FOLDER, file.filename)
            file.save(filepath)

            documents = SimpleDirectoryReader(UPLOAD_FOLDER).load_data()
            index = VectorStoreIndex.from_documents(documents)
            response = "Document uploaded and indexed!"

        elif 'question' in request.form and index is not None:
            question = request.form['question']
            query_engine = index.as_query_engine()
            response = query_engine.query(question).response

    return render_template("index.html", response=response)

if __name__ == '__main__':
    app.run(debug=True)

🔹 templates/index.html(网页界面)

复制代码
<!DOCTYPE html>
<html>
<head>
    <title>RAG Web App</title>
</head>
<body>
    <h1>🧠 RAG + LLM Document QA</h1>

    <form method="POST" enctype="multipart/form-data">
        <p><strong>Upload a .txt document:</strong></p>
        <input type="file" name="file" required>
        <input type="submit" value="Upload">
    </form>

    <form method="POST">
        <p><strong>Ask a question about the document:</strong></p>
        <input type="text" name="question" required style="width:300px;">
        <input type="submit" value="Ask">
    </form>

    <h2>Answer:</h2>
    <p>{{ response }}</p>
</body>
</html>

🔑 步骤四:获取免费 OpenRouter Key

  1. 注册:https://openrouter.ai

  2. 登录后点击右上角头像 → API Keys

  3. 创建新 key,会像这样:

    复制代码
    sk-or-v1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  4. app.py 中粘贴进去替换


🚀 步骤五:运行你的应用!

在终端里运行:

复制代码
python app.py

打开浏览器访问: 👉 http://127.0.0.1:5000


🧪 测试流程

  1. 上传一个 .txt 文档(例如一段英文文章)

  2. 输入问题,比如:

    • "What is the main topic?"

    • "Who is the author?"

  3. 系统会返回基于文档的智能回答 ✅

相关推荐
智算菩萨3 分钟前
【理论讲解】深度多任务学习:概念体系、方法谱系与跨领域建模逻辑
人工智能·机器学习·多任务学习
张彦峰ZYF3 分钟前
借助DeepSeek思考产业落地:蒸馏、小模型微调
人工智能·ai·deepseek-v3·deepseek-r1·蒸馏-小模型微调
蓝鲨硬科技4 分钟前
五一视界与摩尔线程深度合作,释放物理AI进化潜能
人工智能
过河卒_zh15667669 分钟前
网信发布2025年“人工智能+政务”规范应用案例拟入选名单公示
人工智能·大模型·aigc·政务·算法备案
540_5409 分钟前
ADVANCE Day26
人工智能·python·机器学习
IT_陈寒12 分钟前
Redis 性能优化实战:5个被低估的配置项让我节省了40%内存成本
前端·人工智能·后端
乾元13 分钟前
用 AI 做联动:当应用层出现问题,网络如何被“自动拉入决策回路”
运维·开发语言·网络·人工智能·ci/cd·自动化
qq_124987075317 分钟前
基于springboot的智能医院挂号系统(源码+论文+部署+安装)
java·人工智能·spring boot·后端·毕业设计
wenxiaohai12321 分钟前
在anaconda中安装cuda-pytorch
人工智能·pytorch·python·anaconda
IT·陈寒22 分钟前
零配置、开箱即用:seekdb 如何成为 AI 时代的“全能嵌入式数据库”? ——基于 OceanBase seekdb 的实践体验与 AI 开发思考
数据库·人工智能·oceanbase