claude-ruby 0.3.0 → 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: acba80183a9f1ade551f7c6b948f24b02e0f3d1eedf784a50715c48e4bfbec78
4
- data.tar.gz: 35f49c5e0f3e51da700808a48ecd017dbe5251ab0eaaddccab7439a82a5947fb
3
+ metadata.gz: 04c0cdb7e7e035455287513cac3b7576c5a303ded39a41c1fd11b3153d96c318
4
+ data.tar.gz: 501fab83c266f518ddeb9d5851daab241eb4a2fc8474e432d1853f19fc66d21e
5
5
  SHA512:
6
- metadata.gz: af50a4ec445fe61a69a53e2a5408924fccdd5a229caf5b7d8ee1e6216760ac29d72be36eff6f7e4e7f78150020fa2dbd0807a27af6e414e81d056e613c168012
7
- data.tar.gz: 4540eb9f231f5a366c230e570beeffaa5d0082b90b84dc85a67ad5c33dd94114a6b21b5c017545d815c37e12fb93d1c8a6b2cdf818109abf0ad8c86e00f3b77f
6
+ metadata.gz: 141041c2b8d7c724bb0ff1e8679ff2c26d9f1a3eb3b9df6224f2b4f6e02e106a4a42ba7a1f26b806597643aab7b575d53dbe6bc60da2aca3fc5f493433132932
7
+ data.tar.gz: 6317870de2bbea4a224015ee7c2e0555bcb8fc6f2c4c8be173643fac99cef55170795192cddc6add65f65c052af0ba7d3e1758bb5a257e65f0eb82c1fa0d835b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
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
+
6
+ ## [0.3.1] - 2024-06-29
7
+ - Updated documentation
8
+
1
9
  ## [0.3.0] - 2024-06-29
2
10
 
3
11
  - Added constants for the main Claude models
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- claude-ruby (0.2.1)
4
+ claude-ruby (0.3.1)
5
5
  httparty
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -83,7 +83,7 @@ response['content'][0]['text']
83
83
  messages = [
84
84
  {
85
85
  role: "user",
86
- content: "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"
86
+ content: "In which year was the first ever rugby world cup? (A) 1983 (B) 1987 (C) 1991"
87
87
  },
88
88
  {
89
89
  role: "assistant",
@@ -130,6 +130,94 @@ messages = [
130
130
  response = claude_client.messages(messages, { system: system })
131
131
  ```
132
132
 
133
+ ## Models
134
+
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
+
137
+ You can use a different model by specifying it as a parameter in the messages call:
138
+
139
+ ```ruby
140
+ response = claude_client.messages(messages, { model: 'claude-3-haiku-20240307' })
141
+ ````
142
+
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
+
145
+ ```ruby
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
+ ````
155
+
156
+ Example usage:
157
+
158
+ ```ruby
159
+ response = claude_client.messages(messages, { model: Claude::Model::CLAUDE_CHEAPEST })
160
+ ````
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
+
183
+ ## Parameters
184
+
185
+ You can pass in any of the following parameters, which will be included in the Anthropic API call:
186
+
187
+ ```ruby
188
+ model
189
+ system
190
+ max_tokens
191
+ metadata
192
+ stop_sequences
193
+ stream
194
+ temperature
195
+ top_p
196
+ top_k
197
+
198
+ timeout (*)
199
+ ````
200
+ (*) timeout is used for the HTTP request but not passed with the API data
201
+
202
+ Example:
203
+
204
+ ```ruby
205
+ response = claude_client.messages(messages,
206
+ { model: Claude::Model::CLAUDE_SMARTEST,
207
+ max_tokens: 500,
208
+ temperature: 0.1 })
209
+ ````
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
+
133
221
  ## Vision
134
222
 
135
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.0"
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.0
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: []
@@ -38,10 +38,10 @@ files:
38
38
  - LICENSE.txt
39
39
  - README.md
40
40
  - Rakefile
41
- - claude-ruby-0.2.1.gem
42
41
  - lib/claude/client.rb
43
42
  - lib/claude/ruby.rb
44
43
  - lib/claude/ruby/version.rb
44
+ - sig/claude/client.rbs
45
45
  - sig/claude/ruby.rbs
46
46
  homepage: https://github.com/webventures/claude-ruby.git
47
47
  licenses:
Binary file