Class: LittleGhost::ModelOperations
- Inherits:
-
Object
- Object
- LittleGhost::ModelOperations
- Defined in:
- lib/little_ghost/model_operations.rb
Overview
Executes bounded model operations without creating an Agent or Run.
Constant Summary collapse
- MAX_STRUCTURED_RESULT_BYTES =
:nodoc:
1_000_000- MAX_STRUCTURED_RESULT_DEPTH =
64- MAX_STRUCTURED_RESULT_NODES =
100_000- MAX_STRUCTURED_RESULT_REPAIR_ATTEMPTS =
3
Instance Method Summary collapse
- #embed(model:, inputs:, settings: {}, limits: {}, cancellation_token: Support::CancellationToken.new, deadline: nil) ⇒ Object
- #generate(model:, messages:, result_schema: nil, settings: {}, structured_result_repair_attempts: 1, cancellation_token: Support::CancellationToken.new, deadline: nil) ⇒ Object
-
#initialize(model_resolver:) ⇒ ModelOperations
constructor
A new instance of ModelOperations.
Constructor Details
#initialize(model_resolver:) ⇒ ModelOperations
Returns a new instance of ModelOperations.
13 14 15 |
# File 'lib/little_ghost/model_operations.rb', line 13 def initialize(model_resolver:) @model_resolver = model_resolver end |
Instance Method Details
#embed(model:, inputs:, settings: {}, limits: {}, cancellation_token: Support::CancellationToken.new, deadline: nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/little_ghost/model_operations.rb', line 61 def (model:, inputs:, settings: {}, limits: {}, cancellation_token: Support::CancellationToken.new, deadline: nil) request = Embeddings::Request.new(inputs:, settings:, limits:, cancellation_token:, deadline:) resolved = @model_resolver.resolve(model) handle = Instrumentation.start(:embedding, model_provider: resolved.target.provider, model_id: resolved.model_id, model_role: resolved.role, input_count: request.inputs.length) response = resolved.(request) = response..merge(provider: resolved.target.provider, model: resolved.model_id, model_role: resolved.role, input_count: request.inputs.length).compact result = Embeddings::Response.new(vectors: response.vectors, usage: response.usage, metadata:) handle.finish(outcome: :success, dimensions: result.dimensions, **usage_attributes(result.usage)) result rescue => error handle&.finish(outcome: :error, error_type: error.class.name) if handle&.active? raise end |
#generate(model:, messages:, result_schema: nil, settings: {}, structured_result_repair_attempts: 1, cancellation_token: Support::CancellationToken.new, deadline: nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/little_ghost/model_operations.rb', line 17 def generate(model:, messages:, result_schema: nil, settings: {}, structured_result_repair_attempts: 1, cancellation_token: Support::CancellationToken.new, deadline: nil) resolved = @model_resolver.resolve(model) schema = normalize_schema(result_schema) repair_attempts = normalize_repair_attempts(structured_result_repair_attempts) if schema strategy = StructuredOutput.resolve(schema, model: resolved, ordinary_tools: []) if schema handle = Instrumentation.start(:generation, model_provider: resolved.target.provider, model_id: resolved.model_id, model_role: resolved.role, structured: !schema.nil?) usage = Usage.new conversation = .map { || Message.coerce() } response = complete(resolved, messages: conversation, settings:, schema:, strategy:, repair: false, cancellation_token:, deadline:) usage += response.usage output, errors = schema ? parse_structured_response(response., schema, strategy) : [response..text, []] conversation << (schema ? redact_structured_response(response., schema, strategy) : response.) repairs_remaining = repair_attempts while schema && !errors.empty? && repairs_remaining.positive? conversation << (response., strategy, errors:, repairs_remaining:) response = complete(resolved, messages: conversation, settings:, schema:, strategy:, repair: true, cancellation_token:, deadline:) usage += response.usage output, errors = parse_structured_response(response., schema, strategy) conversation << redact_structured_response(response., schema, strategy) repairs_remaining -= 1 end unless errors.empty? raise StructuredResultError.new( "The model did not return a valid structured result after its repair attempts", schema_name: schema.fetch(:name), validation_errors: errors ) end handle.finish(outcome: :success, **usage_attributes(usage)) structured_result = StructuredResult.new(schema_name: schema.fetch(:name), value: output) if schema = schema ? conversation.last : response. RunResult.new( message: , stop_reason: schema ? :structured_result : response.stop_reason, usage:, messages: conversation.freeze, state: DataMap.new, structured_result:, steps: [] ) rescue => error handle&.finish(outcome: :error, error_type: error.class.name) if handle&.active? raise end |