首页 > 数据库 >text2sql-workshop

text2sql-workshop

时间:2024-07-27 22:39:29浏览次数:18  
标签:import self workshop sql query text2sql create

text2sql-workshop

https://github.com/fanqingsong/text2sql-workshop

Text2SQL Workshop

Using OpenAI, Langchain and Postgresql to Talk to Your Data


Overview

  • This repo demonstrates the power of Large Language Models and Generative AI for simplifying access to data: instead of querying a database using SQL, why not doing so using Natural Language?
  • text2sql is a basic Python package which ships with Langchain. It contains simple logic for connecting to a local Postgresql instance, and by leveraging Langchain's create_sql_query_chain, it obtains metadata from our local DB instances and creates multiple prompts which are executed against an LLM (in our case, OpenAI ChatGPT).
  • As a result, we are able to convert questions from Natural Language to SQL Queries that are compliant with Postgresql's dialect.

Usage

  • Create a virtual environment with your tool of choice and install the text2sql Python package
  • Once the package is installed, you can create an IPython kernel and use it in Jupyter - checkout the notebooks provided in the sandbox folder.

Example

from text2sql.core import Text2SQL

sql = Text2SQL(model = "gpt-3.5-turbo")
query = sql.query("How much do we have in total sales?")
print(query)
 
> SELECT SUM("Weekly_Sales") AS total_sales FROM sales
 

Prereqs

  • We use Docker to boot up a Postgresql DB. Just run docker-compose up -d and you should be good to go
  • To ingest data into Postgres, run text2sql/ingest.py (for simplification purposes, the package expects you to be running a local instance of Postgresql at port 5432)
  • Make sure that you properly set your OPENAI_API_KEY
from langchain.chat_models import ChatOpenAI
from langchain.chains import create_sql_query_chain
from langchain.utilities.sql_database import SQLDatabase
from sqlalchemy import create_engine
from pydantic import BaseModel
from langchain_core.runnables.base import Runnable
import pydantic

class Text2SQL(BaseModel):

    uri: str = "postgresql://postgres:changeme@localhost:5432"
    # model: str = "gpt-4-1106-preview"
    model: str = "internlm/internlm2_5-7b-chat-gguf/internlm2_5-7b-chat-q2_k.gguf"
    
    temperature: int = 0

    class Config:
        arbitrary_types_allowed = True

    @pydantic.computed_field()
    @property
    def db(self) -> SQLDatabase:
        return SQLDatabase(engine = create_engine(self.uri))
    
    @pydantic.computed_field()
    @property
    def llm(self) -> ChatOpenAI:
        return ChatOpenAI(
            model = self.model,
            temperature = self.temperature,
            api_key="...",  
            base_url="http://192.168.0.108:1234/v1/",
        )
    
    @pydantic.computed_field()
    @property
    def chain(self) -> Runnable:
        return create_sql_query_chain(
            llm = self.llm,
            db = self.db
        )
    
    def query(self, question: str):

        response = self.chain.invoke({"question": question})
        sql_query = response.split("SQLQuery:")[0]

        return sql_query

 

效果

 

 

 

 

参考

https://python.langchain.com/v0.2/docs/integrations/chat/openai/

https://github.com/Konex25/ChatDB

 

平台

https://github.com/chat2db/Chat2DB

http://chat2db.ai/en-US

 

标签:import,self,workshop,sql,query,text2sql,create
From: https://www.cnblogs.com/lightsong/p/18327642

