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

最终结果预览

前提条件

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

pip install chainlit openai

导入

app.py
from openai import AsyncOpenAI


import chainlit as cl

cl.instrument_openai()

client = AsyncOpenAI(api_key="YOUR_OPENAI_API_KEY")

定义提示模板和 LLM 设置

app.py
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 逻辑包装在 步骤 中。

app.py
@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()

试一试

chainlit run app.py -w

您可以提出类似 计算本月观看视频超过 50 分钟的客户数量 的问题。