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 +4 -4
- data/lib/human_sql/version.rb +1 -1
- data/lib/human_sql.rb +22 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e45103fa9637558a0eadf18130856bf6d53c2c33225315c80e90906962a68fd2
|
4
|
+
data.tar.gz: be46708a8723b7de39926eb7e8872d48466c70bd39700b1d36f7c1a01b1323a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd1cfee88517e5bb5613227f790a3396f6a4e05df8a28cb96a4fb85c4fb1606916995c72490f4af52e63bfda794c14c4430942499576c28390df62e25405a9fd
|
7
|
+
data.tar.gz: a6782bb601f3437ec6cf7ccd6b11893b51861431501c5f4b003200ab29080a6094261a2f6bdb90edd6977722bb2a3bcec68180f1415a24a989795398c8d01f2c
|
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.
|
123
138
|
PROMPT
|
124
139
|
|
125
140
|
call_openai_service(prompt)
|
126
141
|
end
|
127
142
|
end
|
128
|
-
end
|
143
|
+
end
|