human_sql 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/human_sql-0.1.7.gem +0 -0
- data/lib/human_sql/version.rb +1 -1
- data/lib/human_sql.rb +53 -3
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a61925cacbf601619d70681bbb66384d911689155fe732e23dc2f848003008e5
|
4
|
+
data.tar.gz: 9e69254fada3069ceab0c720b4f11f79ebff12047ce329058ca3c31829b9b6be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62e3320021cc7aec5b26cc658cedd12aef67a9147219962f618065e067a8a4e962cfffb5354902c98644f3ea1b79e4cc42aba4ffbbfb2045db2f74eef8429ab7
|
7
|
+
data.tar.gz: daa62db81b58e300f8cb8fb073363ee7f2875ab52d45c792f07d41ef2ad484c8a207f123bea52e1989fc22ded04ac25f83584106f489e7e005aa40d01ae1eec3
|
data/human_sql-0.1.7.gem
ADDED
Binary file
|
data/lib/human_sql/version.rb
CHANGED
data/lib/human_sql.rb
CHANGED
@@ -7,10 +7,10 @@ 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
|
-
@schema_content =
|
13
|
+
@schema_content = summarize_schema_with_ignored_tables
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.run(user_input)
|
@@ -43,6 +43,54 @@ module HumanSQL
|
|
43
43
|
|
44
44
|
private
|
45
45
|
|
46
|
+
def summarize_schema_with_ignored_tables
|
47
|
+
schema_content = File.read(Rails.root.join('db', 'schema.rb'))
|
48
|
+
ignored_tables = HumanSQLConfig[:ignored_tables] || []
|
49
|
+
|
50
|
+
schema_summary = {}
|
51
|
+
schema_content.split("\n").each do |line|
|
52
|
+
if line.strip.start_with?("create_table")
|
53
|
+
table_name = line.match(/"([^"]+)"/)[1]
|
54
|
+
next if ignored_tables.include?(table_name)
|
55
|
+
|
56
|
+
schema_summary[table_name] = { columns: [], has_many: [], belongs_to: [] }
|
57
|
+
end
|
58
|
+
|
59
|
+
current_table = schema_summary.keys.last if schema_summary.any?
|
60
|
+
|
61
|
+
if line.strip.match?(/^t\.(\w+)\s+"([^"]+)"/) && current_table
|
62
|
+
next if ignored_tables.include?(current_table)
|
63
|
+
|
64
|
+
column_type = line.strip.match(/^t\.(\w+)\s+"([^"]+)"/)[1]
|
65
|
+
column_name = line.strip.match(/^t\.(\w+)\s+"([^"]+)"/)[2]
|
66
|
+
schema_summary[current_table][:columns] << "#{column_name}=#{column_type}"
|
67
|
+
end
|
68
|
+
|
69
|
+
if line.strip.start_with?("add_foreign_key")
|
70
|
+
match_data = line.strip.match(/"([^"]+)", "([^"]+)"(?:, column: "([^"]+)")?/)
|
71
|
+
if match_data
|
72
|
+
parent_table = match_data[1]
|
73
|
+
child_table = match_data[2]
|
74
|
+
next if ignored_tables.include?(parent_table) || ignored_tables.include?(child_table)
|
75
|
+
|
76
|
+
schema_summary[child_table] ||= { columns: [], has_many: [], belongs_to: [] }
|
77
|
+
schema_summary[parent_table] ||= { columns: [], has_many: [], belongs_to: [] }
|
78
|
+
schema_summary[child_table][:belongs_to] << parent_table
|
79
|
+
schema_summary[parent_table][:has_many] << child_table
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
schema_summary.map do |table, details|
|
85
|
+
columns = details[:columns].join(",")
|
86
|
+
has_many = details[:has_many].join(",")
|
87
|
+
belongs_to = details[:belongs_to].join(",")
|
88
|
+
%Q{#{table}={#{columns},:has_many=>[#{has_many}],:belongs_to=>[#{belongs_to}]}}
|
89
|
+
end.join("\n")
|
90
|
+
rescue StandardError => e
|
91
|
+
raise "Error processing schema: #{e.message}"
|
92
|
+
end
|
93
|
+
|
46
94
|
def build_query_prompt(user_input, schema_content)
|
47
95
|
prompt = <<-PROMPT
|
48
96
|
The user has requested: "#{user_input}".
|
@@ -51,8 +99,10 @@ module HumanSQL
|
|
51
99
|
#{schema_content}
|
52
100
|
|
53
101
|
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.
|
54
|
-
If it's necessary to access multiple related tables, prefer to use
|
102
|
+
If it's necessary to access multiple related tables, prefer to use preload to optimize data loading.
|
55
103
|
If the user indicates a date and time type field, only the value of the indicated date or time is saved without considering the time zone.
|
104
|
+
|
105
|
+
#{HumanSQLConfig[:additional_instructions]}
|
56
106
|
PROMPT
|
57
107
|
prompt
|
58
108
|
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.
|
4
|
+
version: 0.1.8
|
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-
|
11
|
+
date: 2024-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -58,6 +58,7 @@ files:
|
|
58
58
|
- human_sql-0.1.4.gem
|
59
59
|
- human_sql-0.1.5.gem
|
60
60
|
- human_sql-0.1.6.gem
|
61
|
+
- human_sql-0.1.7.gem
|
61
62
|
- lib/human_sql.rb
|
62
63
|
- lib/human_sql/version.rb
|
63
64
|
- sig/human_sql.rbs
|
@@ -83,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
84
|
- !ruby/object:Gem::Version
|
84
85
|
version: '0'
|
85
86
|
requirements: []
|
86
|
-
rubygems_version: 3.5.
|
87
|
+
rubygems_version: 3.5.22
|
87
88
|
signing_key:
|
88
89
|
specification_version: 4
|
89
90
|
summary: A gem to convert natural language to ActiveRecord queries using OpenAI
|