rails-nl2sql 0.2.4 → 0.2.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ad0e3b00db210b1b61ef7b0081cc2d270929f9564e1b9431881389bbe5cf74a
4
- data.tar.gz: f1240e38861cc58b0754fe20276d399a9206c8280b3b01a54b4795eec0296222
3
+ metadata.gz: 32ceb33965fb596a339bbf54e8945aa3d03df7cd53224076263363694776f699
4
+ data.tar.gz: ad845d46819e09cf9b2ba74bf24529f4f370829f19a5b1564183ce06b6089188
5
5
  SHA512:
6
- metadata.gz: cef48fc2a9f8ea4ed06b6345f64963efe4342677901c1a35406b5296141539dfa6f4f33d7bd15727db9534f25a0df0f8673164f05a117dabae998dfa02519243
7
- data.tar.gz: 7aa4bd9311438ed6d77e423325ee64d5a4d79bedf1f8bbe4f54bef5d6ba3e0a63b2de2b5f5094252a95a06bf67c47e6cf5645a77eacd4fd0d645ed2eb1d5d802
6
+ metadata.gz: 00bf7f7132cefed3bfdcf1d3936b2264e495d8db545aac8551d72273454d9d1605e017cecfd1e1c09f6a4a9a7db625502601feb28ac5b5ada18f68e7fe34b584
7
+ data.tar.gz: efb583c44acee0a05c636f8f462bec47119dbf48e102c9736940e1ecd665ebc90bdde3fb57bf7dba9907e3dbd3e648c752b58196cdbe7af6eea896c85a188329
data/.DS_Store CHANGED
Binary file
data/.gitignore CHANGED
@@ -13,3 +13,6 @@
13
13
  /spec/rails_helper.rb
14
14
  /spec/spec_helper.rb
15
15
  CLAUDE.md
16
+ Gemfile.lock
17
+ /docs/*
18
+ /docs
data/CHANGELOG.md ADDED
@@ -0,0 +1,53 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+
11
+
12
+ ## [0.2.5] - 2025-07-18
13
+ ### Fixed
14
+ - Fixed the ERB Template Variable Binding Issue
15
+
16
+ ## [0.2.4] - 2025-07-18
17
+ ### Fixed
18
+ - Fixed undefined variable error in ERB template processing
19
+
20
+ ## [0.2.3] - 2025-07-18
21
+ ### Added
22
+ - Pluggable LLM provider system with support for OpenAI, Anthropic, and Llama
23
+ - Custom prompt templates via ERB
24
+ - Context window management with configurable schema line limits
25
+ - Comprehensive error handling and validation
26
+
27
+ ### Enhanced
28
+ - Improved security with query validation and banned keywords
29
+ - Better schema caching for performance
30
+ - Support for multiple database types (PostgreSQL, MySQL, SQLite, etc.)
31
+
32
+ ## [0.2.0] - 2025-07-17
33
+ ### Added
34
+ - ActiveRecord integration with `from_nl` method
35
+ - Schema caching for improved performance
36
+ - Multiple provider support (OpenAI, Anthropic, Llama)
37
+ - Configurable prompt templates
38
+ - Enhanced query validation and security
39
+
40
+ ### Changed
41
+ - Improved architecture with provider abstraction
42
+ - Better error handling and user feedback
43
+ - More robust SQL generation and validation
44
+
45
+ ## [0.1.0] - 2025-07-17
46
+ ### Added
47
+ - Initial release
48
+ - Basic natural language to SQL conversion
49
+ - OpenAI integration
50
+ - Rails generator for easy setup
51
+ - Query validation and security features
52
+ - Support for table inclusion/exclusion
53
+ - Basic schema introspection
data/Rakefile CHANGED
@@ -1,2 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
2
6
  task :default => :spec
@@ -1,7 +1,17 @@
1
1
  Rails::Nl2sql.configure do |config|
2
- config.api_key = "YOUR_API_KEY"
2
+ # Set your API key. It's recommended to use an environment variable for security.
3
+ config.api_key = ENV["OPENAI_API_KEY"]
4
+
5
+ # Optional: Set the model to use (default: "gpt-3.5-turbo-instruct")
3
6
  # config.model = "gpt-3.5-turbo-instruct"
7
+
8
+ # Optional: Set a custom provider (default: OpenAI)
4
9
  # config.provider = Rails::Nl2sql::Providers::OpenaiProvider.new(api_key: config.api_key)
5
- # config.prompt_template_path = Rails::Nl2sql.prompt_template_path
10
+ # config.provider = Rails::Nl2sql::Providers::AnthropicProvider.new(api_key: ENV["ANTHROPIC_API_KEY"])
11
+
12
+ # Optional: Set custom prompt template path
13
+ # config.prompt_template_path = Rails.root.join("config", "nl2sql_prompts.yml.erb")
14
+
15
+ # Optional: Limit schema lines to fit within model context window (default: 200)
6
16
  # config.max_schema_lines = 200
7
17
  end
@@ -1,7 +1,17 @@
1
1
  Rails::Nl2sql.configure do |config|
2
- config.api_key = "YOUR_API_KEY"
2
+ # Set your API key. It's recommended to use an environment variable for security.
3
+ config.api_key = ENV["OPENAI_API_KEY"]
4
+
5
+ # Optional: Set the model to use (default: "gpt-3.5-turbo-instruct")
3
6
  # config.model = "gpt-3.5-turbo-instruct"
7
+
8
+ # Optional: Set a custom provider (default: OpenAI)
4
9
  # config.provider = Rails::Nl2sql::Providers::OpenaiProvider.new(api_key: config.api_key)
5
- # config.prompt_template_path = Rails::Nl2sql.prompt_template_path
10
+ # config.provider = Rails::Nl2sql::Providers::AnthropicProvider.new(api_key: ENV["ANTHROPIC_API_KEY"])
11
+
12
+ # Optional: Set custom prompt template path
13
+ # config.prompt_template_path = Rails.root.join("config", "nl2sql_prompts.yml.erb")
14
+
15
+ # Optional: Limit schema lines to fit within model context window (default: 200)
6
16
  # config.max_schema_lines = 200
7
17
  end
@@ -78,9 +78,24 @@ module Rails
78
78
 
79
79
  def build_prompts(input, db_server, retrieved_context)
80
80
  template = Rails::Nl2sql.prompt_template
81
- template_binding = binding
82
- system_prompt = ERB.new(template['system']).result(template_binding)
83
- user_prompt = ERB.new(template['user']).result(template_binding)
81
+
82
+ # Create ERB context with explicit local variables
83
+ erb_context = Object.new
84
+ erb_context.instance_variable_set(:@input, input)
85
+ erb_context.instance_variable_set(:@db_server, db_server)
86
+ erb_context.instance_variable_set(:@retrieved_context, retrieved_context)
87
+
88
+ erb_context.define_singleton_method(:get_binding) do
89
+ binding
90
+ end
91
+
92
+ # Define accessor methods for the ERB template
93
+ erb_context.define_singleton_method(:input) { @input }
94
+ erb_context.define_singleton_method(:db_server) { @db_server }
95
+ erb_context.define_singleton_method(:retrieved_context) { @retrieved_context }
96
+
97
+ system_prompt = ERB.new(template['system']).result(erb_context.get_binding)
98
+ user_prompt = ERB.new(template['user']).result(erb_context.get_binding)
84
99
  [system_prompt, user_prompt]
85
100
  end
86
101
 
@@ -68,7 +68,13 @@ module Rails
68
68
  end
69
69
 
70
70
  def self.build_table_schema(table)
71
- columns = ActiveRecord::Base.connection.columns(table)
71
+ begin
72
+ columns = ActiveRecord::Base.connection.columns(table)
73
+ rescue => e
74
+ # Skip tables that can't be introspected (e.g., PostGIS system tables)
75
+ Rails.logger.debug "Skipping table #{table} due to introspection error: #{e.message}" if defined?(Rails)
76
+ return "-- Table #{table} skipped due to introspection error"
77
+ end
72
78
 
73
79
  schema = "CREATE TABLE #{table} (\n"
74
80
 
@@ -122,7 +128,22 @@ module Rails
122
128
  when :json
123
129
  "JSON"
124
130
  else
125
- column.sql_type || "TEXT"
131
+ # Handle PostGIS geometry types and other spatial types
132
+ sql_type = column.sql_type || "TEXT"
133
+ case sql_type.downcase
134
+ when /geometry/
135
+ "GEOMETRY"
136
+ when /geography/
137
+ "GEOGRAPHY"
138
+ when /point/
139
+ "POINT"
140
+ when /polygon/
141
+ "POLYGON"
142
+ when /linestring/
143
+ "LINESTRING"
144
+ else
145
+ sql_type
146
+ end
126
147
  end
127
148
  end
128
149
 
@@ -159,7 +180,34 @@ module Rails
159
180
  'sys'
160
181
  ]
161
182
 
162
- system_tables.any? { |sys_table| table.include?(sys_table) }
183
+ # PostGIS system tables
184
+ postgis_system_tables = [
185
+ 'geometry_columns',
186
+ 'geography_columns',
187
+ 'spatial_ref_sys',
188
+ 'raster_columns',
189
+ 'raster_overviews',
190
+ 'topology',
191
+ 'layer',
192
+ 'topology_layer'
193
+ ]
194
+
195
+ # PostgreSQL system schemas
196
+ pg_system_schemas = [
197
+ 'pg_',
198
+ 'information_schema'
199
+ ]
200
+
201
+ # Check regular system tables
202
+ return true if system_tables.any? { |sys_table| table.include?(sys_table) }
203
+
204
+ # Check PostGIS system tables
205
+ return true if postgis_system_tables.any? { |sys_table| table == sys_table }
206
+
207
+ # Check PostgreSQL system schemas
208
+ return true if pg_system_schemas.any? { |schema| table.start_with?(schema) }
209
+
210
+ false
163
211
  end
164
212
 
165
213
  # Legacy method for backward compatibility
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module Nl2sql
3
- VERSION = "0.2.4"
3
+ VERSION = "0.2.6"
4
4
  end
5
5
  end
data/lib/rails/nl2sql.rb CHANGED
@@ -39,10 +39,9 @@ module Rails
39
39
  end
40
40
 
41
41
  def self.prompt_template
42
- @prompt_template ||= begin
43
- erb = ERB.new(File.read(prompt_template_path))
44
- YAML.safe_load(erb.result)
45
- end
42
+ # Load the YAML template without evaluating ERB so we can
43
+ # interpolate variables later when building prompts.
44
+ @prompt_template ||= YAML.safe_load(File.read(prompt_template_path))
46
45
  end
47
46
 
48
47
  class Processor
data/rails-nl2sql.gemspec CHANGED
@@ -39,7 +39,8 @@ Gem::Specification.new do |spec|
39
39
  spec.add_dependency "openai", "~> 0.3"
40
40
  spec.add_dependency "anthropic", ">= 0.1"
41
41
  spec.add_development_dependency "bundler", ">= 2.0"
42
- spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "rake", "~> 13.0"
43
+ spec.add_development_dependency "rspec", "~> 3.0"
43
44
  spec.add_development_dependency "rspec-rails", "~> 6.0"
44
45
  spec.add_dependency "railties", ">= 6.0"
45
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-nl2sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Russell Van Curen
@@ -58,14 +58,28 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: '13.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '10.0'
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rspec-rails
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -105,6 +119,7 @@ extra_rdoc_files: []
105
119
  files:
106
120
  - ".DS_Store"
107
121
  - ".gitignore"
122
+ - CHANGELOG.md
108
123
  - Gemfile
109
124
  - Gemfile.lock
110
125
  - LICENSE.txt