Class: LlmGateway::Prompt

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Prompt

Returns a new instance of Prompt.



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

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/llm_gateway/prompt.rb', line 5

def model
  @model
end

Class Method Details

.after_execute(*methods, &block) ⇒ Object



12
13
14
15
# File 'lib/llm_gateway/prompt.rb', line 12

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

.after_execute_callbacksObject



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

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

.before_execute(*methods, &block) ⇒ Object



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

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

.before_execute_callbacksObject



17
18
19
# File 'lib/llm_gateway/prompt.rb', line 17

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

.find_tool(tool_name) ⇒ Object



65
66
67
# File 'lib/llm_gateway/prompt.rb', line 65

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

.inherited(subclass) ⇒ Object



25
26
27
28
29
# File 'lib/llm_gateway/prompt.rb', line 25

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)
end

Instance Method Details

#postObject



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

def post
  LlmGateway::Client.chat(model, prompt, tools: tools, system: system_prompt)
end

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/llm_gateway/prompt.rb', line 35

def run
  run_callbacks(:before_execute, prompt)

  response = post

  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

#system_promptObject



69
70
71
# File 'lib/llm_gateway/prompt.rb', line 69

def system_prompt
  nil
end

#toolsObject



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

def tools
  nil
end