Class: TurnKit::OutputPolicy
- Inherits:
-
Object
- Object
- TurnKit::OutputPolicy
- Defined in:
- lib/turnkit/output_policy.rb
Constant Summary collapse
- DEFAULT_SCHEMA =
{ type: "object", properties: { approved: { type: "boolean" }, violations: { type: "array", items: { type: "object", properties: { rule: { type: "string" }, message: { type: "string" } }, required: [ "rule", "message" ] } } }, required: [ "approved", "violations" ] }.freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#thinking ⇒ Object
readonly
Returns the value of attribute thinking.
Class Method Summary collapse
- .from_file(path, name: nil, **options) ⇒ Object
- .from_skill(skill, **options) ⇒ Object
- .require_image ⇒ Object
- .require_media_analysis ⇒ Object
Instance Method Summary collapse
- #call(output, run: nil, turn: nil) ⇒ Object
-
#initialize(content:, name: "output_policy", model: nil, thinking: nil, client: nil) ⇒ OutputPolicy
constructor
A new instance of OutputPolicy.
Constructor Details
#initialize(content:, name: "output_policy", model: nil, thinking: nil, client: nil) ⇒ OutputPolicy
Returns a new instance of OutputPolicy.
52 53 54 55 56 57 58 59 |
# File 'lib/turnkit/output_policy.rb', line 52 def initialize(content:, name: "output_policy", model: nil, thinking: nil, client: nil) @name = name.to_s @content = content.to_s @model = model @thinking = Agent.normalize_thinking(thinking) @client = client raise ArgumentError, "content is required" if @content.empty? end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
24 25 26 |
# File 'lib/turnkit/output_policy.rb', line 24 def client @client end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
24 25 26 |
# File 'lib/turnkit/output_policy.rb', line 24 def content @content end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
24 25 26 |
# File 'lib/turnkit/output_policy.rb', line 24 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/turnkit/output_policy.rb', line 24 def name @name end |
#thinking ⇒ Object (readonly)
Returns the value of attribute thinking.
24 25 26 |
# File 'lib/turnkit/output_policy.rb', line 24 def thinking @thinking end |
Class Method Details
.from_file(path, name: nil, **options) ⇒ Object
26 27 28 |
# File 'lib/turnkit/output_policy.rb', line 26 def self.from_file(path, name: nil, **) new(name: name || File.basename(path, File.extname(path)), content: File.read(path), **) end |
.from_skill(skill, **options) ⇒ Object
30 31 32 |
# File 'lib/turnkit/output_policy.rb', line 30 def self.from_skill(skill, **) new(name: skill.key, content: skill.content, **) end |
.require_image ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/turnkit/output_policy.rb', line 34 def self.require_image lambda do |output, output_data: nil, turn: nil, **| data = output_data.is_a?(Hash) ? output_data : output images = data.is_a?(Hash) ? data["images"] || data[:images] : nil has_image = Array(images).any? || turn&.conversation&.(turn)&.any?(&:image?) { rule: "image_required", message: "output must include an image result" } unless has_image end end |
.require_media_analysis ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/turnkit/output_policy.rb', line 43 def self.require_media_analysis lambda do |output, output_data: nil, turn: nil, **| data = output_data.is_a?(Hash) ? output_data : output analyses = data.is_a?(Hash) ? data["media_analyses"] || data[:media_analyses] : nil has_analysis = Array(analyses).any? || turn&.conversation&.(turn)&.any?(&:media_analysis?) { rule: "media_analysis_required", message: "output must include a media analysis result" } unless has_analysis end end |
Instance Method Details
#call(output, run: nil, turn: nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/turnkit/output_policy.rb', line 61 def call(output, run: nil, turn: nil) model_name = model || turn&.model || run&.turn&.model || TurnKit.default_model result = if turn turn.internal_model_call( model: model_name, messages: (output), tools: [], instructions: audit_instructions, thinking: thinking, output_schema: DEFAULT_SCHEMA, metadata: { output_policy: name }, purpose: "output_policy", client: client ) else audit_client = client || TurnKit.client audit_client.validate!(model: model_name) chat(audit_client, model: model_name, messages: (output), tools: [], instructions: audit_instructions, thinking: thinking, output_schema: DEFAULT_SCHEMA, metadata: { output_policy: name }) end data = result.output_data || parse_json(result.text) return if data.fetch("approved", false) Array(data["violations"]).map do |violation| attrs = violation.transform_keys(&:to_s) OutputAudit::Violation.new( rule: attrs["rule"] || name, message: attrs["message"] || "output policy failed", metadata: attrs.reject { |key, _| %w[rule message].include?(key) } ) end end |