自然语言查询、分析数据(NL-TO-SQL)的尝试
让查数据这件事,不再是高高在上、遥不可及的技能,而是人人都能玩得转,妙趣横生的小技巧。比如“我的店上周赚了多少钱?哪个商品即将售罄?这个月卖的最多的商品是啥?”,下一秒,答案就像变魔术一样蹦出来。直接看效果
流程如下,
- 前端使用gradio,采集麦克风声音
- 后端收到声音,使用openai的speech-to-text
- 文字通过dbgpt(链接各种自定义模型),转换为sql进行数据库查询
- 返回对应数据
关键代码
# -*- coding: utf-8 -*-
import gradio as gr
from openai import OpenAI
import os
import requests
import json
os.environ["OPENAI_API_KEY"] = "sk-3zxxxxxx" # Replace with your key
client = OpenAI()
def processAudio(audio):
#print(audio)
if audio is None:
return "No audio inputs were provided."
au = open(audio, "rb")
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=au,
response_format="text"
)
print(transcript)
#return transcript
url = "https://demo-dbgpt-backend.sunmi.com/api/v1/chat/completions"
headers = {"Content-Type": "application/json; charset=utf-8"}
data = {
"select_param": "bmw",
"chat_mode": "chat_with_db_execute",
"model_name": "chatgpt_proxyllm",
"user_input": transcript
}
response = requests.post(url, headers=headers, data=json.dumps(data))
response.encoding='utf-8'
if response.status_code == 200:
print(response.text)
return response.text
else:
return "Error making request."
demo2 = gr.Interface(
fn=processAudio,
inputs=[
gr.Audio(type="filepath", label="Upload Audio")
],
outputs="textbox",
title="语音查询数据库 - 融合openai&DBGPT",
description="Please ask database questions in natural language; the data set is fabricated and includes information such as orders, products, users, etc."
)
if __name__ == "__main__":
demo2.launch(server_name='0.0.0.0', server_port=1088)
注意事项
- DBGPT官方部署文档写的不够直观,部署稍费事
- gradio采集麦克风需要使用https,否则出发浏览器安全限制