Skip to main content
This tutorial will guide you through creating an application that generates SQL queries from natural language instructions and evaluates the quality of the generated queries. Along the way, you’ll learn how to use Orq’s deployment feature to enhance SQL generation. By the end of this tutorial, you’ll be ready to experiment with SQL generation in your own projects. Before starting, ensure you have an Orq account. If not, sign up at Orq.ai. Let’s dive in! Additionally, to simplify the process, we’ve prepared a Google Colab file that you can copy and run immediately after replacing your API key. This file provides a ready-to-use environment with all the required configurations set up, allowing you to focus on experimenting with SQL generation without worrying about initial setup. Let’s dive in! Step 1: Setting Up the Environment The following commands install the required libraries for working with the Orq platform, handling datasets, and managing the SQL generation workflow. Feel free to reuse and adapt this code for your projects.
Step 2: Identity Tracking (Optional) Identities in Orq.ai help track user interactions and API usage across your application. They can represent users, teams, or projects and enable better analytics and budget management. Create an Identity through the AI Studio:
  1. Go to Identity Analytics in your workspace
  2. Click Create an Identity
  3. Add the user details (name, email, external ID)
  4. Set optional metadata and budget limits
Learn more about creating identities, see Creating an Identity. Step 3: Initializing the Orq Client The Orq client is the core interface for deploying and invoking models. Here’s how to set it up using an API key, which can be stored as an environment variable (ORQ_API_KEY) or passed directly.
Step 4: Loading the Dataset Use the Hugging Face datasets library to load a dataset containing table schemas and natural language instructions. Convert the dataset to a pandas DataFrame for easy manipulation.
SQL Query Generation Use Case This deployment is designed to generate valid SQL queries based on specific table schemas and user-provided instructions. The model analyzes the instruction and the associated table schema to produce a precise and contextually appropriate SQL query. SQL query generation is particularly useful when automating database interactions, building query assistants, or streamlining the process of accessing structured data through natural language inputs.
Step 5: Generating SQL Queries This step involves invoking the Orq deployment to generate SQL queries for each row in the dataset. The instruction column provides the natural language task, while the input column contains the table schema. The results are stored in a new column named output.
Step 6: Saving and Evaluating Results Save the updated DataFrame containing the SQL queries to a file and evaluate their quality. Use metrics or manual inspection to verify the accuracy and relevance of the generated queries.
Feedback Collection (Optional) Feedback in Orq.ai helps track response quality and identify areas for improvement. You can collect user ratings, defect classifications, and corrections to continuously enhance your application. Provide feedback through the AI Studio:
  1. Go to Logs in your workspace
  2. Find the specific deployment invocation
  3. Use the feedback interface to rate responses
  4. Add defect classifications or corrections as needed
Feedback can also be collected programmatically via the Annotations API. Next Steps Congratulations! You’ve successfully built and tested a SQL generation application using Orq. To further enhance your project:
  • Experiment with different datasets or deployment keys.
  • Refine the prompt to improve SQL generation quality.
  • Integrate the solution into a larger application for automated data access.
For more details and advanced features, visit the Orq documentation.