ai_client 0.4.0 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,46 +1,23 @@
1
- # ai_client/retry_middleware.rb
1
+ # lib/ai_client/retry_middleware.rb
2
2
 
3
3
  class AiClient
4
-
5
- # AiClient.use(
6
- # AiClient::RetryMiddleware.new(
7
- # max_retries: 5,
8
- # base_delay: 2,
9
- # max_delay: 30
10
- # )
11
- # )
12
- #
13
4
  class RetryMiddleware
14
-
15
- # Initializes a new instance of RetryMiddleware.
16
- #
17
- # @param max_retries [Integer] The maximum number of retries to attempt (default: 3).
18
- # @param base_delay [Integer] The base delay in seconds before retrying (default: 2).
19
- # @param max_delay [Integer] The maximum delay in seconds between retries (default: 16).
20
- #
21
- def initialize(max_retries: 3, base_delay: 2, max_delay: 16)
5
+ def initialize(max_retries: 3, base_delay: 1, max_delay: 16)
22
6
  @max_retries = max_retries
23
7
  @base_delay = base_delay
24
8
  @max_delay = max_delay
25
9
  end
26
10
 
27
- # Calls the next middleware, retrying on specific errors.
28
- #
29
- # @param client [AiClient] The client instance that invokes the middleware.
30
- # @param next_middleware [Proc] The next middleware in the chain to call.
31
- # @param args [Array] Any additional arguments to pass to the next middleware.
32
- #
33
- # @raise [StandardError] Reraise the error if max retries are exceeded.
34
- #
35
11
  def call(client, next_middleware, *args)
36
- retries = 0
12
+ @retries = 0
13
+ @client = client
14
+
37
15
  begin
38
16
  next_middleware.call
39
17
  rescue OmniAI::RateLimitError, OmniAI::NetworkError => e
40
- if retries < @max_retries
41
- retries += 1
42
- delay = [@base_delay * (2 ** (retries - 1)), @max_delay].min
43
- client.logger.warn("Retrying in #{delay} seconds due to error: #{e.message}")
18
+ if @retries < @max_retries
19
+ delay = retry_delay(e)
20
+ log_retry(delay, e)
44
21
  sleep(delay)
45
22
  retry
46
23
  else
@@ -48,5 +25,16 @@ class AiClient
48
25
  end
49
26
  end
50
27
  end
28
+
29
+ private
30
+
31
+ def retry_delay(error)
32
+ @retries += 1
33
+ [@base_delay * (2 ** (@retries - 1)), @max_delay].min
34
+ end
35
+
36
+ def log_retry(delay, error)
37
+ @client.logger.warn("Retrying in #{delay} seconds due to error: #{error.message}")
38
+ end
51
39
  end
52
40
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class AiClient
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.3"
5
5
 
6
6
  def version = VERSION
7
7
  def self.version = VERSION
data/lib/ai_client.rb CHANGED
@@ -288,9 +288,11 @@ class AiClient
288
288
  OmniAI::Mistral::Client.new(**client_options)
289
289
 
290
290
  when :ollama
291
+ # SMELL: what if ollama is not running on localhost?
291
292
  OmniAI::OpenAI::Client.new(host: 'http://localhost:11434', api_key: nil, **client_options)
292
293
 
293
294
  when :localai
295
+ # SMELL: what if localai is not running on localhost?
294
296
  OmniAI::OpenAI::Client.new(host: 'http://localhost:8080', api_key: nil, **client_options)
295
297
 
296
298
  when :open_router
@@ -322,6 +324,11 @@ class AiClient
322
324
  def determine_provider(model)
323
325
  return nil if model.nil? || model.empty?
324
326
 
327
+ # SMELL: ollama has many open-source models. Its hard to keep
328
+ # the patterns updated; hgwever, ollama has an API
329
+ # to list the currently installed models.
330
+ # http://localhost:11434/api/tags
331
+ #
325
332
  config.provider_patterns.find { |provider, pattern| model.match?(pattern) }&.first ||
326
333
  raise(ArgumentError, "Unsupported model: #{model}")
327
334
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dewayne VanHoozer
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-10-21 00:00:00.000000000 Z
10
+ date: 2025-02-01 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: active_hash
@@ -247,7 +246,6 @@ metadata:
247
246
  homepage_uri: https://github.com/MadBomber/ai_client
248
247
  source_code_uri: https://github.com/MadBomber/ai_client
249
248
  changelog_uri: https://github.com/MadBomber/ai_client/blob/main/CHANGELOG.md
250
- post_install_message:
251
249
  rdoc_options: []
252
250
  require_paths:
253
251
  - lib
@@ -262,8 +260,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
262
260
  - !ruby/object:Gem::Version
263
261
  version: '0'
264
262
  requirements: []
265
- rubygems_version: 3.5.22
266
- signing_key:
263
+ rubygems_version: 3.6.3
267
264
  specification_version: 4
268
265
  summary: A generic AI Client for many providers
269
266
  test_files: []