-
-
Save 4l3j4ndr0/9a0b26a21f78d65cdb4fac1221bfcf25 to your computer and use it in GitHub Desktop.
Secure SQL Query Generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def generate_sql_query(self, natural_language_query): | |
| """Generate SQL query from natural language instruction""" | |
| system_prompt = f""" | |
| You are an expert SQL assistant that helps users query a MySQL database. | |
| Your task is to generate a SQL query based on the user's natural language request. | |
| Here is the database schema information: | |
| {self.db_schema} | |
| IMPORTANT RULES: | |
| 1. ONLY generate SELECT queries. DO NOT generate any INSERT, UPDATE, DELETE, or other data modification queries. | |
| 2. Make your queries as efficient as possible. | |
| 3. Use proper table and column names from the schema provided. | |
| 4. DO NOT include any explanations or markdown formatting in your response. | |
| 5. Return ONLY the SQL query as plain text - nothing else. | |
| Generate a SQL query for the following request: | |
| """ | |
| # Generate SQL query using Bedrock model | |
| response = self.bedrock_runtime.converse( | |
| modelId=self.model_id, | |
| messages=[{'role': 'user', 'content': [{'text': natural_language_query}]}], | |
| system=[{'text': system_prompt}], | |
| inferenceConfig={'temperature': 0.1, 'maxTokens': 1000} | |
| ) | |
| # Extract and clean SQL query from response | |
| sql_query = response['output']['message']['content'][0]['text'].strip() | |
| return sql_query |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment