human_sql 0.1.4 → 0.1.5

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: d43fde91937997b59b2239bd8669ecdffb020a7424007b48f1504aa0b3384221
4
+ data.tar.gz: 8f219654ff6f0371c76a98aff8e13573b651fcd42d689b8f7556b11a1f7c500d
5
5
  SHA512:
6
- metadata.gz: e39cece766a98e64e0f6bf59a2124348e15a6e970fbee4d25c6184a74508b839445cf9d00012d081240b6dfff4336ff19b12b1ac27c0a587ab212cdba57adbee
7
- data.tar.gz: 66b6ee8fb138967521063913ab8891f36b1950f01ce4dd733555d7cc58da40a3b6f72a1cf843b38a85546cd8f53b13625d2a6cef96d608f9292cc0356d691a28
6
+ metadata.gz: '0489de0620281892772e3e26918655e68e349d81858b803355dc33309fc2750dd80419ef6f537b259697a2258848fc5d11a9f0c0fc61051bdb91236a893400cf'
7
+ data.tar.gz: 0cb6a1eab9988336932a800193fd733f2baf713a4d31604852fea8bc031e0774f461bb724f65b37e1ae5fad39ea8c01f3684952c022c0d23d3453fc7d9b13cbe
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.5"
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
 
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.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Bertin
@@ -55,6 +55,7 @@ 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
58
59
  - lib/human_sql.rb
59
60
  - lib/human_sql/version.rb
60
61
  - sig/human_sql.rbs