Class: RubyLLM::Chat
- Inherits:
-
Object
- Object
- RubyLLM::Chat
- Includes:
- Enumerable
- Defined in:
- lib/ruby_llm/chat.rb
Overview
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#model ⇒ Object
Returns the value of attribute model.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
Instance Method Summary collapse
- #add_message(message_or_attributes) ⇒ Object
- #ask(message, &block) ⇒ Object (also: #say)
- #complete(&block) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(model: nil) ⇒ Chat
constructor
A new instance of Chat.
- #on_end_message(&block) ⇒ Object
- #on_new_message(&block) ⇒ Object
- #with_model(model_id) ⇒ Object
- #with_temperature(temperature) ⇒ Object
- #with_tool(tool) ⇒ Object
- #with_tools(*tools) ⇒ Object
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
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
14 15 16 |
# File 'lib/ruby_llm/chat.rb', line 14 def @messages end |
#model ⇒ Object
Returns the value of attribute model.
14 15 16 |
# File 'lib/ruby_llm/chat.rb', line 14 def model @model end |
#tools ⇒ Object (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
#add_message(message_or_attributes) ⇒ Object
90 91 92 93 94 |
# File 'lib/ruby_llm/chat.rb', line 90 def () = .is_a?(Message) ? : Message.new() << end |
#ask(message, &block) ⇒ Object Also known as: say
28 29 30 31 |
# File 'lib/ruby_llm/chat.rb', line 28 def ask(, &block) role: :user, content: 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(, tools: @tools, temperature: @temperature, model: @model.id, &block) @on[:end_message]&.call(response) 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) .each(&block) end |
#on_end_message(&block) ⇒ Object
68 69 70 71 |
# File 'lib/ruby_llm/chat.rb', line 68 def (&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 (&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
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 |