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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28c8b51819d0630673469964904069f4dd8cfb31a76f9afe439f28f5184a6376
4
- data.tar.gz: 9c17f374223cd3d1eecbe147eef5e926b03c36501b7c7188525549202d59d2a9
3
+ metadata.gz: 252749f91542523c968e6819c589754afd16ff53fe5dc2851ce55b82c67fc42a
4
+ data.tar.gz: eab62bcd1da26e6d3f89bd1d6859122b9524dd769a24978f7fe0277e60867a9d
5
5
  SHA512:
6
- metadata.gz: a0fa556ecb44c2151999eb741a128fc00846d645e31652940c292a4fe2172df3a9088a5b470ec68f352f0d5675633e4ed078fcadd427e18752e92cf90244646d
7
- data.tar.gz: a712c3f78aa08d7c86ddcbd4e7c83e5abf9fde5cb6fca298fdbec80d977bd6bf89fd0cb49ab75f52852b647519a9e0142066a03d5197f7aaf460ad4f27efc767
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
- [INSERT CONTACT METHOD].
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
- TODO: Delete this and the text below, and describe your gem
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
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
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 UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
10
+ bundle add human_sql
15
11
  ```
16
12
 
17
- If bundler is not being used to manage dependencies, install the gem by executing:
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 UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
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
- ## Usage
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
- TODO: Write usage instructions here
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
- ## Development
42
+ ### Disclaimer
28
43
 
29
- 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.
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
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+ ### Development
32
47
 
33
- ## Contributing
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
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/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](https://github.com/[USERNAME]/human_sql/blob/main/CODE_OF_CONDUCT.md).
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
- ## License
64
+ ### Contributing
38
65
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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
- ## Code of Conduct
68
+ ### License
69
+ The gem is available as open source under the terms of the MIT License.
42
70
 
43
- Everyone interacting in the HumanSql project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/human_sql/blob/main/CODE_OF_CONDUCT.md).
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.
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HumanSql
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.3"
5
5
  end
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. The queries must be in a single line of code."
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 => e
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
- formatted_results = results.map do |result|
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
- formatted_results = results.respond_to?(:attributes) ? results.attributes.map { |key, value| "#{key}: #{value}" }.join(', ') : results.to_s
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]} 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.0
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-08 00:00:00.000000000 Z
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