Class: Promptcraft::Command::LlmChatCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/promptcraft/command/llm_chat_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages:, llm:) ⇒ LlmChatCommand

Returns a new instance of LlmChatCommand.



6
7
8
9
# File 'lib/promptcraft/command/llm_chat_command.rb', line 6

def initialize(messages:, llm:)
  @messages = messages
  @llm = llm
end

Instance Attribute Details

#llmObject (readonly)

Returns the value of attribute llm.



4
5
6
# File 'lib/promptcraft/command/llm_chat_command.rb', line 4

def llm
  @llm
end

#messagesObject (readonly)

Returns the value of attribute messages.



4
5
6
# File 'lib/promptcraft/command/llm_chat_command.rb', line 4

def messages
  @messages
end

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/promptcraft/command/llm_chat_command.rb', line 11

def execute
  # cleanse messages of missing content, role, etc
  messages = @messages.reject { |m| m[:content].nil? || m[:content].empty? || m[:role].nil? || m[:role].empty? }
  response = @llm.chat(messages:)

  response_text = response.chat_completion
  {role: "assistant", content: response_text}
rescue => e
  puts e.message
  raise
end