human_sql 0.1.0 → 0.1.1

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: e45103fa9637558a0eadf18130856bf6d53c2c33225315c80e90906962a68fd2
4
+ data.tar.gz: be46708a8723b7de39926eb7e8872d48466c70bd39700b1d36f7c1a01b1323a0
5
5
  SHA512:
6
- metadata.gz: a0fa556ecb44c2151999eb741a128fc00846d645e31652940c292a4fe2172df3a9088a5b470ec68f352f0d5675633e4ed078fcadd427e18752e92cf90244646d
7
- data.tar.gz: a712c3f78aa08d7c86ddcbd4e7c83e5abf9fde5cb6fca298fdbec80d977bd6bf89fd0cb49ab75f52852b647519a9e0142066a03d5197f7aaf460ad4f27efc767
6
+ metadata.gz: cd1cfee88517e5bb5613227f790a3396f6a4e05df8a28cb96a4fb85c4fb1606916995c72490f4af52e63bfda794c14c4430942499576c28390df62e25405a9fd
7
+ data.tar.gz: a6782bb601f3437ec6cf7ccd6b11893b51861431501c5f4b003200ab29080a6094261a2f6bdb90edd6977722bb2a3bcec68180f1415a24a989795398c8d01f2c
@@ -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.1"
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.
123
138
  PROMPT
124
139
 
125
140
  call_openai_service(prompt)
126
141
  end
127
142
  end
128
- end
143
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Bertin