prompt_builder 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/CHANGELOG.md +24 -0
- data/MIT-LICENSE +20 -0
- data/README.md +763 -0
- data/VERSION +1 -0
- data/lib/prompt_builder/content/base.rb +44 -0
- data/lib/prompt_builder/content/input_file.rb +63 -0
- data/lib/prompt_builder/content/input_image.rb +64 -0
- data/lib/prompt_builder/content/input_text.rb +42 -0
- data/lib/prompt_builder/content/input_video.rb +43 -0
- data/lib/prompt_builder/content/output_text.rb +59 -0
- data/lib/prompt_builder/content/reasoning_text.rb +42 -0
- data/lib/prompt_builder/content/refusal_content.rb +42 -0
- data/lib/prompt_builder/content/summary_text.rb +42 -0
- data/lib/prompt_builder/content/text.rb +42 -0
- data/lib/prompt_builder/content.rb +28 -0
- data/lib/prompt_builder/errors.rb +18 -0
- data/lib/prompt_builder/items/base.rb +41 -0
- data/lib/prompt_builder/items/compaction.rb +60 -0
- data/lib/prompt_builder/items/function_call.rb +97 -0
- data/lib/prompt_builder/items/function_call_output.rb +110 -0
- data/lib/prompt_builder/items/item_reference.rb +42 -0
- data/lib/prompt_builder/items/message.rb +113 -0
- data/lib/prompt_builder/items/reasoning.rb +75 -0
- data/lib/prompt_builder/items.rb +13 -0
- data/lib/prompt_builder/response.rb +257 -0
- data/lib/prompt_builder/serializers/base.rb +37 -0
- data/lib/prompt_builder/serializers/chat_completion/request.rb +389 -0
- data/lib/prompt_builder/serializers/chat_completion/response.rb +139 -0
- data/lib/prompt_builder/serializers/chat_completion.rb +30 -0
- data/lib/prompt_builder/serializers/converse/request.rb +623 -0
- data/lib/prompt_builder/serializers/converse/response.rb +140 -0
- data/lib/prompt_builder/serializers/converse.rb +30 -0
- data/lib/prompt_builder/serializers/gemini/request.rb +562 -0
- data/lib/prompt_builder/serializers/gemini/response.rb +233 -0
- data/lib/prompt_builder/serializers/gemini.rb +30 -0
- data/lib/prompt_builder/serializers/messages/request.rb +634 -0
- data/lib/prompt_builder/serializers/messages/response.rb +157 -0
- data/lib/prompt_builder/serializers/messages.rb +30 -0
- data/lib/prompt_builder/serializers/open_responses/request.rb +229 -0
- data/lib/prompt_builder/serializers/open_responses/response.rb +18 -0
- data/lib/prompt_builder/serializers/open_responses.rb +30 -0
- data/lib/prompt_builder/serializers.rb +35 -0
- data/lib/prompt_builder/session.rb +383 -0
- data/lib/prompt_builder/tool_registry.rb +75 -0
- data/lib/prompt_builder/tools/definition.rb +66 -0
- data/lib/prompt_builder/tools.rb +7 -0
- data/lib/prompt_builder/usage.rb +100 -0
- data/lib/prompt_builder.rb +86 -0
- data/prompt_builder.gemspec +41 -0
- metadata +107 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "prompt_builder/errors"
|
|
4
|
+
|
|
5
|
+
# Top-level module for the PromptBuilder gem. Provides a DSL for constructing
|
|
6
|
+
# Open Responses API request payloads and parsing responses.
|
|
7
|
+
module PromptBuilder
|
|
8
|
+
autoload :Content, "prompt_builder/content"
|
|
9
|
+
autoload :Items, "prompt_builder/items"
|
|
10
|
+
autoload :Response, "prompt_builder/response"
|
|
11
|
+
autoload :Session, "prompt_builder/session"
|
|
12
|
+
autoload :ToolRegistry, "prompt_builder/tool_registry"
|
|
13
|
+
autoload :Usage, "prompt_builder/usage"
|
|
14
|
+
autoload :Serializers, "prompt_builder/serializers"
|
|
15
|
+
autoload :Tools, "prompt_builder/tools"
|
|
16
|
+
|
|
17
|
+
VERSION = File.read(File.join(__dir__, "../VERSION")).strip
|
|
18
|
+
|
|
19
|
+
class << self
|
|
20
|
+
# Convert a value to a JSON-safe structure by deep-stringifying Hash keys
|
|
21
|
+
# and converting Symbols to Strings.
|
|
22
|
+
#
|
|
23
|
+
# @param value [Object] the value to convert
|
|
24
|
+
# @return [Object] the JSON-safe value
|
|
25
|
+
def jsonify(value)
|
|
26
|
+
case value
|
|
27
|
+
when Hash
|
|
28
|
+
value.each_with_object({}) { |(k, v), h| h[k.to_s] = jsonify(v) }
|
|
29
|
+
when Array
|
|
30
|
+
value.map { |v| jsonify(v) }
|
|
31
|
+
when Symbol
|
|
32
|
+
value.to_s
|
|
33
|
+
else
|
|
34
|
+
value
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Construct a base64-encoded data URL from raw binary data and a content type.
|
|
39
|
+
#
|
|
40
|
+
# @param data [String] the raw binary data
|
|
41
|
+
# @param content_type [String] the MIME content type (e.g. "image/png", "application/pdf")
|
|
42
|
+
# @return [String] a data URL in the form "data:<content_type>;base64,<encoded_data>"
|
|
43
|
+
def data_url(data, content_type)
|
|
44
|
+
"data:#{content_type};base64,#{[data].pack("m0")}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Parse a data URL into its media type and base64-encoded data.
|
|
48
|
+
#
|
|
49
|
+
# @param url [String, nil] a URL that may be a data URL
|
|
50
|
+
# @return [Array(String, String), nil] a two-element array of +[media_type, data]+
|
|
51
|
+
# or nil if the URL is not a data URL
|
|
52
|
+
def parse_data_url(url)
|
|
53
|
+
return nil unless url
|
|
54
|
+
match = url.match(/\Adata:([^;]+);base64,(.*)\z/m)
|
|
55
|
+
return nil unless match
|
|
56
|
+
[match[1], match[2]]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Returns the global tool registry singleton.
|
|
60
|
+
#
|
|
61
|
+
# @return [ToolRegistry]
|
|
62
|
+
def tool_registry
|
|
63
|
+
@tool_registry ||= ToolRegistry.new
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Register a tool in the global registry.
|
|
67
|
+
#
|
|
68
|
+
# @param name [String] the tool name
|
|
69
|
+
# @param description [String, nil] the tool description
|
|
70
|
+
# @param parameters [Hash, nil] the JSON Schema for parameters
|
|
71
|
+
# @param strict [Boolean] whether strict mode is enabled
|
|
72
|
+
# @yield [Hash] the parsed arguments when the tool is invoked
|
|
73
|
+
# @yieldreturn [Object] the tool output (String, Hash, Array, or any object)
|
|
74
|
+
# @return [Tools::Definition] the registered definition
|
|
75
|
+
def register_tool(name, description: nil, parameters: nil, strict: false, &handler)
|
|
76
|
+
tool_registry.register(name, description: description, parameters: parameters, strict: strict, &handler)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Reset the global tool registry. Primarily used in tests.
|
|
80
|
+
#
|
|
81
|
+
# @return [void]
|
|
82
|
+
def reset_tool_registry!
|
|
83
|
+
@tool_registry = ToolRegistry.new
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Gem::Specification.new do |spec|
|
|
2
|
+
spec.name = "prompt_builder"
|
|
3
|
+
spec.version = File.read(File.expand_path("../VERSION", __FILE__)).strip
|
|
4
|
+
spec.authors = ["Brian Durand"]
|
|
5
|
+
spec.email = ["bbdurand@gmail.com"]
|
|
6
|
+
|
|
7
|
+
spec.summary = "Ruby DSL for building and parsing LLM API requests across OpenAI Responses, OpenAI Chat Completions, Anthropic Messages, Google Gemini, and Bedrock Converse formats"
|
|
8
|
+
|
|
9
|
+
spec.homepage = "https://github.com/bdurand/prompt_builder"
|
|
10
|
+
spec.license = "MIT"
|
|
11
|
+
|
|
12
|
+
spec.metadata = {
|
|
13
|
+
"homepage_uri" => spec.homepage,
|
|
14
|
+
"source_code_uri" => spec.homepage,
|
|
15
|
+
"changelog_uri" => "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
20
|
+
ignore_files = %w[
|
|
21
|
+
.
|
|
22
|
+
AGENTS.md
|
|
23
|
+
Appraisals
|
|
24
|
+
Gemfile
|
|
25
|
+
Gemfile.lock
|
|
26
|
+
Rakefile
|
|
27
|
+
bin/
|
|
28
|
+
gemfiles/
|
|
29
|
+
spec/
|
|
30
|
+
test_app/
|
|
31
|
+
]
|
|
32
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| ignore_files.any? { |path| f.start_with?(path) } }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
spec.require_paths = ["lib"]
|
|
37
|
+
|
|
38
|
+
spec.required_ruby_version = ">= 3.0"
|
|
39
|
+
|
|
40
|
+
spec.add_development_dependency "bundler"
|
|
41
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: prompt_builder
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Brian Durand
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: bundler
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
email:
|
|
27
|
+
- bbdurand@gmail.com
|
|
28
|
+
executables: []
|
|
29
|
+
extensions: []
|
|
30
|
+
extra_rdoc_files: []
|
|
31
|
+
files:
|
|
32
|
+
- CHANGELOG.md
|
|
33
|
+
- MIT-LICENSE
|
|
34
|
+
- README.md
|
|
35
|
+
- VERSION
|
|
36
|
+
- lib/prompt_builder.rb
|
|
37
|
+
- lib/prompt_builder/content.rb
|
|
38
|
+
- lib/prompt_builder/content/base.rb
|
|
39
|
+
- lib/prompt_builder/content/input_file.rb
|
|
40
|
+
- lib/prompt_builder/content/input_image.rb
|
|
41
|
+
- lib/prompt_builder/content/input_text.rb
|
|
42
|
+
- lib/prompt_builder/content/input_video.rb
|
|
43
|
+
- lib/prompt_builder/content/output_text.rb
|
|
44
|
+
- lib/prompt_builder/content/reasoning_text.rb
|
|
45
|
+
- lib/prompt_builder/content/refusal_content.rb
|
|
46
|
+
- lib/prompt_builder/content/summary_text.rb
|
|
47
|
+
- lib/prompt_builder/content/text.rb
|
|
48
|
+
- lib/prompt_builder/errors.rb
|
|
49
|
+
- lib/prompt_builder/items.rb
|
|
50
|
+
- lib/prompt_builder/items/base.rb
|
|
51
|
+
- lib/prompt_builder/items/compaction.rb
|
|
52
|
+
- lib/prompt_builder/items/function_call.rb
|
|
53
|
+
- lib/prompt_builder/items/function_call_output.rb
|
|
54
|
+
- lib/prompt_builder/items/item_reference.rb
|
|
55
|
+
- lib/prompt_builder/items/message.rb
|
|
56
|
+
- lib/prompt_builder/items/reasoning.rb
|
|
57
|
+
- lib/prompt_builder/response.rb
|
|
58
|
+
- lib/prompt_builder/serializers.rb
|
|
59
|
+
- lib/prompt_builder/serializers/base.rb
|
|
60
|
+
- lib/prompt_builder/serializers/chat_completion.rb
|
|
61
|
+
- lib/prompt_builder/serializers/chat_completion/request.rb
|
|
62
|
+
- lib/prompt_builder/serializers/chat_completion/response.rb
|
|
63
|
+
- lib/prompt_builder/serializers/converse.rb
|
|
64
|
+
- lib/prompt_builder/serializers/converse/request.rb
|
|
65
|
+
- lib/prompt_builder/serializers/converse/response.rb
|
|
66
|
+
- lib/prompt_builder/serializers/gemini.rb
|
|
67
|
+
- lib/prompt_builder/serializers/gemini/request.rb
|
|
68
|
+
- lib/prompt_builder/serializers/gemini/response.rb
|
|
69
|
+
- lib/prompt_builder/serializers/messages.rb
|
|
70
|
+
- lib/prompt_builder/serializers/messages/request.rb
|
|
71
|
+
- lib/prompt_builder/serializers/messages/response.rb
|
|
72
|
+
- lib/prompt_builder/serializers/open_responses.rb
|
|
73
|
+
- lib/prompt_builder/serializers/open_responses/request.rb
|
|
74
|
+
- lib/prompt_builder/serializers/open_responses/response.rb
|
|
75
|
+
- lib/prompt_builder/session.rb
|
|
76
|
+
- lib/prompt_builder/tool_registry.rb
|
|
77
|
+
- lib/prompt_builder/tools.rb
|
|
78
|
+
- lib/prompt_builder/tools/definition.rb
|
|
79
|
+
- lib/prompt_builder/usage.rb
|
|
80
|
+
- prompt_builder.gemspec
|
|
81
|
+
homepage: https://github.com/bdurand/prompt_builder
|
|
82
|
+
licenses:
|
|
83
|
+
- MIT
|
|
84
|
+
metadata:
|
|
85
|
+
homepage_uri: https://github.com/bdurand/prompt_builder
|
|
86
|
+
source_code_uri: https://github.com/bdurand/prompt_builder
|
|
87
|
+
changelog_uri: https://github.com/bdurand/prompt_builder/blob/main/CHANGELOG.md
|
|
88
|
+
rdoc_options: []
|
|
89
|
+
require_paths:
|
|
90
|
+
- lib
|
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '3.0'
|
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
|
+
requirements:
|
|
98
|
+
- - ">="
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '0'
|
|
101
|
+
requirements: []
|
|
102
|
+
rubygems_version: 4.0.3
|
|
103
|
+
specification_version: 4
|
|
104
|
+
summary: Ruby DSL for building and parsing LLM API requests across OpenAI Responses,
|
|
105
|
+
OpenAI Chat Completions, Anthropic Messages, Google Gemini, and Bedrock Converse
|
|
106
|
+
formats
|
|
107
|
+
test_files: []
|