Close Menu
    Main Menu
    • Home
    • News
    • Tech
    • Robotics
    • ML & Research
    • AI
    • Digital Transformation
    • AI Ethics & Regulation
    • Thought Leadership in AI

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Scattered Spider Hackers Goal Tech Firm Assist-Desk Directors

    June 7, 2025

    Resident Evil Requiem Revealed, however The place’s Leon Kennedy?

    June 7, 2025

    What Occurs When You Take away the Filters from AI Love Turbines?

    June 7, 2025
    Facebook X (Twitter) Instagram
    UK Tech Insider
    Facebook X (Twitter) Instagram Pinterest Vimeo
    UK Tech Insider
    Home»Machine Learning & Research»Construct a Textual content-to-SQL resolution for information consistency in generative AI utilizing Amazon Nova
    Machine Learning & Research

    Construct a Textual content-to-SQL resolution for information consistency in generative AI utilizing Amazon Nova

    Oliver ChambersBy Oliver ChambersJune 7, 2025No Comments14 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Construct a Textual content-to-SQL resolution for information consistency in generative AI utilizing Amazon Nova
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    Companies depend on exact, real-time insights to make important choices. Nonetheless, enabling non-technical customers to entry proprietary or organizational information with out technical experience stays a problem. Textual content-to-SQL bridges this hole by producing exact, schema-specific queries that empower quicker decision-making and foster a data-driven tradition. The issue lies in acquiring deterministic solutions—exact, constant outcomes wanted for operations resembling producing precise counts or detailed reviews—from proprietary or organizational information. Generative AI gives a number of approaches to question information, however deciding on the correct methodology is important to realize accuracy and reliability.

    This submit evaluates the important thing choices for querying information utilizing generative AI, discusses their strengths and limitations, and demonstrates why Textual content-to-SQL is the only option for deterministic, schema-specific duties. We present the best way to successfully use Textual content-to-SQL utilizing Amazon Nova, a basis mannequin (FM) out there in Amazon Bedrock, to derive exact and dependable solutions out of your information.

    Choices for querying information

    Organizations have a number of choices for querying information, and the selection depends upon the character of the information and the required outcomes. This part evaluates the next approaches to supply readability on when to make use of every and why Textual content-to-SQL is perfect for deterministic, schema-based duties:

    • Retrieval Augmented Era (RAG):
      • Use case – Very best for extracting insights from unstructured or semi-structured sources like paperwork or articles.
      • Strengths – Handles numerous information codecs and supplies narrative-style responses.
      • Limitations – Probabilistic solutions can differ, making it unsuitable for deterministic queries, resembling retrieving precise counts or matching particular schema constraints.
      • Instance – “Summarize suggestions from product critiques.”
    • Generative enterprise intelligence (BI):
      • Use case – Appropriate for high-level insights and abstract technology based mostly on structured and unstructured information.
      • Strengths – Delivers narrative insights for decision-making and tendencies.
      • Limitations – Lacks the precision required for schema-specific or operational queries. Outcomes typically differ in phrasing and focus.
      • Instance – “What had been the important thing drivers of gross sales development final quarter?”
    • Textual content-to-SQL:
      • Use case – Excels in querying structured organizational information immediately from relational schemas.
      • Strengths – Offers deterministic, reproducible outcomes for particular, schema-dependent queries. Very best for exact operations resembling filtering, counting, or aggregating information.
      • Limitations – Requires structured information and predefined schemas.
      • Instance – “What number of sufferers recognized with diabetes visited clinics in New York Metropolis final month?”

    In situations demanding precision and consistency, Textual content-to-SQL outshines RAG and generative BI by delivering correct, schema-driven outcomes. These traits make it the best resolution for operational and structured information queries.

    Answer overview

    This resolution makes use of the Amazon Nova Lite and Amazon Nova Professional massive language fashions (LLMs) to simplify querying proprietary information with pure language, making it accessible to non-technical customers.

    Amazon Bedrock is a completely managed service that simplifies constructing and scaling generative AI purposes by offering entry to main FMs by means of a single API. It permits builders to experiment with and customise these fashions securely and privately, integrating generative AI capabilities into their purposes with out managing infrastructure.

    Inside this method, Amazon Nova represents a brand new technology of FMs delivering superior intelligence and industry-leading price-performance. These fashions, together with Amazon Nova Lite and Amazon Nova Professional, are designed to deal with varied duties resembling textual content, picture, and video understanding, making them versatile instruments for numerous purposes.

    You’ll find the deployment code and detailed directions in our GitHub repo.

    The answer consists of the next key options:

    • Dynamic schema context – Retrieves the database schema dynamically for exact question technology
    • SQL question technology – Converts pure language into SQL queries utilizing the Amazon Nova Professional LLM
    • Question execution – Runs queries on organizational databases and retrieves outcomes
    • Formatted responses – Processes uncooked question outcomes into user-friendly codecs utilizing the Amazon Nova Lite LLM

    The next diagram illustrates the answer structure.

    On this resolution, we use Amazon Nova Professional and Amazon Nova Lite to make the most of their respective strengths, facilitating environment friendly and efficient processing at every stage:

    • Dynamic schema retrieval and SQL question technology – We use Amazon Nova Professional to deal with the interpretation of pure language inputs into SQL queries. Its superior capabilities in complicated reasoning and understanding make it well-suited for precisely decoding person intents and producing exact SQL statements.
    • Formatted response technology – After we run the SQL queries, the uncooked outcomes are processed utilizing Amazon Nova Lite. This mannequin effectively codecs the information into user-friendly outputs, making the data accessible to non-technical customers. Its velocity and cost-effectiveness are advantageous for this stage, the place fast processing and simple presentation are key.

    By strategically deploying Amazon Nova Professional and Amazon Nova Lite on this method, the answer makes positive that every part operates optimally, balancing efficiency, accuracy, and cost-effectiveness.

    Stipulations

    Full the next prerequisite steps:

    1. Set up the AWS Command Line Interface (AWS CLI). For directions, check with Putting in or updating to the newest model of the AWS CLI.
    2. Configure the essential settings that the AWS CLI makes use of to work together with AWS. For extra data, see Configuration and credential file settings within the AWS CLI.
    3. Be certain that Amazon Bedrock is enabled in your AWS account.
    4. Acquire entry to Amazon Nova Lite and Amazon Nova Professional.
    5. Set up Python 3.9 or later, together with required libraries (Streamlit model 1.8.0 or later, Boto3, pymssql, and setting administration packages).
    6. Create a Microsoft SQL Server (model 2016 or later) database with credentials to attach.
      1. Create a secret in AWS Secrets and techniques Supervisor for database credentials and title it mssql_secrets. For directions, see Create an AWS Secrets and techniques Supervisor secret.

    Our pattern code makes use of a Microsoft SQL Server database, however this resolution helps the next companies:

    For extra details about stipulations, check with the GitHub repo.

    Arrange the event setting

    Within the command immediate, navigate to the folder the place the code exists and run the next command:

    python3.9 -m pip set up -r necessities.txt --upgrade

    This command installs the required libraries to run the appliance.

    Load the pattern dataset within the database

    Ensure you have created a secret in Secrets and techniques Supervisor named mssql_secrets as talked about within the stipulations. When you named your secret one thing else, replace the code in app.py (line 29) and load_data.py (line 22).

    After you create the key, run the next command from the code folder:

    This command creates a database named Gross sales with tables Merchandise, Clients, and Orders and hundreds the pattern information in these tables.

    Run the appliance

    To run the appliance, execute the next command:

    Instance queries

    On this part, we discover some pattern queries.

    For our first question, we ask “Who’re the purchasers who purchased smartphones?” This generates the next SQL:

    SELECT DISTINCT CustomerName, ProductName, SUM(Amount) AS TotalSoldQuantity FROM vw_sales WHERE ProductName LIKE '%smartphone%' GROUP BY CustomerName, ProductName, OrderDate; 

    We get the next formatted response:

    1. Alice Johnson, who purchased 1 smartphone on October 14th, 2023.
    2. Ivy Martinez, who purchased 2 smartphones on October fifteenth, 2023.

    Subsequent, we ask “What number of smartphones are in inventory?” This generates the next SQL:

    SELECT DISTINCT ProductName, StockQuantity AS AvailableQuantity FROM vw_sales WHERE ProductName LIKE '%smartphone%'; 

    We get the response “There are 100 smartphones at present in inventory.”

    Sales-bot web interface demonstrating natural language to SQL conversion with Amazon Bedrock, showing smartphone purchase query and results

    Code execution stream

    On this part, we discover the code execution stream. The code reference is from the GitHub repo. Don’t run the completely different components of the code individually.

    Retrieve schema dynamically

    Use INFORMATION_SCHEMA views to extract schema particulars dynamically (code reference from app.py):

    def get_schema_context(db_name, db_view_name):
        conn = connect_to_db()
        cursor = conn.cursor()
        cursor.execute(f"USE {db_name}")
        question = f"SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{db_view_name}'"
        cursor.execute(question)
        schema = cursor.fetchall()
        print("Schema:", schema)
        return 'n'.be part of([f"- {row[0]}: {row[1]}" for row in schema])

    Dynamic schema retrieval adapts robotically to adjustments by querying metadata tables for up to date schema particulars, resembling desk names and column varieties. This facilitates seamless integration of schema updates into the Textual content-to-SQL system, decreasing guide effort and enhancing scalability.

    Take a look at this perform to confirm it adapts robotically when schema adjustments happen.

    Earlier than producing SQL, fetch schema particulars for the related tables to facilitate correct question building.

    Generate a SQL question utilizing Amazon Nova Professional

    Ship the person question and schema context to Amazon Nova Professional (code reference from sql_generator.py):

    def generate_sql_query(query: str, schema_context: str, db_name: str, db_view_name: str = None) -> str:
        
        nova_client = NovaClient()
           
        # Base immediate with SQL technology guidelines
        base_prompt = """
        MS SQL DB {db_name} has one view names '{db_view_name}'. 
        At all times use '{db_view_name}' as desk title to generate your question.
        Create a MS SQL question by rigorously understanding the query and generate the question between tags  and .
        The MS SQL question ought to selects all columns from a view named '{db_view_name}'
        In your SQL question at all times use like situation in the place clasue.
        if a query is requested about product inventory then at all times use 'distinct' in your SQL question.
        By no means Generate an SQL question which provides error upon execution.
          
        
        Query: {query}
        
        Database Schema : {schema_context}
        
        Generate SQL question:
        """
        
        # Format the immediate with the query and schema context
        formatted_prompt = base_prompt.format(
            query=query,
            db_name=db_name,
            db_view_name=db_view_name if db_view_name else "No view title offered",
            schema_context=schema_context if schema_context else "No further context offered"
        )
            
        # Invoke Nova mannequin
        response = nova_client.invoke_model(
            model_id='amazon.nova-pro-v1:0',
            immediate=formatted_prompt,
            temperature=0.1  # Decrease temperature for extra deterministic SQL technology
        )
        
        # Extract SQL question from response utilizing regex
        sql_match = extract_sql_from_nova_response(response)
        if sql_match:
            return sql_match
        else:
            increase ValueError("No SQL question discovered within the response")
        
    def extract_sql_from_nova_response(response):
        strive:
            # Navigate the nested dictionary construction
            content material = response['output']['message']['content']
            # Get the textual content from the primary content material merchandise
            textual content = content material[0]['text']
            
            # Discover the positions of start and finish tags
            begin_tag = ""
            end_tag = ""
            start_pos = textual content.discover(begin_tag)
            end_pos = textual content.discover(end_tag)
            
            # If each tags are discovered, extract the SQL between them
            if start_pos != -1 and end_pos != -1:
                # Add size of start tag to begin place to skip the tag itself
                sql_query = textual content[start_pos + len(begin_tag):end_pos].strip()
                return sql_query
                
            return None
            
        besides (KeyError, IndexError):
            # Return None if the anticipated construction will not be discovered
            return None

    This code establishes a structured context for a text-to-SQL use case, guiding Amazon Nova Professional to generate SQL queries based mostly on a predefined database schema. It supplies consistency by defining a static database context that clarifies desk names, columns, and relationships, serving to forestall ambiguity in question formation. Queries are required to reference the vw_sales view, standardizing information extraction for analytics and reporting. Moreover, every time relevant, the generated queries should embody quantity-related fields, ensuring that enterprise customers obtain key insights on product gross sales, inventory ranges, or transactional counts. To boost search flexibility, the LLM is instructed to make use of the LIKE operator in WHERE circumstances as a substitute of actual matches, permitting for partial matches and accommodating variations in person enter. By imposing these constraints, the code optimizes Textual content-to-SQL interactions, offering structured, related, and business-aligned question technology for gross sales information evaluation.

    Execute a SQL question

    Run the SQL question on the database and seize the outcome (code reference from app.py):

    cursor.execute(sql_command)
    outcome = cursor.fetchall()
    print(outcome)

    Format the question outcomes utilizing Amazon Nova Lite

    Ship the database outcome from the SQL question to Amazon Nova Lite to format it in a human-readable format and print it on the Streamlit UI (code reference from app.py):

    def interact_with_nova(user_input, llm_query, query_response, mannequin="nova"):
        session = boto3.session.Session()
        area = session.region_name
        
        nova_client = NovaClient(region_name=area)
        
        final_prompt = f"""Human: You're a skilled chatbot who's completely satisfied to help the customers. Person questions in given in  tag and ends in  tag. Perceive the query and use data from  to generate a solution. If there are a couple of entery, give a numbered record. By no means retrun  and  in your response.
        for instance : query - "What number of mouse had been offered?"
                      llm response : 
                                    " There have been 3 mouse offered in complete. 
                                    - 1 mouse offered to Mia Perez on October 2nd, 2023. 
                                    - 2 mouse offered to Jack Hernandez on October 1st 2023."
        
        {user_input}
        
        
        {query_response}
        """
        
        strive:
            
                response = nova_client.invoke_model(
                    model_id='amazon.nova-lite-v1:0',
                    immediate=final_prompt,
                    max_tokens=4096,
                    temperature=0.7
                )
                
                content material = response['output']['message']['content']
                textual content = content material[0]['text']
                return textual content
                
                return "Sorry, I could not course of your request."
        
        besides Exception as e:
            print(f"Error in LLM interplay: {str(e)}")
            return "Sorry, an error occurred whereas processing your request."

    Clear up

    Comply with these steps to wash up sources in your AWS setting and keep away from incurring future prices:

    1. Clear up database sources:
    2. Clear up safety sources:
    3. Clear up the frontend (provided that internet hosting the Streamlit utility on Amazon EC2):
      • Cease the EC2 occasion internet hosting the Streamlit utility.
      • Delete related storage volumes.
    4. Clear up further sources (if relevant):
      • Take away Elastic Load Balancers.
      • Delete digital non-public cloud (VPC) configurations.
    5. Test the AWS Administration Console to substantiate all sources have been deleted.

    Conclusion

    Textual content-to-SQL with Amazon Bedrock and Amazon Nova LLMs supplies a scalable resolution for deterministic, schema-based querying. By delivering constant and exact outcomes, it empowers organizations to make knowledgeable choices, enhance operational effectivity, and scale back reliance on technical sources.

    For a extra complete instance of a Textual content-to-SQL resolution constructed on Amazon Bedrock, discover the GitHub repo Setup Amazon Bedrock Agent for Textual content-to-SQL Utilizing Amazon Athena with Streamlit. This open supply venture demonstrates the best way to use Amazon Bedrock and Amazon Nova LLMs to construct a sturdy Textual content-to-SQL agent that may generate complicated queries, self-correct, and question numerous information sources.

    Begin experimenting with Textual content-to-SQL use circumstances in the present day by getting began with Amazon Bedrock.


    In regards to the authors

    Mansi Sharma is a Options Architect for Amazon Net Providers. Mansi is a trusted technical advisor serving to enterprise prospects architect and implement cloud options at scale. She drives buyer success by means of technical management, architectural steerage, and modern problem-solving whereas working with cutting-edge cloud applied sciences. Mansi makes a speciality of generative AI utility improvement and serverless applied sciences.

    Marie Yap is a Principal Options Architect for Amazon Net Providers.  On this position, she helps varied organizations start their journey to the cloud. She additionally makes a speciality of analytics and fashionable information architectures.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Oliver Chambers
    • Website

    Related Posts

    Multi-account assist for Amazon SageMaker HyperPod activity governance

    June 7, 2025

    Implement semantic video search utilizing open supply giant imaginative and prescient fashions on Amazon SageMaker and Amazon OpenSearch Serverless

    June 6, 2025

    Construct a serverless audio summarization answer with Amazon Bedrock and Whisper

    June 6, 2025
    Leave A Reply Cancel Reply

    Top Posts

    Scattered Spider Hackers Goal Tech Firm Assist-Desk Directors

    June 7, 2025

    How AI is Redrawing the World’s Electrical energy Maps: Insights from the IEA Report

    April 18, 2025

    Evaluating the Finest AI Video Mills for Social Media

    April 18, 2025

    Utilizing AI To Repair The Innovation Drawback: The Three Step Resolution

    April 18, 2025
    Don't Miss

    Scattered Spider Hackers Goal Tech Firm Assist-Desk Directors

    By Declan MurphyJune 7, 2025

    A newly recognized wave of cyberattacks by the infamous Scattered Spider hacking group has zeroed…

    Resident Evil Requiem Revealed, however The place’s Leon Kennedy?

    June 7, 2025

    What Occurs When You Take away the Filters from AI Love Turbines?

    June 7, 2025

    Microsoft launches European Safety Program to counter nation-state threats

    June 7, 2025
    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo

    Subscribe to Updates

    Get the latest creative news from SmartMag about art & design.

    UK Tech Insider
    Facebook X (Twitter) Instagram Pinterest
    • About Us
    • Contact Us
    • Privacy Policy
    • Terms Of Service
    • Our Authors
    © 2025 UK Tech Insider. All rights reserved by UK Tech Insider.

    Type above and press Enter to search. Press Esc to cancel.