anthropic-rb 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.reek.yml +3 -0
- data/CHANGELOG.md +17 -1
- data/README.md +55 -19
- data/lib/anthropic/client.rb +4 -4
- data/lib/anthropic/messages.rb +62 -0
- data/lib/anthropic/version.rb +1 -1
- data/lib/anthropic.rb +5 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 137b1e285afd97d1ec45ba94685dfcb17afd4388dbd43dbad200ef61228952bd
|
4
|
+
data.tar.gz: 465bc7914a25c1f438539364de3fe753f379879273ffafac0818305db60d782c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34e45517ffa6f2b35a550f8ff1827618b1a01068cc685f8e7980ea893025cd3811609f80cf2ce5c3d199507252701ad4efcc4ddb7cf2d462b996b5c9aa3229f3
|
7
|
+
data.tar.gz: 2eddc00d9f8c86c45187a332a93e1d218f04af1eaf8cac45ccdcc767dba4e3c9217c17b80753b1c53d64c2b80aa2fa2c8940af1fe56a0c630766ab8e2f4ef0d5
|
data/.reek.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,20 @@
|
|
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
|
+
|
12
|
+
## [0.2.5] - 2023-12-27
|
13
|
+
|
14
|
+
### Fixed
|
15
|
+
|
16
|
+
- Fix documentation for streaming completions.
|
17
|
+
- Yank 0.2.4 release.
|
18
|
+
|
5
19
|
## [0.2.3] - 2023-12-27
|
6
20
|
|
7
21
|
### Fixed
|
@@ -33,7 +47,9 @@
|
|
33
47
|
|
34
48
|
- Initial release
|
35
49
|
|
36
|
-
[Unreleased]: https://github.com/dickdavis/anthropic-rb/compare/v0.
|
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
|
52
|
+
[0.2.5]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.3...v0.2.5
|
37
53
|
[0.2.3]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.2...v0.2.3
|
38
54
|
[0.2.2]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.1...v0.2.2
|
39
55
|
[0.2.1]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.0...v0.2.1
|
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
|
-
|
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
|
|
@@ -50,26 +86,26 @@ Anthropic.completions.create(
|
|
50
86
|
Alternatively, you can stream the response:
|
51
87
|
|
52
88
|
```ruby
|
53
|
-
Anthropic.completions.create(model: 'claude-2', max_tokens_to_sample: 200, prompt: 'Human: Yo what up?\n\nAssistant:') do |event|
|
89
|
+
Anthropic.completions.create(model: 'claude-2', max_tokens_to_sample: 200, prompt: 'Human: Yo what up?\n\nAssistant:', stream: true) do |event|
|
54
90
|
puts event
|
55
91
|
end
|
56
92
|
|
57
93
|
# Output =>
|
58
|
-
# {:
|
59
|
-
# {:
|
60
|
-
# {:
|
61
|
-
# {:
|
62
|
-
# {:
|
63
|
-
# {:
|
64
|
-
# {:
|
65
|
-
# {:
|
66
|
-
# {:
|
67
|
-
# {:
|
68
|
-
# {:
|
69
|
-
# {:
|
70
|
-
# {:
|
71
|
-
# {:
|
72
|
-
# {:
|
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
|
data/lib/anthropic/client.rb
CHANGED
@@ -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
|
data/lib/anthropic/version.rb
CHANGED
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.
|
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
|