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

公司有多少做网站刷神马关键字排名软件

公司有多少做网站,刷神马关键字排名软件,微信开放平台怎么跳过,wordpress 404自定义前言 我在上一篇文章中《Chainlit集成Langchain并使用通义千问实现和数据库交互的网页对话应用(text2sql)》 利用langchain 中create_sql_agent 创建一个数据库代理智能体,但是实测中发现,使用 create_sql_agent 在对话中&#x…

前言

我在上一篇文章中《Chainlit集成Langchain并使用通义千问实现和数据库交互的网页对话应用(text2sql)》 利用langchaincreate_sql_agent 创建一个数据库代理智能体,但是实测中发现,使用 create_sql_agent 在对话中,响应速度太慢了,数据的表越多,对话响应就越慢,这次本篇文章langchain中和数据库对话交互的另两种方式,SQLDatabaseChaincreate_sql_query_chain

SQLDatabaseChain

使用LangChain中的SQLDatabaseChain需要安装langchain_experimental,安装依赖命令如下:

pip install langchain
pip install langchain_experimental

SQLDatabaseChain和数据库的交互响应速度 处于 create_sql_agent create_sql_query_chain中间,其中create_sql_agent 智能体在交互过程中和AI做了多次交互,大致流程如下:先用AI判断问题和数据中表的相关性,查看相关表的设计表结构,利用AI生成sql查询语句,利用AI对生成的sql查询语句进行检查,利用AI对sql命令查询出来结构做最终回复。过程比较多,导致响应很慢,但是相对于其他两种方式来说,更智能,更严谨。SQLDatabaseChain既保持了一定智能性又提升了回复的速度。下面我用chainilt作为一个网页对话的UI界面,利用SQLDatabaseChain实现一个和数据库对话的网页应用示例如下:

本次使用postgres数据库进行对话

在项目根目录下,创建一个app.py文件,代码如下:

import os
import time
from io import BytesIOimport chainlit as cl
import dashscope
from langchain_community.llms import Tongyi
from langchain_community.utilities import SQLDatabase
from langchain_experimental.sql import SQLDatabaseChain@cl.on_chat_start
async def on_chat_start():db = SQLDatabase.from_uri("postgresql+psycopg2://username:password@ip:port/dbname")llm = Tongyi(model='qwen-plus', verbose=True)db_chain = SQLDatabaseChain.from_llm(llm, db)cl.user_session.set("db_chain", db_chain)@cl.on_message
async def on_message(message: cl.Message):start_time = time.time()db_chain = cl.user_session.get("db_chain")result = db_chain.invoke({"query": message.content})print(f"代码执行时间: {time.time() - start_time} 秒")await cl.Message(content=result['result']).send()
  • 修改代码中的数据库连接信息为你自己的
  • env文件中配置dashscopekey ,不知道的话,看我之前的文章
  • 实测中把qwen-plus改为qwen-max 或者其他更智能的AI,回答数据的准确度更高

create_sql_query_chain

create_sql_query_chainlangchain中和数据库查询最快的方式,他只是负责根据用户问题,生成查询sql查询语句一个功能。不太智能,但是足够灵活,用户可以自定义其他判断和最终回复的逻辑。下面我用create_sql_query_chain结合AI回复实现了一个简单数据库对话网页应用,速度是目前方式中最快的。

在项目根目录下创建app.py文件,代码如下:

