human_sql 0.1.0 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +2 -3
- data/LICENSE.txt +1 -1
- data/README.md +49 -20
- data/human_sql-0.1.1.gem +0 -0
- data/lib/human_sql/version.rb +1 -1
- data/lib/human_sql.rb +22 -7
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 252749f91542523c968e6819c589754afd16ff53fe5dc2851ce55b82c67fc42a
|
4
|
+
data.tar.gz: eab62bcd1da26e6d3f89bd1d6859122b9524dd769a24978f7fe0277e60867a9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d8aeb7e0cb4247509445c9105c3885ff8395f576b35c136d0ee410f963110f9880b1f35e37bad6af32c633860486580908b8fab9c67ff9a0fab92f3e771bb08
|
7
|
+
data.tar.gz: eaabff247f028ec2553440ca0f52029a7bd6b32e080f3d2f4b12fcaf2de1c944858b1562488ae666a03bc459c2800cc304456eda7e0a951e792a6fe593c7131d
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -60,8 +60,7 @@ representative at an online or offline event.
|
|
60
60
|
|
61
61
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
62
|
reported to the community leaders responsible for enforcement at
|
63
|
-
[
|
64
|
-
All complaints will be reviewed and investigated promptly and fairly.
|
63
|
+
[mail@nicobertin.dev]. All complaints will be reviewed and investigated promptly and fairly.
|
65
64
|
|
66
65
|
All community leaders are obligated to respect the privacy and security of the
|
67
66
|
reporter of any incident.
|
@@ -129,4 +128,4 @@ For answers to common questions about this code of conduct, see the FAQ at
|
|
129
128
|
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
129
|
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
130
|
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
-
[translations]: https://www.contributor-covenant.org/translations
|
131
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
CHANGED
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
THE SOFTWARE.
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,43 +1,72 @@
|
|
1
1
|
# HumanSql
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/human_sql`. To experiment with that code, run `bin/console` for an interactive prompt.
|
3
|
+
`HumanSql` is a gem that converts natural language queries into ActiveRecord queries using the OpenAI API. This gem allows developers to execute complex database queries by simply providing instructions in plain English (or another supported language).
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
Install the gem and add to the application's Gemfile by executing:
|
7
|
+
After releasing to RubyGems.org, you can install the gem and add it to your application's Gemfile by executing:
|
12
8
|
|
13
9
|
```bash
|
14
|
-
bundle add
|
10
|
+
bundle add human_sql
|
15
11
|
```
|
16
12
|
|
17
|
-
If
|
13
|
+
If Bundler is not being used to manage dependencies, you can install the gem by executing:
|
18
14
|
|
19
15
|
```bash
|
20
|
-
gem install
|
16
|
+
gem install human_sql
|
17
|
+
```
|
18
|
+
|
19
|
+
### Post Installation
|
20
|
+
Once the gem is installed, you'll need to configure it by creating an initializer. In your Rails application, create a file named config/initializers/human_sql.rb and add the following:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
HumanSQLConfig = {
|
24
|
+
api_key: 'your_openai_api_key', # Replace with your actual OpenAI API key
|
25
|
+
default_language: 'english' # You can change this to 'spanish', 'japanese', etc.
|
26
|
+
}
|
21
27
|
```
|
22
28
|
|
23
|
-
|
29
|
+
Make sure to replace 'your_openai_api_key' with your actual API key from OpenAI. The default_language setting controls the language used for the natural language responses.
|
30
|
+
|
31
|
+
### Usage
|
32
|
+
|
33
|
+
To use HumanSql in your application, simply call the run method on HumanSQL::QueryBuilder with a natural language query. For example:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
result = HumanSQL::QueryBuilder.run("Give me the first user from my database")
|
37
|
+
puts result
|
38
|
+
```
|
24
39
|
|
25
|
-
|
40
|
+
This will convert the natural language query into an ActiveRecord query, execute it, and return the results formatted in the specified language.
|
26
41
|
|
27
|
-
|
42
|
+
### Disclaimer
|
28
43
|
|
29
|
-
|
44
|
+
IMPORTANT: By using this gem, you agree that the author is not responsible for any kind of data loss, damage, or issues that may arise from the use of this software. You are solely responsible for verifying the safety of the generated queries before running them in your production environment or on critical data. Use at your own risk.
|
30
45
|
|
31
|
-
|
46
|
+
### Development
|
32
47
|
|
33
|
-
|
48
|
+
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.
|
49
|
+
|
50
|
+
To install this gem onto your local machine, run:
|
51
|
+
|
52
|
+
```bash
|
53
|
+
bundle
|
54
|
+
```
|
55
|
+
|
56
|
+
To release a new version, update the version number in version.rb, and then run:
|
57
|
+
|
58
|
+
```bash
|
59
|
+
bundle exec rake release
|
60
|
+
```
|
34
61
|
|
35
|
-
|
62
|
+
This will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
|
36
63
|
|
37
|
-
|
64
|
+
### Contributing
|
38
65
|
|
39
|
-
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nicobertin/human_sql. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
|
40
67
|
|
41
|
-
|
68
|
+
### License
|
69
|
+
The gem is available as open source under the terms of the MIT License.
|
42
70
|
|
43
|
-
|
71
|
+
### Code of Conduct
|
72
|
+
Everyone interacting in the HumanSql project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the code of conduct.
|
data/human_sql-0.1.1.gem
ADDED
Binary file
|
data/lib/human_sql/version.rb
CHANGED
data/lib/human_sql.rb
CHANGED
@@ -28,9 +28,13 @@ module HumanSQL
|
|
28
28
|
raise "Could not generate a valid query." if generated_query.blank?
|
29
29
|
|
30
30
|
results = execute_query(generated_query)
|
31
|
+
raise "No results found." if results.blank?
|
32
|
+
|
31
33
|
formatted_results = format_results_for_openai(results)
|
32
34
|
natural_language_response = generate_natural_language_response(formatted_results, @user_input)
|
33
35
|
natural_language_response
|
36
|
+
rescue StandardError => e
|
37
|
+
process_error_in_natural_language(e.message)
|
34
38
|
end
|
35
39
|
|
36
40
|
private
|
@@ -53,7 +57,7 @@ module HumanSQL
|
|
53
57
|
messages: [
|
54
58
|
{
|
55
59
|
role: "system",
|
56
|
-
content: "You are an assistant that converts natural language into ActiveRecord queries
|
60
|
+
content: "You are an assistant that converts natural language into ActiveRecord queries and explains results in natural language."
|
57
61
|
},
|
58
62
|
{
|
59
63
|
role: "user",
|
@@ -93,13 +97,13 @@ module HumanSQL
|
|
93
97
|
|
94
98
|
def execute_query(generated_query)
|
95
99
|
eval(generated_query)
|
96
|
-
rescue StandardError
|
100
|
+
rescue StandardError
|
97
101
|
nil
|
98
102
|
end
|
99
103
|
|
100
104
|
def format_results_for_openai(results)
|
101
105
|
if results.is_a?(ActiveRecord::Relation) || results.is_a?(Array)
|
102
|
-
|
106
|
+
results.map do |result|
|
103
107
|
if result.respond_to?(:attributes)
|
104
108
|
result.attributes.map { |key, value| "#{key}: #{value}" }.join(', ')
|
105
109
|
else
|
@@ -107,9 +111,8 @@ module HumanSQL
|
|
107
111
|
end
|
108
112
|
end.join("\\n")
|
109
113
|
else
|
110
|
-
|
114
|
+
results.respond_to?(:attributes) ? results.attributes.map { |key, value| "#{key}: #{value}" }.join(', ') : results.to_s
|
111
115
|
end
|
112
|
-
formatted_results.gsub("\n", "\\n")
|
113
116
|
end
|
114
117
|
|
115
118
|
def generate_natural_language_response(formatted_results, user_input)
|
@@ -119,10 +122,22 @@ module HumanSQL
|
|
119
122
|
|
120
123
|
#{formatted_results}
|
121
124
|
|
122
|
-
Please generate a natural language description that clearly and understandably explains these results to the user in #{HumanSQLConfig[:default_language]}
|
125
|
+
Please generate a natural language description that clearly and understandably explains these results to the user in #{HumanSQLConfig[:default_language]}.
|
126
|
+
PROMPT
|
127
|
+
|
128
|
+
call_openai_service(prompt)
|
129
|
+
end
|
130
|
+
|
131
|
+
def process_error_in_natural_language(error_message)
|
132
|
+
prompt = <<-PROMPT
|
133
|
+
An error occurred while processing the user's query. The error is as follows:
|
134
|
+
|
135
|
+
"#{error_message}"
|
136
|
+
|
137
|
+
Please generate a natural language response that explains the error in a way that is understandable to the user in #{HumanSQLConfig[:default_language]}.
|
123
138
|
PROMPT
|
124
139
|
|
125
140
|
call_openai_service(prompt)
|
126
141
|
end
|
127
142
|
end
|
128
|
-
end
|
143
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: human_sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Bertin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-09-
|
11
|
+
date: 2024-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- LICENSE.txt
|
53
53
|
- README.md
|
54
54
|
- Rakefile
|
55
|
+
- human_sql-0.1.1.gem
|
55
56
|
- lib/human_sql.rb
|
56
57
|
- lib/human_sql/version.rb
|
57
58
|
- sig/human_sql.rbs
|