ai_guardrails 1.2.0
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +149 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +528 -0
- data/Rakefile +12 -0
- data/lib/ai_guardrails/auto_correction.rb +85 -0
- data/lib/ai_guardrails/auto_fix.rb +85 -0
- data/lib/ai_guardrails/background_job.rb +47 -0
- data/lib/ai_guardrails/cache.rb +50 -0
- data/lib/ai_guardrails/cli.rb +17 -0
- data/lib/ai_guardrails/config.rb +13 -0
- data/lib/ai_guardrails/dsl.rb +101 -0
- data/lib/ai_guardrails/json_repair.rb +234 -0
- data/lib/ai_guardrails/logger.rb +45 -0
- data/lib/ai_guardrails/mock_model_client.rb +34 -0
- data/lib/ai_guardrails/provider/base_client.rb +19 -0
- data/lib/ai_guardrails/provider/factory.rb +20 -0
- data/lib/ai_guardrails/provider/openai_client.rb +43 -0
- data/lib/ai_guardrails/runner.rb +40 -0
- data/lib/ai_guardrails/safety_filter.rb +33 -0
- data/lib/ai_guardrails/schema_validator.rb +57 -0
- data/lib/ai_guardrails/version.rb +5 -0
- data/lib/ai_guardrails.rb +40 -0
- data/sig/ai_guardrails.rbs +4 -0
- metadata +122 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AiGuardrails
|
|
4
|
+
# SafetyFilter checks AI outputs for unsafe or unwanted content
|
|
5
|
+
class SafetyFilter
|
|
6
|
+
class UnsafeContentError < StandardError; end
|
|
7
|
+
|
|
8
|
+
# Initialize with optional list of banned words or regex patterns
|
|
9
|
+
def initialize(blocklist: [])
|
|
10
|
+
# convert all items to regex for easier matching
|
|
11
|
+
@blocklist = blocklist.map do |item|
|
|
12
|
+
item.is_a?(Regexp) ? item : /\b#{Regexp.escape(item)}\b/i
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Checks the content
|
|
17
|
+
# Raises UnsafeContentError if any blocked content is detected
|
|
18
|
+
def check!(content)
|
|
19
|
+
@blocklist.each do |pattern|
|
|
20
|
+
raise UnsafeContentError, "Unsafe content detected: #{pattern}" if content.match?(pattern)
|
|
21
|
+
end
|
|
22
|
+
true
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Returns boolean instead of raising
|
|
26
|
+
def safe?(content)
|
|
27
|
+
check!(content)
|
|
28
|
+
true
|
|
29
|
+
rescue UnsafeContentError
|
|
30
|
+
false
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "dry-validation"
|
|
4
|
+
|
|
5
|
+
module AiGuardrails
|
|
6
|
+
# The SchemaValidator builds a runtime validation contract
|
|
7
|
+
# based on a simple Ruby Hash schema (e.g. { name: :string, tags: [:string] }).
|
|
8
|
+
# It uses Dry::Validation under the hood and returns a uniform
|
|
9
|
+
# response format: [success?, result_or_errors].
|
|
10
|
+
class SchemaValidator
|
|
11
|
+
attr_reader :contract
|
|
12
|
+
|
|
13
|
+
def initialize(schema)
|
|
14
|
+
@contract = build_contract(schema)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Returns [success?, result_or_errors]
|
|
18
|
+
def validate(input)
|
|
19
|
+
result = contract.call(input)
|
|
20
|
+
if result.success?
|
|
21
|
+
[true, result.to_h]
|
|
22
|
+
else
|
|
23
|
+
[false, result.errors.to_h]
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
# Dynamically build Dry::Validation contract from simple hash
|
|
30
|
+
# rubocop:disable Metrics/MethodLength
|
|
31
|
+
def build_contract(schema_hash)
|
|
32
|
+
klass = Class.new(Dry::Validation::Contract) do
|
|
33
|
+
params do
|
|
34
|
+
schema_hash.each do |key, type|
|
|
35
|
+
case type
|
|
36
|
+
when :string
|
|
37
|
+
required(key).filled(:string)
|
|
38
|
+
when :integer
|
|
39
|
+
required(key).filled(:integer)
|
|
40
|
+
when :float
|
|
41
|
+
required(key).filled(:float)
|
|
42
|
+
when :boolean
|
|
43
|
+
required(key).filled(:bool)
|
|
44
|
+
when Array
|
|
45
|
+
# assume array of strings for now
|
|
46
|
+
required(key).array(:string)
|
|
47
|
+
else
|
|
48
|
+
raise ArgumentError, "Unsupported type #{type} for key #{key}"
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
klass.new
|
|
54
|
+
end
|
|
55
|
+
# rubocop:enable Metrics/MethodLength
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "ai_guardrails/version"
|
|
4
|
+
require_relative "ai_guardrails/config"
|
|
5
|
+
|
|
6
|
+
require_relative "ai_guardrails/schema_validator"
|
|
7
|
+
require_relative "ai_guardrails/json_repair"
|
|
8
|
+
require_relative "ai_guardrails/mock_model_client"
|
|
9
|
+
|
|
10
|
+
require_relative "ai_guardrails/provider/base_client"
|
|
11
|
+
require_relative "ai_guardrails/provider/openai_client"
|
|
12
|
+
require_relative "ai_guardrails/provider/factory"
|
|
13
|
+
|
|
14
|
+
require_relative "ai_guardrails/auto_correction"
|
|
15
|
+
require_relative "ai_guardrails/safety_filter"
|
|
16
|
+
require_relative "ai_guardrails/logger"
|
|
17
|
+
require_relative "ai_guardrails/runner"
|
|
18
|
+
require_relative "ai_guardrails/dsl"
|
|
19
|
+
require_relative "ai_guardrails/background_job"
|
|
20
|
+
require_relative "ai_guardrails/cli"
|
|
21
|
+
require_relative "ai_guardrails/cache"
|
|
22
|
+
require_relative "ai_guardrails/auto_fix"
|
|
23
|
+
|
|
24
|
+
# Main namespace for the AiGuardrails gem.
|
|
25
|
+
module AiGuardrails
|
|
26
|
+
class Error < StandardError; end
|
|
27
|
+
|
|
28
|
+
class << self
|
|
29
|
+
def config
|
|
30
|
+
@config ||= Config.new
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def configure
|
|
34
|
+
yield(config)
|
|
35
|
+
|
|
36
|
+
Logger.logger = config.logger
|
|
37
|
+
Logger.debug_mode = config.debug
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ai_guardrails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Faisal Raza
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-12-12 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: dry-validation
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.10'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.10'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rubocop
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '1.21'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.21'
|
|
55
|
+
description: |
|
|
56
|
+
AiGuardrails is a Ruby gem that helps developers validate, correct, and enforce schemas
|
|
57
|
+
on AI-generated outputs. It ensures structured data, prevents JSON errors, and provides
|
|
58
|
+
a foundation for adding safety filters and auto-correction in Rails apps, CLI tools,
|
|
59
|
+
background jobs, and scrapers. Think of it as Guardrails.AI for Ruby.
|
|
60
|
+
email:
|
|
61
|
+
- faisalraza.p@gmail.com
|
|
62
|
+
executables: []
|
|
63
|
+
extensions: []
|
|
64
|
+
extra_rdoc_files: []
|
|
65
|
+
files:
|
|
66
|
+
- ".rspec"
|
|
67
|
+
- ".rubocop.yml"
|
|
68
|
+
- CHANGELOG.md
|
|
69
|
+
- CODE_OF_CONDUCT.md
|
|
70
|
+
- LICENSE.txt
|
|
71
|
+
- README.md
|
|
72
|
+
- Rakefile
|
|
73
|
+
- lib/ai_guardrails.rb
|
|
74
|
+
- lib/ai_guardrails/auto_correction.rb
|
|
75
|
+
- lib/ai_guardrails/auto_fix.rb
|
|
76
|
+
- lib/ai_guardrails/background_job.rb
|
|
77
|
+
- lib/ai_guardrails/cache.rb
|
|
78
|
+
- lib/ai_guardrails/cli.rb
|
|
79
|
+
- lib/ai_guardrails/config.rb
|
|
80
|
+
- lib/ai_guardrails/dsl.rb
|
|
81
|
+
- lib/ai_guardrails/json_repair.rb
|
|
82
|
+
- lib/ai_guardrails/logger.rb
|
|
83
|
+
- lib/ai_guardrails/mock_model_client.rb
|
|
84
|
+
- lib/ai_guardrails/provider/base_client.rb
|
|
85
|
+
- lib/ai_guardrails/provider/factory.rb
|
|
86
|
+
- lib/ai_guardrails/provider/openai_client.rb
|
|
87
|
+
- lib/ai_guardrails/runner.rb
|
|
88
|
+
- lib/ai_guardrails/safety_filter.rb
|
|
89
|
+
- lib/ai_guardrails/schema_validator.rb
|
|
90
|
+
- lib/ai_guardrails/version.rb
|
|
91
|
+
- sig/ai_guardrails.rbs
|
|
92
|
+
homepage: https://github.com/logicbunchhq/ai_guardrails
|
|
93
|
+
licenses:
|
|
94
|
+
- MIT
|
|
95
|
+
metadata:
|
|
96
|
+
organization: LogicBunch
|
|
97
|
+
homepage_uri: https://github.com/logicbunchhq/ai_guardrails
|
|
98
|
+
source_code_uri: https://github.com/logicbunchhq/ai_guardrails
|
|
99
|
+
changelog_uri: https://github.com/logicbunchhq/ai_guardrails/blob/main/CHANGELOG.md
|
|
100
|
+
documentation_uri: https://github.com/logicbunchhq/ai_guardrails#readme
|
|
101
|
+
rubygems_keywords: AI, Guardrails, Ruby, LLM, JSON, Schema, Auto-correction, Safety
|
|
102
|
+
post_install_message:
|
|
103
|
+
rdoc_options: []
|
|
104
|
+
require_paths:
|
|
105
|
+
- lib
|
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: 3.0.0
|
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
|
+
requirements:
|
|
113
|
+
- - ">="
|
|
114
|
+
- !ruby/object:Gem::Version
|
|
115
|
+
version: '0'
|
|
116
|
+
requirements: []
|
|
117
|
+
rubygems_version: 3.5.16
|
|
118
|
+
signing_key:
|
|
119
|
+
specification_version: 4
|
|
120
|
+
summary: 'AiGuardrails: Schema validation and safety layer for AI-generated output
|
|
121
|
+
in Ruby'
|
|
122
|
+
test_files: []
|