Class: LlmGateway::Prompt

Inherits:
Object show all
Defined in:
lib/llm_gateway/prompt.rb

Direct Known Subclasses

Agents::Harness

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider: nil, model: nil, reasoning: nil, cache_key: nil, cache_retention: nil) ⇒ Prompt

Returns a new instance of Prompt.



19
20
21
22
23
24
25
# File 'lib/llm_gateway/prompt.rb', line 19

def initialize(provider: nil, model: nil, reasoning: nil, cache_key: nil, cache_retention: nil)
  @provider = provider || self.class.provider
  @model = model || self.class.model
  @reasoning = reasoning || self.class.reasoning
  @cache_key = cache_key
  @cache_retention = cache_retention
end

Instance Attribute Details

#cache_keyObject

Returns the value of attribute cache_key.



7
8
9
# File 'lib/llm_gateway/prompt.rb', line 7

def cache_key
  @cache_key
end

#cache_retentionObject

Returns the value of attribute cache_retention.



7
8
9
# File 'lib/llm_gateway/prompt.rb', line 7

def cache_retention
  @cache_retention
end

Class Method Details

.after_execute(*methods, &block) ⇒ Object



14
15
16
17
# File 'lib/llm_gateway/prompt.rb', line 14

def self.after_execute(*methods, &block)
  self.after_execute_callbacks += methods
  self.after_execute_callbacks += [ block ] if block_given?
end

.before_execute(*methods, &block) ⇒ Object



9
10
11
12
# File 'lib/llm_gateway/prompt.rb', line 9

def self.before_execute(*methods, &block)
  self.before_execute_callbacks += methods
  self.before_execute_callbacks += [ block ] if block_given?
end

.find_tool(name) ⇒ Object



52
53
54
# File 'lib/llm_gateway/prompt.rb', line 52

def self.find_tool(name)
  tools.find { |tool| tool.tool_name == name }
end

.toolsObject



48
49
50
# File 'lib/llm_gateway/prompt.rb', line 48

def self.tools
  const_defined?(:TOOLS, false) ? self::TOOLS : []
end

Instance Method Details

#run(provider: nil, model: nil, reasoning: nil, **options, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/llm_gateway/prompt.rb', line 27

def run(provider: nil, model: nil, reasoning: nil, **options, &block)
  # Resolve the prompt once so dynamic or expensive prompt builders are not
  # evaluated multiple times during a single run.
  input = prompt

  run_callbacks(:before_execute, input)

  response = run_tool_loop(input, provider: resolved_provider(provider), model: model, reasoning: reasoning, **options, &block)

  run_callbacks(:after_execute, response)

  response
end

#stream(input = prompt, provider: nil, model: nil, reasoning: nil, **options, &block) ⇒ Object



41
42
43
44
45
46
# File 'lib/llm_gateway/prompt.rb', line 41

def stream(input = prompt, provider: nil, model: nil, reasoning: nil, **options, &block)
  stream_provider = resolved_provider(provider)
  stream_options = default_stream_options(model: model, reasoning: reasoning).merge(options)

  stream_provider.stream(input, **stream_options, &block)
end

#system_promptObject



60
61
62
# File 'lib/llm_gateway/prompt.rb', line 60

def system_prompt
  nil
end

#toolsObject



56
57
58
# File 'lib/llm_gateway/prompt.rb', line 56

def tools
  self.class.tools.map(&:definition)
end