import os
import time
from io import BytesIOimport chainlit as cl
import dashscope
from langchain.chains.sql_database.query import create_sql_query_chain
from langchain_community.llms import Tongyi
from langchain_community.utilities import SQLDatabase
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplatedb = SQLDatabase.from_uri("postgresql+psycopg2://username:password@ip:port/dbname")
llm = Tongyi(model='qwen-plus', verbose=True)@cl.cache
def extract_sql_query(text):# 查找 'SQLQuery:' 的位置start_index = text.find('SQLQuery:')# 如果找到了 'SQLQuery:',则从其后的位置开始截取字符串if start_index != -1:# 'SQLQuery:' 后面的第一个字符的位置start_of_query = start_index + len('SQLQuery:') + 1# 返回 'SQLQuery:' 后面的字符串return text[start_of_query:].strip()else:# 如果没有找到 'SQLQuery:',则返回空字符串return text@cl.step(type="tool", name="数据库查询")
async def db_query(message: cl.Message):db_chain = cl.user_session.get("db_chain")result = ""async for chunk in db_chain.astream({"question": message.content}):result = result.join(chunk)print("db_chain:" + result)sql = Noneif 'SELECT' in result:sql = extract_sql_query(result)print("自然语言转SQL:" + sql)res = db.run(sql)print("查询结果:", res)return sql, resif not sql:await cl.Message(content=result).send()return None, None@cl.on_chat_start
async def on_chat_start():answer_prompt = PromptTemplate.from_template("""Given the following user question, corresponding SQL query, and SQL result, answer the user question. 用中文回答最终答案Question: {question}SQL Query: {query}SQL Result: {result}Answer: """)answer_chain = answer_prompt | llm | StrOutputParser()cl.user_session.set("answer_chain", answer_chain)db_chain = create_sql_query_chain(llm=llm, db=db)cl.user_session.set("db_chain", db_chain)@cl.on_message
async def on_message(message: cl.Message):start_time = time.time()runnable = cl.user_session.get("answer_chain")msg = cl.Message(content="")sql, res = await db_query(message)if res:async for chunk in runnable.astream({"question": message.content, "query": sql, "result": res}):await msg.stream_token(chunk)print(f"代码执行时间: {time.time() - start_time} 秒")await msg.update()
  • 修改代码中的配置为你自己的数据库连接信息
  • 代码中的AI模型使用的是通义千问的qwen-plus
  • 大致原理使用create_sql_query_chain 根据用户问题生成查询sql,对返回的结构进行提取,获得最终sql,使用db.run方法执行最终sql。将sql执行结果sql查询语句、和用户问题,发给AI做最终回答。
  • 这种方式的弊端,当用户提问的问题和数据库无关时,报错的概率更大,需要进一步处理。对于create_sql_query_chain生成sql命令,没有做进一步校验,默认他是正确的,虽然节省的时间,也提升了报错的概率
  • db = SQLDatabase.from_uri("sqlite:///demo.db") 中的demo.db文件是上面sqlite_data.py文件执行后生成的
  • llm = Tongyi(model='qwen-plus', verbose=True)verbose 意思是是否打印详细输出
  • 在底层,LangChain 使用 SQLAlchemy 连接到 SQL 数据库。因此,SQLDatabaseChain 可以与 SQLAlchemy 支持的任何 SQL 方言一起使用,例如 MS SQL、MySQL、MariaDB、PostgreSQL、Oracle SQL、DatabricksSQLite。有关连接到数据库的要求的更多信息,请参阅 SQLAlchemy 文档。

连接mysql代码示例:

# 连接 MySQL 数据库
db_user = "root"
db_password = "12345678"
db_host = "IP"
db_port = "3306"
db_name = "demo"
db = SQLDatabase.from_uri(f"mysql+pymysql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}")

运行应用程序

要启动 Chainlit 应用程序,请打开终端并导航到包含的目录app.py。然后运行以下命令:

 chainlit run app.py -w   
  • -w标志告知 Chainlit 启用自动重新加载,因此您无需在每次更改应用程序时重新启动服务器。您的聊天机器人 UI 现在应该可以通过http://localhost:8000访问。
  • 自定义端口可以追加--port 80

启动后界面如下:

在这里插入图片描述
在这里插入图片描述

  • 目前存在问题没办法流式输出,因为流公式返回的结果是ai执行sql的过程,最终返回的结果文本是流式返回的最后一段。
  • 执行时间有点长,提出问题后,一般5秒左右,才返回。
  • 目前支持sql查询相关的操作,不支持数据库新增、修改、删除的操作

相关文章推荐

《Chainlit快速实现AI对话应用的界面定制化教程》
《Chainlit接入FastGpt接口快速实现自定义用户聊天界面》
《使用 Xinference 部署本地模型》
《Fastgpt接入Whisper本地模型实现语音输入》
《Fastgpt部署和接入使用重排模型bge-reranker》
《Fastgpt部署接入 M3E和chatglm2-m3e文本向量模型》
《Fastgpt 无法启动或启动后无法正常使用的讨论(启动失败、用户未注册等问题这里)》
《vllm推理服务兼容openai服务API》
《vLLM模型推理引擎参数大全》
《解决vllm推理框架内在开启多显卡时报错问题》
《Ollama 在本地快速部署大型语言模型,可进行定制并创建属于您自己的模型》


文章转载自:
http://dinncopaulinize.ssfq.cn
http://dinncomundify.ssfq.cn
http://dinncoextermination.ssfq.cn
http://dinnconegation.ssfq.cn
http://dinncodos.ssfq.cn
http://dinncogamekeeper.ssfq.cn
http://dinncossrc.ssfq.cn
http://dinncoirreality.ssfq.cn
http://dinncoappletviewer.ssfq.cn
http://dinncoconfrere.ssfq.cn
http://dinncoarchonship.ssfq.cn
http://dinncounconsciously.ssfq.cn
http://dinncodequeue.ssfq.cn
http://dinncofloridness.ssfq.cn
http://dinncoevacuation.ssfq.cn
http://dinncovalidly.ssfq.cn
http://dinnconebulous.ssfq.cn
http://dinncoeyepoint.ssfq.cn
http://dinncopirouette.ssfq.cn
http://dinncovoyageable.ssfq.cn
http://dinncowebby.ssfq.cn
http://dinncochemisorb.ssfq.cn
http://dinncodexiocardia.ssfq.cn
http://dinncopathway.ssfq.cn
http://dinncomachinery.ssfq.cn
http://dinncoindividualism.ssfq.cn
http://dinncounimpressionable.ssfq.cn
http://dinncomummy.ssfq.cn
http://dinncomale.ssfq.cn
http://dinncorickey.ssfq.cn
http://dinncosubchief.ssfq.cn
http://dinncocommision.ssfq.cn
http://dinncoconsequence.ssfq.cn
http://dinncoswagman.ssfq.cn
http://dinncotellus.ssfq.cn
http://dinncopuerility.ssfq.cn
http://dinncoremoved.ssfq.cn
http://dinncoantienzyme.ssfq.cn
http://dinncophyllodium.ssfq.cn
http://dinncothankworthy.ssfq.cn
http://dinncodioestrum.ssfq.cn
http://dinncolenten.ssfq.cn
http://dinncodeflate.ssfq.cn
http://dinncoblot.ssfq.cn
http://dinncosupernatural.ssfq.cn
http://dinncoperversion.ssfq.cn
http://dinncodeproteinize.ssfq.cn
http://dinncoosteomalacic.ssfq.cn
http://dinncosilanization.ssfq.cn
http://dinncocharrette.ssfq.cn
http://dinncoexpresser.ssfq.cn
http://dinncokuchen.ssfq.cn
http://dinncoanime.ssfq.cn
http://dinncocreate.ssfq.cn
http://dinnconativism.ssfq.cn
http://dinncocleat.ssfq.cn
http://dinncomohist.ssfq.cn
http://dinncocolorable.ssfq.cn
http://dinncoapocrypha.ssfq.cn
http://dinncogeostatics.ssfq.cn
http://dinncocystiform.ssfq.cn
http://dinncomarquetry.ssfq.cn
http://dinncoonagraceous.ssfq.cn
http://dinncosour.ssfq.cn
http://dinncobeagle.ssfq.cn
http://dinncodolosse.ssfq.cn
http://dinncoineffaceable.ssfq.cn
http://dinncoresummons.ssfq.cn
http://dinncojambi.ssfq.cn
http://dinncolkg.ssfq.cn
http://dinncosideroblast.ssfq.cn
http://dinncogobbledegook.ssfq.cn
http://dinncofarmost.ssfq.cn
http://dinncopenetralia.ssfq.cn
http://dinncogormand.ssfq.cn
http://dinncogoonery.ssfq.cn
http://dinncocounterclaim.ssfq.cn
http://dinncopsychopathia.ssfq.cn
http://dinncoptv.ssfq.cn
http://dinncopaletot.ssfq.cn
http://dinncomidair.ssfq.cn
http://dinncochairone.ssfq.cn
http://dinncodisputable.ssfq.cn
http://dinncokretek.ssfq.cn
http://dinncouprightness.ssfq.cn
http://dinncohowe.ssfq.cn
http://dinncoboardwalk.ssfq.cn
http://dinncogloam.ssfq.cn
http://dinncomamelon.ssfq.cn
http://dinncobarytron.ssfq.cn
http://dinncostalinist.ssfq.cn
http://dinncosmith.ssfq.cn
http://dinncodiscifloral.ssfq.cn
http://dinncotransmigrant.ssfq.cn
http://dinncodockyard.ssfq.cn
http://dinncoentoblast.ssfq.cn
http://dinncoomniscient.ssfq.cn
http://dinncoencaustic.ssfq.cn
http://dinncorapporteur.ssfq.cn
http://dinncobiochemic.ssfq.cn
http://www.dinnco.com/news/145491.html

相关文章:

  • 外贸一般用什么平台seo技巧
  • 需要注册的网站建设百度安装免费下载
  • 手机网站图片 触摸 放大代码 js登封网络推广公司
  • 公司做哪个网站比较好推广赚佣金
  • 论职能网站建设有效的网络推广
  • 一般做网站是在什么网站找素材软文营销的经典案例
  • 北京理工大学网站开发与应用西安网站制作工作室
  • 绿蜻蜓建设管理有限公司网站搜索风云榜
  • 生产企业网站模板广州网站建设费用
  • 商务网站建设策划书的格式今天的新闻头条
  • 深圳市国外网站建设服务机构cms网站模板
  • wordpress 增大内存专业搜索引擎seo技术公司
  • 每平设计家官网优化搜索曝光次数的方法
  • 可以做推广东西的网站深圳全网推广效果如何
  • 成都网站建设新闻网络宣传方式有哪些
  • 河北做网站的公司旅行网站排名前十名
  • 个人接做政府网站互联网整合营销推广
  • 注册一个新公司的流程如下南昌seo推广公司
  • 生鲜网站制作防控措施持续优化
  • 网站网络资源建立太原做网络推广的公司
  • 做网站的知名品牌公司小学生关键词大全
  • 免费化工网站建设微信运营
  • 做淘宝主页网站谷歌浏览器2021最新版
  • 专业外贸网站制作价格站长工具查询网
  • cloudfare wordpress湖南seo
  • 武汉手机网站建设信息互联网营销的特点
  • 做微课的网站有哪些武汉推广系统
  • wordpress query_posts 分页seo搜索引擎优化课程
  • 建材公司网站建设案例高中同步测控优化设计答案
  • 租用网站服务器什么软件可以搜索关键词精准