claude-ruby 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1abcb271e41e71527b088aec3c34b4112a2c2683889a213b6be9f36e8dcae609
4
- data.tar.gz: ad48b663ac70e5308a5f9e9b3b98a0ae53ed660c2fce1de7a085b083e613669a
3
+ metadata.gz: f0914fd554c4c01686d477c44cf2de423f0faad30d673e39a767d0296da1cac4
4
+ data.tar.gz: fe9e0aa7f262c1af5fe3b0036136739a87f718c040f107c7ad792cb1492fe752
5
5
  SHA512:
6
- metadata.gz: 80913b8e75720d8685686e6ca6ee7077e09e4e0c46cf6fe8123da979deeaff21f54e383160a95e2084da50514ef170be21a60d019ba5a0de5b06bb1b827b6e81
7
- data.tar.gz: 410e6ba1ed7dd2e5ab6f6abeba3e06c9769b9d57ab769139690dcb8d79ad669b394774488b09620216b41616382516022a670b3f6a88bf1793a2315ac47562b1
6
+ metadata.gz: 5dba3fa917472c533a8d515624346ec40ccd95ebd79e803ad9d0fd536a4aaab3b4bb0c9281d97a9baf0c12c87c7c2849b23dec5a51128e28a2099f47274fbf05
7
+ data.tar.gz: a0e033b78ced858eae7d8fb196f40ecb04ef540dadcd76e77ba8e2cff177b12642579cbcb0114d27da1336dd26846fa10ebb3cfa2ae0641870e0db33b2c278e0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [0.3.1] - 2024-06-29
2
+ - Updated documentation
3
+
4
+ ## [0.3.0] - 2024-06-29
5
+
6
+ - Added constants for the main Claude models
7
+ - Added default user_message payload
8
+ - Added parse_response method
9
+
1
10
  ## [0.2.1] - 2024-03-24
2
11
 
3
12
  - Fixed dependencies
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
@@ -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: "Who was the first team to win the rugby world cup?"
86
+ content: "In which year was the first ever rugby world cup? (A) 1983 (B) 1987 (C) 1991"
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 << {role: "assistant", content: "New Zealand won the first Rugby World Cup in 1987"}
85
- messages << {role: "user", content: "Who came third and fourth in that competition?"}
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['content'][0]['text'] # This will give you the updated message
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,100 @@ 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
+ ## Models
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```
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::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
154
+ ````
155
+
156
+ Example usage:
157
+
158
+ ```ruby
159
+ response = claude_client.messages(messages, { model: Claude::Client::MODEL_CLAUDE_CHEAPEST })
160
+ ````
161
+
162
+ ## Parameters
163
+
164
+ You can pass in any of the following parameters, which will be included in the Anthropic API call:
165
+
166
+ ```ruby
167
+ model
168
+ system
169
+ max_tokens
170
+ metadata
171
+ stop_sequences
172
+ stream
173
+ temperature
174
+ top_p
175
+ top_k
176
+ ````
177
+
178
+ Example:
179
+
180
+ ```ruby
181
+ response = claude_client.messages(messages,
182
+ { model: Claude::Client::MODEL_CLAUDE_SMARTEST,
183
+ max_tokens: 500,
184
+ temperature: 0.1 })
185
+ ````
186
+
187
+ ## Vision
188
+
189
+ It's possible to pass an image to the Anthropic API and have Claude describe the image for you.
190
+ Here's an example how to do that using claude-ruby gem:
191
+
192
+ ```ruby
193
+ require 'httparty'
194
+ require 'base64'
195
+
196
+ def fetch_and_encode_image(url)
197
+ response = HTTParty.get(url)
198
+ Base64.strict_encode64(response.body)
199
+ end
200
+
201
+ image_url = "https://images.unsplash.com/photo-1719630668118-fb27d922b165?ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&fm=jpg&fit=crop&w=1080&q=80&fit=max"
202
+ messages = [
203
+ {
204
+ "role": "user",
205
+ "content": [
206
+ {
207
+ "type": "image",
208
+ "source": {
209
+ "type": "base64",
210
+ "media_type": "image/jpeg",
211
+ "data": fetch_and_encode_image(image_url),
212
+ },
213
+ },
214
+ {
215
+ "type": "text",
216
+ "text": "Describe this image."
217
+ }
218
+ ],
219
+ }
220
+ ]
221
+
222
+ response = claude_client.messages(messages)
223
+ image_description = claude_client.parse_response(response)
112
224
  ```
113
225
 
114
226
  For further details of the API visit https://docs.anthropic.com/claude/reference/messages_post
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] || 'claude-3-opus-20240229'
14
- max_tokens = params[:max_tokens] || 1024
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: params[: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)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Claude
4
4
  module Ruby
5
- VERSION = "0.2.1"
5
+ VERSION = "0.3.1"
6
6
  end
7
7
  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.2.1
4
+ version: 0.3.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-03-24 00:00:00.000000000 Z
11
+ date: 2024-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty