anthropic-rb 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e3bf7350209fe6270479e8bc20f1418975257b6cfb1beb2d67547eec8d4e168
4
- data.tar.gz: 38c41a98c7095491ade15304025a8d6febb35ec10d758a7a0b725ad23a4e19e7
3
+ metadata.gz: 137b1e285afd97d1ec45ba94685dfcb17afd4388dbd43dbad200ef61228952bd
4
+ data.tar.gz: 465bc7914a25c1f438539364de3fe753f379879273ffafac0818305db60d782c
5
5
  SHA512:
6
- metadata.gz: a69135f37ef25151067595106f6955c4fea146d4a95eb4093925d4a8eb26f9be46b4dcf4920e3a0cc8e55b462690a23ee929ab93468b358d51169aea90ed9f3d
7
- data.tar.gz: 0405140a9574e4e4afa23504714f25687ed7a47acb5a02adec25e4b5e7ab6007ab3d160835e82a46795dba63f91b83801ba671db32cc8f5e47098e86278d9c06
6
+ metadata.gz: 34e45517ffa6f2b35a550f8ff1827618b1a01068cc685f8e7980ea893025cd3811609f80cf2ce5c3d199507252701ad4efcc4ddb7cf2d462b996b5c9aa3229f3
7
+ data.tar.gz: 2eddc00d9f8c86c45187a332a93e1d218f04af1eaf8cac45ccdcc767dba4e3c9217c17b80753b1c53d64c2b80aa2fa2c8940af1fe56a0c630766ab8e2f4ef0d5
data/.reek.yml CHANGED
@@ -5,3 +5,6 @@ detectors:
5
5
  TooManyStatements:
6
6
  exclude:
7
7
  - 'Anthropic::Client#self.post'
8
+ BooleanParameter:
9
+ exclude:
10
+ - 'Anthropic::Messages#initialize'
data/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.3.0] - 2023-12-28
6
+
7
+ ### Added
8
+
9
+ - Add support for sending headers to client.
10
+ - Add support for beta Messages API.
11
+
5
12
  ## [0.2.5] - 2023-12-27
6
13
 
7
14
  ### Fixed
@@ -40,7 +47,8 @@
40
47
 
41
48
  - Initial release
42
49
 
43
- [Unreleased]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.5...HEAD
50
+ [Unreleased]: https://github.com/dickdavis/anthropic-rb/compare/v0.3.0...HEAD
51
+ [0.3.0]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.5...v0.3.0
44
52
  [0.2.5]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.3...v0.2.5
45
53
  [0.2.3]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.2...v0.2.3
46
54
  [0.2.2]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.1...v0.2.2
data/README.md CHANGED
@@ -6,7 +6,7 @@ The goal of this project is feature parity with Anthropic's Python SDK until an
6
6
 
7
7
  ## Usage
8
8
 
9
- anthropic-rb will default to the value of the `ANTHROPIC_API_KEY` environment variable. However, you may initialize the library with your API key:
9
+ anthropic-rb will default to the value of the `ANTHROPIC_API_KEY` environment variable. However, you may initialize the library with your API key. You must set your API key before using the library.
10
10
 
11
11
  ```ruby
12
12
  require 'anthropic-rb'
@@ -16,7 +16,7 @@ Anthropic.setup do |config|
16
16
  end
17
17
  ```
18
18
 
19
- You can also specify an API version to use by setting the `ANTHROPIC_API_VERSION` environment variable or during initialization:
19
+ You can also specify an API version to use by setting the `ANTHROPIC_API_VERSION` environment variable or during initialization. This is optional; if not set, the library will default to `2023-06-01`.
20
20
 
21
21
  ```ruby
22
22
  require 'anthropic-rb'
@@ -26,7 +26,43 @@ Anthropic.setup do |config|
26
26
  end
27
27
  ```
28
28
 
