Module: Phronomy::Runnable

Included in:
Agent::Base, Graph::CompiledGraph, OutputParser::Base, PromptTemplate
Defined in:
lib/phronomy/runnable.rb

Overview

Base interface for executable graph components. Provides invoke / stream / batch.

Instance Method Summary collapse

Instance Method Details

#batch(inputs, config: {}) ⇒ Object

Batch execution. Default calls invoke sequentially.



20
21
22
# File 'lib/phronomy/runnable.rb', line 20

def batch(inputs, config: {})
  inputs.map { |input| invoke(input, config: config) }
end

#invoke(input, config: {}) ⇒ Object

Synchronous execution. Must be overridden in subclasses.

Raises:

  • (NotImplementedError)


8
9
10
# File 'lib/phronomy/runnable.rb', line 8

def invoke(input, config: {})
  raise NotImplementedError, "#{self.class}#invoke is not implemented"
end

#stream(input, config: {}) {|result| ... } ⇒ Object

Streaming execution. Default yields the invoke result as a single chunk.

Yields:

  • (result)


13
14
15
16
17
# File 'lib/phronomy/runnable.rb', line 13

def stream(input, config: {}, &block)
  result = invoke(input, config: config)
  yield result if block
  result
end

#trace(name, input: nil, **meta, &block) ⇒ Object

Convenience wrapper that delegates to the global tracer. Yields a span; the block must return [result, usage] where usage is a Phronomy::TokenUsage or nil. Returns only the result value.

Examples:

trace("my_chain", input: input) { [invoke(input), nil] }


30
31
32
# File 'lib/phronomy/runnable.rb', line 30

def trace(name, input: nil, **meta, &block)
  Phronomy.configuration.tracer.trace(name, input: input, **meta, &block)
end