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
16
17
# 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 = {}

  ensure_valid_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

#toolsObject (readonly)

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 Also known as: say



19
20
21
22
# File 'lib/ruby_llm/chat.rb', line 19

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

#with_tool(tool) ⇒ Object

Raises:



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

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

  @tools[tool.name] = tool
  self
end

#with_tools(*tools) ⇒ Object



33
34
35
36
# File 'lib/ruby_llm/chat.rb', line 33

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