rails-nl2sql 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 87e9ce4d9e33ac2e3620b824a2103b452f0faccc4ea3f50fb5535a3bbdfd2791
4
+ data.tar.gz: d098ab06c8ac24a0cef6b31fd827470667dafb4f014a7d8cd4b9f63b70a479a0
5
+ SHA512:
6
+ metadata.gz: ff7b5738ad5b10aec1a005a91858e54a46f7af95e0adbcb862337c7d6c3069c89cdf8578463233e3e0b705f914c491b0fec2ef6631f3dd9296680ade9f3d019a
7
+ data.tar.gz: 2b2449a921c1262000c99eae8cbc6532964576c46ebb8a1f47dc1781c947a491b1bb95aef63a72a5bcdad30368975ba4f20f40164727213fc564a60c122d72e5
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
10
+ /spec/dummy/
11
+ /*.gem
12
+ /.rspec
13
+ /spec/rails_helper.rb
14
+ /spec/spec_helper.rb
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rails-nl2sql.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,95 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails-nl2sql (0.1.0)
5
+ openai (~> 0.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (6.1.4.7)
11
+ actionview (= 6.1.4.7)
12
+ activesupport (= 6.1.4.7)
13
+ rack (~> 2.0, >= 2.0.9)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (6.1.4.7)
18
+ activesupport (= 6.1.4.7)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activesupport (6.1.4.7)
24
+ concurrent-ruby (~> 1.0, >= 1.0.2)
25
+ i18n (>= 1.6, < 2)
26
+ minitest (>= 5.1)
27
+ tzinfo (~> 2.0)
28
+ zeitwerk (~> 2.3)
29
+ builder (3.3.0)
30
+ concurrent-ruby (1.3.5)
31
+ crass (1.0.6)
32
+ diff-lcs (1.6.2)
33
+ erubi (1.13.1)
34
+ i18n (1.14.7)
35
+ concurrent-ruby (~> 1.0)
36
+ loofah (2.24.1)
37
+ crass (~> 1.0.2)
38
+ nokogiri (>= 1.12.0)
39
+ method_source (1.1.0)
40
+ mini_portile2 (2.8.9)
41
+ minitest (5.25.4)
42
+ nokogiri (1.13.10)
43
+ mini_portile2 (~> 2.8.0)
44
+ racc (~> 1.4)
45
+ openai (0.3.0)
46
+ racc (1.8.1)
47
+ rack (2.2.17)
48
+ rack-test (2.2.0)
49
+ rack (>= 1.3)
50
+ rails-dom-testing (2.3.0)
51
+ activesupport (>= 5.0.0)
52
+ minitest
53
+ nokogiri (>= 1.6)
54
+ rails-html-sanitizer (1.5.0)
55
+ loofah (~> 2.19, >= 2.19.1)
56
+ railties (6.1.4.7)
57
+ actionpack (= 6.1.4.7)
58
+ activesupport (= 6.1.4.7)
59
+ method_source
60
+ rake (>= 0.13)
61
+ thor (~> 1.0)
62
+ rake (10.5.0)
63
+ rspec-core (3.13.5)
64
+ rspec-support (~> 3.13.0)
65
+ rspec-expectations (3.13.5)
66
+ diff-lcs (>= 1.2.0, < 2.0)
67
+ rspec-support (~> 3.13.0)
68
+ rspec-mocks (3.13.5)
69
+ diff-lcs (>= 1.2.0, < 2.0)
70
+ rspec-support (~> 3.13.0)
71
+ rspec-rails (6.1.5)
72
+ actionpack (>= 6.1)
73
+ activesupport (>= 6.1)
74
+ railties (>= 6.1)
75
+ rspec-core (~> 3.13)
76
+ rspec-expectations (~> 3.13)
77
+ rspec-mocks (~> 3.13)
78
+ rspec-support (~> 3.13)
79
+ rspec-support (3.13.4)
80
+ thor (1.3.2)
81
+ tzinfo (2.0.6)
82
+ concurrent-ruby (~> 1.0)
83
+ zeitwerk (2.6.18)
84
+
85
+ PLATFORMS
86
+ ruby
87
+
88
+ DEPENDENCIES
89
+ bundler (~> 1.17)
90
+ rails-nl2sql!
91
+ rake (~> 10.0)
92
+ rspec-rails
93
+
94
+ BUNDLED WITH
95
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Van Curen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # Rails NL2SQL
2
+
3
+ This gem provides an easy way to integrate natural language to SQL functionality into your Ruby on Rails projects. It uses AI models to convert natural language queries into SQL statements.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rails-nl2sql'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rails-nl2sql
20
+
21
+ Next, you need to run the install generator:
22
+
23
+ $ rails generate rails:nl2sql:install
24
+
25
+ This will create an initializer file at `config/initializers/rails_nl2sql.rb`. You will need to configure your OpenAI API key in this file.
26
+
27
+ ```ruby
28
+ # config/initializers/rails_nl2sql.rb
29
+ Rails::Nl2sql.configure do |config|
30
+ config.api_key = ENV["OPENAI_API_KEY"] # It's recommended to use an environment variable
31
+ # config.model = "text-davinci-003" # Optional: Specify the AI model to use (default is text-davinci-003)
32
+ end
33
+ ```
34
+
35
+ Make sure to set the `OPENAI_API_KEY` environment variable in your development and production environments.
36
+
37
+ ## Usage
38
+
39
+ To execute a natural language query, you can use the `execute` method:
40
+
41
+ ```ruby
42
+ results = Rails::Nl2sql::Processor.execute("Show me all the users from California")
43
+ ```
44
+
45
+ You can also specify which tables to include or exclude:
46
+
47
+ ```ruby
48
+ results = Rails::Nl2sql::Processor.execute("Show me all the orders for the user with email 'test@example.com'", include: ["users", "orders"])
49
+ ```
50
+
51
+ ### Getting a list of tables
52
+
53
+ ```ruby
54
+ Rails::Nl2sql::Processor.get_tables
55
+ ```
56
+
57
+ ### Getting the schema
58
+
59
+ ```ruby
60
+ Rails::Nl2sql::Processor.get_schema(include: ["users", "orders"])
61
+ ```
62
+
63
+ ## Development
64
+
65
+ 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.
66
+
67
+ 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).
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vancuren/rails-nl2sql.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails/nl2sql"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ require 'rails/generators'
2
+
3
+ module Rails
4
+ module Nl2sql
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def copy_initializer
10
+ template 'rails_nl2sql.rb', 'config/initializers/rails_nl2sql.rb'
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ Rails::Nl2sql.configure do |config|
2
+ config.api_key = "YOUR_API_KEY"
3
+ # config.model = "text-davinci-003"
4
+ end
@@ -0,0 +1,26 @@
1
+ require "openai"
2
+
3
+ module Rails
4
+ module Nl2sql
5
+ class QueryGenerator
6
+ def initialize(api_key, model = "text-davinci-003")
7
+ @client = OpenAI::Client.new(api_key: api_key)
8
+ @model = model
9
+ end
10
+
11
+ def generate_query(prompt, schema)
12
+ full_prompt = "Given the following schema:\n\n#{schema}\n\nGenerate a SQL query for the following request:\n\n#{prompt}"
13
+
14
+ response = @client.completions(
15
+ parameters: {
16
+ model: @model,
17
+ prompt: full_prompt,
18
+ max_tokens: 150
19
+ }
20
+ )
21
+
22
+ response.choices.first.text.strip
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ module Rails
2
+ module Nl2sql
3
+ class QueryValidator
4
+ def self.validate(query)
5
+ # Basic validation: prevent destructive commands
6
+ disallowed_keywords = %w(DROP DELETE UPDATE INSERT TRUNCATE ALTER CREATE)
7
+ if disallowed_keywords.any? { |keyword| query.upcase.include?(keyword) }
8
+ raise "Query contains disallowed keywords."
9
+ end
10
+
11
+ # Use Rails' built-in sanitization to be safe
12
+ begin
13
+ ActiveRecord::Base.connection.execute("EXPLAIN #{query}")
14
+ rescue ActiveRecord::StatementInvalid => e
15
+ raise "Invalid SQL query: #{e.message}"
16
+ end
17
+
18
+ true
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ require "rails/nl2sql"
2
+ require "rails"
3
+
4
+ module Rails
5
+ module Nl2sql
6
+ class Railtie < Rails::Railtie
7
+ initializer "rails-nl2sql.configure_rails_initialization" do |app|
8
+ app.config.after_initialize do
9
+ # Nothing to do here for now
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Rails
2
+ module Nl2sql
3
+ class SchemaBuilder
4
+ def self.build_schema(options = {})
5
+ tables = ActiveRecord::Base.connection.tables
6
+ tables -= options[:exclude] if options[:exclude]
7
+ tables = options[:include] if options[:include]
8
+
9
+ schema = {}
10
+ tables.each do |table|
11
+ schema[table] = ActiveRecord::Base.connection.columns(table).map(&:name)
12
+ end
13
+
14
+ schema
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module Rails
2
+ module Nl2sql
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -0,0 +1,40 @@
1
+ require "rails/nl2sql/version"
2
+ require "rails/nl2sql/query_generator"
3
+ require "rails/nl2sql/schema_builder"
4
+ require "rails/nl2sql/query_validator"
5
+
6
+ module Rails
7
+ module Nl2sql
8
+ class Error < StandardError; end
9
+
10
+ class << self
11
+ attr_accessor :api_key
12
+ attr_accessor :model
13
+ end
14
+ @@model = "text-davinci-003"
15
+
16
+ def self.configure
17
+ yield self
18
+ end
19
+
20
+ class Processor
21
+ def self.execute(natural_language_query, options = {})
22
+ schema = SchemaBuilder.build_schema(options)
23
+ query_generator = QueryGenerator.new(Rails::Nl2sql.api_key, Rails::Nl2sql.model)
24
+ generated_query = query_generator.generate_query(natural_language_query, schema)
25
+
26
+ QueryValidator.validate(generated_query)
27
+
28
+ ActiveRecord::Base.connection.execute(generated_query)
29
+ end
30
+
31
+ def self.get_tables(options = {})
32
+ ActiveRecord::Base.connection.tables
33
+ end
34
+
35
+ def self.get_schema(options = {})
36
+ SchemaBuilder.build_schema(options)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rails/nl2sql/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails-nl2sql"
8
+ spec.version = Rails::Nl2sql::VERSION
9
+ spec.authors = ["Russell Van Curen"]
10
+ spec.email = ["russell@vancuren.net"]
11
+
12
+ spec.summary = %q{A Ruby on Rails gem for converting natural language to SQL.}
13
+ spec.description = %q{This gem provides an easy way to integrate natural language to SQL functionality into your Ruby on Rails projects. It uses AI models to convert natural language queries into SQL statements.}
14
+ spec.homepage = "https://github.com/vancuren/rails-nl2sql"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/vancuren/rails-nl2sql"
24
+ spec.metadata["changelog_uri"] = "https://github.com/vancuren/rails-nl2sql/blob/main/CHANGELOG.md"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_dependency "openai", "~> 0.3"
40
+ spec.add_development_dependency "bundler", "~> 1.17"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "rspec-rails", "~> 6.0"
43
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-nl2sql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Russell Van Curen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: openai
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '6.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '6.0'
69
+ description: This gem provides an easy way to integrate natural language to SQL functionality
70
+ into your Ruby on Rails projects. It uses AI models to convert natural language
71
+ queries into SQL statements.
72
+ email:
73
+ - russell@vancuren.net
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/generators/rails/nl2sql/install_generator.rb
87
+ - lib/generators/rails/nl2sql/templates/rails_nl2sql.rb
88
+ - lib/rails/nl2sql.rb
89
+ - lib/rails/nl2sql/query_generator.rb
90
+ - lib/rails/nl2sql/query_validator.rb
91
+ - lib/rails/nl2sql/railtie.rb
92
+ - lib/rails/nl2sql/schema_builder.rb
93
+ - lib/rails/nl2sql/version.rb
94
+ - rails-nl2sql.gemspec
95
+ homepage: https://github.com/vancuren/rails-nl2sql
96
+ licenses:
97
+ - MIT
98
+ metadata:
99
+ allowed_push_host: https://rubygems.org
100
+ homepage_uri: https://github.com/vancuren/rails-nl2sql
101
+ source_code_uri: https://github.com/vancuren/rails-nl2sql
102
+ changelog_uri: https://github.com/vancuren/rails-nl2sql/blob/main/CHANGELOG.md
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubygems_version: 3.0.3.1
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: A Ruby on Rails gem for converting natural language to SQL.
122
+ test_files: []