Class: RubyLLM::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/client.rb

Overview

Client class for handling LLM provider interactions

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



6
7
8
# File 'lib/ruby_llm/client.rb', line 6

def initialize
  @providers = {}
end

Instance Method Details

#chat(messages, model: nil, temperature: 0.7, stream: false, tools: nil, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_llm/client.rb', line 10

def chat(messages, model: nil, temperature: 0.7, stream: false, tools: nil, &block)
  # Convert any hash messages to Message objects
  formatted_messages = messages.map do |msg|
    msg.is_a?(Message) ? msg : Message.new(**msg)
  end

  provider = provider_for(model)
  response_messages = provider.chat(
    formatted_messages,
    model: model,
    temperature: temperature,
    stream: stream,
    tools: tools,
    &block
  )

  # Always return an array of messages, even for single responses
  response_messages.is_a?(Array) ? response_messages : [response_messages]
end

#list_models(provider = nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/ruby_llm/client.rb', line 30

def list_models(provider = nil)
  if provider
    provider_for(nil, provider).list_models
  else
    all_providers.flat_map(&:list_models)
  end
end