claude-ruby 0.3.1 → 0.4.1

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: f0914fd554c4c01686d477c44cf2de423f0faad30d673e39a767d0296da1cac4
4
- data.tar.gz: fe9e0aa7f262c1af5fe3b0036136739a87f718c040f107c7ad792cb1492fe752
3
+ metadata.gz: f9a25095b21f1e2a394c2c783902e72db6f8c442fe9f9e47d7196a02956875db
4
+ data.tar.gz: 5a7900af12bfaf9cd1d59292641dd9e79af25e8ba5aec21c259f93f85a1b8e54
5
5
  SHA512:
6
- metadata.gz: 5dba3fa917472c533a8d515624346ec40ccd95ebd79e803ad9d0fd536a4aaab3b4bb0c9281d97a9baf0c12c87c7c2849b23dec5a51128e28a2099f47274fbf05
7
- data.tar.gz: a0e033b78ced858eae7d8fb196f40ecb04ef540dadcd76e77ba8e2cff177b12642579cbcb0114d27da1336dd26846fa10ebb3cfa2ae0641870e0db33b2c278e0
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- claude-ruby (0.3.1)
4
+ claude-ruby (0.4.1)
5
5
  httparty
6
6
 
7
7
  GEM
@@ -51,6 +51,7 @@ GEM
51
51
  PLATFORMS
52
52
  arm64-darwin-22
53
53
  ruby
54
+ x86_64-linux
54
55
 
55
56
  DEPENDENCIES
56
57
  claude-ruby!
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 Clause Sonnet by default, which is currently ```claude-3-5-sonnet-20240620```
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::Client::MODEL_CLAUDE_OPUS_LATEST
147
- Claude::Client::MODEL_CLAUDE_SONNET_LATEST
148
- Claude::Client::MODEL_CLAUDE_HAIKU_LATEST
149
-
150
- Claude::Client::MODEL_CLAUDE_FASTEST
151
- Claude::Client::MODEL_CLAUDE_CHEAPEST
152
- Claude::Client::MODEL_CLAUDE_BALANCED
153
- Claude::Client::MODEL_CLAUDE_SMARTEST
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::Client::MODEL_CLAUDE_CHEAPEST })
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::Client::MODEL_CLAUDE_SMARTEST,
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
- class Client
6
- MODEL_CLAUDE_3_OPUS_20240229 = 'claude-3-opus-20240229'
5
+ module Model
6
+ CLAUDE_3_OPUS_20240229 = 'claude-3-opus-20240229'
7
7
 
8
- MODEL_CLAUDE_3_SONNET_20240229 = 'claude-3-sonnet-20240229'
9
- MODEL_CLAUDE_3_5_SONNET_20240620 = 'claude-3-5-sonnet-20240620'
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
- MODEL_CLAUDE_3_HAIKU_20240307 = 'claude-3-haiku-20240307'
12
+ CLAUDE_3_5_HAIKU_20241022 = 'claude-3-5-haiku-20241022'
13
+ CLAUDE_3_HAIKU_20240307 = 'claude-3-haiku-20240307'
12
14
 
13
- MODEL_CLAUDE_OPUS_LATEST = MODEL_CLAUDE_3_OPUS_20240229
14
- MODEL_CLAUDE_SONNET_LATEST = MODEL_CLAUDE_3_5_SONNET_20240620
15
- MODEL_CLAUDE_HAIKU_LATEST = MODEL_CLAUDE_3_HAIKU_20240307
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
- MODEL_CLAUDE_FASTEST = MODEL_CLAUDE_HAIKU_LATEST
18
- MODEL_CLAUDE_CHEAPEST = MODEL_CLAUDE_HAIKU_LATEST
19
- MODEL_CLAUDE_BALANCED = MODEL_CLAUDE_SONNET_LATEST
20
- MODEL_CLAUDE_SMARTEST = MODEL_CLAUDE_3_5_SONNET_20240620
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
- MODEL_CLAUDE_DEFAULT = MODEL_CLAUDE_SONNET_LATEST
24
+ CLAUDE_DEFAULT = CLAUDE_BALANCED
25
+ end
23
26
 
24
- def initialize(api_key)
27
+ class Client
28
+
29
+ def initialize(api_key, endpoint: nil, timeout: 60)
25
30
  @api_key = api_key
26
- @endpoint = 'https://api.anthropic.com/v1'
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] || MODEL_CLAUDE_DEFAULT
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(url, data)
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Claude
4
4
  module Ruby
5
- VERSION = "0.3.1"
5
+ VERSION = "0.4.1"
6
6
  end
7
7
  end
@@ -0,0 +1,7 @@
1
+ module Claude
2
+ class Client
3
+ @api_key: String
4
+ @endpoint: String
5
+ @timeout: Integer
6
+ end
7
+ end
data/sig/claude/ruby.rbs CHANGED
@@ -1,6 +1,5 @@
1
1
  module Claude
2
2
  module Ruby
3
3
  VERSION: String
4
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
4
  end
6
5
  end
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.3.1
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-06-29 00:00:00.000000000 Z
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: Unofficial ruby SDK for interacting with the Anthropic API, for generating
28
- and streaming messages through Claude.
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: