claude-ruby 0.3.1 → 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/CHANGELOG.md +5 -0
- data/README.md +45 -11
- data/lib/claude/client.rb +53 -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: 04c0cdb7e7e035455287513cac3b7576c5a303ded39a41c1fd11b3153d96c318
|
4
|
+
data.tar.gz: 501fab83c266f518ddeb9d5851daab241eb4a2fc8474e432d1853f19fc66d21e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 141041c2b8d7c724bb0ff1e8679ff2c26d9f1a3eb3b9df6224f2b4f6e02e106a4a42ba7a1f26b806597643aab7b575d53dbe6bc60da2aca3fc5f493433132932
|
7
|
+
data.tar.gz: 6317870de2bbea4a224015ee7c2e0555bcb8fc6f2c4c8be173643fac99cef55170795192cddc6add65f65c052af0ba7d3e1758bb5a257e65f0eb82c1fa0d835b
|
data/CHANGELOG.md
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: '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
|
-
|
6
|
-
|
5
|
+
module Model
|
6
|
+
CLAUDE_3_OPUS_20240229 = 'claude-3-opus-20240229'
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
CLAUDE_3_SONNET_20240229 = 'claude-3-sonnet-20240229'
|
9
|
+
CLAUDE_3_5_SONNET_20240620 = 'claude-3-5-sonnet-20240620'
|
10
10
|
|
11
|
-
|
11
|
+
CLAUDE_3_HAIKU_20240307 = 'claude-3-haiku-20240307'
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
22
|
+
CLAUDE_DEFAULT = CLAUDE_SONNET_LATEST
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
+
class Client
|
26
|
+
|
27
|
+
def initialize(api_key, endpoint: nil, timeout: 60)
|
25
28
|
@api_key = api_key
|
26
|
-
@endpoint =
|
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] ||
|
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(
|
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
|
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.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-
|
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:
|
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:
|