This application is a Streamlit-based tool that utilizes NLP and Neo4j to generate SQL queries dynamically. It fetches the schema from a Neo4j database, processes user queries using Sentence Transformers, and generates SQL queries via the ArliAI API.
- Fetch Schema from Neo4j: Extracts table and column descriptions along with relationships.
- Sentence Transformer Embeddings: Converts schema and user queries into vector embeddings.
- Similarity-Based Schema Pruning: Identifies relevant tables and columns based on query similarity.
- Relationship-Aware Filtering: Ensures key columns related to relationships are retained.
- SQL Query Generation: Uses ArliAI API to generate SQL queries based on the refined schema.
- Configurable Similarity Threshold: Allows fine-tuning relevance detection for query context.
- Secure Credentials Input: Uses password fields for sensitive information in Streamlit UI.
Ensure you have the following installed:
- Python 3.8+
- Neo4j (Running instance with a valid database schema)
- Virtual environment (optional but recommended)
$ git clone https://github.com/your-repo/sql-query-generator.git
$ cd sql-query-generator$ python -m venv venv
$ source venv/bin/activate # On macOS/Linux
$ venv\Scripts\activate # On Windows$ pip install -r requirements.txt$ streamlit run app.py- Neo4j URI: The connection string for Neo4j (e.g.,
bolt://localhost:7687). - Neo4j User & Password: Authentication credentials.
- Database Name: The Neo4j database containing the schema.
- ArliAI API Key: Required to generate SQL queries.
- Similarity Threshold: Adjusts the sensitivity for schema relevance detection.
- User Query: Natural language input describing the desired SQL query.
- The schema is fetched from Neo4j.
- The schema is converted into vector embeddings.
- The user inputs a query.
- The system identifies relevant schema elements.
- Relationship-aware pruning refines column selection.
- A SQL query is generated via the ArliAI API.
- The final query is displayed in the Streamlit UI.
- Connects to Neo4j.
- Fetches table and column schema.
- Retrieves table relationships.
- Generates vector embeddings for schema and queries.
- Converts the entire schema into vector embeddings.
- Identifies tables relevant to the user query based on embedding similarity.
- Extracts columns related to inter-table relationships.
- Filters out unrelated columns while retaining key relationship attributes.
- Uses the ArliAI API to generate SQL queries based on a refined schema.
User Query: List all users, their orders, and the products in those orders.
SELECT users.id, users.name, orders.id AS order_id, orders.date, products.name AS product_name
FROM users
JOIN orders ON users.id = orders.user_id
JOIN order_products ON orders.id = order_products.order_id
JOIN products ON order_products.product_id = products.id;- Connection Issues: Ensure the Neo4j instance is running and accessible.
- Embedding Errors: Verify that
sentence-transformersis installed and working. - API Failure: Check if the ArliAI API key is valid and the service is online.
- Incorrect SQL Queries: Adjust the similarity threshold or refine column selection.
This project is licensed under the MIT License.