prompt_schema 0.1.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 +6 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +166 -0
- data/Rakefile +12 -0
- data/lib/prompt_schema/prompt.rb +106 -0
- data/lib/prompt_schema/schema.rb +20 -0
- data/lib/prompt_schema/schema_compiler.rb +196 -0
- data/lib/prompt_schema/type_schema_compiler.rb +116 -0
- data/lib/prompt_schema/version.rb +5 -0
- data/lib/prompt_schema.rb +42 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 62c2a73666e89f46d35417027681166656c91a54481f283d913dd9f27dcab593
|
4
|
+
data.tar.gz: 63bc30999ca378e39ede7bb33954341d6cd3e3d5c5aa5c629de717b94fd8ccd6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 27f80256717d64e83e07f68dec2ced8c0b0d751a63ac9394ea0f36a70f9c8a29eecebbfcbe0d04a3c03f2611c06d665f21adb0ce63f93c8456aa3a1d4f235c7c
|
7
|
+
data.tar.gz: 49d75e170c6005f665374c5f1e7b052f5cb5137a24cb729fefd8aa172dfb6a3e09d76dbcd03c87cedb62c49063309ce6030eb7ae542c069f1693538a36aa7221
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Nolan J Tait
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
# PromptSchema
|
2
|
+
|
3
|
+
This is a library to give you BAML style JSON schemas that you can feed to an
|
4
|
+
LLM using your existing `dry-schema` with annotated types.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Install the gem and add to the application's Gemfile by executing:
|
9
|
+
|
10
|
+
```bash
|
11
|
+
bundle add prompt_schema
|
12
|
+
```
|
13
|
+
|
14
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
15
|
+
|
16
|
+
```bash
|
17
|
+
gem install prompt_schema
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Schemas are defined as a class that wraps a regular `dry-schema`:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
schema = Dry::Schema.JSON do
|
26
|
+
required(:user).hash do
|
27
|
+
required(:email).maybe(:string)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
prompt_schema = PromptSchema::Schema.new(schema)
|
32
|
+
```
|
33
|
+
|
34
|
+
For convenience you can use `define` to have this wrapping be done for you:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
schema = PromptSchema.define do
|
38
|
+
required(:user).hash do
|
39
|
+
required(:email).maybe(:string)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
schema.is_a?(PromptSchema::Schema) #=> true
|
44
|
+
schema.dry_schema.is_a?(Dry::Schema) #=> true
|
45
|
+
|
46
|
+
result = schema.call({ user: { email: "email@example.com" }})
|
47
|
+
result.success? #=> true
|
48
|
+
```
|
49
|
+
|
50
|
+
Using this schema we can print a ready to go prompt.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
schema.prompt
|
54
|
+
```
|
55
|
+
|
56
|
+
Which would return a string of a BAML style prompt:
|
57
|
+
|
58
|
+
```
|
59
|
+
Answer in JSON using this schema:
|
60
|
+
{
|
61
|
+
user: {
|
62
|
+
email: string or null,
|
63
|
+
},
|
64
|
+
}
|
65
|
+
```
|
66
|
+
|
67
|
+
To get the descriptions we can annotate our own types using `dry-types`:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
module Types
|
71
|
+
include Dry.Types()
|
72
|
+
|
73
|
+
Email = Types::String.meta(
|
74
|
+
description: "An email address",
|
75
|
+
example: "email@example.com"
|
76
|
+
)
|
77
|
+
end
|
78
|
+
|
79
|
+
schema = PromptSchema.define do
|
80
|
+
required(:user).hash do
|
81
|
+
required(:email).maybe(Types::Email)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
schema.prompt
|
86
|
+
```
|
87
|
+
|
88
|
+
Which would now annotate the field
|
89
|
+
|
90
|
+
```
|
91
|
+
Answer in JSON using this schema:
|
92
|
+
{
|
93
|
+
user: {
|
94
|
+
// An email address
|
95
|
+
// @example email@example.com
|
96
|
+
email: string or null,
|
97
|
+
},
|
98
|
+
}
|
99
|
+
```
|
100
|
+
|
101
|
+
This can be useful to get the best parts of BAML in a ruby native way.
|
102
|
+
|
103
|
+
## How it works
|
104
|
+
|
105
|
+
All `Dry::Schema` objects have an Abstract Syntax Tree (AST) that we can tap
|
106
|
+
into to build another representation.
|
107
|
+
|
108
|
+
We turn the dry schema AST (and its corresponding type schema AST) into
|
109
|
+
a structure that looks like this:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
schema = Dry::Schema.Params do
|
113
|
+
required(:user).hash do
|
114
|
+
required(:email).value(Types::Email)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
compiled = PromptSchema.compile(schema)
|
119
|
+
|
120
|
+
expected = {
|
121
|
+
keys: {
|
122
|
+
user: {
|
123
|
+
type: "hash",
|
124
|
+
required: true,
|
125
|
+
nullable: false,
|
126
|
+
keys: {
|
127
|
+
email: {
|
128
|
+
type: "string",
|
129
|
+
required: true,
|
130
|
+
nullable: false,
|
131
|
+
example: "email@example.com",
|
132
|
+
description: "An email address"
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
compiled == expected #=> true
|
140
|
+
```
|
141
|
+
|
142
|
+
Then we use a renderer to take this compiled schema and output a string that can
|
143
|
+
be used as part of a prompt.
|
144
|
+
|
145
|
+
Rendering of the prompt is handled through a Phlex component. You could take
|
146
|
+
this same representation and write your own version.
|
147
|
+
|
148
|
+
## Development
|
149
|
+
|
150
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
151
|
+
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
152
|
+
prompt that will allow you to experiment.
|
153
|
+
|
154
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
155
|
+
release a new version, update the version number in `version.rb`, and then run
|
156
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
157
|
+
git commits and the created tag, and push the `.gem` file to
|
158
|
+
[rubygems.org](https://rubygems.org).
|
159
|
+
|
160
|
+
## Contributing
|
161
|
+
|
162
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nolantait/prompt_schema.
|
163
|
+
|
164
|
+
## License
|
165
|
+
|
166
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PromptSchema
|
4
|
+
# Generates a prompt for an LLM based on a compiled schema.
|
5
|
+
class Prompt < Phlex::HTML
|
6
|
+
# @param schema [Hash] The compiled schema to generate the prompt for.
|
7
|
+
# This should be the output of `PromptSchema::SchemaCompiler.call`.
|
8
|
+
# @example
|
9
|
+
# PromptSchema::Prompt.new(schema).call
|
10
|
+
# @return [String] The generated prompt.
|
11
|
+
def initialize(schema)
|
12
|
+
@compiled = SchemaCompiler.call(schema)
|
13
|
+
@indentation = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def view_template
|
17
|
+
text "Answer in JSON using this schema:"
|
18
|
+
render_structure(@compiled[:keys])
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def render_structure(keys, nullable: false, nested: false)
|
24
|
+
closing = [
|
25
|
+
"}",
|
26
|
+
(" or null" if nullable),
|
27
|
+
("," if nested)
|
28
|
+
].compact.join
|
29
|
+
|
30
|
+
inside("{", closing) do
|
31
|
+
render_keys(keys)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def render_keys(keys)
|
36
|
+
keys.each do |key, info|
|
37
|
+
item(key, info)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def item(key, info)
|
42
|
+
comment info[:description] if info[:description]
|
43
|
+
example info[:example] if info[:example]
|
44
|
+
handle_type(key, info)
|
45
|
+
end
|
46
|
+
|
47
|
+
def handle_type(key, info) # rubocop:disable Metrics/AbcSize
|
48
|
+
case info[:type]
|
49
|
+
when "array"
|
50
|
+
if info[:member].is_a?(Hash)
|
51
|
+
inside("#{key}: [", "],") do
|
52
|
+
render_structure(
|
53
|
+
info[:member][:keys],
|
54
|
+
nullable: info[:nullable],
|
55
|
+
nested: true
|
56
|
+
)
|
57
|
+
end
|
58
|
+
else
|
59
|
+
text("#{key}: #{info[:member]}[]")
|
60
|
+
end
|
61
|
+
when "hash"
|
62
|
+
inside("#{key}: {", "},") do
|
63
|
+
render_keys(info[:keys])
|
64
|
+
end
|
65
|
+
else
|
66
|
+
text("#{key}: ", newline: false)
|
67
|
+
|
68
|
+
types = [info[:type], ("null" if info[:nullable])]
|
69
|
+
|
70
|
+
text(
|
71
|
+
types.compact.join(" or "),
|
72
|
+
newline: false,
|
73
|
+
indent: false
|
74
|
+
)
|
75
|
+
|
76
|
+
text(",", indent: false)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def inside(opening, closing, &)
|
81
|
+
text opening
|
82
|
+
with_indentation(&)
|
83
|
+
text closing
|
84
|
+
end
|
85
|
+
|
86
|
+
def with_indentation
|
87
|
+
@indentation += 2
|
88
|
+
yield
|
89
|
+
@indentation -= 2
|
90
|
+
end
|
91
|
+
|
92
|
+
def comment(content, **)
|
93
|
+
text("// #{content}", **)
|
94
|
+
end
|
95
|
+
|
96
|
+
def example(content, **)
|
97
|
+
comment("@example #{content}", **)
|
98
|
+
end
|
99
|
+
|
100
|
+
def text(content, newline: true, indent: true)
|
101
|
+
plain " " * @indentation if indent
|
102
|
+
raw safe(content)
|
103
|
+
plain "\n" if newline
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PromptSchema
|
4
|
+
# Represents a Dry::Schema schema, providing methods to access its
|
5
|
+
# properties and to generate prompts based on the schema.
|
6
|
+
class Schema
|
7
|
+
attr_reader :dry_schema
|
8
|
+
|
9
|
+
def initialize(dry_schema)
|
10
|
+
@dry_schema = dry_schema
|
11
|
+
end
|
12
|
+
|
13
|
+
def [](...) = @dry_schema.[](...)
|
14
|
+
def call(...) = @dry_schema.call(...)
|
15
|
+
|
16
|
+
def prompt
|
17
|
+
Prompt.call(@dry_schema)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,196 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PromptSchema
|
4
|
+
# Compiles a Dry::Schema schema into a hash representation that can be used
|
5
|
+
# to generate prompts for LLMs.
|
6
|
+
class SchemaCompiler # rubocop:disable Metrics/ClassLength
|
7
|
+
EMPTY_HASH = {}.freeze
|
8
|
+
|
9
|
+
PREDICATE_TO_TYPE = {
|
10
|
+
array?: "array",
|
11
|
+
bool?: "bool",
|
12
|
+
date?: "date",
|
13
|
+
date_time?: "date_time",
|
14
|
+
decimal?: "float",
|
15
|
+
float?: "float",
|
16
|
+
hash?: "hash",
|
17
|
+
int?: "integer",
|
18
|
+
nil?: "nil",
|
19
|
+
str?: "string",
|
20
|
+
time?: "time"
|
21
|
+
}.freeze
|
22
|
+
|
23
|
+
attr_reader :keys
|
24
|
+
|
25
|
+
def self.call(schema) = new(schema).call
|
26
|
+
|
27
|
+
# @param schema [Dry::Schema::Schema] The schema to compile.
|
28
|
+
def initialize(schema)
|
29
|
+
@schema = schema
|
30
|
+
@type_schema = TypeSchemaCompiler.call(schema.type_schema.to_ast)
|
31
|
+
@keys = EMPTY_HASH.dup
|
32
|
+
end
|
33
|
+
|
34
|
+
# @return [Hash] The compiled schema as a hash.
|
35
|
+
def to_h
|
36
|
+
{ keys: keys }
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Hash] The compiled schema as a hash
|
40
|
+
def call
|
41
|
+
visit(@schema.to_ast)
|
42
|
+
to_h
|
43
|
+
end
|
44
|
+
|
45
|
+
# @param node [Array] The AST node to visit.
|
46
|
+
# @param opts [Hash] Options for visiting the node.
|
47
|
+
# @return [void]
|
48
|
+
def visit(node, **)
|
49
|
+
meth, rest = node
|
50
|
+
public_send(:"visit_#{meth}", rest, **)
|
51
|
+
end
|
52
|
+
|
53
|
+
# @param node [Array] The AST node to visit.
|
54
|
+
# @param opts [Hash] Options for visiting the node.
|
55
|
+
# @return [void]
|
56
|
+
def visit_set(node, **opts)
|
57
|
+
key = opts[:key]
|
58
|
+
target = key ? self.class.new(@schema) : self
|
59
|
+
|
60
|
+
node.each { |child| target.visit(child, **opts.except(:member)) }
|
61
|
+
|
62
|
+
return unless key
|
63
|
+
|
64
|
+
target_info = opts[:member] ? { member: target.to_h } : target.to_h
|
65
|
+
type = opts[:member] ? "array" : "hash"
|
66
|
+
data = { **keys[key], type:, **target_info }
|
67
|
+
|
68
|
+
keys.update(key => data)
|
69
|
+
end
|
70
|
+
|
71
|
+
# @param node [Array] The AST node to visit.
|
72
|
+
# @param opts [Hash] Options for visiting the node.
|
73
|
+
# @return [void]
|
74
|
+
def visit_and(node, **)
|
75
|
+
left, right = node
|
76
|
+
|
77
|
+
visit(left, **)
|
78
|
+
visit(right, **)
|
79
|
+
end
|
80
|
+
|
81
|
+
# @param node [Array] The AST node to visit.
|
82
|
+
# @param opts [Hash] Options for visiting the node.
|
83
|
+
# @return [void]
|
84
|
+
def visit_implication(node, **)
|
85
|
+
case node
|
86
|
+
in [:not, [:predicate, [:nil?, _]]], el
|
87
|
+
visit(el, **, nullable: true)
|
88
|
+
else
|
89
|
+
node.each do |el|
|
90
|
+
visit(el, **, required: false)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# @param node [Array] The AST node to visit.
|
96
|
+
# @param opts [Hash] Options for visiting the node.
|
97
|
+
# @return [void]
|
98
|
+
def visit_each(node, **opts)
|
99
|
+
opts[:key_path] ||= []
|
100
|
+
opts[:key_path] << :items
|
101
|
+
|
102
|
+
visit(node, **opts, member: true)
|
103
|
+
end
|
104
|
+
|
105
|
+
# @param node [Array] The AST node to visit.
|
106
|
+
# @param opts [Hash] Options for visiting the node.
|
107
|
+
# @return [void]
|
108
|
+
def visit_key(node, **opts)
|
109
|
+
name, rest = node
|
110
|
+
opts[:key_path] ||= []
|
111
|
+
opts[:key_path] << name
|
112
|
+
|
113
|
+
visit(rest, **opts, key: name, required: true)
|
114
|
+
opts[:key_path].pop
|
115
|
+
end
|
116
|
+
|
117
|
+
# @param node [Array] The AST node to visit.
|
118
|
+
# @param opts [Hash] Options for visiting the node.
|
119
|
+
# @return [void]
|
120
|
+
def visit_predicate(node, **opts)
|
121
|
+
name, rest = node
|
122
|
+
key = opts[:key]
|
123
|
+
|
124
|
+
case name
|
125
|
+
when :key?
|
126
|
+
visit_predicate_key(rest, **opts)
|
127
|
+
when :type?
|
128
|
+
visit_predicate_type(rest, **opts)
|
129
|
+
else
|
130
|
+
visit_predicate_base_type(name, key, **opts)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# @param name [Symbol] The name of the predicate.
|
135
|
+
# @param key [Symbol] The key to assign the type to.
|
136
|
+
# @param opts [Hash] Additional options for assigning the type.
|
137
|
+
# @return [void]
|
138
|
+
def visit_predicate_base_type(name, key, **opts)
|
139
|
+
type = PREDICATE_TO_TYPE[name]
|
140
|
+
return unless type
|
141
|
+
|
142
|
+
assign_type(
|
143
|
+
key,
|
144
|
+
type,
|
145
|
+
**opts,
|
146
|
+
nullable: opts.fetch(:nullable, false)
|
147
|
+
)
|
148
|
+
end
|
149
|
+
|
150
|
+
# @param node [Array] The AST node to visit.
|
151
|
+
# @param opts [Hash] Options for visiting the node.
|
152
|
+
# @return [void]
|
153
|
+
def visit_predicate_key(node, **opts)
|
154
|
+
(_, name), = node
|
155
|
+
keys[name] ||= {}
|
156
|
+
keys[name] = { **keys[name], required: opts.fetch(:required, true) }
|
157
|
+
end
|
158
|
+
|
159
|
+
# @param node [Array] The AST node to visit.
|
160
|
+
# @param opts [Hash] Options for visiting the node.
|
161
|
+
# @return [void]
|
162
|
+
def visit_predicate_type(node, **opts)
|
163
|
+
(_type, type_class), _input = node
|
164
|
+
key = opts[:key]
|
165
|
+
|
166
|
+
if type_class.is_a?(Dry::Types::Type)
|
167
|
+
keys[key].merge!(type_class.meta)
|
168
|
+
|
169
|
+
assign_type(
|
170
|
+
key,
|
171
|
+
type_class.name.downcase,
|
172
|
+
**opts,
|
173
|
+
nullable: opts.fetch(:nullable, false)
|
174
|
+
)
|
175
|
+
else
|
176
|
+
visit(type_class.to_ast, **opts, key: key, member: true)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
# @param key [Symbol] The key to assign the type to.
|
181
|
+
# @param type [String] The type to assign to the key.
|
182
|
+
# @param opts [Hash] Additional options for assigning the type.
|
183
|
+
# @return [void]
|
184
|
+
def assign_type(key, type, **opts)
|
185
|
+
if keys.dig(key, :type)
|
186
|
+
keys[key][:member] = type
|
187
|
+
else
|
188
|
+
keys[key][:type] = type
|
189
|
+
keys[key][:nullable] = opts.fetch(:nullable, false)
|
190
|
+
end
|
191
|
+
|
192
|
+
meta = @type_schema.dig(*opts[:key_path])
|
193
|
+
keys[key].merge!(meta.slice(:description, :example)) if meta
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PromptSchema
|
4
|
+
# Compiles a Dry::Types AST into a type schema hash. This is required because
|
5
|
+
# we need to get the specific metadata information which is only available in
|
6
|
+
# the Dry::Types AST, not in the Dry::Schema AST.
|
7
|
+
class TypeSchemaCompiler
|
8
|
+
def self.call(ast) = new.call(ast)
|
9
|
+
|
10
|
+
def call(ast)
|
11
|
+
visit(ast)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def visit(node)
|
17
|
+
type, body = node
|
18
|
+
send("visit_#{type}", body)
|
19
|
+
end
|
20
|
+
|
21
|
+
def visit_schema(node)
|
22
|
+
keys, _options, _meta = node
|
23
|
+
keys.to_h { |key| visit(key) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def visit_key(node)
|
27
|
+
name, required, type_node = node
|
28
|
+
type_info = visit(type_node)
|
29
|
+
[name, { required: required, **type_info }]
|
30
|
+
end
|
31
|
+
|
32
|
+
def visit_nominal(node)
|
33
|
+
klass, meta = node
|
34
|
+
{
|
35
|
+
type: dry_type_name(klass),
|
36
|
+
**meta
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
def visit_maybe(node)
|
41
|
+
inner = visit(node)
|
42
|
+
inner[:type] = "maybe.#{inner[:type]}"
|
43
|
+
inner
|
44
|
+
end
|
45
|
+
|
46
|
+
def visit_array(node)
|
47
|
+
member_node, meta = node
|
48
|
+
|
49
|
+
member_info = if member_node.is_a?(Array) && member_node.first == :schema
|
50
|
+
visit_schema(member_node.last)
|
51
|
+
elsif member_node.is_a?(Class)
|
52
|
+
{ type: dry_type_name(member_node) }
|
53
|
+
else
|
54
|
+
visit(member_node)
|
55
|
+
end
|
56
|
+
|
57
|
+
{
|
58
|
+
type: "array",
|
59
|
+
items: member_info,
|
60
|
+
**meta
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
def visit_sum(node)
|
65
|
+
*types, meta = node
|
66
|
+
type_names = types.map { |t| visit(t)[:type] }
|
67
|
+
{
|
68
|
+
type: type_names.join(" | "),
|
69
|
+
**meta
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
def visit_map(node)
|
74
|
+
key_type, value_type, meta = node
|
75
|
+
{
|
76
|
+
type: "map[#{visit(key_type)[:type]} => #{visit(value_type)[:type]}]",
|
77
|
+
**meta
|
78
|
+
}
|
79
|
+
end
|
80
|
+
|
81
|
+
def visit_constrained(node)
|
82
|
+
base_type, rule = node
|
83
|
+
base = visit(base_type)
|
84
|
+
base[:rule] = rule
|
85
|
+
base
|
86
|
+
end
|
87
|
+
|
88
|
+
def visit_constructor(node)
|
89
|
+
base_type, _fn = node
|
90
|
+
visit(base_type)
|
91
|
+
end
|
92
|
+
|
93
|
+
def visit_lax(node)
|
94
|
+
visit(node)
|
95
|
+
end
|
96
|
+
|
97
|
+
def visit_enum(node)
|
98
|
+
type_node, mapping = node
|
99
|
+
{
|
100
|
+
type: "enum(#{visit(type_node)[:type]})",
|
101
|
+
values: mapping.values
|
102
|
+
}
|
103
|
+
end
|
104
|
+
|
105
|
+
def visit_any(meta)
|
106
|
+
{
|
107
|
+
type: "any",
|
108
|
+
**meta
|
109
|
+
}
|
110
|
+
end
|
111
|
+
|
112
|
+
def dry_type_name(klass)
|
113
|
+
Dry::Types.identifier(klass) || klass.name || klass.to_s
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "debug"
|
4
|
+
|
5
|
+
require "dry/schema"
|
6
|
+
require "dry/types"
|
7
|
+
require "phlex"
|
8
|
+
|
9
|
+
require_relative "prompt_schema/version"
|
10
|
+
require_relative "prompt_schema/type_schema_compiler"
|
11
|
+
require_relative "prompt_schema/schema_compiler"
|
12
|
+
require_relative "prompt_schema/schema"
|
13
|
+
require_relative "prompt_schema/prompt"
|
14
|
+
|
15
|
+
module PromptSchema
|
16
|
+
class Error < StandardError; end
|
17
|
+
|
18
|
+
# Defines a schema using Dry::Schema and returns a Schema instance.
|
19
|
+
#
|
20
|
+
# @example
|
21
|
+
# PromptSchema.define do
|
22
|
+
# required(:name).filled(:string)
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# @return [PromptSchema::Schema]
|
26
|
+
def self.define(&)
|
27
|
+
dry_schema = Dry::Schema.JSON(&)
|
28
|
+
Schema.new(dry_schema)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Compiles a schema into a format suitable for generating prompts.
|
32
|
+
# This is a wrapper around `PromptSchema::SchemaCompiler.call`.
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# PromptSchema.compile(schema)
|
36
|
+
#
|
37
|
+
# @param schema [Dry::Schema::Schema] The schema to compile.
|
38
|
+
# @return [Hash] The compiled schema as a hash.
|
39
|
+
def self.compile(schema)
|
40
|
+
SchemaCompiler.call(schema)
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prompt_schema
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nolan J Tait
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: dry-schema
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '1'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '1'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: dry-types
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1'
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: phlex
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2'
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2'
|
54
|
+
description: Generate BAML style prompts from dry-schema that can get and check structured
|
55
|
+
responses from LLMs
|
56
|
+
email:
|
57
|
+
- nolanjtait@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".rspec"
|
63
|
+
- ".rubocop.yml"
|
64
|
+
- CHANGELOG.md
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/prompt_schema.rb
|
69
|
+
- lib/prompt_schema/prompt.rb
|
70
|
+
- lib/prompt_schema/schema.rb
|
71
|
+
- lib/prompt_schema/schema_compiler.rb
|
72
|
+
- lib/prompt_schema/type_schema_compiler.rb
|
73
|
+
- lib/prompt_schema/version.rb
|
74
|
+
homepage: https://github.com/nolantait/prompt_schema
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata:
|
78
|
+
homepage_uri: https://github.com/nolantait/prompt_schema
|
79
|
+
source_code_uri: https://github.com/nolantait/prompt_schema
|
80
|
+
changelog_uri: https://github.com/nolantait/prompt_schema
|
81
|
+
rubygems_mfa_required: 'true'
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 3.4.0
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubygems_version: 3.7.0
|
97
|
+
specification_version: 4
|
98
|
+
summary: Generate prompt schemas for LLMs to get and check structured data.
|
99
|
+
test_files: []
|