claude-ruby 0.2.0 → 0.3.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 +14 -0
- data/Gemfile.lock +2 -15
- data/README.md +69 -11
- data/claude-ruby-0.2.1.gem +0 -0
- data/lib/claude/client.rb +34 -3
- data/lib/claude/ruby/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: acba80183a9f1ade551f7c6b948f24b02e0f3d1eedf784a50715c48e4bfbec78
|
4
|
+
data.tar.gz: 35f49c5e0f3e51da700808a48ecd017dbe5251ab0eaaddccab7439a82a5947fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af50a4ec445fe61a69a53e2a5408924fccdd5a229caf5b7d8ee1e6216760ac29d72be36eff6f7e4e7f78150020fa2dbd0807a27af6e414e81d056e613c168012
|
7
|
+
data.tar.gz: 4540eb9f231f5a366c230e570beeffaa5d0082b90b84dc85a67ad5c33dd94114a6b21b5c017545d815c37e12fb93d1c8a6b2cdf818109abf0ad8c86e00f3b77f
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
## [0.3.0] - 2024-06-29
|
2
|
+
|
3
|
+
- Added constants for the main Claude models
|
4
|
+
- Added default user_message payload
|
5
|
+
- Added parse_response method
|
6
|
+
|
7
|
+
## [0.2.1] - 2024-03-24
|
8
|
+
|
9
|
+
- Fixed dependencies
|
10
|
+
|
11
|
+
## [0.2.0] - 2024-03-24
|
12
|
+
|
13
|
+
- Changed to use httparty
|
14
|
+
|
1
15
|
## [0.1.1] - 2024-03-10
|
2
16
|
|
3
17
|
- Initial release
|
data/Gemfile.lock
CHANGED
@@ -1,30 +1,22 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
claude-ruby (0.2.
|
5
|
-
|
4
|
+
claude-ruby (0.2.1)
|
5
|
+
httparty
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
10
|
ast (2.4.2)
|
11
11
|
docile (1.4.0)
|
12
|
-
domain_name (0.6.20240107)
|
13
|
-
http-accept (1.7.0)
|
14
|
-
http-cookie (1.0.5)
|
15
|
-
domain_name (~> 0.5)
|
16
12
|
httparty (0.21.0)
|
17
13
|
mini_mime (>= 1.0.0)
|
18
14
|
multi_xml (>= 0.5.2)
|
19
15
|
json (2.7.1)
|
20
16
|
language_server-protocol (3.17.0.3)
|
21
|
-
mime-types (3.5.2)
|
22
|
-
mime-types-data (~> 3.2015)
|
23
|
-
mime-types-data (3.2024.0305)
|
24
17
|
mini_mime (1.1.5)
|
25
18
|
minitest (5.22.2)
|
26
19
|
multi_xml (0.6.0)
|
27
|
-
netrc (0.11.0)
|
28
20
|
parallel (1.24.0)
|
29
21
|
parser (3.3.0.5)
|
30
22
|
ast (~> 2.4.1)
|
@@ -33,11 +25,6 @@ GEM
|
|
33
25
|
rainbow (3.1.1)
|
34
26
|
rake (13.1.0)
|
35
27
|
regexp_parser (2.9.0)
|
36
|
-
rest-client (2.1.0)
|
37
|
-
http-accept (>= 1.7.0, < 2.0)
|
38
|
-
http-cookie (>= 1.0.2, < 2.0)
|
39
|
-
mime-types (>= 1.16, < 4.0)
|
40
|
-
netrc (~> 0.8)
|
41
28
|
rexml (3.2.6)
|
42
29
|
rubocop (1.62.0)
|
43
30
|
json (~> 2.3)
|
data/README.md
CHANGED
@@ -59,33 +59,52 @@ It requires an array of messages where each message is a hash with two propertie
|
|
59
59
|
|
60
60
|
Simple example with a single user message:
|
61
61
|
|
62
|
+
```ruby
|
63
|
+
messages = claude_client.user_message("Who was the first team to win the rugby world cup?")
|
64
|
+
response = claude_client.messages(messages)
|
65
|
+
```
|
66
|
+
|
67
|
+
The response contains a bunch of metadata and the model's message response.
|
68
|
+
To extract the message text you can use:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
claude_client.parse_response(response)
|
72
|
+
```
|
73
|
+
|
74
|
+
Or parse the response yourself:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
response['content'][0]['text']
|
78
|
+
```
|
79
|
+
|
80
|
+
```claude_client.user_message``` is just for simple user messages. For more complex messages you can specify the payload in detail:
|
81
|
+
|
62
82
|
```ruby
|
63
83
|
messages = [
|
64
84
|
{
|
65
85
|
role: "user",
|
66
|
-
content: "
|
86
|
+
content: "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"
|
87
|
+
},
|
88
|
+
{
|
89
|
+
role: "assistant",
|
90
|
+
content: "The best answer is ("
|
67
91
|
}
|
68
92
|
]
|
69
93
|
|
70
94
|
response = claude_client.messages(messages)
|
71
95
|
```
|
72
96
|
|
73
|
-
The response contains a bunch of metadata and the model's message response.
|
74
|
-
To extract the message text you can you code like this:
|
75
|
-
|
76
|
-
```ruby
|
77
|
-
response['content'][0]['text']
|
78
|
-
```
|
79
97
|
|
80
98
|
You can continue the conversation by calling the `messages` method again with an expanded messages array:
|
81
99
|
|
82
100
|
```ruby
|
83
101
|
|
84
|
-
messages
|
85
|
-
messages << {role: "
|
102
|
+
messages = [{ role: "user", content: "Who was the first team to win the rugby world cup?" }]
|
103
|
+
messages << { role: "assistant", content: "New Zealand won the first Rugby World Cup in 1987" }
|
104
|
+
messages << { role: "user", content: "Who came third and fourth in that competition?" }
|
86
105
|
|
87
106
|
response = claude_client.messages(messages)
|
88
|
-
puts response
|
107
|
+
puts claude_client.parse_response(response) # This will give you the updated message
|
89
108
|
```
|
90
109
|
|
91
110
|
Example with a more sophisticated message structure:
|
@@ -108,7 +127,46 @@ messages = [
|
|
108
127
|
},
|
109
128
|
]
|
110
129
|
|
111
|
-
response = claude_client.messages(messages, {system: system})
|
130
|
+
response = claude_client.messages(messages, { system: system })
|
131
|
+
```
|
132
|
+
|
133
|
+
## Vision
|
134
|
+
|
135
|
+
It's possible to pass an image to the Anthropic API and have Claude describe the image for you.
|
136
|
+
Here's an example how to do that using claude-ruby gem:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
require 'httparty'
|
140
|
+
require 'base64'
|
141
|
+
|
142
|
+
def fetch_and_encode_image(url)
|
143
|
+
response = HTTParty.get(url)
|
144
|
+
Base64.strict_encode64(response.body)
|
145
|
+
end
|
146
|
+
|
147
|
+
image_url = "https://images.unsplash.com/photo-1719630668118-fb27d922b165?ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&fm=jpg&fit=crop&w=1080&q=80&fit=max"
|
148
|
+
messages = [
|
149
|
+
{
|
150
|
+
"role": "user",
|
151
|
+
"content": [
|
152
|
+
{
|
153
|
+
"type": "image",
|
154
|
+
"source": {
|
155
|
+
"type": "base64",
|
156
|
+
"media_type": "image/jpeg",
|
157
|
+
"data": fetch_and_encode_image(image_url),
|
158
|
+
},
|
159
|
+
},
|
160
|
+
{
|
161
|
+
"type": "text",
|
162
|
+
"text": "Describe this image."
|
163
|
+
}
|
164
|
+
],
|
165
|
+
}
|
166
|
+
]
|
167
|
+
|
168
|
+
response = claude_client.messages(messages)
|
169
|
+
image_description = claude_client.parse_response(response)
|
112
170
|
```
|
113
171
|
|
114
172
|
For further details of the API visit https://docs.anthropic.com/claude/reference/messages_post
|
Binary file
|
data/lib/claude/client.rb
CHANGED
@@ -3,6 +3,23 @@ require 'json'
|
|
3
3
|
|
4
4
|
module Claude
|
5
5
|
class Client
|
6
|
+
MODEL_CLAUDE_3_OPUS_20240229 = 'claude-3-opus-20240229'
|
7
|
+
|
8
|
+
MODEL_CLAUDE_3_SONNET_20240229 = 'claude-3-sonnet-20240229'
|
9
|
+
MODEL_CLAUDE_3_5_SONNET_20240620 = 'claude-3-5-sonnet-20240620'
|
10
|
+
|
11
|
+
MODEL_CLAUDE_3_HAIKU_20240307 = 'claude-3-haiku-20240307'
|
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
|
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
|
21
|
+
|
22
|
+
MODEL_CLAUDE_DEFAULT = MODEL_CLAUDE_SONNET_LATEST
|
6
23
|
|
7
24
|
def initialize(api_key)
|
8
25
|
@api_key = api_key
|
@@ -10,15 +27,16 @@ module Claude
|
|
10
27
|
end
|
11
28
|
|
12
29
|
def messages(messages, params = {})
|
13
|
-
model = params[:model] ||
|
14
|
-
max_tokens = params[:max_tokens] ||
|
30
|
+
model = params[:model] || MODEL_CLAUDE_DEFAULT
|
31
|
+
max_tokens = params[:max_tokens] || 4096
|
32
|
+
system = params[:system] || "You are a helpful assistant."
|
15
33
|
|
16
34
|
url = "#{@endpoint}/messages"
|
17
35
|
|
18
36
|
data = {
|
19
37
|
model: model,
|
20
38
|
messages: messages,
|
21
|
-
system:
|
39
|
+
system: system,
|
22
40
|
max_tokens: max_tokens,
|
23
41
|
metadata: params[:metadata],
|
24
42
|
stop_sequences: params[:stop_sequences],
|
@@ -39,6 +57,19 @@ module Claude
|
|
39
57
|
}
|
40
58
|
end
|
41
59
|
|
60
|
+
def user_message(user_message)
|
61
|
+
[
|
62
|
+
{
|
63
|
+
"role": "user",
|
64
|
+
"content": user_message,
|
65
|
+
}
|
66
|
+
]
|
67
|
+
end
|
68
|
+
|
69
|
+
def parse_response(response)
|
70
|
+
response['content'][0]['text']
|
71
|
+
end
|
72
|
+
|
42
73
|
private
|
43
74
|
|
44
75
|
def post_api(url, data)
|
data/lib/claude/ruby/version.rb
CHANGED
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: claude-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.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-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: httparty
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- LICENSE.txt
|
39
39
|
- README.md
|
40
40
|
- Rakefile
|
41
|
+
- claude-ruby-0.2.1.gem
|
41
42
|
- lib/claude/client.rb
|
42
43
|
- lib/claude/ruby.rb
|
43
44
|
- lib/claude/ruby/version.rb
|