Class: Rach::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(providers: nil, access_token: nil, model: nil, logger: nil, **kwargs) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rach/client.rb', line 6

def initialize(providers: nil, access_token: nil, model: nil, logger: nil, **kwargs)
  @tracker = UsageTracker.new
  @providers = {}
  @logger = logger
  @default_model = model

  if providers
    setup_providers(providers)
  elsif access_token && model
    provider = Provider.for(model)
    setup_providers({ provider.key => { access_token: access_token } })
  else
    raise ArgumentError, "Either (providers) or (access_token AND model) must be provided"
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/rach/client.rb', line 3

def client
  @client
end

#loggerObject

Returns the value of attribute logger.



4
5
6
# File 'lib/rach/client.rb', line 4

def logger
  @logger
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/rach/client.rb', line 3

def model
  @model
end

#providersObject (readonly)

Returns the value of attribute providers.



3
4
5
# File 'lib/rach/client.rb', line 3

def providers
  @providers
end

#trackerObject (readonly)

Returns the value of attribute tracker.



3
4
5
# File 'lib/rach/client.rb', line 3

def tracker
  @tracker
end

Instance Method Details

#chat(input, **options) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rach/client.rb', line 22

def chat(input, **options)
  prompt = input.is_a?(Prompt) ? input : Prompt.new(input, **options)
  model = prompt.model || @default_model

  raise ArgumentError, "No model specified" unless model

  provider_key = Provider.for(model).key
  client = @providers[provider_key]

  # Filter out options that are already handled by Prompt
  filtered_options = options.reject { |k, _| [:model, :temperature, :response_format, :tools].include?(k) }

  request_params = {
    model:,
    messages: prompt.to_messages,
    response_format: prompt.response_format,
    temperature: prompt.temperature,
    tools: prompt.tools&.map(&:function_schema),
    **filtered_options  # Pass through remaining options to the underlying client
  }.compact


  response = client.chat(parameters: request_params)
  tracker.track(response)
  response
end