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
34
35
36
37
38
39
40
# File 'lib/llm_gateway/prompt.rb', line 31

def initialize(model)
  @model = model
  @connection = if model.is_a?(String)
    LlmGateway.configured_clients.values.find do |client|
      client.client.model_key == model
    end
  else
    model
  end
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



76
77
78
# File 'lib/llm_gateway/prompt.rb', line 76

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



64
65
66
67
68
69
70
# File 'lib/llm_gateway/prompt.rb', line 64

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

#runObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/llm_gateway/prompt.rb', line 42

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



80
81
82
# File 'lib/llm_gateway/prompt.rb', line 80

def system_prompt
  nil
end

#toolsObject



72
73
74
# File 'lib/llm_gateway/prompt.rb', line 72

def tools
  nil
end