29
- The default API version is `2023-06-01`.
29
+ ### Messages API
30
+
31
+ You can send a request to the Messages API. The Messages API is currently in beta; as such, you'll need to pass the `beta` flag when calling the API to ensure the correct header is included.
32
+
33
+ ```ruby
34
+ Anthropic.messages(beta: true).create(model: 'claude-2.1', max_tokens: 200, messages: [{role: 'user', content: 'Yo what up?'}])
35
+
36
+ # Output =>
37
+ # {
38
+ # id: "msg_013ePdwEkb4RMC1hCE61Hbm8",
39
+ # type: "message",
40
+ # role: "assistant",
41
+ # content: [{type: "text", text: "Hello! Not much up with me, just chatting. How about you?"}],
42
+ # model: "claude-2.1",
43
+ # stop_reason: "end_turn",
44
+ # stop_sequence: nil
45
+ # }
46
+ ```
47
+
48
+ Alternatively, you can stream the response:
49
+
50
+ ```ruby
51
+ Anthropic.messages(beta: true).create(model: 'claude-2.1', max_tokens: 200, messages: [{role: 'user', content: 'Yo what up?'}], stream: true) do |event|
52
+ puts event
53
+ end
54
+
55
+ # Output =>
56
+ # { type: 'message_start', message: { id: 'msg_012pkeozZynwyNvSagwL7kMw', type: 'message', role: 'assistant', content: [], model: 'claude-2.1', stop_reason: nil, stop_sequence: nil } }
57
+ # { type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } }
58
+ # { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'Hello' } }
59
+ # { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: '.' } }
60
+ # { type: 'content_block_stop', index: 0 }
61
+ # { type: 'message_delta', delta: { stop_reason: 'end_turn', stop_sequence: nil } }
62
+ # { type: 'message_stop' }
63
+ ```
64
+
65
+ ### Completions API
30
66
 
31
67
  To make a request to the Completions API:
32
68
 
