class RubyLLM::Chat
Represents a conversation with an AI model
Attributes
Public Class Methods
# File lib/ruby_llm/chat.rb, line 12 def initialize(model: nil, provider: nil, assume_model_exists: false, context: nil) if assume_model_exists && !provider raise ArgumentError, 'Provider must be specified if assume_model_exists is true' end @context = context @config = context&.config || RubyLLM.config model_id = model || @config.default_model with_model(model_id, provider: provider, assume_exists: assume_model_exists) @temperature = nil @messages = [] @tools = {} @tool_prefs = { choice: nil, calls: nil } @concurrency = normalize_tool_concurrency(@config.tool_concurrency) @params = {} @headers = {} @schema = nil @thinking = nil @on = { new_message: nil, end_message: nil, tool_call: nil, tool_result: nil } @callbacks = Hash.new { |callbacks, name| callbacks[name] = [] } end
Public Instance Methods
# File lib/ruby_llm/chat.rb, line 165 def add_message(message_or_attributes) message = message_or_attributes.is_a?(Message) ? message_or_attributes : Message.new(message_or_attributes) messages << message message end
Source
# File lib/ruby_llm/chat.rb, line 141 def after_message(&) add_callback(:after_message, &) end
Source
# File lib/ruby_llm/chat.rb, line 149 def after_tool_result(&) add_callback(:after_tool_result, &) end
# File lib/ruby_llm/chat.rb, line 39 def ask(message = nil, with: nil, &) add_message role: :user, content: build_content(message, with) complete(&) end
Also aliased as: say
Source
# File lib/ruby_llm/chat.rb, line 137 def before_message(&) add_callback(:before_message, &) end
Source
# File lib/ruby_llm/chat.rb, line 145 def before_tool_call(&) add_callback(:before_tool_call, &) end
Source
# File lib/ruby_llm/chat.rb, line 161 def complete(&) instrument_completion(&) end
Source
# File lib/ruby_llm/chat.rb, line 157 def cost Cost.aggregate(messages.map { |message| message.cost(model: message.model_info || model) }) end
Source
# File lib/ruby_llm/chat.rb, line 176 def instance_variables super - %i[@connection @config] end
Calls superclass method
Source
# File lib/ruby_llm/chat.rb, line 125 def on_end_message(&) set_legacy_callback(:end_message, :on_end_message, :after_message, &) end
Source
# File lib/ruby_llm/chat.rb, line 121 def on_new_message(&) set_legacy_callback(:new_message, :on_new_message, :before_message, &) end
Source
# File lib/ruby_llm/chat.rb, line 129 def on_tool_call(&) set_legacy_callback(:tool_call, :on_tool_call, :before_tool_call, &) end
Source
# File lib/ruby_llm/chat.rb, line 133 def on_tool_result(&) set_legacy_callback(:tool_result, :on_tool_result, :after_tool_result, &) end
Source
# File lib/ruby_llm/chat.rb, line 172 def reset_messages! @messages.clear end
Mutates this chat by removing all in-memory messages.
Source
# File lib/ruby_llm/chat.rb, line 94 def with_context(context) @context = context @config = context.config with_model(@model.id, provider: @provider.slug, assume_exists: true) self end
Source
# File lib/ruby_llm/chat.rb, line 106 def with_headers(**headers) @headers = headers self end
# File lib/ruby_llm/chat.rb, line 46 def with_instructions(instructions, append: false, replace: nil) append ||= (replace == false) unless replace.nil? if append append_system_instruction(instructions) else replace_system_instruction(instructions) end self end
# File lib/ruby_llm/chat.rb, line 76 def with_model(model_id, provider: nil, assume_exists: false) @model, @provider = Models.resolve(model_id, provider:, assume_exists:, config: @config) @connection = @provider.connection self end
Source
# File lib/ruby_llm/chat.rb, line 101 def with_params(**params) @params = params self end
Source
# File lib/ruby_llm/chat.rb, line 111 def with_schema(schema) schema_instance = schema.is_a?(Class) ? schema.new : schema @schema = normalize_schema_payload( schema_instance.respond_to?(:to_json_schema) ? schema_instance.to_json_schema : schema_instance ) self end
# File lib/ruby_llm/chat.rb, line 82 def with_temperature(temperature) @temperature = temperature self end
# File lib/ruby_llm/chat.rb, line 87 def with_thinking(effort: nil, budget: nil) raise ArgumentError, 'with_thinking requires :effort or :budget' if effort.nil? && budget.nil? @thinking = Thinking::Config.new(effort: effort, budget: budget) self end
# File lib/ruby_llm/chat.rb, line 58 def with_tool(tool, choice: nil, calls: nil, concurrency: @concurrency) unless tool.nil? tool_instance = tool.is_a?(Class) ? tool.new : tool @tools[tool_instance.name.to_sym] = tool_instance end update_tool_options(choice:, calls:) update_tool_concurrency(concurrency) self end
# File lib/ruby_llm/chat.rb, line 68 def with_tools(*tools, replace: false, choice: nil, calls: nil, concurrency: @concurrency) @tools.clear if replace tools.compact.each { |tool| with_tool tool } update_tool_options(choice:, calls:) update_tool_concurrency(concurrency) self end