Class: Rach::Client
- Inherits:
-
Object
- Object
- Rach::Client
- Defined in:
- lib/rach/client.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#providers ⇒ Object
readonly
Returns the value of attribute providers.
-
#tracker ⇒ Object
readonly
Returns the value of attribute tracker.
Instance Method Summary collapse
- #chat(input, **options) ⇒ Object
-
#initialize(providers: nil, access_token: nil, model: nil, logger: nil, **kwargs) ⇒ Client
constructor
A new instance of Client.
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
#client ⇒ Object (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/rach/client.rb', line 3 def client @client end |
#logger ⇒ Object
Returns the value of attribute logger.
4 5 6 |
# File 'lib/rach/client.rb', line 4 def logger @logger end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/rach/client.rb', line 3 def model @model end |
#providers ⇒ Object (readonly)
Returns the value of attribute providers.
3 4 5 |
# File 'lib/rach/client.rb', line 3 def providers @providers end |
#tracker ⇒ Object (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
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, **) prompt = input.is_a?(Prompt) ? input : Prompt.new(input, **) 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 = .reject { |k, _| [:model, :temperature, :response_format, :tools].include?(k) } request_params = { model:, messages: prompt., response_format: prompt.response_format, temperature: prompt.temperature, tools: prompt.tools&.map(&:function_schema), ** # Pass through remaining options to the underlying client }.compact response = client.chat(parameters: request_params) tracker.track(response) response end |