当前位置: 首页 > news >正文

【Langchain系列五】DbGPT——Langchain+PG构建结构化数据库智能问答系统

1. 官方资料

  • 构建一个基于SQL数据的问答系统
  • 构建基于SQL数据的问答系统

2. 安装依赖

pip install langchain langchain_community pyjwt langgraph psycopg2

3. 完整代码

import os
import re
import time
from operator import itemgetterfrom langchain.chains.sql_database.query import create_sql_query_chain
from langchain_community.agent_toolkits import SQLDatabaseToolkit
from langchain_community.chat_models.zhipuai import ChatZhipuAI
from langchain_community.tools import QuerySQLDataBaseTool
from langchain_community.utilities import SQLDatabase
from langchain_core.messages import AIMessage, SystemMessage, HumanMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate, ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import RunnablePassthrough
from langchain_ollama import ChatOllama
from langgraph.prebuilt import create_react_agent# 设置OpenAI API密钥(GLM-4-Flash是免费模型,其它模型会消耗token,智谱的是目前免费模型最好的)
os.environ["ZHIPUAI_API_KEY"] = "你的key"
# llm = ChatZhipuAI(model="GLM-4-Flash", temperature=0.5)
# 本地模型也可以,但是glm4(9b)的效果明显优于qwen2.5:7b,和线上GLM-4-Flash的差不多
# llm=ChatOllama(base_url="http://localhost:11434", model="qwen2.5:7b", temperature=0.5)
llm = ChatOllama(base_url="http://localhost:11434", model="glm4:latest", temperature=0.5)
# qwen 效果有点差,罗里吧嗦还不对
# os.environ["DASHSCOPE_API_KEY"] = "密钥"
# llm = ChatTongyi(model="qwen-turbo", verbose=True)
# 讯飞星火(貌似不支持数据库的智能问答,会报错找不到model kwords
# llm = ChatSparkLLM(spark_app_id='应用id',spark_api_key='应用key',spark_api_secret='应用密钥',spark_llm_domain='generalv3.5')ai_db = SQLDatabase.from_uri("postgresql://postgres:123456@localhost:5433/ai_data",include_tables=['engineering_defect_detail'])def query_with_agent(q: str):"""基于代理的agent+提示词的查询优化(给定提示词后,可以解决枚举问题,但是给出的答案不是基于sql真实执行的结果):return:"""prompt = '''Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.Use the following format strictly:Question: "Question here"SQLQuery: "SQL Query to run"SQLResult: "The actual result after executing the SQLQuery in the database"Answer: "Final answer here"Only use the following tables:['engineering_defect_detail(pk_defect_id, defect_component_id (部件), defect_component_type_id (部件种类), defect_position_id (部位), defect_description_id (缺陷描述), message (缺陷描述), detail (详情), levl ('1'一般,'2'严重,'3'危急), type ('0'可见光,'1'红外线), tower_name (杆塔名称), contri_company_name (出资单位名称), property_company_name (资产单位名称), maintain_company_name (运维单位名称), start_tower_no (起始杆号), end_tower_code (终止杆号), search_defect_name (查缺发现人), search_defect_num (查缺发现人人资编码), polling_time (巡检时间), sortie_num (架次信息), aircrew_name (机组名称), plan_name (归属方案名称), cruise_name (航检员名称), cruise_number (航检员编号), task_type (任务类型), line_iden (线路唯一标识), line_name (线路名称), task_year (任务年度), voltage_level (电压等级), electricty_type (交流、直流), single_double (同塔双回), sortie_position (架次位置), defect_check_person (视频查缺人员), defect_check_time (视频查缺时间), quality_check_person (质检人员), quality_check_time (质检时间), visible_operator_id (可见光操作员), infrared_operator_id (红外操作员), laser_operator_id (激光操作员), visible_operator_name (可见光操作员名称), infrared_operator_name (红外操作员名称), laser_operator_name (激光操作员名称), patrol_index (巡视轮次), down_base_dist (下基档距), elevation (高程), longtitude (经度), latitude (纬度), crm_iden (crm唯一标识), business_tag (业务标签(1:年度任务、2:三项专跨京广、3:三项专跨京沪)), defect_type (缺陷类型 0 普通(一次查缺) 1抽检(二次查缺))), and index keys: idx_polling_time(`polling_time`) , idx_air
http://www.dtcms.com/a/337558.html

相关文章:

  • Pageable 菜教程
  • AI应用商业化加速落地 2025智能体爆发与端侧创新成增长引擎
  • BeeWorks 私有化会议系统:筑牢企业会议安全防线,赋能高效协同
  • 敏感数据加密平台设计实战:如何为你的系统打造安全“保险柜”
  • 高防IP如何实现秒级切换?
  • 技术赋能安全:智慧工地构建城市建设新防线
  • AR技术为消防救援装上“智能透视眼”
  • K8s的命名空间需要创建吗
  • 容器化 Android 开发效率:cpolar 内网穿透服务优化远程协作流程
  • Redis主从架构、哨兵模式及集群比较
  • 利用深度强化学习进行芯片布局
  • 【1:1复刻R版】python版火山图函数一键出图
  • iOS 正式包签名指南
  • 【LeetCode】15. 三数之和
  • 从财务整合到患者管理:德国医疗集团 Asklepios完成 SAP S/4HANA 全链条升级路径
  • 贪心算法(Greedy Algorithm)详解
  • 【机器学习】Macro-F1(宏平均 F1)是什么?
  • SWMM排水管网水力、水质建模及在海绵与水环境中的应用技术-模拟降雨和污染物质经过地面、排水管网、蓄水和处理
  • Jenkins启动端口修改失败查找日志
  • 音频算法工程师技能1
  • Vue2篇——第五章 Vue.js 自定义指令与插槽核心
  • 【序列晋升】:9 Service Mesh微服务通信的基础设施革命
  • 电子元器件-电容终篇:基本原理、参数解读、电路作用、分类及区别、应用场景、选型、降频及实战案例
  • Linux 系统~存储高级技术
  • C++ 中的 delete 与 default 关键字详解
  • diffusion model(1.4) 相关论文阅读清单
  • 遥感数字图像处理教程——第三章课后习题
  • flutter项目适配鸿蒙
  • 人工智能 | 基于大数据的皮肤病症状数据可视化分析系统(matlab源码)
  • Java设计模式-桥接模式