anthropic-rb 0.3.0 → 0.4.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 +12 -1
- data/README.md +32 -5
- data/anthropic-rb.gemspec +42 -0
- data/lib/anthropic/messages.rb +15 -1
- data/lib/anthropic/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5e066105f3157621c27e2f974ed43bde9f14e6e55187d1d7a4f095b79a5a1ba
|
4
|
+
data.tar.gz: f32d479f7ec06c29a0347f69084a4378a64d69233d78ed280f5a1c924e15d708
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90040c86504a3dfd28e0093d1b8525252387f4e978271908aa158d1cffe10a3dd5766aadc243bc31f45ca6dd38ecf1259d009b2da9f2f78670fcc114c76574a3
|
7
|
+
data.tar.gz: 8ca46c62fe204b3b2403cce1267b6661f43c52159c8d31877b9d2522be604427996eedcbac67ba22a1b6df703259ededd98ea2b86d49755d7aa057835458321c
|
data/.reek.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,16 @@
|
|
2
2
|
|
3
3
|
## [Unreleased]
|
4
4
|
|
5
|
+
## [0.4.0] - 2024-04-20
|
6
|
+
|
7
|
+
### Added
|
8
|
+
|
9
|
+
- Add support for the tools beta.
|
10
|
+
|
11
|
+
### Updated
|
12
|
+
|
13
|
+
- Remove beta header for Messages API as the API is no longer in beta.
|
14
|
+
|
5
15
|
## [0.3.0] - 2023-12-28
|
6
16
|
|
7
17
|
### Added
|
@@ -47,7 +57,8 @@
|
|
47
57
|
|
48
58
|
- Initial release
|
49
59
|
|
50
|
-
[Unreleased]: https://github.com/dickdavis/anthropic-rb/compare/v0.
|
60
|
+
[Unreleased]: https://github.com/dickdavis/anthropic-rb/compare/v0.4.0...HEAD
|
61
|
+
[0.4.0]: https://github.com/dickdavis/anthropic-rb/compare/v0.3.0...v0.4.0
|
51
62
|
[0.3.0]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.5...v0.3.0
|
52
63
|
[0.2.5]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.3...v0.2.5
|
53
64
|
[0.2.3]: https://github.com/dickdavis/anthropic-rb/compare/v0.2.2...v0.2.3
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@ The goal of this project is feature parity with Anthropic's Python SDK until an
|
|
9
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
|
-
require 'anthropic
|
12
|
+
require 'anthropic'
|
13
13
|
|
14
14
|
Anthropic.setup do |config|
|
15
15
|
config.api_key = 'YOUR_API_KEY'
|
@@ -19,7 +19,7 @@ end
|
|
19
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
|
-
require 'anthropic
|
22
|
+
require 'anthropic'
|
23
23
|
|
24
24
|
Anthropic.setup do |config|
|
25
25
|
config.api_version = '2023-06-01'
|
@@ -28,10 +28,10 @@ end
|
|
28
28
|
|
29
29
|
### Messages API
|
30
30
|
|
31
|
-
You can send a request to the Messages API.
|
31
|
+
You can send a request to the Messages API.
|
32
32
|
|
33
33
|
```ruby
|
34
|
-
Anthropic.messages
|
34
|
+
Anthropic.messages.create(model: 'claude-2.1', max_tokens: 200, messages: [{role: 'user', content: 'Yo what up?'}])
|
35
35
|
|
36
36
|
# Output =>
|
37
37
|
# {
|
@@ -48,7 +48,7 @@ Anthropic.messages(beta: true).create(model: 'claude-2.1', max_tokens: 200, mess
|
|
48
48
|
Alternatively, you can stream the response:
|
49
49
|
|
50
50
|
```ruby
|
51
|
-
Anthropic.messages
|
51
|
+
Anthropic.messages.create(model: 'claude-2.1', max_tokens: 200, messages: [{role: 'user', content: 'Yo what up?'}], stream: true) do |event|
|
52
52
|
puts event
|
53
53
|
end
|
54
54
|
|
@@ -62,6 +62,33 @@ end
|
|
62
62
|
# { type: 'message_stop' }
|
63
63
|
```
|
64
64
|
|
65
|
+
You can also experiment with the new tools beta by passing the `beta` flag when calling the API. This will ensure each request includes the correct beta header.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
tools = [
|
69
|
+
{
|
70
|
+
name: 'get_weather',
|
71
|
+
description: 'Get the current weather in a given location',
|
72
|
+
input_schema: {
|
73
|
+
type: 'object',
|
74
|
+
properties: {
|
75
|
+
location: { type: 'string' }
|
76
|
+
},
|
77
|
+
required: ['location']
|
78
|
+
}
|
79
|
+
}
|
80
|
+
]
|
81
|
+
|
82
|
+
Anthropic.messages(beta: true).create(
|
83
|
+
model: 'claude-3-opus-20240229',
|
84
|
+
max_tokens: 200,
|
85
|
+
tools:,
|
86
|
+
messages: [{role: 'user', content: 'What is the weather like in Nashville?'}]
|
87
|
+
)
|
88
|
+
```
|
89
|
+
|
90
|
+
Streaming is currently not supported by the tools beta. You can find out more information about tools in the [documentation](https://docs.anthropic.com/claude/docs/tool-use).
|
91
|
+
|
65
92
|
### Completions API
|
66
93
|
|
67
94
|
To make a request to the Completions API:
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/anthropic/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'anthropic-rb'
|
7
|
+
spec.version = Anthropic::VERSION
|
8
|
+
spec.authors = ['Dick Davis']
|
9
|
+
spec.email = ['dick@hey.com']
|
10
|
+
|
11
|
+
spec.summary = 'Ruby bindings for the Anthropic API'
|
12
|
+
spec.homepage = 'https://github.com/dickdavis/anthropic-rb'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
16
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
17
|
+
spec.metadata['source_code_uri'] = 'https://github.com/dickdavis/anthropic-rb'
|
18
|
+
spec.metadata['changelog_uri'] = 'https://github.com/dickdavis/anthropic-rb/blob/main/CHANGELOG.md'
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(__dir__) do
|
23
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
24
|
+
(File.expand_path(f) == __FILE__) ||
|
25
|
+
f.start_with?(*%w[bin/ spec/ .git .github/ Gemfile])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
spec.bindir = 'exe'
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ['lib']
|
31
|
+
|
32
|
+
# Uncomment to register a new dependency of your gem
|
33
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, check out our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
38
|
+
|
39
|
+
spec.required_ruby_version = '>= 3.1'
|
40
|
+
spec.add_dependency 'httpx', '>= 1.1.5'
|
41
|
+
spec.add_dependency 'json-schema', '>= 4.1.1'
|
42
|
+
end
|
data/lib/anthropic/messages.rb
CHANGED
@@ -7,6 +7,9 @@ module Anthropic
|
|
7
7
|
# Error for when the API version is not supported.
|
8
8
|
class UnsupportedApiVersionError < StandardError; end
|
9
9
|
|
10
|
+
# Error for when a beta feature is not used correctly
|
11
|
+
class UnsupportedBetaOptionError < StandardError; end
|
12
|
+
|
10
13
|
ENDPOINT = 'https://api.anthropic.com/v1/messages'
|
11
14
|
V1_SCHEMA = {
|
12
15
|
type: 'object',
|
@@ -18,6 +21,14 @@ module Anthropic
|
|
18
21
|
system: { type: 'string' },
|
19
22
|
stop_sequences: { type: 'array', items: { type: 'string' } },
|
20
23
|
temperature: { type: 'number' },
|
24
|
+
tools: {
|
25
|
+
type: 'array',
|
26
|
+
items: {
|
27
|
+
name: { type: 'string' },
|
28
|
+
description: { type: 'string' },
|
29
|
+
input_schema: { type: 'object' }
|
30
|
+
}
|
31
|
+
},
|
21
32
|
top_k: { type: 'integer' },
|
22
33
|
top_p: { type: 'number' },
|
23
34
|
metadata: { type: 'object' },
|
@@ -31,7 +42,10 @@ module Anthropic
|
|
31
42
|
end
|
32
43
|
|
33
44
|
def create(**params, &)
|
45
|
+
raise UnsupportedBetaOptionError, 'Tool use is not yet supported in streaming mode' if params[:stream] && beta
|
46
|
+
|
34
47
|
JSON::Validator.validate!(schema_for_api_version, params)
|
48
|
+
|
35
49
|
return Anthropic::Client.post(ENDPOINT, params, additional_headers) unless params[:stream]
|
36
50
|
|
37
51
|
Anthropic::Client.post_as_stream(ENDPOINT, params, additional_headers, &)
|
@@ -56,7 +70,7 @@ module Anthropic
|
|
56
70
|
def additional_headers
|
57
71
|
return {} unless beta
|
58
72
|
|
59
|
-
{ 'anthropic-beta' => '
|
73
|
+
{ 'anthropic-beta' => 'tools-2024-04-04' }
|
60
74
|
end
|
61
75
|
end
|
62
76
|
end
|
data/lib/anthropic/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anthropic-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dick Davis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httpx
|
@@ -54,6 +54,7 @@ files:
|
|
54
54
|
- LICENSE.txt
|
55
55
|
- README.md
|
56
56
|
- Rakefile
|
57
|
+
- anthropic-rb.gemspec
|
57
58
|
- lib/anthropic.rb
|
58
59
|
- lib/anthropic/client.rb
|
59
60
|
- lib/anthropic/completions.rb
|