ruby-text2sql 1.0.0 → 1.0.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 +4 -4
- data/.rubocop.yml +6 -1
- data/CHANGELOG.md +43 -0
- data/README.md +28 -5
- data/lib/generators/ruby_text2sql/install/install_generator.rb +17 -0
- data/lib/generators/ruby_text2sql/install/templates/initializer.rb +16 -0
- data/lib/ruby/text2sql/sql_executor.rb +33 -3
- data/lib/ruby/text2sql/version.rb +1 -1
- data/lib/ruby/text2sql.rb +19 -2
- data/ruby-text2sql.gemspec +43 -0
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e649f583364a150b51556fdd7013c99ff0d850971488be8791a95ca13a4898a2
|
4
|
+
data.tar.gz: d0d877636f08d33774c124b33ccbebc0fa43b028ec80fc352f394ccefcfa3260
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae4aba96a152e536eff54299024b256218ff963b5cfa1d68472f88012117ceea2dd8771726f5b87aa6c30de4c9444e4ba25e4a6c04016f66bdb77371338e6a37
|
7
|
+
data.tar.gz: 3c8fd4908543e7dd8d3317f4e38d55d1cedfa2a302db31dd8a4e038a0e77ea0c3cd5e52b687b596168692ddb432f526f5fc995ed0e7058bbc322446c209f51ff
|
data/.rubocop.yml
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
AllCops:
|
2
|
-
TargetRubyVersion: 2.
|
2
|
+
TargetRubyVersion: 2.7.1
|
3
3
|
NewCops: enable
|
4
|
+
SuggestExtensions: false
|
4
5
|
|
5
6
|
Style/StringLiterals:
|
6
7
|
Enabled: true
|
@@ -31,5 +32,9 @@ RSpec/ExampleLength:
|
|
31
32
|
RSpec/MultipleMemoizedHelpers:
|
32
33
|
Enabled: false
|
33
34
|
|
35
|
+
# Disable problematic cops that are causing errors
|
36
|
+
Capybara/RSpec/PredicateMatcher:
|
37
|
+
Enabled: false
|
38
|
+
|
34
39
|
require:
|
35
40
|
- rubocop-rspec
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,48 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
1
8
|
## [Unreleased]
|
2
9
|
|
10
|
+
### Added
|
11
|
+
- New features that will be in the next release
|
12
|
+
|
13
|
+
### Changed
|
14
|
+
- Changes in existing functionality
|
15
|
+
|
16
|
+
### Deprecated
|
17
|
+
- Soon-to-be removed features
|
18
|
+
|
19
|
+
### Removed
|
20
|
+
- Now removed features
|
21
|
+
|
22
|
+
### Fixed
|
23
|
+
- Any bug fixes
|
24
|
+
|
25
|
+
### Security
|
26
|
+
- Vulnerability fixes
|
27
|
+
|
28
|
+
## [1.0.1] - 2024-12-19
|
29
|
+
|
30
|
+
### Added
|
31
|
+
- SQL execution with safety controls
|
32
|
+
|
33
|
+
### Fixed
|
34
|
+
- Fixed RuboCop configuration warnings
|
35
|
+
- Resolved RSpec verified double reference issues
|
36
|
+
|
37
|
+
## [1.0.0] - 2024-12-19
|
38
|
+
|
39
|
+
### Added
|
40
|
+
- Initial stable release
|
41
|
+
- Core Text2SQL functionality
|
42
|
+
- Schema parsing capabilities
|
43
|
+
|
3
44
|
## [0.1.0] - 2024-09-14
|
4
45
|
|
46
|
+
### Added
|
5
47
|
- Initial release
|
48
|
+
- Basic Text2SQL conversion functionality
|
data/README.md
CHANGED
@@ -15,26 +15,49 @@ Using these, Text2SQL generates SQL queries, executes them, and returns results
|
|
15
15
|
|
16
16
|
Install the gem and add to the application's Gemfile by executing:
|
17
17
|
|
18
|
-
$ bundle add ruby-
|
18
|
+
$ bundle add ruby-text2sql
|
19
19
|
|
20
20
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
21
21
|
|
22
|
-
$ gem install ruby-
|
22
|
+
$ gem install ruby-text2sql
|
23
23
|
|
24
24
|
## Usage
|
25
25
|
|
26
26
|
- Set your `OPENAI_API_KEY` as an environment variable in `.env`.
|
27
27
|
|
28
|
-
- Use `Text2sql.call` method with a plain-text query. Here
|
28
|
+
- Use `Ruby::Text2sql.call` method with a plain-text query. Here's an example:
|
29
29
|
```
|
30
|
-
require 'text2sql'
|
31
|
-
|
32
30
|
response = Ruby::Text2sql.call("List all users who registered in the last 30 days")
|
33
31
|
puts "SQL Query: #{response[:sql_query]}" # Outputs the generated SQL query
|
34
32
|
puts "Query Result: #{response[:query_result]}" # Outputs the result of the SQL query
|
35
33
|
puts "Response: #{response[:natural_language_response]}" # Outputs a human-readable response
|
36
34
|
```
|
37
35
|
|
36
|
+
## Configuration: Allowed SQL Actions
|
37
|
+
|
38
|
+
After installing the gem, you can generate an initializer to control which SQL actions are permitted by running:
|
39
|
+
|
40
|
+
```sh
|
41
|
+
rails generate ruby_text2sql:install
|
42
|
+
```
|
43
|
+
|
44
|
+
This will create `config/initializers/ruby_text2sql.rb` with content like:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
Ruby::Text2sql.configure do |config|
|
48
|
+
# Allow only SELECT queries by default (safest)
|
49
|
+
config.allowed_actions = [:select]
|
50
|
+
|
51
|
+
# Example: Allow SELECT, INSERT, and UPDATE queries
|
52
|
+
# config.allowed_actions = [:select, :insert, :update]
|
53
|
+
|
54
|
+
# Example: To allow DELETE queries (use with caution)
|
55
|
+
# config.allowed_actions = [:select, :insert, :update, :delete]
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
**Note:** Only the actions listed in `allowed_actions` (as symbols) will be permitted for execution. This helps protect your database from dangerous or unwanted queries.
|
60
|
+
|
38
61
|
## Development
|
39
62
|
|
40
63
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators"
|
4
|
+
|
5
|
+
module RubyText2sql
|
6
|
+
module Generators
|
7
|
+
class InstallGenerator < Rails::Generators::Base
|
8
|
+
source_root File.expand_path("templates", __dir__)
|
9
|
+
|
10
|
+
desc "Creates a Ruby::Text2sql initializer in your application."
|
11
|
+
|
12
|
+
def copy_initializer
|
13
|
+
template "initializer.rb", "config/initializers/ruby_text2sql.rb"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# config/initializers/ruby_text2sql.rb
|
4
|
+
# This initializer configures which SQL actions are allowed by the ruby-text2sql gem.
|
5
|
+
# Only the actions listed here (as symbols) will be permitted for execution.
|
6
|
+
|
7
|
+
Ruby::Text2sql.configure do |config|
|
8
|
+
# Allow only SELECT queries by default (safest)
|
9
|
+
config.allowed_actions = [:select]
|
10
|
+
|
11
|
+
# Example: Allow SELECT, INSERT, and UPDATE queries
|
12
|
+
# config.allowed_actions = [:select, :insert, :update]
|
13
|
+
|
14
|
+
# Example: To allow DELETE queries (use with caution)
|
15
|
+
# config.allowed_actions = [:select, :insert, :update, :delete]
|
16
|
+
end
|
@@ -3,10 +3,40 @@
|
|
3
3
|
module Ruby
|
4
4
|
module Text2sql
|
5
5
|
class SQLExecutor
|
6
|
-
def
|
7
|
-
|
6
|
+
def initialize(allowed_actions: [:select], sql_query: nil)
|
7
|
+
@allowed_actions = allowed_actions.map(&:to_sym)
|
8
|
+
@sql_query = sql_query
|
9
|
+
end
|
10
|
+
|
11
|
+
def execute
|
12
|
+
return { status: :failed, error: "Action ':#{query_type}' is not allowed." } unless query_allowed?(query_type)
|
13
|
+
|
14
|
+
if query_type == :select
|
15
|
+
result = ActiveRecord::Base.connection.execute(@sql_query)
|
16
|
+
result.to_a
|
17
|
+
else
|
18
|
+
begin
|
19
|
+
ActiveRecord::Base.transaction do
|
20
|
+
ActiveRecord::Base.connection.execute(@sql_query)
|
21
|
+
end
|
22
|
+
{ status: :success }
|
23
|
+
rescue StandardError => e
|
24
|
+
{ status: :failed, error: e.message }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def query_allowed?(action)
|
32
|
+
@allowed_actions.include?(action)
|
33
|
+
end
|
34
|
+
|
35
|
+
def query_type
|
36
|
+
first_word = @sql_query.strip.split.first
|
37
|
+
return nil if first_word.nil?
|
8
38
|
|
9
|
-
|
39
|
+
first_word.upcase.downcase.to_sym
|
10
40
|
end
|
11
41
|
end
|
12
42
|
end
|
data/lib/ruby/text2sql.rb
CHANGED
@@ -9,6 +9,22 @@ module Ruby
|
|
9
9
|
module Text2sql
|
10
10
|
class Error < StandardError; end
|
11
11
|
|
12
|
+
class Configuration
|
13
|
+
attr_accessor :allowed_actions
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@allowed_actions = [:select]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.configuration
|
21
|
+
@configuration ||= Configuration.new
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.configure
|
25
|
+
yield(configuration)
|
26
|
+
end
|
27
|
+
|
12
28
|
class << self
|
13
29
|
def call(user_request)
|
14
30
|
# Step 1: Parse the schema automatically
|
@@ -17,8 +33,9 @@ module Ruby
|
|
17
33
|
# Step 2: Generate SQL query using OpenAI
|
18
34
|
sql_query = generate_sql_query(user_request, schema)
|
19
35
|
|
20
|
-
# Step 3: Execute the generated SQL query using SQLExecutor
|
21
|
-
query_result = SQLExecutor.new.
|
36
|
+
# Step 3: Execute the generated SQL query using SQLExecutor, using configured allowed_actions
|
37
|
+
query_result = SQLExecutor.new(allowed_actions: Ruby::Text2sql.configuration.allowed_actions,
|
38
|
+
sql_query: sql_query).execute
|
22
39
|
|
23
40
|
# Step 4: Generate a natural language response from the query result
|
24
41
|
natural_language_response = generate_response(user_request, query_result)
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/ruby/text2sql/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ruby-text2sql"
|
7
|
+
spec.version = "1.0.1"
|
8
|
+
spec.authors = ["Nicolas Fabre"]
|
9
|
+
spec.email = ["nicofh93@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "A Ruby gem to convert natural language to SQL queries using LLMs."
|
12
|
+
spec.description = "Text2SQL is a gem designed to generate SQL queries from natural language inputs.
|
13
|
+
It leverages machine learning models to interpret schema files and user inputs,
|
14
|
+
generating SQL queries that can be executed and returning results in a human-readable format."
|
15
|
+
spec.homepage = "https://github.com/nicofh/ruby-text2sql"
|
16
|
+
spec.license = "MIT"
|
17
|
+
spec.required_ruby_version = ">= 2.7.1"
|
18
|
+
|
19
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
20
|
+
|
21
|
+
spec.metadata["homepage_uri"] = "https://github.com/nicofh/ruby-text2sql"
|
22
|
+
spec.metadata["source_code_uri"] = "https://github.com/nicofh/ruby-text2sql"
|
23
|
+
spec.metadata["changelog_uri"] = "https://github.com/nicofh/ruby-text2sql/blob/main/CHANGELOG.md"
|
24
|
+
|
25
|
+
# Specify which files should be added to the gem when it is released.
|
26
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
27
|
+
spec.files = Dir.chdir(__dir__) do
|
28
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
29
|
+
(File.expand_path(f) == __FILE__) ||
|
30
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
spec.bindir = "exe"
|
34
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
35
|
+
spec.require_paths = ["lib"]
|
36
|
+
|
37
|
+
# Uncomment to register a new dependency of your gem
|
38
|
+
spec.add_dependency "ruby-openai", "~> 7.1"
|
39
|
+
|
40
|
+
# For more information and examples about making a new gem, check out our
|
41
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
42
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
43
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-text2sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Fabre
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-openai
|
@@ -41,10 +41,13 @@ files:
|
|
41
41
|
- LICENSE.txt
|
42
42
|
- README.md
|
43
43
|
- Rakefile
|
44
|
+
- lib/generators/ruby_text2sql/install/install_generator.rb
|
45
|
+
- lib/generators/ruby_text2sql/install/templates/initializer.rb
|
44
46
|
- lib/ruby/text2sql.rb
|
45
47
|
- lib/ruby/text2sql/schema_parser.rb
|
46
48
|
- lib/ruby/text2sql/sql_executor.rb
|
47
49
|
- lib/ruby/text2sql/version.rb
|
50
|
+
- ruby-text2sql.gemspec
|
48
51
|
- sig/ruby/text2sql.rbs
|
49
52
|
homepage: https://github.com/nicofh/ruby-text2sql
|
50
53
|
licenses:
|
@@ -63,14 +66,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
66
|
requirements:
|
64
67
|
- - ">="
|
65
68
|
- !ruby/object:Gem::Version
|
66
|
-
version: 2.
|
69
|
+
version: 2.7.1
|
67
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
71
|
requirements:
|
69
72
|
- - ">="
|
70
73
|
- !ruby/object:Gem::Version
|
71
74
|
version: '0'
|
72
75
|
requirements: []
|
73
|
-
rubygems_version: 3.
|
76
|
+
rubygems_version: 3.1.2
|
74
77
|
signing_key:
|
75
78
|
specification_version: 4
|
76
79
|
summary: A Ruby gem to convert natural language to SQL queries using LLMs.
|