> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainlit.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Text to SQL

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

<Frame caption="Preview of the final result">
  <video controls autoPlay loop muted src="https://mintcdn.com/chainlit-43/bY2N4qUEa_bGPLfY/images/text-to-sql.mp4?fit=max&auto=format&n=bY2N4qUEa_bGPLfY&q=85&s=443b735ff775418ee94ddacc165b0347" data-path="images/text-to-sql.mp4" />
</Frame>

## Prerequisites

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

```bash theme={null}
pip install chainlit openai
```

## Imports

```python app.py theme={null}
from openai import AsyncOpenAI


import chainlit as cl

cl.instrument_openai()

client = AsyncOpenAI(api_key="YOUR_OPENAI_API_KEY")
```

## Define a prompt template and LLM settings

````python app.py theme={null}
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](/api-reference/lifecycle-hooks/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](/concepts/step).

```python app.py theme={null}
@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()
```

## Try it out

```bash theme={null}
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`.
