rails-nl2sql 0.2.6 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.DS_Store +0 -0
- data/CHANGELOG.md +12 -1
- data/PLAN.md +30 -0
- data/README.md +72 -0
- data/bin/rails-nl2sql +5 -0
- data/lib/rails/nl2sql/active_record_extension.rb +2 -0
- data/lib/rails/nl2sql/cli.rb +23 -0
- data/lib/rails/nl2sql/prompt_builder.rb +29 -0
- data/lib/rails/nl2sql/providers/gemini_provider.rb +48 -0
- data/lib/rails/nl2sql/query_generator.rb +44 -26
- data/lib/rails/nl2sql/schema_builder.rb +1 -1
- data/lib/rails/nl2sql/version.rb +1 -1
- data/lib/rails/nl2sql.rb +16 -13
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef91bcc7db96e148113aa100b915ef59c82d680da31658ebb57ce394b9a48b8d
|
4
|
+
data.tar.gz: 0f8a22ed3c09624dd7464c8de62a3fb98c8aee8fdf5cb6c3fe47b122d5e0bac0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83b4a297e39e5caec803253adb5851d55b0b817216acecbd5d79409c4de21705e3e22908ab0224dac504573450c78847477391a0b7c7ccd9c656fa679b6cb8b3
|
7
|
+
data.tar.gz: 0bce6a48bbcf4ce79764f967ab6cd2b1be63af2dc4ae3be3b5184cd194e4b3e7bf85b8e355169df8e3689cef2739ee883c349acce0fbb452379c98fb8f84576f
|
data/.DS_Store
CHANGED
Binary file
|
data/CHANGELOG.md
CHANGED
@@ -7,7 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## [Unreleased]
|
9
9
|
|
10
|
-
|
10
|
+
## [0.2.8] - 2025-07-21
|
11
|
+
- Adding support for Google's Gemini LLM provider.
|
12
|
+
- Adding a command-line interface (CLI).
|
13
|
+
- Improving the documentation.
|
14
|
+
- Adding a debug mode.
|
15
|
+
- Adding support for the Oracle database adapter.
|
16
|
+
- Refactoring the code to make it more modular and easier to understand.
|
17
|
+
- Adding more comments to the code.
|
18
|
+
|
19
|
+
## [0.2.7] - 2025-07-18
|
20
|
+
### Fixed
|
21
|
+
- Fix from_nl trailing semicolon
|
11
22
|
|
12
23
|
## [0.2.5] - 2025-07-18
|
13
24
|
### Fixed
|
data/PLAN.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
# Plan for Improving rails-nl2sql Gem
|
3
|
+
|
4
|
+
This document outlines a plan to improve the `rails-nl2sql` gem. The focus is on improving the developer experience, expanding the gem's capabilities, and making it more robust.
|
5
|
+
|
6
|
+
## 1. Improve Developer Experience
|
7
|
+
|
8
|
+
* **Add a command-line interface (CLI):** A CLI would make it easier for developers to use the gem without having to write code. The CLI could be used to generate queries, get schema information, and more.
|
9
|
+
* **Improve the documentation:** The documentation should be more comprehensive and include more examples. It should also be easier to navigate.
|
10
|
+
* **Add a debug mode:** A debug mode would make it easier for developers to troubleshoot problems with the gem. The debug mode could log information about the queries that are being generated, the schema that is being used, and more.
|
11
|
+
|
12
|
+
## 2. Expand Capabilities
|
13
|
+
|
14
|
+
* **Add support for more database adapters:** The gem currently only supports PostgreSQL, MySQL, and SQLite. It would be beneficial to add support for other popular database adapters, such as Oracle and SQL Server.
|
15
|
+
* **Add support for more LLM providers:** The gem currently only supports OpenAI, Anthropic and Llama. It would be beneficial to add support for other popular LLM providers, such as Google's Gemini.
|
16
|
+
* **Add support for more complex queries:** The gem currently only supports simple SELECT queries. It would be beneficial to add support for more complex queries, such as queries with joins, subqueries, and window functions.
|
17
|
+
|
18
|
+
## 3. Make the Gem More Robust
|
19
|
+
|
20
|
+
* **Add more tests:** The gem currently has a good test suite, but it could be improved. More tests should be added to cover all of the gem's functionality.
|
21
|
+
* **Add more error handling:** The gem currently has some error handling, but it could be improved. More error handling should be added to handle all possible errors that could occur.
|
22
|
+
* **Add a performance benchmark:** A performance benchmark would help to ensure that the gem is performing well. The benchmark could be used to track the performance of the gem over time and to identify any performance bottlenecks.
|
23
|
+
|
24
|
+
## 4. Improve the Codebase
|
25
|
+
|
26
|
+
* **Refactor the code:** The code could be refactored to make it more modular and easier to understand.
|
27
|
+
* **Add more comments:** The code could be better commented to make it easier for other developers to understand.
|
28
|
+
* **Use a consistent coding style:** The code should be formatted using a consistent coding style.
|
29
|
+
|
30
|
+
This is just a high-level plan. More detailed planning will be required for each of these items.
|
data/README.md
CHANGED
@@ -52,13 +52,24 @@ returns an `ActiveRecord::Relation` so you can chain scopes, pagination and
|
|
52
52
|
other query modifiers as usual.
|
53
53
|
|
54
54
|
```ruby
|
55
|
+
# Get all users who signed up last week and limit the results to 10.
|
55
56
|
User.from_nl("all users who signed up last week").limit(10)
|
57
|
+
|
58
|
+
# Get all users from California and order them by their name.
|
59
|
+
User.from_nl("all users from california").order(:name)
|
60
|
+
|
61
|
+
# Get all users who have an order with a total greater than 100.
|
62
|
+
User.from_nl("all users who have an order with a total greater than 100")
|
56
63
|
```
|
57
64
|
|
58
65
|
You can also specify which tables to include or exclude:
|
59
66
|
|
60
67
|
```ruby
|
68
|
+
# Get all the orders for the user with email 'test@example.com', but only include the `users` and `orders` tables.
|
61
69
|
results = Rails::Nl2sql::Processor.execute("Show me all the orders for the user with email 'test@example.com'", include: ["users", "orders"])
|
70
|
+
|
71
|
+
# Get all the orders for the user with email 'test@example.com', but exclude the `payments` table.
|
72
|
+
results = Rails::Nl2sql::Processor.execute("Show me all the orders for the user with email 'test@example.com'", exclude: ["payments"])
|
62
73
|
```
|
63
74
|
|
64
75
|
### Getting a list of tables
|
@@ -96,6 +107,26 @@ Rails::Nl2sql.configure do |config|
|
|
96
107
|
end
|
97
108
|
```
|
98
109
|
|
110
|
+
### Llama
|
111
|
+
|
112
|
+
You can also use a local Llama-based HTTP endpoint. You'll need to have a local Llama server running.
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
Rails::Nl2sql.configure do |config|
|
116
|
+
config.provider = Rails::Nl2sql::Providers::LlamaProvider.new(endpoint: "http://localhost:8080/completion")
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
### Google Gemini
|
121
|
+
|
122
|
+
You can also use Google's Gemini models. You'll need to have the `google-apis-aiplatform_v1` gem installed and be authenticated with Google Cloud.
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
Rails::Nl2sql.configure do |config|
|
126
|
+
config.provider = Rails::Nl2sql::Providers::GeminiProvider.new(api_key: ENV['GOOGLE_API_KEY'])
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
99
130
|
## Prompt Templates
|
100
131
|
|
101
132
|
The prompts used to talk to the LLM are defined in a YAML/ERB template. You can override this template
|
@@ -115,12 +146,53 @@ Set the path via `config.prompt_template_path`.
|
|
115
146
|
Large schemas can exceed the model context window. Use `config.max_schema_lines` to automatically truncate
|
116
147
|
the schema snippet sent to the model. Only the first N lines are included.
|
117
148
|
|
149
|
+
## Debug Mode
|
150
|
+
|
151
|
+
This gem provides a debug mode for troubleshooting problems. When debug mode is enabled, the gem will log information about the queries that are being generated, the schema that is being used, and more.
|
152
|
+
|
153
|
+
To enable debug mode, set the `debug` option to `true` in your initializer file:
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
# config/initializers/rails_nl2sql.rb
|
157
|
+
Rails::Nl2sql.configure do |config|
|
158
|
+
config.debug = true
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
118
162
|
## Development
|
119
163
|
|
120
164
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
121
165
|
|
122
166
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
123
167
|
|
168
|
+
## Command-Line Interface (CLI)
|
169
|
+
|
170
|
+
This gem provides a command-line interface (CLI) for interacting with the NL2SQL processor.
|
171
|
+
|
172
|
+
### `query`
|
173
|
+
|
174
|
+
Converts a natural language query to SQL.
|
175
|
+
|
176
|
+
```bash
|
177
|
+
$ rails-nl2sql query "Show me all the users from California"
|
178
|
+
```
|
179
|
+
|
180
|
+
### `schema`
|
181
|
+
|
182
|
+
Displays the database schema.
|
183
|
+
|
184
|
+
```bash
|
185
|
+
$ rails-nl2sql schema
|
186
|
+
```
|
187
|
+
|
188
|
+
### `tables`
|
189
|
+
|
190
|
+
Displays the database tables.
|
191
|
+
|
192
|
+
```bash
|
193
|
+
$ rails-nl2sql tables
|
194
|
+
```
|
195
|
+
|
124
196
|
## Contributing
|
125
197
|
|
126
198
|
Bug reports and pull requests are welcome on GitHub at https://github.com/vancuren/rails-nl2sql.
|
data/bin/rails-nl2sql
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'rails/nl2sql'
|
3
|
+
|
4
|
+
module Rails
|
5
|
+
module Nl2sql
|
6
|
+
class CLI < Thor
|
7
|
+
desc "query [NATURAL_LANGUAGE_QUERY]", "Converts a natural language query to SQL"
|
8
|
+
def query(natural_language_query)
|
9
|
+
puts Rails::Nl2sql::Processor.generate_query_only(natural_language_query)
|
10
|
+
end
|
11
|
+
|
12
|
+
desc "schema", "Displays the database schema"
|
13
|
+
def schema
|
14
|
+
puts Rails::Nl2sql::Processor.get_schema
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "tables", "Displays the database tables"
|
18
|
+
def tables
|
19
|
+
puts Rails::Nl2sql::Processor.get_tables
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Nl2sql
|
5
|
+
class PromptBuilder
|
6
|
+
def self.build(input, db_server, retrieved_context)
|
7
|
+
template = Rails::Nl2sql.prompt_template
|
8
|
+
|
9
|
+
erb_context = Object.new
|
10
|
+
erb_context.instance_variable_set(:@input, input)
|
11
|
+
erb_context.instance_variable_set(:@db_server, db_server)
|
12
|
+
erb_context.instance_variable_set(:@retrieved_context, retrieved_context)
|
13
|
+
|
14
|
+
erb_context.define_singleton_method(:get_binding) do
|
15
|
+
binding
|
16
|
+
end
|
17
|
+
|
18
|
+
erb_context.define_singleton_method(:input) { @input }
|
19
|
+
erb_context.define_singleton_method(:db_server) { @db_server }
|
20
|
+
erb_context.define_singleton_method(:retrieved_context) { @retrieved_context }
|
21
|
+
|
22
|
+
system_prompt = ERB.new(template['system']).result(erb_context.get_binding)
|
23
|
+
user_prompt = ERB.new(template['user']).result(erb_context.get_binding)
|
24
|
+
|
25
|
+
"#{system_prompt}\n\n#{user_prompt}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'google/apis/aiplatform_v1'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Nl2sql
|
5
|
+
module Providers
|
6
|
+
class GeminiProvider < Base
|
7
|
+
def initialize(api_key:, model: 'gemini-pro')
|
8
|
+
# Authenticate with Google Cloud
|
9
|
+
# You can use a service account or your user credentials.
|
10
|
+
# For more information, see: https://cloud.google.com/docs/authentication
|
11
|
+
Google::Auth.new.apply(scope: 'https://www.googleapis.com/auth/cloud-platform')
|
12
|
+
|
13
|
+
@client = Google::Apis::AiplatformV1::AIPlatformService.new
|
14
|
+
@model = model
|
15
|
+
end
|
16
|
+
|
17
|
+
def complete(prompt:, **params)
|
18
|
+
# The Gemini API uses a different request format than the other providers.
|
19
|
+
# We need to convert the prompt into a format that the Gemini API can understand.
|
20
|
+
request = Google::Apis::AiplatformV1::GoogleCloudAiplatformV1PredictRequest.new(
|
21
|
+
instances: [
|
22
|
+
{
|
23
|
+
prompt: prompt
|
24
|
+
}
|
25
|
+
],
|
26
|
+
parameters: {
|
27
|
+
temperature: params[:temperature] || 0.2,
|
28
|
+
maxOutputTokens: params[:max_tokens] || 256,
|
29
|
+
topP: params[:top_p] || 0.95,
|
30
|
+
topK: params[:top_k] || 40
|
31
|
+
}
|
32
|
+
)
|
33
|
+
|
34
|
+
# The endpoint for the Gemini API is different for each region.
|
35
|
+
# We need to get the endpoint for the region that the user is in.
|
36
|
+
# For more information, see: https://cloud.google.com/vertex-ai/docs/generative-ai/learn/models
|
37
|
+
endpoint = 'us-central1-aiplatform.googleapis.com' # Or another regional endpoint
|
38
|
+
|
39
|
+
response = @client.predict_project_location_publisher_model(endpoint, request)
|
40
|
+
|
41
|
+
# The Gemini API returns a different response format than the other providers.
|
42
|
+
# We need to convert the response into a format that the other providers can understand.
|
43
|
+
response.predictions.first['content']
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -1,20 +1,32 @@
|
|
1
1
|
require "erb"
|
2
2
|
require "yaml"
|
3
|
+
require "rails/nl2sql/prompt_builder"
|
4
|
+
|
3
5
|
module Rails
|
4
6
|
module Nl2sql
|
5
7
|
class QueryGenerator
|
6
8
|
DEFAULT_MODEL = 'gpt-3.5-turbo-instruct'
|
7
9
|
|
10
|
+
# Initializes a new QueryGenerator.
|
11
|
+
#
|
12
|
+
# @param provider [Object] The AI provider to use for generating queries.
|
13
|
+
# @param model [String] The name of the AI model to use.
|
8
14
|
def initialize(provider: nil, model: DEFAULT_MODEL)
|
9
15
|
@provider = provider || Rails::Nl2sql.provider || default_provider(model)
|
10
16
|
@model = model
|
11
17
|
end
|
12
18
|
|
19
|
+
# Generates a SQL query from a natural language prompt.
|
20
|
+
#
|
21
|
+
# @param prompt [String] The natural language prompt.
|
22
|
+
# @param schema [String] The database schema.
|
23
|
+
# @param db_server [String] The type of database server.
|
24
|
+
# @param tables [Array<String>] The tables to include in the schema.
|
25
|
+
# @return [String] The generated SQL query.
|
13
26
|
def generate_query(prompt, schema, db_server = 'PostgreSQL', tables = nil)
|
14
27
|
retrieved_context = build_context(schema, tables)
|
15
28
|
|
16
|
-
|
17
|
-
full_prompt = "#{system_prompt}\n\n#{user_prompt}"
|
29
|
+
full_prompt = PromptBuilder.build(prompt, db_server, retrieved_context)
|
18
30
|
|
19
31
|
response = @provider.complete(prompt: full_prompt, max_tokens: 500, temperature: 0.1)
|
20
32
|
generated_query = extract_text(response)
|
@@ -27,10 +39,18 @@ module Rails
|
|
27
39
|
|
28
40
|
private
|
29
41
|
|
42
|
+
# Returns the default AI provider.
|
43
|
+
#
|
44
|
+
# @param model [String] The name of the AI model to use.
|
45
|
+
# @return [Object] The default AI provider.
|
30
46
|
def default_provider(model)
|
31
47
|
Providers::OpenaiProvider.new(api_key: Rails::Nl2sql.api_key, model: model)
|
32
48
|
end
|
33
49
|
|
50
|
+
# Extracts the text from the AI provider's response.
|
51
|
+
#
|
52
|
+
# @param response [Object] The response from the AI provider.
|
53
|
+
# @return [String] The extracted text.
|
34
54
|
def extract_text(response)
|
35
55
|
if response.is_a?(Hash)
|
36
56
|
response.dig('choices', 0, 'text')&.strip
|
@@ -39,6 +59,11 @@ module Rails
|
|
39
59
|
end
|
40
60
|
end
|
41
61
|
|
62
|
+
# Builds the context for the AI prompt.
|
63
|
+
#
|
64
|
+
# @param schema [String] The database schema.
|
65
|
+
# @param tables [Array<String>] The tables to include in the schema.
|
66
|
+
# @return [String] The context for the AI prompt.
|
42
67
|
def build_context(schema, tables)
|
43
68
|
context = if tables&.any?
|
44
69
|
filter_schema_by_tables(schema, tables)
|
@@ -48,6 +73,10 @@ module Rails
|
|
48
73
|
apply_context_window(context)
|
49
74
|
end
|
50
75
|
|
76
|
+
# Applies the context window to the context.
|
77
|
+
#
|
78
|
+
# @param context [String] The context for the AI prompt.
|
79
|
+
# @return [String] The context with the context window applied.
|
51
80
|
def apply_context_window(context)
|
52
81
|
max_lines = Rails::Nl2sql.max_schema_lines
|
53
82
|
return context unless max_lines
|
@@ -58,6 +87,11 @@ module Rails
|
|
58
87
|
lines.first(max_lines).join("\n")
|
59
88
|
end
|
60
89
|
|
90
|
+
# Filters the schema by the given tables.
|
91
|
+
#
|
92
|
+
# @param schema [String] The database schema.
|
93
|
+
# @param tables [Array<String>] The tables to include in the schema.
|
94
|
+
# @return [String] The filtered schema.
|
61
95
|
def filter_schema_by_tables(schema, tables)
|
62
96
|
lines = schema.split("\n")
|
63
97
|
filtered_lines = []
|
@@ -76,36 +110,17 @@ module Rails
|
|
76
110
|
filtered_lines.join("\n")
|
77
111
|
end
|
78
112
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
erb_context = Object.new
|
84
|
-
erb_context.instance_variable_set(:@input, input)
|
85
|
-
erb_context.instance_variable_set(:@db_server, db_server)
|
86
|
-
erb_context.instance_variable_set(:@retrieved_context, retrieved_context)
|
87
|
-
|
88
|
-
erb_context.define_singleton_method(:get_binding) do
|
89
|
-
binding
|
90
|
-
end
|
91
|
-
|
92
|
-
# Define accessor methods for the ERB template
|
93
|
-
erb_context.define_singleton_method(:input) { @input }
|
94
|
-
erb_context.define_singleton_method(:db_server) { @db_server }
|
95
|
-
erb_context.define_singleton_method(:retrieved_context) { @retrieved_context }
|
96
|
-
|
97
|
-
system_prompt = ERB.new(template['system']).result(erb_context.get_binding)
|
98
|
-
user_prompt = ERB.new(template['user']).result(erb_context.get_binding)
|
99
|
-
[system_prompt, user_prompt]
|
100
|
-
end
|
101
|
-
|
113
|
+
# Cleans the SQL response from the AI provider.
|
114
|
+
#
|
115
|
+
# @param query [String] The SQL query to clean.
|
116
|
+
# @return [String] The cleaned SQL query.
|
102
117
|
def clean_sql_response(query)
|
103
118
|
return query unless query
|
104
119
|
|
105
120
|
query = query.gsub(/```sql\n?/, '')
|
106
121
|
query = query.gsub(/```\n?/, '')
|
107
122
|
query = query.strip
|
108
|
-
query = query.gsub(
|
123
|
+
query = query.gsub(/^(.*?)(SELECT|WITH|INSERT|UPDATE|DELETE|CREATE|DROP|ALTER)/i, '\1')
|
109
124
|
lines = query.split("\n")
|
110
125
|
sql_lines = []
|
111
126
|
|
@@ -122,6 +137,9 @@ module Rails
|
|
122
137
|
cleaned_query
|
123
138
|
end
|
124
139
|
|
140
|
+
# Validates the safety of the SQL query.
|
141
|
+
#
|
142
|
+
# @param query [String] The SQL query to validate.
|
125
143
|
def validate_query_safety(query)
|
126
144
|
return unless query
|
127
145
|
|
data/lib/rails/nl2sql/version.rb
CHANGED
data/lib/rails/nl2sql.rb
CHANGED
@@ -3,6 +3,7 @@ require "rails/nl2sql/providers/base"
|
|
3
3
|
require "rails/nl2sql/providers/openai_provider"
|
4
4
|
require "rails/nl2sql/providers/anthropic_provider"
|
5
5
|
require "rails/nl2sql/providers/llama_provider"
|
6
|
+
require "rails/nl2sql/providers/gemini_provider"
|
6
7
|
require "rails/nl2sql/query_generator"
|
7
8
|
require "rails/nl2sql/schema_builder"
|
8
9
|
require "rails/nl2sql/query_validator"
|
@@ -20,6 +21,7 @@ module Rails
|
|
20
21
|
attr_accessor :model
|
21
22
|
attr_accessor :provider
|
22
23
|
attr_accessor :max_schema_lines
|
24
|
+
attr_accessor :debug
|
23
25
|
|
24
26
|
def prompt_template_path=(path)
|
25
27
|
@prompt_template = nil
|
@@ -33,6 +35,7 @@ module Rails
|
|
33
35
|
|
34
36
|
@model = 'gpt-3.5-turbo-instruct'
|
35
37
|
@max_schema_lines = 200
|
38
|
+
@debug = false
|
36
39
|
|
37
40
|
def self.configure
|
38
41
|
yield self
|
@@ -46,19 +49,7 @@ module Rails
|
|
46
49
|
|
47
50
|
class Processor
|
48
51
|
def self.execute(natural_language_query, options = {})
|
49
|
-
|
50
|
-
schema = SchemaBuilder.build_schema(options)
|
51
|
-
tables = options[:tables]
|
52
|
-
|
53
|
-
query_generator = QueryGenerator.new(model: Rails::Nl2sql.model)
|
54
|
-
generated_query = query_generator.generate_query(
|
55
|
-
natural_language_query,
|
56
|
-
schema,
|
57
|
-
db_server,
|
58
|
-
tables
|
59
|
-
)
|
60
|
-
|
61
|
-
QueryValidator.validate(generated_query)
|
52
|
+
generated_query = self.generate_query_only(natural_language_query, options)
|
62
53
|
ActiveRecord::Base.connection.execute(generated_query)
|
63
54
|
end
|
64
55
|
|
@@ -67,6 +58,13 @@ module Rails
|
|
67
58
|
schema = SchemaBuilder.build_schema(options)
|
68
59
|
tables = options[:tables]
|
69
60
|
|
61
|
+
if Rails::Nl2sql.debug && defined?(Rails)
|
62
|
+
Rails.logger.debug "--- Rails NL2SQL Debug Info ---"
|
63
|
+
Rails.logger.debug "Natural Language Query: #{natural_language_query}"
|
64
|
+
Rails.logger.debug "Schema: \n#{schema}"
|
65
|
+
Rails.logger.debug "Tables: #{tables.inspect}" if tables
|
66
|
+
end
|
67
|
+
|
70
68
|
query_generator = QueryGenerator.new(model: Rails::Nl2sql.model)
|
71
69
|
generated_query = query_generator.generate_query(
|
72
70
|
natural_language_query,
|
@@ -75,6 +73,11 @@ module Rails
|
|
75
73
|
tables
|
76
74
|
)
|
77
75
|
|
76
|
+
if Rails::Nl2sql.debug && defined?(Rails)
|
77
|
+
Rails.logger.debug "Generated Query: #{generated_query}"
|
78
|
+
Rails.logger.debug "--- End Rails NL2SQL Debug Info ---"
|
79
|
+
end
|
80
|
+
|
78
81
|
QueryValidator.validate(generated_query)
|
79
82
|
generated_query
|
80
83
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-nl2sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Russell Van Curen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-07-
|
11
|
+
date: 2025-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: openai
|
@@ -123,9 +123,11 @@ files:
|
|
123
123
|
- Gemfile
|
124
124
|
- Gemfile.lock
|
125
125
|
- LICENSE.txt
|
126
|
+
- PLAN.md
|
126
127
|
- README.md
|
127
128
|
- Rakefile
|
128
129
|
- bin/console
|
130
|
+
- bin/rails-nl2sql
|
129
131
|
- bin/setup
|
130
132
|
- lib/generators/rails/nl2sql/install_generator.rb
|
131
133
|
- lib/generators/rails/nl2sql/templates/rails_nl2sql.rb
|
@@ -134,9 +136,12 @@ files:
|
|
134
136
|
- lib/rails-nl2sql.rb
|
135
137
|
- lib/rails/nl2sql.rb
|
136
138
|
- lib/rails/nl2sql/active_record_extension.rb
|
139
|
+
- lib/rails/nl2sql/cli.rb
|
140
|
+
- lib/rails/nl2sql/prompt_builder.rb
|
137
141
|
- lib/rails/nl2sql/prompts/default.yml.erb
|
138
142
|
- lib/rails/nl2sql/providers/anthropic_provider.rb
|
139
143
|
- lib/rails/nl2sql/providers/base.rb
|
144
|
+
- lib/rails/nl2sql/providers/gemini_provider.rb
|
140
145
|
- lib/rails/nl2sql/providers/llama_provider.rb
|
141
146
|
- lib/rails/nl2sql/providers/openai_provider.rb
|
142
147
|
- lib/rails/nl2sql/query_generator.rb
|