Class: RubyLLM::Chat

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: nil) ⇒ Chat

Returns a new instance of Chat.



9
10
11
12
13
14
15
# File 'lib/ruby_llm/chat.rb', line 9

def initialize(model: nil)
  model_id = model || RubyLLM.config.default_model
  @model = Models.find(model_id)
  @provider = Models.provider_for(model_id)
  @messages = []
  @tools = []
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



7
8
9
# File 'lib/ruby_llm/chat.rb', line 7

def messages
  @messages
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/ruby_llm/chat.rb', line 7

def model
  @model
end

#tools(*tools) ⇒ Object (readonly) Also known as: with_tools

Returns the value of attribute tools.



7
8
9
# File 'lib/ruby_llm/chat.rb', line 7

def tools
  @tools
end

Instance Method Details

#ask(message, &block) ⇒ Object



17
18
19
20
# File 'lib/ruby_llm/chat.rb', line 17

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

#each(&block) ⇒ Object



38
39
40
# File 'lib/ruby_llm/chat.rb', line 38

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

#tool(tool) ⇒ Object Also known as: with_tool

Raises:



22
23
24
25
26
27
# File 'lib/ruby_llm/chat.rb', line 22

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

  @tools << tool
  self
end