Let’s build a simple app that helps users to create SQL queries with natural language.

Preview of the final result

Prerequisites

This example has extra dependencies. You can install them with:

pip install chainlit openai

Imports

app.py
import os
from openai import AsyncOpenAI


from chainlit.playground.providers.openai import ChatOpenAI
import chainlit as cl


client = AsyncOpenAI(api_key="YOUR_OPENAI_API_KEY")

Define a prompt template and LLM settings

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": ["```"],
}

Add the Assistant Logic

Here, we decorate the main function with the @on_message decorator to tell Chainlit to run the main function each time a user sends a message.

Then, we wrap our text to sql logic in a Step.

app.py
@cl.step(type="llm", root=True, language="sql")
async def text_to_sql(text: str):
    # Create a ChatGeneration to enable the prompt playground
    generation = cl.ChatGeneration(
        provider=ChatOpenAI.id,
        messages=[
            cl.GenerationMessage(
                role="user",
                template=template,
                formatted=template.format(input=text),
            )
        ],
        settings=settings,
        inputs={"input": text},
    )

    # Call OpenAI
    stream = await client.chat.completions.create(
        messages=[m.to_openai() for m in generation.messages], stream=True, **settings
    )

    current_step = cl.context.current_step

    async for part in stream:
        if token := part.choices[0].delta.content or "":
            await current_step.stream_token(token)

    generation.completion = current_step.output

    current_step.generation = generation


@cl.on_message
async def main(message: cl.Message):
    await text_to_sql(message.content)

Try it out

chainlit run app.py -w

You can ask questions like Compute the number of customers who watched more than 50 minutes of video this month.