使用Chainlit接入通义千问快速实现一个自然语言转sql语言的智能体

embedded/2024/10/18 9:19:03/

文本到 SQL

让我们构建一个简单的应用程序,帮助用户使用自然语言创建 SQL 查询。

最终结果预览

先决条件

此示例有额外的依赖项。你可以使用以下命令安装它们:

pip install chainlit openai

导入

应用程序

from openai import AsyncOpenAIimport chainlit as cl
cl.instrument_openai()
client = AsyncOpenAI(api_key="YOUR_OPENAI_API_KEY")

定义提示模板和 LLM 设置

代码

template = """SQL tables (and columns):
* Customers(customer_id, signup_date)
* Streaming(customer_id, video_id, watch_date, watch_minutes)A well-written SQL query that {input}:
```"""settings = {"model": "gpt-3.5-turbo","temperature": 0,"max_tokens": 500,"top_p": 1,"frequency_penalty": 0,"presence_penalty": 0,"stop": ["```"],
}

添加辅助逻辑

在这里,我们用@on_message main装饰器装饰该函数,以告诉 Chainlit在每次用户发送消息时运行该main函数。

然后,我们在步骤中将文本包装到 SQL 逻辑中。

应用程序

@cl.set_starters
async def starters():return [cl.Starter(label=">50 minutes watched",message="Compute the number of customers who watched more than 50 minutes of video this month.")]@cl.on_message
async def main(message: cl.Message):stream = await client.chat.completions.create(messages=[{"role": "user","content": template.format(input=message.content),}], stream=True, **settings)msg = await cl.Message(content="", language="sql").send()async for part in stream:if token := part.choices[0].delta.content or "":await msg.stream_token(token)await msg.update()

​完整代码如下:

import base64
from io import BytesIO
from pathlib import Pathimport chainlit as cl
from chainlit.element import ElementBased
from chainlit.input_widget import Select, Slider, Switch, TextInput
from openai import AsyncOpenAIclient = AsyncOpenAI()author = "Tarzan"template = """SQL tables (and columns):
* Customers(customer_id, signup_date)
* Streaming(customer_id, video_id, watch_date, watch_minutes)A well-written SQL query that {input}:
```"""def encode_image(image_path):with open(image_path, "rb") as image_file:return base64.b64encode(image_file.read()).decode("utf-8")@cl.on_settings_update
async def on_settings_update(settings: cl.chat_settings):cl.user_session.set("settings", settings)@cl.step(type="tool")
async def tool():# Simulate a running taskawait cl.sleep(2)return "Response from the tool!"@cl.on_chat_start
async def start_chat():settings = await cl.ChatSettings([TextInput(id="SystemPrompt", label="System Prompt", initial="You are a helpful assistant."),Select(id="Model",label="Model",values=["qwen-turbo", "qwen-plus", "qwen-max", "qwen-long"],initial_index=0,),Slider(id="Temperature",label="Temperature",initial=1,min=0,max=2,step=0.1,),Slider(id="MaxTokens",label="MaxTokens",initial=1000,min=1000,max=3000,step=100,),Switch(id="Streaming", label="Stream Tokens", initial=True),]).send()cl.user_session.set("settings", settings)cl.user_session.set("message_history",[{"role": "system", "content": settings["SystemPrompt"]}],)content = "你好,我是泰山AI智能客服,有什么可以帮助您吗?"msg = cl.Message(content=content, author=author)await msg.send()@cl.on_message
async def on_message(message: cl.Message):settings = cl.user_session.get("settings")print('settings', settings)streaming = settings['Streaming']model = settings['Model']images = [file for file in message.elements if "image" in file.mime]files = [file for file in message.elements if "application" in file.mime]messages = cl.user_session.get("message_history")if files:files = files[:3]file_ids = []for file in files:file_object = await client.files.create(file=Path(file.path), purpose="file-extract")file_ids.append(f"fileid://{file_object.id}")flies_content = {"role": "system","content": ",".join(file_ids)}messages.append(flies_content)if images and model in ["qwen-plus", "qwen-max"]:# Only process the first 3 imagesimages = images[:3]images_content = [{"type": "image_url","image_url": {"url": f"data:{image.mime};base64,{encode_image(image.path)}"},}for image in images]model = "qwen-vl" + model[4:]img_message = [{"role": "user","content": [{"type": "text", "text": message.content}, *images_content],}]messages = messages + img_messagemsg = cl.Message(content="", author=author)await msg.send()# Call the tool# tool_res = await toolmessages.append({"role": "user", "content": template.format(input=message.content)})print('messages', messages)response = await client.chat.completions.create(model=model,messages=messages,temperature=settings['Temperature'],max_tokens=int(settings['MaxTokens']),stream=streaming)if streaming:async for part in response:if token := part.choices[0].delta.content or "":await msg.stream_token(token)else:if token := response.choices[0].message.content or "":await msg.stream_token(token)print('messages', messages)messages.append({"role": "assistant", "content": msg.content})cl.user_session.set("message_history", messages)await msg.update()

试试看

chainlit run .\text2sql.py -w

您可以提出类似这样的问题Compute the number of customers who watched more than 50 minutes of video this month。

在这里插入图片描述


http://www.ppmy.cn/embedded/90426.html

相关文章

HarmonyOS多目标产物构建最佳实践

背景 在Android或iOS开发时经常会有打“马甲”包的场景,就是一套代码打出不同主题的包,一个公司的产品可能针对不同用户提供不同的应用,比如抖音有国内版也有国外版,滴滴有个人版还有企业版,同样的在鸿蒙平台也有类似…

ComfyUI: 报EP Error错误(onnxruntime)

🤶背景描述 在使用反推提示词的时候,按照上一篇介绍的方法是可以正常使用的。 但是看后台的时候,发现有一个错误: *************** EP Error *************** EP Error D:\a\_work\1\s\onnxruntime\python\onnxruntime_pybind_s…

”关于“八股文”对程序员开发作用

在程序员开发的语境中,“八股文”通常指的是那些被广泛讨论、反复练习的技术面试问题和答案,这些问题往往围绕经典的技术知识点,如算法、数据结构、设计模式等。对于“八股文”对程序员开发的作用,可以从以下几个方面进行分析&…

Vulnhub入门篇-Kioptrix2014

1.环境配置 下载地址:https://download.vulnhub.com/kioptrix/kiop2014.tar.bz2 攻击机kali:192.168.26.128(Nat模式) 靶机配置:Nat模式 这里注意,根据官网地址说明,需要我们先将网络适配器…

vue2怎么上传文件夹,并展示文件夹内的图片?

我使用的是element-ui组件库,发现el-upload组件并不能满足需求,于是用原生实现一下,这里贴一下关键代码,如果大家有更好的实现方法,欢迎分享!! 实现效果:

打造专业级电子书,一键导出分享

在这个数字化的时代,电子书作为一种传统的视觉传达方式,依然具有独特的魅力和价值。一本高质量、高颜值的电子书不仅能吸引观众的眼球,还能有效传达信息,提升品牌形象。那么,如何制作出一本高颜值的电子书呢&#xff1…

fastjson-流程分析

参考视频:fasfjson反序列化漏洞1-流程分析 分析版本 fastjson1.2.24 JDK 8u65 分析过程 新建Person类 public class Person {private String name;private int age;public Person() {System.out.println("constructor_0");}public Person(String na…

C++11深度剖析

目录 🚀 前言:C11简介 一: 🔥 统一的列表初始化💫 2.1 {}初始化 二: 🔥 std::initializer_list 💫 2.1 std::initializer_list是什么类型💫 2.2 s…