claude-ruby 0.3.1 → 0.4.0

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: 04c0cdb7e7e035455287513cac3b7576c5a303ded39a41c1fd11b3153d96c318
4
+ data.tar.gz: 501fab83c266f518ddeb9d5851daab241eb4a2fc8474e432d1853f19fc66d21e
5
5
  SHA512:
6
- metadata.gz: 5dba3fa917472c533a8d515624346ec40ccd95ebd79e803ad9d0fd536a4aaab3b4bb0c9281d97a9baf0c12c87c7c2849b23dec5a51128e28a2099f47274fbf05
7
- data.tar.gz: a0e033b78ced858eae7d8fb196f40ecb04ef540dadcd76e77ba8e2cff177b12642579cbcb0114d27da1336dd26846fa10ebb3cfa2ae0641870e0db33b2c278e0
6
+ metadata.gz: 141041c2b8d7c724bb0ff1e8679ff2c26d9f1a3eb3b9df6224f2b4f6e02e106a4a42ba7a1f26b806597643aab7b575d53dbe6bc60da2aca3fc5f493433132932
7
+ data.tar.gz: 6317870de2bbea4a224015ee7c2e0555bcb8fc6f2c4c8be173643fac99cef55170795192cddc6add65f65c052af0ba7d3e1758bb5a257e65f0eb82c1fa0d835b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## [0.4.0] - 2024-07-09
2
+ - Refactored model constants into a separate Model module
3
+ - Support custom endpoints
4
+ - Allow timeout to be specified
5
+
1
6
  ## [0.3.1] - 2024-06-29
2
7
  - Updated documentation
3
8
 
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: 'you-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,53 @@ 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_SONNET_20240229 = 'claude-3-sonnet-20240229'
9
+ CLAUDE_3_5_SONNET_20240620 = 'claude-3-5-sonnet-20240620'
10
10
 
11
- MODEL_CLAUDE_3_HAIKU_20240307 = 'claude-3-haiku-20240307'
11
+ CLAUDE_3_HAIKU_20240307 = 'claude-3-haiku-20240307'
12
12
 
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
13
+ CLAUDE_OPUS_LATEST = CLAUDE_3_OPUS_20240229
14
+ CLAUDE_SONNET_LATEST = CLAUDE_3_5_SONNET_20240620
15
+ CLAUDE_HAIKU_LATEST = CLAUDE_3_HAIKU_20240307
16
16
 
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
17
+ CLAUDE_FASTEST = CLAUDE_HAIKU_LATEST
18
+ CLAUDE_CHEAPEST = CLAUDE_HAIKU_LATEST
19
+ CLAUDE_BALANCED = CLAUDE_SONNET_LATEST
20
+ CLAUDE_SMARTEST = CLAUDE_3_5_SONNET_20240620
21
21
 
22
- MODEL_CLAUDE_DEFAULT = MODEL_CLAUDE_SONNET_LATEST
22
+ CLAUDE_DEFAULT = CLAUDE_SONNET_LATEST
23
+ end
23
24
 
24
- def initialize(api_key)
25
+ class Client
26
+
27
+ def initialize(api_key, endpoint: nil, timeout: 60)
25
28
  @api_key = api_key
26
- @endpoint = 'https://api.anthropic.com/v1'
29
+ @endpoint = endpoint || anthropic_endpoint
30
+ @timeout = timeout
31
+
32
+ raise(ArgumentError, "api_key is required") if api_key.nil?
33
+ end
34
+
35
+ def version
36
+ 'v1'
37
+ end
38
+
39
+ def anthropic_endpoint
40
+ "https://api.anthropic.com/#{version}"
41
+ end
42
+
43
+ def messages_endpoint
44
+ "#{anthropic_endpoint}/messages"
27
45
  end
28
46
 
29
47
  def messages(messages, params = {})
30
- model = params[:model] || MODEL_CLAUDE_DEFAULT
48
+ model = params[:model] || Model::CLAUDE_DEFAULT
31
49
  max_tokens = params[:max_tokens] || 4096
32
50
  system = params[:system] || "You are a helpful assistant."
33
-
34
- url = "#{@endpoint}/messages"
51
+ timeout = params[:timeout] || @timeout
35
52
 
36
53
  data = {
37
54
  model: model,
@@ -46,7 +63,7 @@ module Claude
46
63
  top_k: params[:top_k],
47
64
  }.compact
48
65
 
49
- post_api(url, data)
66
+ post_api(messages_endpoint, data, timeout)
50
67
  end
51
68
 
52
69
  def headers
@@ -70,16 +87,30 @@ module Claude
70
87
  response['content'][0]['text']
71
88
  end
72
89
 
90
+ # for backwards compatibility with version 0.3.1
91
+ def self.const_missing(const_name)
92
+ if const_name.to_s.match(/^MODEL_(CLAUDE_.+)$/)
93
+ new_const_name = $1
94
+ if Claude::Model.constants.include?(new_const_name.to_sym)
95
+ warn "[DEPRECATION] `#{const_name}` is deprecated. Please use `Claude::Model::#{new_const_name}` instead."
96
+ Claude::Model.const_get(new_const_name)
97
+ else
98
+ super
99
+ end
100
+ else
101
+ super
102
+ end
103
+ end
104
+
73
105
  private
74
106
 
75
- def post_api(url, data)
76
- response = HTTParty.post(url, body: data.to_json, headers: headers)
107
+ def post_api(url, data, timeout)
108
+ response = HTTParty.post(url, body: data.to_json, headers: headers, timeout: timeout)
77
109
  if response && response['type'] == 'error'
78
110
  raise StandardError.new("#{response['error']['type']}: #{response['error']['message']}")
79
111
  else
80
112
  JSON.parse(response.body)
81
113
  end
82
114
  end
83
-
84
115
  end
85
116
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Claude
4
4
  module Ruby
5
- VERSION = "0.3.1"
5
+ VERSION = "0.4.0"
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.0
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-07-09 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: