omniai-anthropic 1.2.1 → 1.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb7e8977e3dc6cdd5770822c5eb2ce65aed056994edc321a0feeb4f240f603fb
4
- data.tar.gz: 5465ae68dd2d5762c579547d40118e6f4c8b9b5a2e46117d2824d22ab0e9bb58
3
+ metadata.gz: 81f979cca2281d6d2462bfd301d9112d2347c6db184b77ff5aaad73e4fb141ef
4
+ data.tar.gz: 906591303f337817e2f086169fe382d69e26a98674aad3feaedcb239e1087161
5
5
  SHA512:
6
- metadata.gz: f62501a64b8b0367f0d2d318802df1c82a818013497d7f201f2f3c6d9bdbd2dfcc7014bc7851e2b566dc11498dc4a9abefb1e61ade0c913fe6520e239c202fd0
7
- data.tar.gz: 4bee864a29284258a4847556ec039a9a7c3684dd04c669cc5a6eafa1c6bb4c2dc012101e24c386dc8f584b53cf04648f162fbe29ad4a1289741ba3844b86c212
6
+ metadata.gz: 5510b9cafbeea22e709f515eb0179ec6cb49e251fdd1391b6a1527f06908d2582ca22c56b06af3aee4cbb80ad63cb9aac047c0ed706eefa84cc69eb90c0ec0c2
7
+ data.tar.gz: 3ea6790fad0c04efc10f987125e64755e5a53416e5652440f65d660a7c276206bbc62d632c0c475555ba151bf2eae7103682aea71fea13388b3ef602a540ce0e
data/README.md CHANGED
@@ -67,7 +67,7 @@ completion.choice.message.content # 'The capital of Canada is Ottawa.'
67
67
  `model` takes an optional string (default is `claude-3-haiku-20240307`):
68
68
 
69
69
  ```ruby
70
- completion = client.chat('Provide code for fibonacci', model: OmniAI::Anthropic::Chat::Model::CLAUDE_OPUS)
70
+ completion = client.chat('Provide code for fibonacci', model: OmniAI::Anthropic::Chat::Model::CLAUDE_SONET)
71
71
  completion.choice.message.content # 'def fibonacci(n)...end'
72
72
  ```
73
73
 
@@ -5,13 +5,13 @@ module OmniAI
5
5
  class Chat
6
6
  # A completion returned by the API.
7
7
  class Completion < OmniAI::Chat::Completion
8
- # @return [Array<OmniAI::Chat::Choice>]
8
+ # @return [Array<OmniAI::Chat::MessageChoice>]
9
9
  def choices
10
10
  @choices ||= begin
11
11
  role = @data['role']
12
12
 
13
13
  @data['content'].map do |data, index|
14
- OmniAI::Chat::Choice.for(data: {
14
+ OmniAI::Chat::MessageChoice.for(data: {
15
15
  'index' => index,
16
16
  'message' => { 'role' => role, 'content' => data['text'] },
17
17
  })
@@ -27,20 +27,6 @@ module OmniAI
27
27
 
28
28
  protected
29
29
 
30
- # @param response [HTTP::Response]
31
- # @return [OmniAI::Anthropic::Chat::Stream]
32
- def stream!(response:)
33
- raise Error, "#{self.class.name}#stream! unstreamable" unless @stream
34
-
35
- Stream.new(response:).stream! { |chunk| @stream.call(chunk) }
36
- end
37
-
38
- # @param response [HTTP::Response]
39
- # @param response [OmniAI::Anthropic::Chat::Completion]
40
- def complete!(response:)
41
- Completion.new(data: response.parse)
42
- end
43
-
44
30
  # @return [Hash]
45
31
  def payload
46
32
  OmniAI::Anthropic.config.chat_options.merge({
@@ -24,15 +24,19 @@ module OmniAI
24
24
 
25
25
  # @param api_key [String] optional - defaults to `OmniAI::Anthropic.config.api_key`
26
26
  # @param host [String] optional - defaults to `OmniAI::Anthropic.config.host`
27
+ # @param version [String] optional - defaults to `OmniAI::Anthropic.config.version`
28
+ # @param logger [Logger] optional - defaults to `OmniAI::Anthropic.config.logger`
29
+ # @param timeout [Integer] optional - defaults to `OmniAI::Anthropic.config.timeout`
27
30
  def initialize(
28
31
  api_key: OmniAI::Anthropic.config.api_key,
32
+ host: OmniAI::Anthropic.config.host,
29
33
  version: OmniAI::Anthropic.config.version,
30
34
  logger: OmniAI::Anthropic.config.logger,
31
- host: OmniAI::Anthropic.config.host
35
+ timeout: OmniAI::Anthropic.config.timeout
32
36
  )
33
37
  raise(ArgumentError, %(ENV['ANTHROPIC_API_KEY'] must be defined or `api_key` must be passed)) if api_key.nil?
34
38
 
35
- super(api_key:, logger:)
39
+ super(api_key:, host:, logger:, timeout:)
36
40
 
37
41
  @host = host
38
42
  @version = version
@@ -40,10 +44,10 @@ module OmniAI
40
44
 
41
45
  # @return [HTTP::Client]
42
46
  def connection
43
- HTTP
44
- .headers('x-api-key': @api_key)
45
- .headers('anthropic-version': @version)
46
- .persistent('https://api.anthropic.com')
47
+ @connection ||= super.headers({
48
+ 'x-api-key': @api_key,
49
+ 'anthropic-version': @version,
50
+ }.compact)
47
51
  end
48
52
 
49
53
  # @raise [OmniAI::Error]
@@ -2,16 +2,30 @@
2
2
 
3
3
  module OmniAI
4
4
  module Anthropic
5
- # Config for the Anthropic `api_key` / `host` / `logger` / `version` / `chat_options`.
5
+ # Configuration for Anthropic.
6
6
  class Config < OmniAI::Config
7
- attr_accessor :version, :chat_options
7
+ DEFAULT_HOST = 'https://api.anthropic.com'
8
+ DEFAULT_VERSION = '2023-06-01'
8
9
 
9
- def initialize
10
- super
11
- @api_key = ENV.fetch('ANTHROPIC_API_KEY', nil)
12
- @host = ENV.fetch('ANTHROPIC_HOST', 'https://api.anthropic.com')
13
- @version = ENV.fetch('ANTHROPIC_VERSION', '2023-06-01')
14
- @chat_options = { max_tokens: 4096 }
10
+ # @!attribute [rw] version
11
+ # @return [String, nil] passed as `anthropic-version` if specified
12
+ attr_accessor :version
13
+
14
+ # @param api_key [String, nil] optional - defaults to `ENV['ANTHROPIC_API_KEY']`
15
+ # @param host [String, nil] optional - defaults to `ENV['ANTHROPIC_HOST'] w/ fallback to `DEFAULT_HOST`
16
+ # @param version [String, nil] optional - defaults to `ENV['ANTHROPIC_VERSION'] w/ fallback to `DEFAULT_VERSION`
17
+ # @param logger [Logger, nil] optional - defaults to
18
+ # @param timeout [Integer, Hash, nil] optional
19
+ def initialize(
20
+ api_key: ENV.fetch('ANTHROPIC_API_KEY', nil),
21
+ host: ENV.fetch('ANTHROPIC_HOST', DEFAULT_HOST),
22
+ version: ENV.fetch('ANTHROPIC_VERSION', DEFAULT_VERSION),
23
+ logger: nil,
24
+ timeout: nil
25
+ )
26
+ super(api_key:, host:, logger:, timeout:)
27
+ @version = version
28
+ @chat_options[:max_tokens] = 4096
15
29
  end
16
30
  end
17
31
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module OmniAI
4
4
  module Anthropic
5
- VERSION = '1.2.1'
5
+ VERSION = '1.4.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniai-anthropic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-20 00:00:00.000000000 Z
11
+ date: 2024-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: event_stream_parser