ruby-text2sql 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +22 -0
- data/README.md +24 -9
- data/lib/ruby/text2sql/version.rb +1 -1
- data/lib/ruby/text2sql.rb +13 -7
- metadata +8 -8
- data/ruby-text2sql.gemspec +0 -40
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dfaa44f0339d328ce49d43002837c15aecaa6f26ef61ed19a0815f2ee88a5fb4
|
4
|
+
data.tar.gz: 6fed3d7a77b38851fa4790ff881103ef82938e7a37440ab9c303fac0f5af0d10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3409fa5ad821c60e177533bdbdd19ee7d77e605ebc42810bee2d912b600e3fb568a9210aa9bac845bb91f04564822f7510ee6fc99024defdf8bd8a90b2d04c9
|
7
|
+
data.tar.gz: f0f4eaeef6f1c88e78f8b8c9b175f2fb6b29d41ea9e50fa65b54add829d7f2647d2aca723d3975f284537652274c05f807a7d11eeb50ca6ce2bb4a200ef76dfe
|
data/.rubocop.yml
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
AllCops:
|
2
2
|
TargetRubyVersion: 2.6
|
3
|
+
NewCops: enable
|
3
4
|
|
4
5
|
Style/StringLiterals:
|
5
6
|
Enabled: true
|
@@ -11,3 +12,24 @@ Style/StringLiteralsInInterpolation:
|
|
11
12
|
|
12
13
|
Layout/LineLength:
|
13
14
|
Max: 120
|
15
|
+
|
16
|
+
FactoryBot/CreateList:
|
17
|
+
AutoCorrect: true
|
18
|
+
|
19
|
+
Style/Documentation:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Metrics/MethodLength:
|
23
|
+
Max: 20
|
24
|
+
|
25
|
+
RSpec/MultipleExpectations:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
RSpec/ExampleLength:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
RSpec/MultipleMemoizedHelpers:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
require:
|
35
|
+
- rubocop-rspec
|
data/README.md
CHANGED
@@ -1,24 +1,39 @@
|
|
1
1
|
# Ruby::Text2sql
|
2
2
|
|
3
|
-
|
3
|
+
**Chat with your Database!**
|
4
4
|
|
5
|
-
|
5
|
+
How It Works?
|
6
6
|
|
7
|
-
|
7
|
+
Text2SQL takes:
|
8
|
+
- **Your natural language Query** (in any language).
|
9
|
+
- **Your OpenAI API Key** to process the query.
|
10
|
+
- **Your Database Schema** to understand table structures.
|
11
|
+
|
12
|
+
Using these, Text2SQL generates SQL queries, executes them, and returns results in a human-readable format—simplifying data access for both developers and non-developers.
|
8
13
|
|
9
|
-
|
14
|
+
## Installation
|
10
15
|
|
11
16
|
Install the gem and add to the application's Gemfile by executing:
|
12
17
|
|
13
|
-
$ bundle add
|
18
|
+
$ bundle add ruby-text2sq
|
14
19
|
|
15
20
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
21
|
|
17
|
-
$ gem install
|
22
|
+
$ gem install ruby-text2sq
|
18
23
|
|
19
24
|
## Usage
|
20
25
|
|
21
|
-
|
26
|
+
- Set your `OPENAI_API_KEY` as an environment variable in `.env`.
|
27
|
+
|
28
|
+
- Use `Text2sql.call` method with a plain-text query. Here’s an example:
|
29
|
+
```
|
30
|
+
require 'text2sql'
|
31
|
+
|
32
|
+
response = Ruby::Text2sql.call("List all users who registered in the last 30 days")
|
33
|
+
puts "SQL Query: #{response[:sql_query]}" # Outputs the generated SQL query
|
34
|
+
puts "Query Result: #{response[:query_result]}" # Outputs the result of the SQL query
|
35
|
+
puts "Response: #{response[:natural_language_response]}" # Outputs a human-readable response
|
36
|
+
```
|
22
37
|
|
23
38
|
## Development
|
24
39
|
|
@@ -28,7 +43,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
28
43
|
|
29
44
|
## Contributing
|
30
45
|
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nicofh/ruby-text2sql. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nicofh/ruby-text2sql/blob/master/CODE_OF_CONDUCT.md).
|
32
47
|
|
33
48
|
## License
|
34
49
|
|
@@ -36,4 +51,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
36
51
|
|
37
52
|
## Code of Conduct
|
38
53
|
|
39
|
-
Everyone interacting in the Ruby::Text2sql project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
54
|
+
Everyone interacting in the Ruby::Text2sql project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nicofh/ruby-text2sql/blob/master/CODE_OF_CONDUCT.md).
|
data/lib/ruby/text2sql.rb
CHANGED
@@ -33,25 +33,31 @@ module Ruby
|
|
33
33
|
private
|
34
34
|
|
35
35
|
def client
|
36
|
-
@client ||= OpenAI::Client.new(access_token: ENV
|
36
|
+
@client ||= OpenAI::Client.new(access_token: ENV.fetch("OPENAI_API_KEY", nil))
|
37
37
|
end
|
38
38
|
|
39
39
|
def generate_sql_query(user_request, schema)
|
40
|
-
full_prompt = "Ruby Schema:\n#{schema}\n\
|
41
|
-
|
40
|
+
full_prompt = "Ruby Schema:\n#{schema}\n\n
|
41
|
+
User Request: #{user_request}\n\n
|
42
|
+
Generate only the SQL query to answer the request.
|
43
|
+
Do not include any explanation or additional text.
|
44
|
+
If the request doesn't make sense just return 'SELECT '#{user_request}';'."
|
45
|
+
|
42
46
|
response = client.chat(
|
43
47
|
parameters: {
|
44
48
|
model: "gpt-3.5-turbo",
|
45
49
|
messages: [{ role: "system", content: "You are an expert SQL generator." },
|
46
|
-
|
47
|
-
|
48
|
-
|
50
|
+
{ role: "user", content: full_prompt }]
|
51
|
+
}
|
52
|
+
)
|
49
53
|
|
50
54
|
response["choices"][0]["message"]["content"].strip
|
51
55
|
end
|
52
56
|
|
53
57
|
def generate_response(user_request, query_result)
|
54
|
-
result_prompt = "User Request: #{user_request}\
|
58
|
+
result_prompt = "User Request: #{user_request}\n
|
59
|
+
SQL Query Result: #{query_result.inspect}\n\n
|
60
|
+
Answer the user's request in natural language. Answer in the same language as the question"
|
55
61
|
|
56
62
|
response = client.chat(
|
57
63
|
parameters: {
|
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:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Fabre
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-openai
|
@@ -24,10 +24,10 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '7.1'
|
27
|
-
description:
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
description: |-
|
28
|
+
Text2SQL is a gem designed to generate SQL queries from natural language inputs.
|
29
|
+
It leverages machine learning models to interpret schema files and user inputs,
|
30
|
+
generating SQL queries that can be executed and returning results in a human-readable format.
|
31
31
|
email:
|
32
32
|
- nicofh93@gmail.com
|
33
33
|
executables: []
|
@@ -45,7 +45,6 @@ files:
|
|
45
45
|
- lib/ruby/text2sql/schema_parser.rb
|
46
46
|
- lib/ruby/text2sql/sql_executor.rb
|
47
47
|
- lib/ruby/text2sql/version.rb
|
48
|
-
- ruby-text2sql.gemspec
|
49
48
|
- sig/ruby/text2sql.rbs
|
50
49
|
homepage: https://github.com/nicofh/ruby-text2sql
|
51
50
|
licenses:
|
@@ -55,6 +54,7 @@ metadata:
|
|
55
54
|
homepage_uri: https://github.com/nicofh/ruby-text2sql
|
56
55
|
source_code_uri: https://github.com/nicofh/ruby-text2sql
|
57
56
|
changelog_uri: https://github.com/nicofh/ruby-text2sql/blob/main/CHANGELOG.md
|
57
|
+
rubygems_mfa_required: 'true'
|
58
58
|
post_install_message:
|
59
59
|
rdoc_options: []
|
60
60
|
require_paths:
|
@@ -70,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
70
|
- !ruby/object:Gem::Version
|
71
71
|
version: '0'
|
72
72
|
requirements: []
|
73
|
-
rubygems_version: 3.
|
73
|
+
rubygems_version: 3.3.3
|
74
74
|
signing_key:
|
75
75
|
specification_version: 4
|
76
76
|
summary: A Ruby gem to convert natural language to SQL queries using LLMs.
|
data/ruby-text2sql.gemspec
DELETED
@@ -1,40 +0,0 @@
|
|
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 = Ruby::Text2sql::VERSION
|
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. It leverages machine learning models to interpret schema files and user inputs, generating SQL queries that can be executed and returning results in a human-readable format."
|
13
|
-
spec.homepage = "https://github.com/nicofh/ruby-text2sql"
|
14
|
-
spec.license = "MIT"
|
15
|
-
spec.required_ruby_version = ">= 2.6.0"
|
16
|
-
|
17
|
-
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
18
|
-
|
19
|
-
spec.metadata["homepage_uri"] = "https://github.com/nicofh/ruby-text2sql"
|
20
|
-
spec.metadata["source_code_uri"] = "https://github.com/nicofh/ruby-text2sql"
|
21
|
-
spec.metadata["changelog_uri"] = "https://github.com/nicofh/ruby-text2sql/blob/main/CHANGELOG.md"
|
22
|
-
|
23
|
-
# Specify which files should be added to the gem when it is released.
|
24
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
-
spec.files = Dir.chdir(__dir__) do
|
26
|
-
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
-
(File.expand_path(f) == __FILE__) ||
|
28
|
-
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
|
29
|
-
end
|
30
|
-
end
|
31
|
-
spec.bindir = "exe"
|
32
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
33
|
-
spec.require_paths = ["lib"]
|
34
|
-
|
35
|
-
# Uncomment to register a new dependency of your gem
|
36
|
-
spec.add_dependency "ruby-openai", "~> 7.1"
|
37
|
-
|
38
|
-
# For more information and examples about making a new gem, check out our
|
39
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
40
|
-
end
|