基于通义千问大模型和Flask的智慧问数系统

系统概述

智慧问数系统是一个利用通义千问大模型和Flask框架构建的智能SQL生成与查询平台。该系统能够将自然语言查询自动转换为SQL语句,并执行查询返回结果,极大地简化了数据分析人员的工作流程。

系统架构

系统采用前后端分离架构,主要包含以下模块:

  1. 前端界面 :基于HTML5和Tailwind CSS构建的用户交互界面
  2. 后端服务 :使用Flask框架搭建的RESTful API
  3. AI引擎 :基于通义千问大模型的SQL生成模块
  4. 数据库连接 :支持多种数据库的连接与查询

核心功能

  1. 自然语言转SQL
  2. SQL查询执行
  3. 查询结果可视化
  4. 查询历史管理
  5. 数据导出功能
  6. 技术亮点

1. 通义千问大模型集成

系统通过OpenAI SDK与通义千问大模型进行集成,实现自然语言到SQL的智能转换。核心处理逻辑如下:

def _generate_sql(query, schema_type="charging", table_name=None):
    try:
        client = OpenAI(
            api_key='api-key',
            base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
        )
        db_schema = DatabaseSchema()
        
        if schema_type == "charging":
            table_structure = db_schema.get_charging_schema(table_name)
        else:
            table_structure = db_schema.get_business_schema(table_name)
        
        if not table_structure:
            return "错误:未找到指定的表结构"
        
        prompt = f'''您是SQL专家,现在需要根据以下具体场景编写一个正确且规范的SQL查询语句。
        问题情境:用户希望从数据库中查询满足特定条件的数据,请提供该SQL查询任务的详细描述。
        问题需求:{query}。请基于以下表格结构{table_structure}

        请使用以下格式编写SQL代码,并确保其解答了用户的问题:

        ```sql
        -- 请在此处编写SQL查询语句
        SELECT ...
        FROM ...
        WHERE ...
        GROUP BY ...
        HAVING ...
        ORDER BY ... ;
        ```
        '''
        
        completion = client.chat.completions.create(
            model="qwen-max-2025-01-25",
            messages=[{'role': 'user', 'content': prompt}]
        )
        
        return completion.choices[0].message.content
        
    except Exception as e:
        return f"SQL生成失败:{str(e)}"

2. Flask后端服务

后端采用Flask框架,提供RESTful API接口,主要包含以下路由:

@app.route('/generate_sql_pile_use_day_new', methods=['POST'])
def generate_sql_api():
    """处理SQL生成请求"""
    try:
        data = request.get_json()
        query = data.get('query')
        
        if not query:
            return make_response(False, message='查询描述不能为空', status_code=400)
            
        result = get_sql_from_query(
            query=query,
            schema_type='charging',
            table_name='pile_use_day_new'
        )
        
        return make_response(result['success'], data={
            'sql': result.get('sql'),
            'query_data': result.get('data')
        }, message=result.get('message'))
        
    except Exception as e:
        return make_response(False, message=str(e), status_code=500)

3. 前端交互界面

前端采用响应式设计,提供友好的用户交互体验:

<div class="main-content p-8">
    <div class="max-w-4xl mx-auto">
        <h1 class="text-3xl font-bold text-white mb-8">智能SQL生成器</h1>
        <div class="glass-effect p-6 rounded-lg">
            <textarea id="queryInput" class="w-full p-4 bg-transparent text-white border border-gray-700 rounded-lg" placeholder="请输入您的查询需求..."></textarea>
            <button onclick="generateSQL()" class="mt-4 px-6 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors">生成SQL</button>
        </div>
        <div id="resultContainer" class="mt-8 hidden">
            <div class="glass-effect p-6 rounded-lg">
                <h2 class="text-xl font-bold text-white mb-4">生成的SQL语句</h2>
                <pre id="sqlResult" class="bg-gray-800 p-4 rounded text-white"></pre>
            </div>
            <div class="glass-effect p-6 rounded-lg mt-4">
                <h2 class="text-xl font-bold text-white mb-4">查询结果</h2>
                <div id="queryResult" class="result-table"></div>
            </div>
        </div>
    </div>
</div>

系统实现

在这里插入图片描述

系统优势

  1. 高效性 :快速将自然语言转换为SQL语句
  2. 准确性 :基于大模型的智能理解确保SQL语句的正确性
  3. 易用性 :简洁直观的用户界面
  4. 扩展性 :模块化设计便于功能扩展
  5. 安全性 :严格的输入验证和错误处理

应用场景

  1. 数据分析师快速生成SQL查询
  2. 业务人员自助查询数据
  3. 数据库管理员进行复杂查询
  4. 数据科学家进行数据探索
  5. 企业报表自动化生成

未来展望

  1. 支持更多数据库类型
  2. 增加查询优化建议功能
  3. 实现查询历史管理
  4. 集成数据可视化功能
  5. 开发移动端应用
    通过本系统的构建,我们展示了如何将先进的大模型技术与传统Web开发相结合,为企业提供高效、智能的数据查询解决方案。这种技术组合不仅提高了工作效率,也为未来的智能化数据分析系统开发提供了新的思路。
Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