@@ -55,21 +91,21 @@ Anthropic.completions.create(model: 'claude-2', max_tokens_to_sample: 200, promp
55
91
  end
56
92
 
57
93
  # Output =>
58
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>" Hello", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
59
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>"!", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
60
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>" Not", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
61
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>" much", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
62
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>",", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
63
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>" just", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
64
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>" chatting", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
65
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>" with", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
66
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>" people", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
67
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>".", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
68
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>" How", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
69
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>" about", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
70
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>" you", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
71
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>"?", :stop_reason=>nil, :model=>"claude-2.1", :stop=>nil, :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
72
- # {:type=>"completion", :id=>"compl_01G6cEfdZtLEEJVRzwUShiDY", :completion=>"", :stop_reason=>"stop_sequence", :model=>"claude-2.1", :stop=>"\n\nHuman:", :log_id=>"compl_01G6cEfdZtLEEJVRzwUShiDY"}
94
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: ' Hello', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
95
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: '!', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
96
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: ' Not', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
97
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: ' much', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
98
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: ',', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
99
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: ' just', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
100
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: ' chatting', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
101
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: ' with', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
102
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: ' people', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
103
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: '.', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
104
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: ' How', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
105
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: ' about', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
106
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: ' you', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
107
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: '?', stop_reason: nil, model: 'claude-2.1', stop: nil, log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
108
+ # { type: 'completion', id: 'compl_01G6cEfdZtLEEJVRzwUShiDY', completion: '', stop_reason: 'stop_sequence', model: 'claude-2.1', stop: "\n\nHuman:", log_id: 'compl_01G6cEfdZtLEEJVRzwUShiDY' }
73
109
  ```
74
110
 
75
111
  ## Installation
@@ -32,13 +32,13 @@ module Anthropic
32
32
  # Provides a client for sending HTTP requests.
33
33
  class Client
34
34
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
35
- def self.post(url, data)
35
+ def self.post(url, data, headers = {})
36
36
  response = HTTPX.with(
37
37
  headers: {
38
38
  'Content-Type' => 'application/json',
39
39
  'x-api-key' => Anthropic.api_key,
40
40
  'anthropic-version' => Anthropic.api_version
41
- }
41
+ }.merge(headers)
42
42
  ).post(url, json: data)
43
43
 
44
44
  response_body = JSON.parse(response.body, symbolize_names: true)
@@ -65,13 +65,13 @@ module Anthropic
65
65
  end
66
66
  end
67
67
 
68
- def self.post_as_stream(url, data)
68
+ def self.post_as_stream(url, data, headers = {})
69
69
  response = HTTPX.plugin(:stream).with(
70
70
  headers: {
71
71
  'Content-Type' => 'application/json',
72
72
  'x-api-key' => Anthropic.api_key,
73
73
  'anthropic-version' => Anthropic.api_version
74
- }
74
+ }.merge(headers)
75
75
  ).post(url, json: data, stream: true)
76
76
 
77
77
  response.each_line do |line|
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ ##
5
+ # Provides bindings for the Anthropic messages API
6
+ class Messages
7
+ # Error for when the API version is not supported.
8
+ class UnsupportedApiVersionError < StandardError; end
9
+
10
+ ENDPOINT = 'https://api.anthropic.com/v1/messages'
11
+ V1_SCHEMA = {
12
+ type: 'object',
13
+ required: %w[model messages max_tokens],
14
+ properties: {
15
+ model: { type: 'string' },
16
+ messages: { type: 'array' },
17
+ max_tokens: { type: 'integer' },
18
+ system: { type: 'string' },
19
+ stop_sequences: { type: 'array', items: { type: 'string' } },
20
+ temperature: { type: 'number' },
21
+ top_k: { type: 'integer' },
22
+ top_p: { type: 'number' },
23
+ metadata: { type: 'object' },
24
+ stream: { type: 'boolean' }
25
+ },
26
+ additionalProperties: false
27
+ }.freeze
28
+
29
+ def initialize(beta: false)
30
+ @beta = beta
31
+ end
32
+
33
+ def create(**params, &)
34
+ JSON::Validator.validate!(schema_for_api_version, params)
35
+ return Anthropic::Client.post(ENDPOINT, params, additional_headers) unless params[:stream]
36
+
37
+ Anthropic::Client.post_as_stream(ENDPOINT, params, additional_headers, &)
38
+ rescue JSON::Schema::ValidationError => error
39
+ raise ArgumentError, error.message
40
+ end
41
+
42
+ private
43
+
44
+ attr_reader :beta
45
+
46
+ def schema_for_api_version
47
+ api_version = Anthropic.api_version
48
+ case api_version
49
+ when '2023-06-01'
50
+ V1_SCHEMA
51
+ else
52
+ raise UnsupportedApiVersionError, "Unsupported API version: #{api_version}"
53
+ end
54
+ end
55
+
56
+ def additional_headers
57
+ return {} unless beta
58
+
59
+ { 'anthropic-beta' => 'messages-2023-12-15' }
60
+ end
61
+ end
62
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Anthropic
4
- VERSION = '0.2.5'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/anthropic.rb CHANGED
@@ -6,6 +6,7 @@ require 'json-schema'
6
6
 
7
7
  require_relative 'anthropic/client'
8
8
  require_relative 'anthropic/completions'
9
+ require_relative 'anthropic/messages'
9
10
  require_relative 'anthropic/version'
10
11
 
11
12
  ##
@@ -43,4 +44,8 @@ module Anthropic
43
44
  def self.completions
44
45
  Completions.new
45
46
  end
47
+
48
+ def self.messages(...)
49
+ Messages.new(...)
50
+ end
46
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anthropic-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dick Davis
@@ -57,6 +57,7 @@ files:
57
57
  - lib/anthropic.rb
58
58
  - lib/anthropic/client.rb
59
59
  - lib/anthropic/completions.rb
60
+ - lib/anthropic/messages.rb
60
61
  - lib/anthropic/version.rb
61
62
  - sig/anthropic/rb.rbs
62
63
  homepage: https://github.com/dickdavis/anthropic-rb