claude-ruby 0.3.1 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +2 -1
- data/README.md +45 -11
- data/lib/claude/client.rb +55 -22
- data/lib/claude/ruby/version.rb +1 -1
- data/sig/claude/client.rbs +7 -0
- data/sig/claude/ruby.rbs +0 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9a25095b21f1e2a394c2c783902e72db6f8c442fe9f9e47d7196a02956875db
|
4
|
+
data.tar.gz: 5a7900af12bfaf9cd1d59292641dd9e79af25e8ba5aec21c259f93f85a1b8e54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: faa79c579a7180b187afca76c94cf6a972dfb016f32f0355767d69c4716b81611879fdf6bb4ded4e98e6ce136f8ff56abfab66fc1a623b717e847e229e34a944
|
7
|
+
data.tar.gz: d797536e40b66eb4724071fbf92bce78c282aa7c822da4fc2b4685865f0d87061731c7e48f3dcd7fcf82348766f647615164138e1c8a5e115f9d6c1f3cc5bf09
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
## [0.4.1] - 2024-11-06
|
2
|
+
- Added the latest Claude models
|
3
|
+
-
|
4
|
+
## [0.4.0] - 2024-07-09
|
5
|
+
- Refactored model constants into a separate Model module
|
6
|
+
- Support custom endpoints
|
7
|
+
- Allow timeout to be specified
|
8
|
+
|
1
9
|
## [0.3.1] - 2024-06-29
|
2
10
|
- Updated documentation
|
3
11
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -132,7 +132,7 @@ response = claude_client.messages(messages, { system: system })
|
|
132
132
|
|
133
133
|
## Models
|
134
134
|
|
135
|
-
If you don't specify a model, then the gem will use the latest version of
|
135
|
+
If you don't specify a model, then the gem will use the latest version of Claude Sonnet by default, which is currently ```claude-3-5-sonnet-20240620```
|
136
136
|
|
137
137
|
You can use a different model by specifying it as a parameter in the messages call:
|
138
138
|
|
@@ -143,22 +143,43 @@ response = claude_client.messages(messages, { model: 'claude-3-haiku-20240307' }
|
|
143
143
|
There are some constants defined so you can choose an appropriate model for your use-case and not have to worry about updating it when new Claude models are released:
|
144
144
|
|
145
145
|
```ruby
|
146
|
-
Claude::
|
147
|
-
Claude::
|
148
|
-
Claude::
|
149
|
-
|
150
|
-
Claude::
|
151
|
-
Claude::
|
152
|
-
Claude::
|
153
|
-
Claude::
|
146
|
+
Claude::Model::CLAUDE_OPUS_LATEST
|
147
|
+
Claude::Model::CLAUDE_SONNET_LATEST
|
148
|
+
Claude::Model::CLAUDE_HAIKU_LATEST
|
149
|
+
|
150
|
+
Claude::Model::CLAUDE_FASTEST
|
151
|
+
Claude::Model::CLAUDE_CHEAPEST
|
152
|
+
Claude::Model::CLAUDE_BALANCED
|
153
|
+
Claude::Model::CLAUDE_SMARTEST
|
154
154
|
````
|
155
155
|
|
156
156
|
Example usage:
|
157
157
|
|
158
158
|
```ruby
|
159
|
-
response = claude_client.messages(messages, { model: Claude::
|
159
|
+
response = claude_client.messages(messages, { model: Claude::Model::CLAUDE_CHEAPEST })
|
160
160
|
````
|
161
161
|
|
162
|
+
## Timeout
|
163
|
+
|
164
|
+
You can optionally set a timeout (integer) which will determine the maximum number of seconds to wait for the API call to complete.
|
165
|
+
|
166
|
+
There are two ways to do this:
|
167
|
+
|
168
|
+
1. Set a default timeout when instantiating the claude_client \
|
169
|
+
This timeout value will be used for all API calls unless overridden.
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
claude_client = Claude::Client.new(api_key, timeout: 10)
|
173
|
+
```
|
174
|
+
|
175
|
+
2. Pass in a timeout value as a parameter when calling the messages method. \
|
176
|
+
This timeout value will be used only for that specific messages request.
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
response = claude_client.messages(messages, { timeout: 10 })
|
180
|
+
```
|
181
|
+
|
182
|
+
|
162
183
|
## Parameters
|
163
184
|
|
164
185
|
You can pass in any of the following parameters, which will be included in the Anthropic API call:
|
@@ -173,17 +194,30 @@ stream
|
|
173
194
|
temperature
|
174
195
|
top_p
|
175
196
|
top_k
|
197
|
+
|
198
|
+
timeout (*)
|
176
199
|
````
|
200
|
+
(*) timeout is used for the HTTP request but not passed with the API data
|
177
201
|
|
178
202
|
Example:
|
179
203
|
|
180
204
|
```ruby
|
181
205
|
response = claude_client.messages(messages,
|
182
|
-
{ model: Claude::
|
206
|
+
{ model: Claude::Model::CLAUDE_SMARTEST,
|
183
207
|
max_tokens: 500,
|
184
208
|
temperature: 0.1 })
|
185
209
|
````
|
186
210
|
|
211
|
+
## Custom endpoint
|
212
|
+
|
213
|
+
By default claude-ruby will use the latest official Anthropic API endpoint at the time that the gem version is released.
|
214
|
+
|
215
|
+
You can optionally optionally override this - e.g. for testing, or for using a beta endpoint.
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
claude_client = Claude::Client.new(api_key, endpoint: 'your-custom-endpoint')
|
219
|
+
```
|
220
|
+
|
187
221
|
## Vision
|
188
222
|
|
189
223
|
It's possible to pass an image to the Anthropic API and have Claude describe the image for you.
|
data/lib/claude/client.rb
CHANGED
@@ -2,36 +2,55 @@ require 'httparty'
|
|
2
2
|
require 'json'
|
3
3
|
|
4
4
|
module Claude
|
5
|
-
|
6
|
-
|
5
|
+
module Model
|
6
|
+
CLAUDE_3_OPUS_20240229 = 'claude-3-opus-20240229'
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
CLAUDE_3_5_SONNET_20241022 = 'claude-3-5-sonnet-20241022'
|
9
|
+
CLAUDE_3_5_SONNET_20240620 = 'claude-3-5-sonnet-20240620'
|
10
|
+
CLAUDE_3_SONNET_20240229 = 'claude-3-sonnet-20240229'
|
10
11
|
|
11
|
-
|
12
|
+
CLAUDE_3_5_HAIKU_20241022 = 'claude-3-5-haiku-20241022'
|
13
|
+
CLAUDE_3_HAIKU_20240307 = 'claude-3-haiku-20240307'
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
15
|
+
CLAUDE_OPUS_LATEST = CLAUDE_3_OPUS_20240229
|
16
|
+
CLAUDE_SONNET_LATEST = CLAUDE_3_5_SONNET_20241022
|
17
|
+
CLAUDE_HAIKU_LATEST = CLAUDE_3_5_HAIKU_20241022
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
CLAUDE_FASTEST = CLAUDE_HAIKU_LATEST
|
20
|
+
CLAUDE_CHEAPEST = CLAUDE_3_HAIKU_20240307
|
21
|
+
CLAUDE_BALANCED = CLAUDE_SONNET_LATEST
|
22
|
+
CLAUDE_SMARTEST = CLAUDE_3_5_SONNET_20240620
|
21
23
|
|
22
|
-
|
24
|
+
CLAUDE_DEFAULT = CLAUDE_BALANCED
|
25
|
+
end
|
23
26
|
|
24
|
-
|
27
|
+
class Client
|
28
|
+
|
29
|
+
def initialize(api_key, endpoint: nil, timeout: 60)
|
25
30
|
@api_key = api_key
|
26
|
-
@endpoint =
|
31
|
+
@endpoint = endpoint || anthropic_endpoint
|
32
|
+
@timeout = timeout
|
33
|
+
|
34
|
+
raise(ArgumentError, "api_key is required") if api_key.nil?
|
35
|
+
end
|
36
|
+
|
37
|
+
def version
|
38
|
+
'v1'
|
39
|
+
end
|
40
|
+
|
41
|
+
def anthropic_endpoint
|
42
|
+
"https://api.anthropic.com/#{version}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def messages_endpoint
|
46
|
+
"#{anthropic_endpoint}/messages"
|
27
47
|
end
|
28
48
|
|
29
49
|
def messages(messages, params = {})
|
30
|
-
model = params[:model] ||
|
50
|
+
model = params[:model] || Model::CLAUDE_DEFAULT
|
31
51
|
max_tokens = params[:max_tokens] || 4096
|
32
52
|
system = params[:system] || "You are a helpful assistant."
|
33
|
-
|
34
|
-
url = "#{@endpoint}/messages"
|
53
|
+
timeout = params[:timeout] || @timeout
|
35
54
|
|
36
55
|
data = {
|
37
56
|
model: model,
|
@@ -46,7 +65,7 @@ module Claude
|
|
46
65
|
top_k: params[:top_k],
|
47
66
|
}.compact
|
48
67
|
|
49
|
-
post_api(
|
68
|
+
post_api(messages_endpoint, data, timeout)
|
50
69
|
end
|
51
70
|
|
52
71
|
def headers
|
@@ -70,16 +89,30 @@ module Claude
|
|
70
89
|
response['content'][0]['text']
|
71
90
|
end
|
72
91
|
|
92
|
+
# for backwards compatibility with version 0.3.1
|
93
|
+
def self.const_missing(const_name)
|
94
|
+
if const_name.to_s.match(/^MODEL_(CLAUDE_.+)$/)
|
95
|
+
new_const_name = $1
|
96
|
+
if Claude::Model.constants.include?(new_const_name.to_sym)
|
97
|
+
warn "[DEPRECATION] `#{const_name}` is deprecated. Please use `Claude::Model::#{new_const_name}` instead."
|
98
|
+
Claude::Model.const_get(new_const_name)
|
99
|
+
else
|
100
|
+
super
|
101
|
+
end
|
102
|
+
else
|
103
|
+
super
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
73
107
|
private
|
74
108
|
|
75
|
-
def post_api(url, data)
|
76
|
-
response = HTTParty.post(url, body: data.to_json, headers: headers)
|
109
|
+
def post_api(url, data, timeout)
|
110
|
+
response = HTTParty.post(url, body: data.to_json, headers: headers, timeout: timeout)
|
77
111
|
if response && response['type'] == 'error'
|
78
112
|
raise StandardError.new("#{response['error']['type']}: #{response['error']['message']}")
|
79
113
|
else
|
80
114
|
JSON.parse(response.body)
|
81
115
|
end
|
82
116
|
end
|
83
|
-
|
84
117
|
end
|
85
118
|
end
|
data/lib/claude/ruby/version.rb
CHANGED
data/sig/claude/ruby.rbs
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: claude-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Web Ventures Ltd
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -24,8 +24,8 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
-
description:
|
28
|
-
|
27
|
+
description: Ruby SDK for interacting with the Anthropic API, for generating and streaming
|
28
|
+
messages through Claude AI.
|
29
29
|
email:
|
30
30
|
- webven@mailgab.com
|
31
31
|
executables: []
|
@@ -41,6 +41,7 @@ files:
|
|
41
41
|
- lib/claude/client.rb
|
42
42
|
- lib/claude/ruby.rb
|
43
43
|
- lib/claude/ruby/version.rb
|
44
|
+
- sig/claude/client.rbs
|
44
45
|
- sig/claude/ruby.rbs
|
45
46
|
homepage: https://github.com/webventures/claude-ruby.git
|
46
47
|
licenses:
|