相关文章

  • Registry Workshop —— 强大的注册表编辑工具
    RegistryWorkshop——强大的注册表编辑工具简介RegistryWorkshop是一款高级的注册表编辑工具,除了RegEdit的特性外,RegistryWorkshop提供许多其他功能提高注册表编辑操作效率:能够剪切,复制和粘贴注册项和键值名,还可以进行撤销和重做操作;能够快速地查找和替换所需注册......
  • IconWorkshop软件安装包下载
    简介:IconWorkshop是一款专业的图标制作软件,通过IconWorkshop可以为创建Windows图标,制作Mac图标以及Unix等操作系统创建图标。为Vista/7系统创建256x256的Windows图标,为OSXLion系统创建1024x1024的Macintosh图标。以及为Unix/Linux系统制作PNG图标。安装包获取地址......
  • 【Text2SQL 论文】SQLova:首次将 PLM 应用到 NL2SQL 中
    论文:AComprehensiveExplorationonWikiSQLwithTable-AwareWordContextualization⭐⭐⭐⭐KR2MLWorkshopatNeurIPS2019,arXiv:1902.01069Code:SQLova|GitHub参考文章:将预训练语言模型引入WikiSQL任务|CSDN一、论文速度这篇论文对SQLNet进行改进,首......
  • NL2SQL实践系列(1):深入解析Prompt工程在text2sql中的应用技巧
    NL2SQL实践系列(1):深入解析Prompt工程在text2sql中的应用技巧NL2SQL基础系列(1):业界顶尖排行榜、权威测评数据集及LLM大模型(SpidervsBIRD)全面对比优劣分析[Text2SQL、Text2DSL]NL2SQL基础系列(2):主流大模型与微调方法精选集,Text2SQL经典算法技术回顾七年发展脉络梳理NL2SQL进......
  • NL2SQL进阶系列(2):DAIL-SQL、DB-GPT开源应用实践详解Text2SQL
    NL2SQL进阶系列(2):DAIL-SQL、DB-GPT开源应用实践详解[Text2SQL]NL2SQL基础系列(1):业界顶尖排行榜、权威测评数据集及LLM大模型(SpidervsBIRD)全面对比优劣分析[Text2SQL、Text2DSL]NL2SQL基础系列(2):主流大模型与微调方法精选集,Text2SQL经典算法技术回顾七年发展脉络梳理NL2SQL......
  • NL2SQL进阶系列(1):DB-GPT-Hub、SQLcoder、Text2SQL开源应用实践详解
    NL2SQL进阶系列(1):DB-GPT-Hub、SQLcoder、Text2SQL开源应用实践详解NL2SQL基础系列(1):业界顶尖排行榜、权威测评数据集及LLM大模型(SpidervsBIRD)全面对比优劣分析[Text2SQL、Text2DSL]NL2SQL基础系列(2):主流大模型与微调方法精选集,Text2SQL经典算法技术回顾七年发展脉络梳理1.......
  • NL2SQL基础系列(1):业界顶尖排行榜、权威测评数据集及LLM大模型(Spider vs BIRD)全面对比
    NL2SQL基础系列(1):业界顶尖排行榜、权威测评数据集及LLM大模型(SpidervsBIRD)全面对比优劣分析[Text2SQL、Text2DSL]Text-to-SQL(或者Text2SQL),顾名思义就是把文本转化为SQL语言,更学术一点的定义是:把数据库领域下的自然语言(NaturalLanguage,NL)问题,转化为在关系型数据库中可以执行的......
  • AWS ECS + CloudMap + Lambda workshop (一)
    第一篇:创建一个由ECS托管的nodejs服务一)创建拥有AdministratorAccess的IAMUser,在权限设置时,直接给予AdministratorAccess二)ECR服务注册代码仓库输入名称,完成Copy镜像库的URIXXXXXXXX.dkr.ecr.cn-northwest-1.amazonaws.com.cn/nodejs-test-app使用docker命令上传代......
  • 软件测试/人工智能|人工智能与智能化测试Workshop
    在数字化时代,软件测试不再仅仅是繁琐的手动过程,智能测试与智能化测试技术正在成为测试领域的创新引擎,为质量保障和软件开发提供前所未有的机会。为了帮助大家系统地掌握人工智能在测试领域的应用,我们特别推出了为期2天的人工智能与智能化测试Workshop,本次活动支持线上和线下两种......
  • 如何实施符合功能安全及ASPICE要求的模型动态测试 ——TPT Workshop邀请函
    尊敬的女士/先生:2023年3月,北汇信息与诸多工程师相约上海,成功举办了今年第一场TPTWorkshop活动,与大家进行了深入的技术交流。如今,2023年已渐渐步入尾声,我们将在北汇信息上海总部再次举办题为“如何实施符合功能安全及ASPICE要求的模型动态测试”的TPTWorkshop活动,诚邀各位新老......