Class: LlmGateway::Prompt

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

Constant Summary collapse

UNSET =
Object.new.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider = nil, model = nil) ⇒ Prompt

Returns a new instance of Prompt.



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

def initialize(provider = nil, model = nil)
  @provider = provider || self.class.provider
  @model = model || self.class.model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#providerObject (readonly)

Returns the value of attribute provider.



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

def provider
  @provider
end

Class Method Details

.after_execute(*methods, &block) ⇒ Object



34
35
36
37
# File 'lib/llm_gateway/prompt.rb', line 34

def self.after_execute(*methods, &block)
  after_execute_callbacks.concat(methods)
  after_execute_callbacks << block if block_given?
end

.after_execute_callbacksObject



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

def self.after_execute_callbacks
  @after_execute_callbacks ||= []
end

.before_execute(*methods, &block) ⇒ Object



29
30
31
32
# File 'lib/llm_gateway/prompt.rb', line 29

def self.before_execute(*methods, &block)
  before_execute_callbacks.concat(methods)
  before_execute_callbacks << block if block_given?
end

.before_execute_callbacksObject



39
40
41
# File 'lib/llm_gateway/prompt.rb', line 39

def self.before_execute_callbacks
  @before_execute_callbacks ||= []
end

.find_tool(tool_name) ⇒ Object



94
95
96
# File 'lib/llm_gateway/prompt.rb', line 94

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

.inherited(subclass) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/llm_gateway/prompt.rb', line 47

def self.inherited(subclass)
  super
  subclass.instance_variable_set(:@before_execute_callbacks, before_execute_callbacks.dup)
  subclass.instance_variable_set(:@after_execute_callbacks, after_execute_callbacks.dup)
  subclass.provider = provider
  subclass.model = model
end

.model(value = UNSET) ⇒ Object



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

def model(value = UNSET)
  @model = value unless value.equal?(UNSET)
  @model
end

.model=(value) ⇒ Object



24
25
26
# File 'lib/llm_gateway/prompt.rb', line 24

def model=(value)
  @model = value
end

.provider(value = UNSET) ⇒ Object



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

def provider(value = UNSET)
  @provider = value unless value.equal?(UNSET)
  @provider
end

.provider=(value) ⇒ Object



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

def provider=(value)
  @provider = value
end

Instance Method Details

#runObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/llm_gateway/prompt.rb', line 60

def run
  run_callbacks(:before_execute, prompt)

  response = stream

  response_content = if respond_to?(:extract_response)
    extract_response(response)
  else
    response[:choices][0][:content]
  end

  result = if respond_to?(:parse_response)
    parse_response(response_content)
  else
    response_content
  end

  run_callbacks(:after_execute, response, response_content)

  result
end

#stream(provider: nil, model: nil, **options) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/llm_gateway/prompt.rb', line 82

def stream(provider: nil, model: nil, **options)
  stream_provider = provider || self.provider
  stream_model = model || self.model
  options[:model] = stream_model if stream_model

  stream_provider.stream(prompt, tools: tools, system: system_prompt, **options)
end

#system_promptObject



98
99
100
# File 'lib/llm_gateway/prompt.rb', line 98

def system_prompt
  nil
end

#toolsObject



90
91
92
# File 'lib/llm_gateway/prompt.rb', line 90

def tools
  nil
end