Empowering Efficiency: Transforming Text into SQL with Lyzr SDK
Tags: ai, promptengineering, sql, nlp
Published: 2024-03-19 13:21:32

Experience the future of streamlined data analysis today with FormulaGen, a groundbreaking module within Lyzr SDK. Powered by OpenAI’s Large Language Model (LLM), FormulaGen simplifies complex interactions with patterns, regular expressions, SQL queries, and spreadsheet formulas.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ygygp85hrylv6joubl1r.png)

With FormulaGen’s text_to_sql feature, effortlessly translate natural language text queries into SQL queries with detailed explanations, empowering informed decision-making and enhancing efficiency in data analysis.

**Why use Lyzr SDK’s?**

With Lyzr SDKs, crafting your own GenAI application is a breeze, requiring only a few lines of code to get up and running swiftly.

[Checkout Lyzr SDK's](https://docs.lyzr.ai/homepage)

Experience the intuitive process of creating cutting-edge GenAI applications with Lyzr. Give it a try now and discover the possibilities for yourself!

Let’s Build Together — [Discord](https://discord.com/invite/nm7zSyEFA2)

**Lets get Started!**

Create a new file Hello.py and use that

**Install and import the dependencies**

```
!pip install lyzr
from lyzr import FormulaGen
from google.colab import userdata
import os
```
In this snippet, the os and PIL libraries are imported for file and image handling respectively. Additionally, streamlit is imported for creating interactive web applications. The lyzr library is imported to utilize its functionalities, particularly the FormulaGen module, which likely assists in simplifying complex data analysis tasks, given its name.

To proceed, integrate the OpenAI API key into your Streamlit application using Streamlit’s secrets management functionality. In your script, ensure you replace the placeholder “OPENAI_API_KEY” with the designated key name as defined within Streamlit secrets, where your actual OpenAI API key is stored. This step secures the API key within your application, ensuring its confidentiality and proper usage.

```
# Set OpenAI API key
os.environ["OPENAI_API_KEY"] = st.secrets["apikey"]
```
This step secures the API key within your application, ensuring its confidentiality and proper usage.

```
# Define the Streamlit app layout
def app():

    # Text input for user to input the text
    text_input = st.text_area("Enter your texts:")

    # Button to trigger the conversion
    if st.button("Convert to SQL"):
        # Text to SQL conversion
        result = generate.text_to_sql(text_input)

        # Display the result
        st.subheader("Generated SQL Query:")
        st.code(result, language="sql")
```
This Streamlit application defines a simple layout for user interaction. It includes a text area where users can input text data, and a button labeled “Convert to SQL” to trigger the conversion process. When the button is clicked, the generate.text_to_sql function is called to convert the input text into SQL query format.

After conversion, the resulting SQL query is displayed below a subheader labeled “Generated SQL Query” using the st.subheader function. The SQL query is presented in a code block format using the st.code function, with the language parameter set to "sql" to ensure proper syntax highlighting for SQL code. Overall, this code snippet demonstrates the basic structure of a Streamlit app for converting text to SQL queries and displaying the generated SQL output to the user.

```
# Run the app
if __name__ == "__main__":
    app()
```
This conditional statement ensures that the app() function, responsible for running the Streamlit application, is executed only when the Python script is directly run as the main program. This prevents the app from running if the script is imported as a module into another program, maintaining modularity and preventing unintended execution.

These code snippets exemplify the power of Streamlit and the Lyzr library in simplifying complex tasks such as converting natural language text into SQL queries. By combining intuitive user interfaces with advanced backend functionality, developers can create powerful tools that enhance productivity and streamline data analysis workflows.

With continued innovation and integration of cutting-edge technologies, the possibilities for creating impactful applications are limitless. Embrace the future of streamlined data analysis and empower your projects with the tools showcased here.

Try out the Application: [https://querygenius-lyzr.streamlit.app/
](https://querygenius-lyzr.streamlit.app/
)
For Source Code : [https://github.com/isakshay007/Text2SQL](https://github.com/isakshay007/Text2SQL)

**References**

Lyzr Website: [Lyzr
](https://www.lyzr.ai/)

Book a Demo: [Demo](https://www.lyzr.ai/book-demo/)

Lyzr Community Channels: [Discord](https://discord.com/invite/nm7zSyEFA2)

Slack : [Slack](https://anybodycanai.slack.com/join/shared_invite/zt-2a7fr38f7-_QDOY1W1WSlSiYNAEncLGw#/shared-invite/email)