Class: RubyLLM::Chat

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby_llm/chat.rb

Overview

Represents a conversation with an AI model. Handles message history, streaming responses, and tool integration with a simple, conversational API.

Example:

chat = RubyLLM.chat
chat.ask "What's the best way to learn Ruby?"
chat.ask "Can you elaborate on that?"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: nil) ⇒ Chat

Returns a new instance of Chat.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_llm/chat.rb', line 16

def initialize(model: nil)
  model_id = model || RubyLLM.config.default_model
  self.model = model_id
  @temperature = 0.7
  @messages = []
  @tools = {}
  @on = {
    new_message: nil,
    end_message: nil
  }
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



14
15
16
# File 'lib/ruby_llm/chat.rb', line 14

def messages
  @messages
end

#modelObject

Returns the value of attribute model.



14
15
16
# File 'lib/ruby_llm/chat.rb', line 14

def model
  @model
end

#toolsObject (readonly)

Returns the value of attribute tools.



14
15
16
# File 'lib/ruby_llm/chat.rb', line 14

def tools
  @tools
end

Instance Method Details

#ask(message, &block) ⇒ Object Also known as: say



28
29
30
31
# File 'lib/ruby_llm/chat.rb', line 28

def ask(message, &block)
  add_message role: :user, content: message
  complete(&block)
end

#complete(&block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby_llm/chat.rb', line 77

def complete(&block)
  @on[:new_message]&.call
  response = @provider.complete(messages, tools: @tools, temperature: @temperature, model: @model.id, &block)
  @on[:end_message]&.call(response)

  add_message response
  if response.tool_call?
    handle_tool_calls response, &block
  else
    response
  end
end

#each(&block) ⇒ Object



73
74
75
# File 'lib/ruby_llm/chat.rb', line 73

def each(&block)
  messages.each(&block)
end

#on_end_message(&block) ⇒ Object



68
69
70
71
# File 'lib/ruby_llm/chat.rb', line 68

def on_end_message(&block)
  @on[:end_message] = block
  self
end

#on_new_message(&block) ⇒ Object



63
64
65
66
# File 'lib/ruby_llm/chat.rb', line 63

def on_new_message(&block)
  @on[:new_message] = block
  self
end

#with_model(model_id) ⇒ Object



53
54
55
56
# File 'lib/ruby_llm/chat.rb', line 53

def with_model(model_id)
  self.model = model_id
  self
end

#with_temperature(temperature) ⇒ Object



58
59
60
61
# File 'lib/ruby_llm/chat.rb', line 58

def with_temperature(temperature)
  @temperature = temperature
  self
end

#with_tool(tool) ⇒ Object

Raises:



35
36
37
38
39
40
41
# File 'lib/ruby_llm/chat.rb', line 35

def with_tool(tool)
  raise Error, "Model #{@model.id} doesn't support function calling" unless @model.supports_functions

  tool_instance = tool.is_a?(Class) ? tool.new : tool
  @tools[tool_instance.name.to_sym] = tool_instance
  self
end

#with_tools(*tools) ⇒ Object



43
44
45
46
# File 'lib/ruby_llm/chat.rb', line 43

def with_tools(*tools)
  tools.each { |tool| with_tool tool }
  self
end