Module: RubyConversations::Concerns::ConversationChat

Extended by:
ActiveSupport::Concern
Included in:
RubyConversations::ConversationManager
Defined in:
lib/ruby_conversations/concerns/conversation_chat.rb

Overview

Handles chat-related functionality for Conversation

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tools_optionalObject

Returns the value of attribute tools_optional.



12
13
14
# File 'lib/ruby_conversations/concerns/conversation_chat.rb', line 12

def tools_optional
  @tools_optional
end

Instance Method Details

#call_llm(system_message: nil, &block) ⇒ Object Also known as: execute



18
19
20
21
22
23
24
25
# File 'lib/ruby_conversations/concerns/conversation_chat.rb', line 18

def call_llm(system_message: nil, &block)
  validate_conversation_state
  chat_messages = generate_chat_response(system_message, &block)

  validate_tool_calls(chat_messages) if tools_required?

  store_and_update_conversation(chat_messages)
end

#enable_image_generation(size:, output_format:) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/ruby_conversations/concerns/conversation_chat.rb', line 59

def enable_image_generation(size:, output_format:)
  image_generation_tool_config = {
    type: 'image_generation',
    size:,
    output_format:
  }.compact

  chat.with_params(tools: [image_generation_tool_config])
end

#llmObject



55
56
57
# File 'lib/ruby_conversations/concerns/conversation_chat.rb', line 55

def llm
  model_identifier
end

#tool_calls?(chat_messages) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/ruby_conversations/concerns/conversation_chat.rb', line 51

def tool_calls?(chat_messages)
  chat_messages.any? { |message| message.tool_calls.present? }
end

#tool_namesObject



47
48
49
# File 'lib/ruby_conversations/concerns/conversation_chat.rb', line 47

def tool_names
  tool&.name || tools&.map(&:name)
end

#tools_configured?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/ruby_conversations/concerns/conversation_chat.rb', line 43

def tools_configured?
  tool.present? || tools&.present?
end

#tools_required?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ruby_conversations/concerns/conversation_chat.rb', line 14

def tools_required?
  !tools_optional
end

#validate_tool_calls(chat_messages) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_conversations/concerns/conversation_chat.rb', line 29

def validate_tool_calls(chat_messages)
  return unless tools_configured?
  return if tool_calls?(chat_messages)

  last_message = chat_messages.last&.content
  error_message = "No tool call found for tool(s): '#{tool_names}'. Please check the prompt."

  raise RubyConversations::ToolCallValidationError.new(
    error_message,
    last_message: last_message,
    tool_names: tool_names
  )
end