claude-ruby 0.2.1 → 0.3.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: 1abcb271e41e71527b088aec3c34b4112a2c2683889a213b6be9f36e8dcae609
4
- data.tar.gz: ad48b663ac70e5308a5f9e9b3b98a0ae53ed660c2fce1de7a085b083e613669a
3
+ metadata.gz: acba80183a9f1ade551f7c6b948f24b02e0f3d1eedf784a50715c48e4bfbec78
4
+ data.tar.gz: 35f49c5e0f3e51da700808a48ecd017dbe5251ab0eaaddccab7439a82a5947fb
5
5
  SHA512:
6
- metadata.gz: 80913b8e75720d8685686e6ca6ee7077e09e4e0c46cf6fe8123da979deeaff21f54e383160a95e2084da50514ef170be21a60d019ba5a0de5b06bb1b827b6e81
7
- data.tar.gz: 410e6ba1ed7dd2e5ab6f6abeba3e06c9769b9d57ab769139690dcb8d79ad669b394774488b09620216b41616382516022a670b3f6a88bf1793a2315ac47562b1
6
+ metadata.gz: af50a4ec445fe61a69a53e2a5408924fccdd5a229caf5b7d8ee1e6216760ac29d72be36eff6f7e4e7f78150020fa2dbd0807a27af6e414e81d056e613c168012
7
+ data.tar.gz: 4540eb9f231f5a366c230e570beeffaa5d0082b90b84dc85a67ad5c33dd94114a6b21b5c017545d815c37e12fb93d1c8a6b2cdf818109abf0ad8c86e00f3b77f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
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
+
1
7
  ## [0.2.1] - 2024-03-24
2
8
 
3
9
  - Fixed dependencies
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: "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 << {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,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] || '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.0"
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.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-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
@@ -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