ai_client 0.3.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -81,7 +81,6 @@ class AiClient
81
81
  end
82
82
 
83
83
 
84
- private
85
84
 
86
85
  # Retrieves the ORC client instance.
87
86
  #
@@ -91,6 +90,10 @@ class AiClient
91
90
  @orc_client ||= add_open_router_extensions || raise("OpenRouter extensions are not available")
92
91
  end
93
92
 
93
+
94
+ private
95
+
96
+
94
97
  # Retrieves models from the ORC client.
95
98
  #
96
99
  # @return [Array<Hash>] List of models.
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class AiClient
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.1"
5
+
6
+ def version = VERSION
7
+ def self.version = VERSION
5
8
  end
data/lib/ai_client.rb CHANGED
@@ -85,10 +85,12 @@ class AiClient
85
85
  attr_reader :client, # OmniAI's client instance
86
86
  :provider, # [Symbol]
87
87
  :model, # [String]
88
- :logger,
88
+ :logger,
89
+ :last_message,
89
90
  :last_response,
90
91
  :timeout,
91
- :config # Instance configuration
92
+ :config, # Instance configuration
93
+ :context # chat-bot context
92
94
 
93
95
  # Initializes a new AiClient instance.
94
96
  #
@@ -114,35 +116,16 @@ class AiClient
114
116
  # - :timeout [Integer] Timeout value for requests.
115
117
  # @yield [config] An optional block to configure the instance.
116
118
  #
117
- def initialize(model, **options, &block)
118
- # Assign the instance variable @config from the class variable @@config
119
- @config = self.class.class_config.dup
120
-
121
- # Yield the @config to a block if given
122
- yield(@config) if block_given?
123
-
124
- # Merge in an instance-specific YAML file
125
- if options.has_key?(:config)
126
- @config.merge! Config.load(options[:config])
127
- options.delete(:config) # Lconfig not supported by OmniAI
128
- end
129
-
130
- @model = model
131
- explicit_provider = options.fetch(:provider, config.provider)
132
-
133
- @provider = validate_provider(explicit_provider) || determine_provider(model)
134
-
135
- provider_config = @config.providers[@provider] || {}
136
-
137
- @logger = options[:logger] || @config.logger
138
- @timeout = options[:timeout] || @config.timeout
139
- @base_url = options[:base_url] || provider_config[:base_url]
140
- @options = options.merge(provider_config)
119
+ def initialize(model = nil, **options, &block)
120
+ @context = [] # An Array of String or response objects
121
+ @last_messages = nil
122
+ @last_response = nil
141
123
 
142
- # @client is an instance of an OmniAI::* class
143
- @client = create_client
124
+ setup_config(options, &block)
125
+ set_provider_and_model(model, options[:provider])
126
+ setup_instance_variables(options)
144
127
 
145
- @last_response = nil
128
+ @client = create_client
146
129
  end
147
130
 
148
131
 
@@ -174,13 +157,13 @@ class AiClient
174
157
  # @return [String] The extracted content.
175
158
  # @raise [NotImplementedError] If content extraction is not implemented for the provider.
176
159
  #
177
- def content
160
+ def content(response=last_response)
178
161
  case @provider
179
162
  when :localai, :mistral, :ollama, :open_router, :openai
180
- last_response.data.tunnel 'content'
163
+ response.data.tunnel 'content'
181
164
 
182
165
  when :anthropic, :google
183
- last_response.data.tunnel 'text'
166
+ response.data.tunnel 'text'
184
167
 
185
168
  else
186
169
  raise NotImplementedError, "Content extraction not implemented for provider: #{@provider}"
@@ -219,6 +202,47 @@ class AiClient
219
202
  ##############################################
220
203
  private
221
204
 
205
+ def setup_config(options, &block)
206
+ @config = self.class.class_config.dup
207
+
208
+ yield(@config) if block_given?
209
+
210
+ if options.key?(:config)
211
+ @config.merge!(Config.load(options[:config]))
212
+ options.delete(:config) # config not supported by OmniAI
213
+ end
214
+ end
215
+
216
+
217
+ def set_provider_and_model(my_model, my_provider)
218
+ if my_model.nil?
219
+ if my_provider.nil?
220
+ @provider = @config.default_provider.to_sym
221
+ else
222
+ @provider = validate_provider(my_provider)
223
+ end
224
+ @model = @config.default_model[@provider]
225
+ else
226
+ @model = my_model
227
+ if my_provider.nil?
228
+ @provider = determine_provider(my_model)
229
+ else
230
+ @provider = validate_provider(my_provider)
231
+ end
232
+ end
233
+ end
234
+
235
+
236
+ def setup_instance_variables(options)
237
+ provider_config = @config.providers[@provider] || {}
238
+
239
+ @logger = options[:logger] || @config.logger
240
+ @timeout = options[:timeout] || @config.timeout
241
+ @base_url = options[:base_url] || provider_config[:base_url]
242
+ @options = options.merge(provider_config)
243
+ end
244
+
245
+
222
246
  # Validates the specified provider.
223
247
  #
224
248
  # @param provider [Symbol] The provider to validate.
@@ -296,6 +320,8 @@ class AiClient
296
320
  # @raise [ArgumentError] If the model is unsupported.
297
321
  #
298
322
  def determine_provider(model)
323
+ return nil if model.nil? || model.empty?
324
+
299
325
  config.provider_patterns.find { |provider, pattern| model.match?(pattern) }&.first ||
300
326
  raise(ArgumentError, "Unsupported model: #{model}")
301
327
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dewayne VanHoozer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-20 00:00:00.000000000 Z
11
+ date: 2024-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_hash