human_sql 0.1.4 → 0.1.6

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: 2105327e3f06a71f6a2e87920bd571bd82e6c2d7c891a9bf939f685a9796a2dc
4
- data.tar.gz: 5012bb480c854b5b86a891130dd73ac938e7cf1138428a6918d76193836f8d9d
3
+ metadata.gz: 0b99e3383795c42236a802343a72f13c0de24240d35476a871d424bf3d87f795
4
+ data.tar.gz: 43f3c4d3a6f801bf1fb5109cbc58ec5f2fef9cfc14ac875a4a1dc5e9ea78930a
5
5
  SHA512:
6
- metadata.gz: e39cece766a98e64e0f6bf59a2124348e15a6e970fbee4d25c6184a74508b839445cf9d00012d081240b6dfff4336ff19b12b1ac27c0a587ab212cdba57adbee
7
- data.tar.gz: 66b6ee8fb138967521063913ab8891f36b1950f01ce4dd733555d7cc58da40a3b6f72a1cf843b38a85546cd8f53b13625d2a6cef96d608f9292cc0356d691a28
6
+ metadata.gz: 8e981540d9770d4b9db1ed0476c5417175ab813983c645ff351dc49aa3e1bbbd239ee36e2b6afd3effbd3387e04edbf417c098714c45c433f49aa5c6aa67ea32
7
+ data.tar.gz: b114d7dac96701c265a8fe52efc99b9039784a4e607c31540c9c0380f2c8bf7e901ee0387c65760f6eea3fa4651065100b6c1faee6dfe9b68ae0035ac7ce6dc2
Binary file
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HumanSql
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.6"
5
5
  end
data/lib/human_sql.rb CHANGED
@@ -7,7 +7,7 @@ require_relative 'human_sql/version'
7
7
  module HumanSQL
8
8
  class QueryBuilder
9
9
  OPENAI_API_URL = 'https://api.openai.com/v1/chat/completions'
10
-
10
+
11
11
  def initialize(user_input)
12
12
  @user_input = user_input
13
13
  @schema_content = File.read(Rails.root.join('db', 'schema.rb'))
@@ -19,8 +19,11 @@ module HumanSQL
19
19
 
20
20
  def generate_query
21
21
  prompt = build_query_prompt(@user_input, @schema_content)
22
+
22
23
  generated_query_response = call_openai_service(prompt)&.strip
23
- extract_active_record_query(generated_query_response)
24
+ extracted_query = extract_active_record_query(generated_query_response)
25
+
26
+ extracted_query
24
27
  end
25
28
 
26
29
  def get_results
@@ -32,6 +35,7 @@ module HumanSQL
32
35
 
33
36
  formatted_results = format_results_for_openai(results)
34
37
  natural_language_response = generate_natural_language_response(formatted_results, @user_input)
38
+
35
39
  natural_language_response
36
40
  rescue StandardError => e
37
41
  process_error_in_natural_language(e.message)
@@ -40,7 +44,7 @@ module HumanSQL
40
44
  private
41
45
 
42
46
  def build_query_prompt(user_input, schema_content)
43
- <<-PROMPT
47
+ prompt = <<-PROMPT
44
48
  The user has requested: "#{user_input}".
45
49
  This is the database schema:
46
50
 
@@ -49,11 +53,12 @@ module HumanSQL
49
53
  Please generate an ActiveRecord query based on this schema. The query should be in a single line of code and return the result according to the user's request.
50
54
  If it's necessary to access multiple related tables, prefer to use `includes` over `joins` to optimize data loading.
51
55
  PROMPT
56
+ prompt
52
57
  end
53
58
 
54
59
  def call_openai_service(prompt)
55
60
  body = {
56
- model: "gpt-4",
61
+ model: "gpt-4o-mini",
57
62
  messages: [
58
63
  {
59
64
  role: "system",
@@ -84,12 +89,14 @@ module HumanSQL
84
89
  response_body = JSON.parse(response.body)
85
90
  response_body.dig('choices', 0, 'message', 'content')
86
91
  else
87
- raise "Error in OpenAI API: #{response.code} - #{response.body}"
92
+ error_message = "Error in OpenAI API: #{response.code} - #{response.body}"
93
+ raise error_message
88
94
  end
89
95
  end
90
96
 
91
97
  def extract_active_record_query(response)
92
98
  return nil if response.blank?
99
+
93
100
  code_lines = response.lines.select { |line| line.strip.match(/^[A-Za-z]\w*\./) }
94
101
  return nil if code_lines.empty?
95
102
  code_lines.join("\n").strip
@@ -103,15 +110,17 @@ module HumanSQL
103
110
 
104
111
  def format_results_for_openai(results)
105
112
  if results.is_a?(ActiveRecord::Relation) || results.is_a?(Array)
106
- results.map do |result|
113
+ formatted = results.map do |result|
107
114
  if result.respond_to?(:attributes)
108
115
  result.attributes.map { |key, value| "#{key}: #{value}" }.join(', ')
109
116
  else
110
117
  result.to_s
111
118
  end
112
119
  end.join("\\n")
120
+ formatted
113
121
  else
114
- results.respond_to?(:attributes) ? results.attributes.map { |key, value| "#{key}: #{value}" }.join(', ') : results.to_s
122
+ formatted = results.respond_to?(:attributes) ? results.attributes.map { |key, value| "#{key}: #{value}" }.join(', ') : results.to_s
123
+ formatted
115
124
  end
116
125
  end
117
126
 
@@ -123,6 +132,7 @@ module HumanSQL
123
132
  #{formatted_results}
124
133
 
125
134
  Please generate a natural language description that clearly and understandably explains these results to the user in #{HumanSQLConfig[:default_language]}.
135
+ Do not ask for confirmation, just confirm what has already been done.
126
136
  PROMPT
127
137
 
128
138
  call_openai_service(prompt)
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.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Bertin
@@ -55,6 +55,8 @@ files:
55
55
  - human_sql-0.1.1.gem
56
56
  - human_sql-0.1.2.gem
57
57
  - human_sql-0.1.3.gem
58
+ - human_sql-0.1.4.gem
59
+ - human_sql-0.1.5.gem
58
60
  - lib/human_sql.rb
59
61
  - lib/human_sql/version.rb
60
62
  - sig/human_sql.rbs